]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural2/src/org/simantics/structural2/scl/ReadComponentTypeInterfaceRequest.java
Some enhancements made by Antti for multiple readers
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / scl / ReadComponentTypeInterfaceRequest.java
1 package org.simantics.structural2.scl;
2
3 import gnu.trove.map.hash.THashMap;
4
5 import java.util.Map;
6
7 import org.simantics.databoard.Bindings;
8 import org.simantics.db.ReadGraph;
9 import org.simantics.db.Resource;
10 import org.simantics.db.common.request.ResourceRead;
11 import org.simantics.db.common.utils.NameUtils;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.db.layer0.scl.SCLDatabaseException;
14 import org.simantics.db.layer0.util.Layer0Utils;
15 import org.simantics.layer0.Layer0;
16 import org.simantics.scl.compiler.environment.Environment;
17 import org.simantics.scl.compiler.environment.Environments;
18 import org.simantics.scl.compiler.errors.CompilationError;
19 import org.simantics.scl.compiler.top.SCLExpressionCompilationException;
20 import org.simantics.scl.compiler.types.Type;
21
22 public class ReadComponentTypeInterfaceRequest extends ResourceRead<Map<String, ComponentTypeProperty>> {
23         
24     Environment environment;
25     
26         public ReadComponentTypeInterfaceRequest(Resource resource, Environment environment) {
27                 super(resource);
28                 this.environment = environment;
29         }
30
31         private void collect(ReadGraph graph, Resource t, THashMap<String, ComponentTypeProperty> result) throws DatabaseException {
32                 Layer0 L0 = Layer0.getInstance(graph);
33                 Map<String,Resource> domain = Layer0Utils.getDomainOf(graph, t); 
34                 for(Map.Entry<String, Resource> entry : domain.entrySet()) {
35                         String name = entry.getKey();
36                         Resource relation = entry.getValue();
37                         if(graph.isSubrelationOf(relation, L0.HasProperty)) {
38                                 String typeName = graph.getPossibleRelatedValue(relation, L0.RequiresValueType, Bindings.STRING);
39                                 if(typeName == null) continue;
40                                 Type type;
41                                 try {
42                                     type = Environments.getType(environment, typeName);
43                                 } catch(SCLExpressionCompilationException e) {
44                             StringBuilder b = new StringBuilder();
45                             b.append("Couldn't compile interface properties in type '" + NameUtils.getSafeName(graph, t) + " for environment " + environment);
46                             b.append("':\n");
47                             StringBuilder b2 = new StringBuilder();
48                             for(CompilationError error : e.getErrors()) {
49                                 b2.append(error.description);
50                                 b2.append('\n');
51                             }
52                             System.err.println(b.toString() + b2.toString());
53                             throw new SCLDatabaseException(b.toString()+b2.toString(), b2.toString(), e.getErrors());
54                                 }
55                                 ComponentTypeProperty property = new ComponentTypeProperty(relation, type);
56                                 result.put(name, property);
57                         }
58                 }
59         }
60         
61         @Override
62         public Map<String, ComponentTypeProperty> perform(ReadGraph graph)
63                         throws DatabaseException {
64                 
65                 THashMap<String, ComponentTypeProperty> result = 
66                                 new THashMap<String, ComponentTypeProperty>();
67                 
68                 collect(graph, resource, result);
69                 //for(Resource t : graph.getSupertypes(resource)) collect(graph, t, result);
70                 
71                 return result;
72                 
73         }
74
75 }