]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.objmap2/src/org/simantics/objmap/structural/schema/DefaultSchema.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.objmap2 / src / org / simantics / objmap / structural / schema / DefaultSchema.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2013 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.objmap.structural.schema;
13
14
15
16 import java.util.Stack;
17
18 import gnu.trove.map.hash.THashMap;
19
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.common.utils.NameUtils;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.objmap.exceptions.MappingException;
25 import org.simantics.objmap.graph.schema.ILinkType;
26 import org.simantics.objmap.graph.schema.IMappingSchema;
27 import org.simantics.objmap.structural.IStructuralObject;
28 import org.simantics.objmap.structural.StructuralResource;
29
30
31 /**
32  * 
33  */
34 public class DefaultSchema implements IMappingSchema<StructuralResource, IStructuralObject> {
35
36     THashMap<Resource, ILinkType<StructuralResource, IStructuralObject>> domainLinkTypes = 
37         new THashMap<Resource, ILinkType<StructuralResource, IStructuralObject>>();
38     THashMap<Class<?>, ILinkType<StructuralResource, IStructuralObject>> rangeLinkTypes = 
39         new THashMap<Class<?>, ILinkType<StructuralResource, IStructuralObject>>();
40     
41     public void addLinkType(SimpleLinkType linkType) {
42         domainLinkTypes.put(linkType.domainType, linkType);
43         rangeLinkTypes.put(linkType.rangeType, linkType);
44     }
45     
46     public void addLinkType(AdaptedLinkType linkType) {
47         domainLinkTypes.put(linkType.domainType, linkType);
48         rangeLinkTypes.put(linkType.rangeType, linkType);
49     }
50     
51     @Override
52     public ILinkType<StructuralResource, IStructuralObject> linkTypeOfDomainElement(ReadGraph g, StructuralResource element) throws MappingException {        
53         try {
54                 
55                 for(Resource type : g.getTypes(element.getResource())) {
56
57                         ILinkType<StructuralResource, IStructuralObject> linkType = domainLinkTypes.get(type);
58                         if(linkType != null) return linkType;
59                         
60                 }
61                 
62                 throw new MappingException("Didn't find a link type for " +
63                                 NameUtils.getSafeName(g, element.getResource()) + ".");
64                                 
65         } catch (DatabaseException e) {
66             throw new MappingException(e);
67         }
68     }
69
70     @Override
71     public ILinkType<StructuralResource, IStructuralObject> linkTypeOfRangeElement(IStructuralObject element) throws MappingException {
72         ILinkType<StructuralResource, IStructuralObject> type = rangeLinkTypes.get(element.getClass());
73                 if(type == null)  {
74                         Stack<Class<?>> clazzes = new Stack<Class<?>>();
75                 for (Class<?> clazz : element.getClass().getInterfaces()) {
76                         clazzes.add(clazz);
77                 }
78                 clazzes.add(element.getClass().getSuperclass());
79                 
80                         while (!clazzes.isEmpty()) {
81                                 Class<?> clazz = clazzes.pop();
82                         
83                                 type = rangeLinkTypes.get(clazz);
84                                 if (type != null)
85                                         return type;
86                                 for (Class<?> c : clazz.getInterfaces())
87                                         clazzes.add(c);
88                                 
89                         }
90                         throw new MappingException("Didn't find a link type for " +     element + ".");
91                 }
92                 return type;
93     }
94
95     
96     public ILinkType<StructuralResource, IStructuralObject> linkTypeOfDomainType(ReadGraph g, Resource type)  {        
97         return domainLinkTypes.get(type);
98     }
99     
100     public ILinkType<StructuralResource, IStructuralObject> linkTypeOfRangeType(Class<?> clazz) {
101         ILinkType<StructuralResource, IStructuralObject> type = rangeLinkTypes.get(clazz);
102         if(type == null)  {
103                 Stack<Class<?>> clazzes = new Stack<Class<?>>();
104                 for (Class<?> c : clazz.getInterfaces()) {
105                         clazzes.add(c);
106                 }
107                 clazzes.add(clazz.getSuperclass());
108                 
109                 while (!clazzes.isEmpty()) {
110                                 Class<?> c = clazzes.pop();
111                         
112                                 type = rangeLinkTypes.get(c);
113                                 if (type != null)
114                                         return type;
115                                 for (Class<?> c2 : c.getInterfaces())
116                                         clazzes.add(c2);
117                                 
118                         }
119                 
120                 }
121         return null;
122     }
123 }