X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.objmap2%2Fsrc%2Forg%2Fsimantics%2Fobjmap%2Fgraph%2Fschema%2FDefaultSchema.java;fp=bundles%2Forg.simantics.objmap2%2Fsrc%2Forg%2Fsimantics%2Fobjmap%2Fgraph%2Fschema%2FDefaultSchema.java;h=0c81090079b5b3808ad91243a2405714a5c838e3;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hp=e5adac845ad937c3d9bcfeb68ffaf1b19c76034d;hpb=24e2b34260f219f0d1644ca7a138894980e25b14;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/DefaultSchema.java b/bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/DefaultSchema.java index e5adac845..0c8109007 100644 --- a/bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/DefaultSchema.java +++ b/bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/DefaultSchema.java @@ -1,98 +1,98 @@ -/******************************************************************************* - * Copyright (c) 2007, 2013 Association for Decentralized Information Management - * in Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.objmap.graph.schema; - - - -import gnu.trove.map.hash.THashMap; - -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.common.utils.NameUtils; -import org.simantics.db.exception.DatabaseException; -import org.simantics.objmap.exceptions.MappingException; - - -/** - * - */ -public class DefaultSchema implements IMappingSchema { - - THashMap> domainLinkTypes = - new THashMap>(); - THashMap, ILinkType> rangeLinkTypes = - new THashMap, ILinkType>(); - - public void addLinkType(SimpleLinkType linkType) { - domainLinkTypes.put(linkType.domainType, linkType); - rangeLinkTypes.put(linkType.rangeType, linkType); - } - - public void addLinkType(AdaptedLinkType linkType) { - domainLinkTypes.put(linkType.domainType, linkType); - rangeLinkTypes.put(linkType.rangeType, linkType); - } - - @Override - public ILinkType linkTypeOfDomainElement(ReadGraph g, Resource element) throws MappingException { - try { - - for(Resource type : g.getTypes(element)) { - - ILinkType linkType = domainLinkTypes.get(type); - if(linkType != null) return linkType; - - } - - throw new MappingException("Didn't find a link type for " + - NameUtils.getSafeName(g, element) + "."); - - } catch (DatabaseException e) { - throw new MappingException(e); - } - } - - @Override - public ILinkType linkTypeOfRangeElement(Object element) throws MappingException { - ILinkType type = rangeLinkTypes.get(element.getClass()); - if(type == null) { - for (Class clazz : element.getClass().getInterfaces()) { - type = rangeLinkTypes.get(clazz); - if (type != null) - return type; - - } - throw new MappingException("Didn't find a link type for " + element + "."); - } - return type; - } - - - public ILinkType linkTypeOfDomainType(ReadGraph g, Resource type) { - return domainLinkTypes.get(type); - } - - public ILinkType linkTypeOfRangeType(Class clazz) { - ILinkType type = rangeLinkTypes.get(clazz); - if(type == null) { - // FIXME: c is not referenced at all, should it be? - // TODO: should this method take inheritance into account ? - for (Class c : clazz.getInterfaces()) { - type = rangeLinkTypes.get(clazz); - if (type != null) - return type; - - } - } - return null; - } -} +/******************************************************************************* + * Copyright (c) 2007, 2013 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.objmap.graph.schema; + + + +import gnu.trove.map.hash.THashMap; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.utils.NameUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.objmap.exceptions.MappingException; + + +/** + * + */ +public class DefaultSchema implements IMappingSchema { + + THashMap> domainLinkTypes = + new THashMap>(); + THashMap, ILinkType> rangeLinkTypes = + new THashMap, ILinkType>(); + + public void addLinkType(SimpleLinkType linkType) { + domainLinkTypes.put(linkType.domainType, linkType); + rangeLinkTypes.put(linkType.rangeType, linkType); + } + + public void addLinkType(AdaptedLinkType linkType) { + domainLinkTypes.put(linkType.domainType, linkType); + rangeLinkTypes.put(linkType.rangeType, linkType); + } + + @Override + public ILinkType linkTypeOfDomainElement(ReadGraph g, Resource element) throws MappingException { + try { + + for(Resource type : g.getTypes(element)) { + + ILinkType linkType = domainLinkTypes.get(type); + if(linkType != null) return linkType; + + } + + throw new MappingException("Didn't find a link type for " + + NameUtils.getSafeName(g, element) + "."); + + } catch (DatabaseException e) { + throw new MappingException(e); + } + } + + @Override + public ILinkType linkTypeOfRangeElement(Object element) throws MappingException { + ILinkType type = rangeLinkTypes.get(element.getClass()); + if(type == null) { + for (Class clazz : element.getClass().getInterfaces()) { + type = rangeLinkTypes.get(clazz); + if (type != null) + return type; + + } + throw new MappingException("Didn't find a link type for " + element + "."); + } + return type; + } + + + public ILinkType linkTypeOfDomainType(ReadGraph g, Resource type) { + return domainLinkTypes.get(type); + } + + public ILinkType linkTypeOfRangeType(Class clazz) { + ILinkType type = rangeLinkTypes.get(clazz); + if(type == null) { + // FIXME: c is not referenced at all, should it be? + // TODO: should this method take inheritance into account ? + for (Class c : clazz.getInterfaces()) { + type = rangeLinkTypes.get(clazz); + if (type != null) + return type; + + } + } + return null; + } +}