]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/ImmutableComponentPropertyContentRequest.java
Still working for multiple readers
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / ImmutableComponentPropertyContentRequest.java
1 package org.simantics.modeling;
2
3 import java.util.Arrays;
4 import java.util.HashMap;
5 import java.util.HashSet;
6 import java.util.Set;
7
8 import org.simantics.db.ConverterExternalValue;
9 import org.simantics.db.ExternalValue;
10 import org.simantics.db.ReadGraph;
11 import org.simantics.db.Resource;
12 import org.simantics.db.common.request.BinaryRead;
13 import org.simantics.db.common.request.UniqueRead;
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.layer0.request.PropertyInfo;
16 import org.simantics.db.layer0.request.UnescapedPropertyMapOfResource;
17 import org.simantics.db.layer0.variable.ValueAccessor;
18 import org.simantics.db.layer0.variable.Variable;
19 import org.simantics.diagram.Logger;
20 import org.simantics.layer0.Layer0;
21 import org.simantics.scl.runtime.SCLContext;
22 import org.simantics.scl.runtime.function.Function1;
23 import org.simantics.scl.runtime.function.FunctionImpl1;
24
25 public class ImmutableComponentPropertyContentRequest extends BinaryRead<Resource, PropertyInfo, ImmutableComponentPropertyContent> {
26
27         protected ImmutableComponentPropertyContentRequest(Resource subject, PropertyInfo predicate) {
28                 super(subject, predicate);
29         }
30
31
32         static class ExcludedSubjects extends UniqueRead<Set<Resource>> {
33
34                 @Override
35                 public Set<Resource> perform(ReadGraph graph) throws DatabaseException {
36                         Layer0 L0 = Layer0.getInstance(graph);
37                         return new HashSet<Resource>(Arrays.asList(L0.HasName,
38                                         L0.HasLabel,
39                                         L0.HasDataType,
40                                         L0.HasValueType,
41                                         L0.RequiresValueType,
42                                         L0.hasStandardResource,
43                                         L0.RequiresValueType,
44                                         L0.HasDescription,
45                                         L0.valueAccessor,
46                                         L0.domainChildren,
47                                         L0.domainProperties,
48                                         L0.classifications,
49                                         L0.Entity_methods,
50                                         L0.default_,
51                                         L0.required,
52                                         L0.readOnly,
53                                         L0.valid,
54                                         L0.validator));
55
56                 }
57                 
58         }
59         
60         
61         public ImmutableComponentPropertyContent getPossibleContent(ReadGraph graph) throws DatabaseException {
62                 
63                 Layer0 L0 = Layer0.getInstance(graph);
64
65                 Resource resource = parameter;
66                 PropertyInfo pi = parameter2;
67
68                 Resource value = graph.getPossibleObject(resource, pi.predicate);
69                 if(value != null) {
70                         
71                         if(graph.isInstanceOf(value, L0.Literal)) {
72                                 Object literal = graph.getPossibleValue(value);
73                                 if(literal != null) {
74                                         return new ImmutableComponentPropertyContent(pi, value, literal, null);
75                                 }
76                         } else if(graph.isInstanceOf(value, L0.SCLValue)) {
77                                 
78                                 Resource converter = graph.getPossibleObject(value, L0.ConvertsToValueWith);
79                                 if(converter != null) {
80
81                                         ExternalValue ev = graph.adapt(converter, ExternalValue.class);
82                                         if(ev instanceof ConverterExternalValue) {
83                                                 ConverterExternalValue cev = (ConverterExternalValue)ev;
84                                                 try {
85                                                         Function1 fn  = cev.getFunction(graph, resource, value, pi.predicate);
86                                                         return new ImmutableComponentPropertyContent(pi, value, null, fn);
87                                                 } catch (DatabaseException e) {
88                                                         Logger.defaultLogError(e);
89                                                 }
90                                         } else {
91                                                 System.err.println("Undefined converter " + graph.getURI(converter));
92                                         }
93                                         
94                                 }
95                                 
96                         } else if(graph.isInstanceOf(value, L0.Value)) {
97                                 
98                                 Resource valueAccessor = graph.getPossibleObject(value, L0.valueAccessor);
99                                 if(valueAccessor != null) {
100                                         FunctionImpl1 fn = new FunctionImpl1() {
101         
102                                                 @Override
103                                                 public Object apply(Object p0) {
104                                                         try {
105                                                                 ReadGraph graph = (ReadGraph)SCLContext.getCurrent().get("graph");
106                                                                 ValueAccessor accessor = graph.getValue2(valueAccessor, p0);
107                                                                 return accessor.getValue(graph, (Variable)p0);
108                                                         } catch (DatabaseException e) {
109                                                                 throw new IllegalStateException(e);
110                                                         }
111                                                 }
112         
113                                         };
114                                         
115                                         return new ImmutableComponentPropertyContent(pi, value, null, fn);
116                                         
117                                 }
118                                 
119                         }
120                 }
121                 
122                 return null;
123
124         }
125
126         private void process(ReadGraph graph, Resource subject, ImmutableComponentPropertyContent result) throws DatabaseException {
127                 
128                 Set<Resource> exclusions = graph.syncRequest(new ExcludedSubjects());
129                 if(exclusions.contains(subject)) return;
130                 
131                 for(PropertyInfo pi : graph.syncRequest(new UnescapedPropertyMapOfResource(subject)).values()) {
132
133                         // This is somewhat awkward
134                         if(parameter2.predicate.equals(pi.predicate)) continue;
135                         
136                         try {
137                                 if(pi.isHasProperty) {
138                                         ImmutableComponentPropertyContent pc = graph.syncRequest(new ImmutableComponentPropertyContentRequest(subject, pi));
139                                         if(pc != null) {
140                                                 if(result.properties == null) result.properties = new HashMap<>();
141                                                 result.properties.put(pc.pi.name, pc);
142                                         }
143                                 }
144                         } catch (DatabaseException e) {
145                                 e.printStackTrace();
146                         }
147                 }
148         }
149         
150         @Override
151         public ImmutableComponentPropertyContent perform(ReadGraph graph) throws DatabaseException {
152
153                 ImmutableComponentPropertyContent result = getPossibleContent(graph);
154                 if(result == null) return null;
155
156                 process(graph, result.pi.predicate, result);
157                 process(graph, result.valueResource, result);
158
159                 return result;
160                 
161         }
162
163         
164 }