]> gerrit.simantics Code Review - simantics/platform.git/blob
623275277da031167d108b1c5e2503f444b5de64
[simantics/platform.git] /
1 package org.simantics.graph.compiler.internal.translation;
2
3 import gnu.trove.map.hash.TObjectIntHashMap;
4 import gnu.trove.set.hash.TIntHashSet;
5
6 import java.util.ArrayList;
7 import java.util.Collection;
8
9 import org.antlr.runtime.tree.Tree;
10 import org.simantics.graph.compiler.SourceInfo.DefinitionPosition;
11 import org.simantics.graph.compiler.internal.parsing.GraphParser;
12 import org.simantics.graph.compiler.internal.store.LocationStore;
13 import org.simantics.graph.compiler.internal.store.PreValueStore;
14 import org.simantics.graph.compiler.internal.templates.TemplateDefinition;
15 import org.simantics.graph.compiler.internal.templates.TemplateDefinitionStore;
16 import org.simantics.graph.compiler.internal.templates.TemplateInstanceStore;
17 import org.simantics.graph.compiler.internal.values.TreeValue;
18 import org.simantics.graph.query.Paths;
19 import org.simantics.graph.query.UriUtils;
20 import org.simantics.graph.store.GraphStore;
21 import org.simantics.ltk.ISource;
22 import org.simantics.ltk.Problem;
23 import org.simantics.ltk.antlr.ANTLRUtils;
24
25 public class GraphTranslator extends GraphStore {
26
27         ISource source;
28         Collection<Problem> problems;
29         
30         TObjectIntHashMap<String> variables = new TObjectIntHashMap<String>();
31         ArrayList<DefinitionPosition> definitionPositions = new ArrayList<DefinitionPosition>();
32         TIntHashSet definedResources = new TIntHashSet();
33         
34         LocationStore locations;
35         TemplateInstanceStore templateInstances;
36         TemplateDefinitionStore templateDefinitions;
37         PreValueStore preValues;
38         
39         Paths paths;
40         int layer0;
41         
42         public GraphTranslator(Paths paths, Collection<Problem> problems, GraphStore store) {
43                 super(store);           
44                 this.problems = problems;
45                 this.paths = paths;
46                 
47                 locations = getStore(LocationStore.class);
48                 if(locations == null) {
49                         locations = new LocationStore();
50                         addStore(LocationStore.class, locations);
51                 }
52                 
53                 templateInstances = getStore(TemplateInstanceStore.class);
54                 if(templateInstances == null) {
55                         templateInstances = new TemplateInstanceStore();
56                         addStore(TemplateInstanceStore.class, templateInstances);
57                 }
58                 
59                 templateDefinitions = getStore(TemplateDefinitionStore.class);
60                 if(templateDefinitions == null) {
61                         templateDefinitions = new TemplateDefinitionStore();
62                         addStore(TemplateDefinitionStore.class, templateDefinitions);
63                 }
64                 
65                 preValues = getStore(PreValueStore.class);
66                 if(preValues == null) {
67                         preValues = new PreValueStore();
68                         addStore(PreValueStore.class, preValues);
69                 }
70                         
71                 layer0 = identities.createPathToId(paths.Layer0);               
72         }
73         
74         public void setSource(ISource source) {
75                 this.source = source;
76         }
77         
78         public void translateGraph(Tree tree) {         
79                 int count = tree.getChildCount();
80                 for(int i=0;i<count;++i)
81                         translateResourceDefinition(tree.getChild(i));
82         }
83
84         protected void locate(int resource, Tree tree) {
85                 locations.add(resource, ANTLRUtils.location(source, tree));
86         }
87         
88         public int translateResourceDefinition(Tree tree) {
89                 Tree subjectTree = tree.getChild(0);
90                 int subject = translateResource(subjectTree);
91                 
92                 int count = tree.getChildCount();
93                 for(int i=1;i<count;++i) {
94                         Tree child = tree.getChild(i);
95                         switch(child.getType()) {
96                         case GraphParser.PROPERTY:
97                                 translateProperty(subject, subjectTree, child);
98                                 break;
99                         case GraphParser.TEMPLATE_INSTANCE:
100                                 translateTemplateInstance(subject, child);
101                                 break;
102                         case GraphParser.TEMPLATE_DEFINITION:
103                                 translateTemplateDefinition(subject, child);
104                                 break;
105                         }
106                 }
107                 return subject;
108         }
109
110         private void translateProperty(int subject, Tree subjectTree, Tree tree) {
111                 int predicate = translateResource(tree.getChild(0));
112                 if(predicate == instanceOfId || predicate == inheritsId || predicate == subrelationOfId)
113                         if(definedResources.add(subject)) {                             
114                         definitionPositions.add(new DefinitionPosition(subject,
115                                         ANTLRUtils.getStartLine(subjectTree) + source.startLine(),
116                                         ANTLRUtils.getStartIndex(subjectTree) + source.startPos(),
117                                         ANTLRUtils.getStopIndex(subjectTree) + source.startPos()
118                         ));
119                         }
120                 int count = tree.getChildCount();
121                 for(int i=1;i<count;++i) {
122                         int object = translateResourceDefinition(tree.getChild(i));
123                         statements.add(subject, predicate, object);
124                 }
125         }       
126          
127         private void translateTemplateInstance(int subject, Tree tree) {
128                 int parameterCount = tree.getChildCount();
129                 int[] templateInstance = new int[parameterCount+1];
130                 templateInstance[0] = translateResource(tree.getChild(0));
131                 templateInstance[1] = subject;
132                 for(int i=1;i<parameterCount;++i) {
133                         Tree child = tree.getChild(i);
134                         if(child.getType() == GraphParser.RESOURCE)
135                                 templateInstance[i+1] = translateResourceDefinition(child);
136                         else
137                                 templateInstance[i+1] = translateResource(child);
138                 }
139                 templateInstances.add(templateInstance);
140         }       
141         
142         private void translateTemplateDefinition(int subject, Tree tree) {
143                 ArrayList<String> parameters = new ArrayList<String>(); 
144
145                 int i=0;
146                 while(i < tree.getChildCount() && 
147                                 tree.getChild(i).getType() == GraphParser.VARIABLE) {
148                         parameters.add(tree.getChild(i).getChild(0).getText());
149                         ++i;
150                 }
151
152                 GraphStore store = new GraphStore();
153                 ChildGraphTranslator translator = new ChildGraphTranslator(this, store);
154                 translator.setSource(source);
155                 while(i < tree.getChildCount()) {
156                         translator.translateResourceDefinition(tree.getChild(i));
157                         ++i;
158                 }       
159                 
160                 templateDefinitions.add(subject, 
161                                 new TemplateDefinition(parameters.toArray(new String[parameters.size()]), 
162                                                 translator.correspondences.toArray(), 
163                                                 store));
164         }
165
166         public static void printTree(Tree tree) {
167                 printTree(0, tree);
168         }
169         
170         int equalsId = 0;
171         int inheritsId = 0;
172         int subrelationOfId = 0;
173         int instanceOfId = 0;
174         int hasDomainId = 0;
175         int hasRangeId = 0;
176         int domainOfId = 0;
177         int requiresValueTypeId = 0;
178         int DatatypeId = 0;
179         
180         private int translateResource(Tree tree) {
181                 switch(tree.getType()) {
182                 case GraphParser.EMBEDDED_VALUE: {
183                         int id = identities.newResource();
184                         preValues.setValue(id, new TreeValue(source, tree.getChild(0)));
185                         locate(id, tree.getChild(0));
186                         return id;
187                 }
188                 case GraphParser.EMBEDDED_TYPE: {
189                         int DataType = identities.newResource();
190                         values.setValue(DataType, new DataTypeTranslator(source, problems).translate(tree.getChild(0)));
191                         if(instanceOfId == 0)
192                                 instanceOfId = identities.getChild(layer0, "InstanceOf");
193                         if(DatatypeId == 0)
194                                 DatatypeId = identities.getChild(layer0, "DataType");
195                         statements.add(DataType, instanceOfId, DatatypeId);
196                         locate(DataType, tree.getChild(0));
197                         return DataType;
198                 }
199                 case GraphParser.BLANK: {
200                         int id = identities.newResource();
201                         locate(id, tree);
202                         return id;
203                 }
204                 case GraphParser.ID:
205                         return getVariable(tree, tree.getText());
206                 case GraphParser.VARIABLE: {
207                         int id = identities.getRoot(tree.getChild(0).getText());
208                         locate(id, tree);
209                         return id;
210                 }
211                 case GraphParser.URI: {
212                         String uri = tree.getText();
213                         uri = uri.substring(1, uri.length()-1);
214                         int id = identities.createPathToId(UriUtils.uriToPath(uri));
215                         locate(id, tree);
216                         return id;
217                 }
218                 case GraphParser.REF: {
219                         int parent = translateResource(tree.getChild(0));
220                         Tree nameNode = tree.getChild(1);
221                         String name = nameNode.getText();
222                         if(nameNode.getType() == GraphParser.STRING) {
223                                 name = name.substring(1, name.length()-1);
224                         }
225                         int id = identities.getChild(parent, name);
226                         locate(id, tree);
227                         return id;
228                 }
229                 case GraphParser.EQUALS:
230                         if(equalsId == 0)
231                                 equalsId = identities.getChild(layer0, "Equals");
232                         return equalsId;
233                 case GraphParser.INHERITS:
234                         if(inheritsId == 0)
235                                 inheritsId = identities.getChild(layer0, "Inherits");
236                         return inheritsId;
237                 case GraphParser.SUBRELATION_OF:
238                         if(subrelationOfId == 0)
239                                 subrelationOfId = identities.getChild(layer0, "SubrelationOf");
240                         return subrelationOfId;
241                 case GraphParser.INSTANCE_OF:
242                         if(instanceOfId == 0)
243                                 instanceOfId = identities.getChild(layer0, "InstanceOf");
244                         return instanceOfId;            
245                 case GraphParser.HAS_DOMAIN:
246             if(hasDomainId == 0)
247                 hasDomainId = identities.getChild(layer0, "HasDomain");
248             return hasDomainId;                 
249                 case GraphParser.HAS_RANGE:
250             if(hasRangeId == 0)
251                 hasRangeId = identities.getChild(layer0, "HasRange");
252             return hasRangeId;
253                 case GraphParser.DOMAIN_OF:
254             if(domainOfId == 0)
255                 domainOfId = identities.getChild(layer0, "DomainOf");
256             return domainOfId;     
257         case GraphParser.REQUIRES_VALUE_TYPE:
258             if(requiresValueTypeId == 0)
259                 requiresValueTypeId = identities.getChild(layer0, "RequiresValueType");
260             return requiresValueTypeId;            
261                 default: 
262                         // TODO
263                         System.out.println("-- " + GraphParser.tokenNames[tree.getType()]);
264                         printTree(tree);
265                         return 0;
266                 }               
267         }
268
269         public int getVariable(Tree place, String text) {
270                 if(variables.containsKey(text))
271                         return variables.get(text);
272                 else {
273                         int id = identities.newResource();
274                         variables.put(text, id);
275                         locate(id, place);
276                         return id;
277                 }
278         }
279         
280         public TObjectIntHashMap<String> getVariables() {
281                 return variables;
282         }
283         
284         public ArrayList<DefinitionPosition> getDefinitionPositions() {
285                 return definitionPositions;
286         }
287
288         public static void printTree(int indent, Tree tree) {
289                 for(int i=0;i<indent;++i)
290                         System.out.print("  ");
291                 System.out.println(tree.getText());
292                 for(int i=0;i<tree.getChildCount();++i)
293                         printTree(indent+1, tree.getChild(i));
294         }       
295 }