]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/ImmutableComponentVariableContentRequest.java
Work in progress
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / ImmutableComponentVariableContentRequest.java
1 package org.simantics.modeling;
2
3 import java.util.HashMap;
4 import java.util.HashSet;
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.exception.DatabaseException;
12 import org.simantics.db.layer0.request.PropertyInfo;
13 import org.simantics.db.layer0.request.UnescapedPropertyMapOfResource;
14 import org.simantics.layer0.Layer0;
15 import org.simantics.structural.stubs.StructuralResource2;
16 import org.simantics.structural2.ConnectionImpl;
17 import org.simantics.structural2.queries.ConnectionPointMapOfResource;
18
19 public class ImmutableComponentVariableContentRequest extends ResourceRead<ImmutableComponentVariableContent> {
20
21         protected ImmutableComponentVariableContentRequest(Resource resource) {
22                 super(resource);
23         }
24         
25         @Override
26         public ImmutableComponentVariableContent perform(ReadGraph graph) throws DatabaseException {
27                 
28                 Layer0 L0 = Layer0.getInstance(graph);
29                 String name = graph.getRelatedValue(resource, L0.HasName, Bindings.STRING);
30                 ImmutableComponentVariableContent result = new ImmutableComponentVariableContent(resource, name);
31
32                 StructuralResource2 STR = StructuralResource2.getInstance(graph);
33                 Resource componentType = graph.getPossibleType(resource, STR.Component);
34                 if(componentType != null)
35                         result.procedural = graph.isInstanceOf(componentType, STR.ProceduralComponentType); 
36                 
37                 Map<String,PropertyInfo> pis = graph.syncRequest(new UnescapedPropertyMapOfResource(resource));
38                 
39                 for(Map.Entry<String, PropertyInfo> e : pis.entrySet()) {
40                         PropertyInfo pi = e.getValue();
41                         if(pi.isHasProperty) {
42                                 ImmutableComponentPropertyContent pc = graph.syncRequest(new ImmutableComponentPropertyContentRequest(resource, pi));
43                                 if(pc != null) {
44                                         if(result.properties == null) result.properties = new HashMap<>();
45                                         result.properties.put(pc.pi.name, pc);
46                                 }
47                         }
48                 }
49
50                 Map<String,PropertyInfo> cps = graph.syncRequest(new ConnectionPointMapOfResource(graph, resource));
51                 for(Map.Entry<String, PropertyInfo> e : cps.entrySet()) {
52                         PropertyInfo pi = e.getValue();
53                         Resource conn = graph.getPossibleObject(resource, pi.predicate);
54                         if(conn != null) {
55                                 if(result.properties == null) result.properties = new HashMap<>();
56                                 result.properties.put(pi.name, new ImmutableComponentPropertyContent(pi, conn, new ConnectionImpl(pi.predicate), null));
57                         }
58                 }
59                 
60                 HashSet<Resource> childSet = null;
61                 
62                 Resource container = resource;
63                 Resource possibleType = graph.getPossibleType(resource, STR.Component);
64                 if(possibleType != null) {
65                         Resource def = graph.getPossibleObject(possibleType, STR.IsDefinedBy);
66                         if(def != null) container = def;
67                 }
68                 
69                 for(Resource child : graph.getChildren(container).values()) {
70                         if(childSet == null)
71                                 childSet = new HashSet<>();
72                         childSet.add(child);
73                 }
74                 
75                 if(childSet != null) {
76                         result.children = new HashMap<>();
77                         for(Resource child : childSet) {
78                                 ImmutableComponentVariableContent content = graph.syncRequest(new ImmutableComponentVariableContentRequest(child)); 
79                                 result.children.put(content.name, content);
80                         }
81                 }
82                 
83                 return result;
84                 
85         }
86
87         
88 }