1 package org.simantics.graph.compiler.internal.translation;
3 import gnu.trove.map.hash.TObjectIntHashMap;
4 import gnu.trove.set.hash.TIntHashSet;
6 import java.util.ArrayList;
7 import java.util.Collection;
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;
25 public class GraphTranslator extends GraphStore {
28 Collection<Problem> problems;
30 TObjectIntHashMap<String> variables = new TObjectIntHashMap<String>();
31 ArrayList<DefinitionPosition> definitionPositions = new ArrayList<DefinitionPosition>();
32 TIntHashSet definedResources = new TIntHashSet();
34 LocationStore locations;
35 TemplateInstanceStore templateInstances;
36 TemplateDefinitionStore templateDefinitions;
37 PreValueStore preValues;
42 public GraphTranslator(Paths paths, Collection<Problem> problems, GraphStore store) {
44 this.problems = problems;
47 locations = getStore(LocationStore.class);
48 if(locations == null) {
49 locations = new LocationStore();
50 addStore(LocationStore.class, locations);
53 templateInstances = getStore(TemplateInstanceStore.class);
54 if(templateInstances == null) {
55 templateInstances = new TemplateInstanceStore();
56 addStore(TemplateInstanceStore.class, templateInstances);
59 templateDefinitions = getStore(TemplateDefinitionStore.class);
60 if(templateDefinitions == null) {
61 templateDefinitions = new TemplateDefinitionStore();
62 addStore(TemplateDefinitionStore.class, templateDefinitions);
65 preValues = getStore(PreValueStore.class);
66 if(preValues == null) {
67 preValues = new PreValueStore();
68 addStore(PreValueStore.class, preValues);
71 layer0 = identities.createPathToId(paths.Layer0);
74 public void setSource(ISource source) {
78 public void translateGraph(Tree tree) {
79 int count = tree.getChildCount();
80 for(int i=0;i<count;++i)
81 translateResourceDefinition(tree.getChild(i));
84 protected void locate(int resource, Tree tree) {
85 locations.add(resource, ANTLRUtils.location(source, tree));
88 public int translateResourceDefinition(Tree tree) {
89 Tree subjectTree = tree.getChild(0);
90 int subject = translateResource(subjectTree);
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);
99 case GraphParser.TEMPLATE_INSTANCE:
100 translateTemplateInstance(subject, child);
102 case GraphParser.TEMPLATE_DEFINITION:
103 translateTemplateDefinition(subject, child);
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()
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);
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);
137 templateInstance[i+1] = translateResource(child);
139 templateInstances.add(templateInstance);
142 private void translateTemplateDefinition(int subject, Tree tree) {
143 ArrayList<String> parameters = new ArrayList<String>();
146 while(i < tree.getChildCount() &&
147 tree.getChild(i).getType() == GraphParser.VARIABLE) {
148 parameters.add(tree.getChild(i).getChild(0).getText());
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));
160 templateDefinitions.add(subject,
161 new TemplateDefinition(parameters.toArray(new String[parameters.size()]),
162 translator.correspondences.toArray(),
166 public static void printTree(Tree tree) {
172 int subrelationOfId = 0;
173 int instanceOfId = 0;
177 int requiresValueTypeId = 0;
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));
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");
194 DatatypeId = identities.getChild(layer0, "DataType");
195 statements.add(DataType, instanceOfId, DatatypeId);
196 locate(DataType, tree.getChild(0));
199 case GraphParser.BLANK: {
200 int id = identities.newResource();
205 return getVariable(tree, tree.getText());
206 case GraphParser.VARIABLE: {
207 int id = identities.getRoot(tree.getChild(0).getText());
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));
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);
225 int id = identities.getChild(parent, name);
229 case GraphParser.EQUALS:
231 equalsId = identities.getChild(layer0, "Equals");
233 case GraphParser.INHERITS:
235 inheritsId = identities.getChild(layer0, "Inherits");
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");
245 case GraphParser.HAS_DOMAIN:
247 hasDomainId = identities.getChild(layer0, "HasDomain");
249 case GraphParser.HAS_RANGE:
251 hasRangeId = identities.getChild(layer0, "HasRange");
253 case GraphParser.DOMAIN_OF:
255 domainOfId = identities.getChild(layer0, "DomainOf");
257 case GraphParser.REQUIRES_VALUE_TYPE:
258 if(requiresValueTypeId == 0)
259 requiresValueTypeId = identities.getChild(layer0, "RequiresValueType");
260 return requiresValueTypeId;
263 System.out.println("-- " + GraphParser.tokenNames[tree.getType()]);
269 public int getVariable(Tree place, String text) {
270 if(variables.containsKey(text))
271 return variables.get(text);
273 int id = identities.newResource();
274 variables.put(text, id);
280 public TObjectIntHashMap<String> getVariables() {
284 public ArrayList<DefinitionPosition> getDefinitionPositions() {
285 return definitionPositions;
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));