X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.objmap2%2Fsrc%2Forg%2Fsimantics%2Fobjmap%2Fgraph%2Fschema%2FSimpleSchema.java;h=b7d90433bdc76835d150276feb7d07bdb811c235;hb=d318a62cad7e8bd2781c32ebec08c52e7d051f98;hp=734f101f421eb71e19d616628ab6bc2f5bef4f3b;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/SimpleSchema.java b/bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/SimpleSchema.java index 734f101f4..b7d90433b 100644 --- a/bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/SimpleSchema.java +++ b/bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/SimpleSchema.java @@ -1,94 +1,94 @@ -/******************************************************************************* - * 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 java.util.Stack; - -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; - - -/** - * An implementation of IMappingSchema that contains - * only SimpleLinkTypes. The link type of any domain - * element is based solely on its type in database and - * the link type of any range element is based on its class. - * @author Hannu Niemistö - */ -public class SimpleSchema 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) { - Stack> clazzes = new Stack>(); - for (Class clazz : element.getClass().getInterfaces()) { - clazzes.add(clazz); - } - clazzes.add(element.getClass().getSuperclass()); - - - while (!clazzes.isEmpty()) { - Class clazz = clazzes.pop(); - - type = rangeLinkTypes.get(clazz); - if (type != null) - return type; - for (Class c : clazz.getInterfaces()) - clazzes.add(c); - - } - throw new MappingException("Didn't find a link type for " + element.getClass() + "."); - } - return type; - } - -} +/******************************************************************************* + * 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 java.util.Stack; + +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; + + +/** + * An implementation of IMappingSchema that contains + * only SimpleLinkTypes. The link type of any domain + * element is based solely on its type in database and + * the link type of any range element is based on its class. + * @author Hannu Niemistö + */ +public class SimpleSchema 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) { + Stack> clazzes = new Stack>(); + for (Class clazz : element.getClass().getInterfaces()) { + clazzes.add(clazz); + } + clazzes.add(element.getClass().getSuperclass()); + + + while (!clazzes.isEmpty()) { + Class clazz = clazzes.pop(); + + type = rangeLinkTypes.get(clazz); + if (type != null) + return type; + for (Class c : clazz.getInterfaces()) + clazzes.add(c); + + } + throw new MappingException("Didn't find a link type for " + element.getClass() + "."); + } + return type; + } + +}