]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/SimpleSchema.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.objmap2 / src / org / simantics / objmap / graph / schema / SimpleSchema.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.objmap.graph.schema;\r
13 \r
14 \r
15 import java.util.Stack;\r
16 \r
17 import gnu.trove.map.hash.THashMap;\r
18 \r
19 import org.simantics.db.ReadGraph;\r
20 import org.simantics.db.Resource;\r
21 import org.simantics.db.common.utils.NameUtils;\r
22 import org.simantics.db.exception.DatabaseException;\r
23 import org.simantics.objmap.exceptions.MappingException;\r
24 \r
25 \r
26 /**\r
27  * An implementation of IMappingSchema that contains\r
28  * only SimpleLinkTypes. The link type of any domain\r
29  * element is based solely on its type in database and\r
30  * the link type of any range element is based on its class.\r
31  * @author Hannu Niemistö\r
32  */\r
33 public class SimpleSchema implements IMappingSchema<Resource,Object> {\r
34 \r
35     THashMap<Resource, ILinkType<Resource,Object>> domainLinkTypes = \r
36         new THashMap<Resource, ILinkType<Resource,Object>>();\r
37     THashMap<Class<?>, ILinkType<Resource,Object>> rangeLinkTypes = \r
38         new THashMap<Class<?>, ILinkType<Resource,Object>>();\r
39     \r
40     public void addLinkType(SimpleLinkType<Object> linkType) {\r
41         domainLinkTypes.put(linkType.domainType, linkType);\r
42         rangeLinkTypes.put(linkType.rangeType, linkType);\r
43     }\r
44     \r
45     public void addLinkType(AdaptedLinkType<Object> linkType) {\r
46         domainLinkTypes.put(linkType.domainType, linkType);\r
47         rangeLinkTypes.put(linkType.rangeType, linkType);\r
48     }\r
49     \r
50     @Override\r
51     public ILinkType<Resource,Object> linkTypeOfDomainElement(ReadGraph g, Resource element) throws MappingException {        \r
52         try {\r
53                 \r
54                 for(Resource type : g.getTypes(element)) {\r
55 \r
56                         ILinkType<Resource,Object> linkType = domainLinkTypes.get(type);\r
57                         if(linkType != null) return linkType;\r
58                         \r
59                 }\r
60                 \r
61                 throw new MappingException("Didn't find a link type for " +     NameUtils.getSafeName(g, element) + ".");\r
62                                 \r
63         } catch (DatabaseException e) {\r
64             throw new MappingException(e);\r
65         }\r
66     }\r
67 \r
68     @Override\r
69     public ILinkType<Resource,Object> linkTypeOfRangeElement(Object element) throws MappingException {\r
70         ILinkType<Resource,Object> type = rangeLinkTypes.get(element.getClass());\r
71         if(type == null)  {\r
72                 Stack<Class<?>> clazzes = new Stack<Class<?>>();\r
73                 for (Class<?> clazz : element.getClass().getInterfaces()) {\r
74                         clazzes.add(clazz);\r
75                 }\r
76                 clazzes.add(element.getClass().getSuperclass());\r
77                 \r
78                 \r
79                         while (!clazzes.isEmpty()) {\r
80                                 Class<?> clazz = clazzes.pop();\r
81                         \r
82                                 type = rangeLinkTypes.get(clazz);\r
83                                 if (type != null)\r
84                                         return type;\r
85                                 for (Class<?> c : clazz.getInterfaces())\r
86                                         clazzes.add(c);\r
87                                 \r
88                         }\r
89                         throw new MappingException("Didn't find a link type for " +     element.getClass() + ".");\r
90                 }\r
91                 return type;\r
92     }\r
93 \r
94 }\r