1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2013 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.objmap.structural.schema;
\r
16 import java.util.Stack;
\r
18 import gnu.trove.map.hash.THashMap;
\r
20 import org.simantics.db.ReadGraph;
\r
21 import org.simantics.db.Resource;
\r
22 import org.simantics.db.common.utils.NameUtils;
\r
23 import org.simantics.db.exception.DatabaseException;
\r
24 import org.simantics.objmap.exceptions.MappingException;
\r
25 import org.simantics.objmap.graph.schema.ILinkType;
\r
26 import org.simantics.objmap.graph.schema.IMappingSchema;
\r
27 import org.simantics.objmap.structural.IStructuralObject;
\r
28 import org.simantics.objmap.structural.StructuralResource;
\r
34 public class DefaultSchema implements IMappingSchema<StructuralResource, IStructuralObject> {
\r
36 THashMap<Resource, ILinkType<StructuralResource, IStructuralObject>> domainLinkTypes =
\r
37 new THashMap<Resource, ILinkType<StructuralResource, IStructuralObject>>();
\r
38 THashMap<Class<?>, ILinkType<StructuralResource, IStructuralObject>> rangeLinkTypes =
\r
39 new THashMap<Class<?>, ILinkType<StructuralResource, IStructuralObject>>();
\r
41 public void addLinkType(SimpleLinkType linkType) {
\r
42 domainLinkTypes.put(linkType.domainType, linkType);
\r
43 rangeLinkTypes.put(linkType.rangeType, linkType);
\r
46 public void addLinkType(AdaptedLinkType linkType) {
\r
47 domainLinkTypes.put(linkType.domainType, linkType);
\r
48 rangeLinkTypes.put(linkType.rangeType, linkType);
\r
52 public ILinkType<StructuralResource, IStructuralObject> linkTypeOfDomainElement(ReadGraph g, StructuralResource element) throws MappingException {
\r
55 for(Resource type : g.getTypes(element.getResource())) {
\r
57 ILinkType<StructuralResource, IStructuralObject> linkType = domainLinkTypes.get(type);
\r
58 if(linkType != null) return linkType;
\r
62 throw new MappingException("Didn't find a link type for " +
\r
63 NameUtils.getSafeName(g, element.getResource()) + ".");
\r
65 } catch (DatabaseException e) {
\r
66 throw new MappingException(e);
\r
71 public ILinkType<StructuralResource, IStructuralObject> linkTypeOfRangeElement(IStructuralObject element) throws MappingException {
\r
72 ILinkType<StructuralResource, IStructuralObject> type = rangeLinkTypes.get(element.getClass());
\r
74 Stack<Class<?>> clazzes = new Stack<Class<?>>();
\r
75 for (Class<?> clazz : element.getClass().getInterfaces()) {
\r
78 clazzes.add(element.getClass().getSuperclass());
\r
80 while (!clazzes.isEmpty()) {
\r
81 Class<?> clazz = clazzes.pop();
\r
83 type = rangeLinkTypes.get(clazz);
\r
86 for (Class<?> c : clazz.getInterfaces())
\r
90 throw new MappingException("Didn't find a link type for " + element + ".");
\r
96 public ILinkType<StructuralResource, IStructuralObject> linkTypeOfDomainType(ReadGraph g, Resource type) {
\r
97 return domainLinkTypes.get(type);
\r
100 public ILinkType<StructuralResource, IStructuralObject> linkTypeOfRangeType(Class<?> clazz) {
\r
101 ILinkType<StructuralResource, IStructuralObject> type = rangeLinkTypes.get(clazz);
\r
103 Stack<Class<?>> clazzes = new Stack<Class<?>>();
\r
104 for (Class<?> c : clazz.getInterfaces()) {
\r
107 clazzes.add(clazz.getSuperclass());
\r
109 while (!clazzes.isEmpty()) {
\r
110 Class<?> c = clazzes.pop();
\r
112 type = rangeLinkTypes.get(c);
\r
115 for (Class<?> c2 : c.getInterfaces())
\r