]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural2/src/org/simantics/structural2/scl/ReadComponentTypeInterfaceRequest.java
Fix errors with procedural user components for computational values
[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.Collections;
6 import java.util.Map;
7
8 import org.simantics.databoard.Bindings;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.common.request.ResourceRead;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.db.layer0.util.Layer0Utils;
14 import org.simantics.layer0.Layer0;
15 import org.simantics.scl.compiler.environment.Environment;
16 import org.simantics.scl.compiler.environment.Environments;
17 import org.simantics.scl.compiler.top.SCLExpressionCompilationException;
18 import org.simantics.scl.compiler.types.Type;
19
20 public class ReadComponentTypeInterfaceRequest extends ResourceRead<Map<String, ComponentTypeProperty>> {
21         
22     Environment environment;
23     
24         public ReadComponentTypeInterfaceRequest(Resource resource, Environment environment) {
25                 super(resource);
26                 this.environment = environment;
27         }
28
29         private void collect(ReadGraph graph, Resource t, THashMap<String, ComponentTypeProperty> result) throws DatabaseException {
30                 Layer0 L0 = Layer0.getInstance(graph);
31                 Map<String,Resource> domain = Layer0Utils.getDomainOf(graph, t); 
32                 for(Map.Entry<String, Resource> entry : domain.entrySet()) {
33                         String name = entry.getKey();
34                         Resource relation = entry.getValue();
35                         if(graph.isSubrelationOf(relation, L0.HasProperty)) {
36                                 String typeName = graph.getPossibleRelatedValue(relation, L0.RequiresValueType, Bindings.STRING);
37                                 if(typeName == null) continue;
38                                 Type type;
39                                 try {
40                                     type = Environments.getType(environment, typeName);
41                                 } catch(SCLExpressionCompilationException e) {
42                                     e.printStackTrace();
43                                     continue;
44                                 }
45                                 ComponentTypeProperty property = new ComponentTypeProperty(relation, type);
46                                 result.put(name, property);
47                         }
48                 }
49         }
50         
51         @Override
52         public Map<String, ComponentTypeProperty> perform(ReadGraph graph)
53                         throws DatabaseException {
54                 if (resource == null)
55                   return Collections.emptyMap();
56
57                 THashMap<String, ComponentTypeProperty> result = 
58                                 new THashMap<String, ComponentTypeProperty>();
59                 
60                 // TODO: For Antti to consider
61                 // resource == null with procedural user components
62                 if (resource != null)
63                     collect(graph, resource, result);
64                 //for(Resource t : graph.getSupertypes(resource)) collect(graph, t, result);
65                 
66                 return result;
67                 
68         }
69
70 }