1 package org.simantics.modeling;
3 import java.util.Arrays;
4 import java.util.HashMap;
5 import java.util.HashSet;
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;
25 public class ImmutableComponentPropertyContentRequest extends BinaryRead<Resource, PropertyInfo, ImmutableComponentPropertyContent> {
27 protected ImmutableComponentPropertyContentRequest(Resource subject, PropertyInfo predicate) {
28 super(subject, predicate);
32 static class ExcludedSubjects extends UniqueRead<Set<Resource>> {
35 public Set<Resource> perform(ReadGraph graph) throws DatabaseException {
36 Layer0 L0 = Layer0.getInstance(graph);
37 return new HashSet<Resource>(Arrays.asList(L0.HasName,
42 L0.hasStandardResource,
61 public ImmutableComponentPropertyContent getPossibleContent(ReadGraph graph) throws DatabaseException {
63 Layer0 L0 = Layer0.getInstance(graph);
65 Resource resource = parameter;
66 PropertyInfo pi = parameter2;
68 Resource value = graph.getPossibleObject(resource, pi.predicate);
71 if(graph.isInstanceOf(value, L0.Literal)) {
72 Object literal = graph.getPossibleValue(value);
74 return new ImmutableComponentPropertyContent(pi, value, literal, null);
76 } else if(graph.isInstanceOf(value, L0.SCLValue)) {
78 Resource converter = graph.getPossibleObject(value, L0.ConvertsToValueWith);
79 if(converter != null) {
81 ExternalValue ev = graph.adapt(converter, ExternalValue.class);
82 if(ev instanceof ConverterExternalValue) {
83 ConverterExternalValue cev = (ConverterExternalValue)ev;
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);
91 System.err.println("Undefined converter " + graph.getURI(converter));
96 } else if(graph.isInstanceOf(value, L0.Value)) {
98 Resource valueAccessor = graph.getPossibleObject(value, L0.valueAccessor);
99 if(valueAccessor != null) {
100 FunctionImpl1 fn = new FunctionImpl1() {
103 public Object apply(Object p0) {
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);
115 return new ImmutableComponentPropertyContent(pi, value, null, fn);
126 private void process(ReadGraph graph, Resource subject, ImmutableComponentPropertyContent result) throws DatabaseException {
128 Set<Resource> exclusions = graph.syncRequest(new ExcludedSubjects());
129 if(exclusions.contains(subject)) return;
131 for(PropertyInfo pi : graph.syncRequest(new UnescapedPropertyMapOfResource(subject)).values()) {
133 // This is somewhat awkward
134 if(parameter2.predicate.equals(pi.predicate)) continue;
137 if(pi.isHasProperty) {
138 ImmutableComponentPropertyContent pc = graph.syncRequest(new ImmutableComponentPropertyContentRequest(subject, pi));
140 if(result.properties == null) result.properties = new HashMap<>();
141 result.properties.put(pc.pi.name, pc);
144 } catch (DatabaseException e) {
151 public ImmutableComponentPropertyContent perform(ReadGraph graph) throws DatabaseException {
153 ImmutableComponentPropertyContent result = getPossibleContent(graph);
154 if(result == null) return null;
156 process(graph, result.pi.predicate, result);
157 process(graph, result.valueResource, result);