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