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.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;
24 public class ImmutableComponentPropertyContentRequest extends BinaryRead<Resource, PropertyInfo, ImmutableComponentPropertyContent> {
26 protected ImmutableComponentPropertyContentRequest(Resource subject, PropertyInfo predicate) {
27 super(subject, predicate);
31 static class ExcludedSubjects extends UniqueRead<Set<Resource>> {
34 public Set<Resource> perform(ReadGraph graph) throws DatabaseException {
35 Layer0 L0 = Layer0.getInstance(graph);
36 return new HashSet<Resource>(Arrays.asList(L0.HasName,
41 L0.hasStandardResource,
60 public ImmutableComponentPropertyContent getPossibleContent(ReadGraph graph) throws DatabaseException {
62 Layer0 L0 = Layer0.getInstance(graph);
64 Resource resource = parameter;
65 PropertyInfo pi = parameter2;
67 Resource value = graph.getPossibleObject(resource, pi.predicate);
70 if(graph.isInstanceOf(value, L0.Literal)) {
71 Object literal = graph.getPossibleValue(value);
73 return new ImmutableComponentPropertyContent(pi, value, literal, null);
75 } else if(graph.isInstanceOf(value, L0.SCLValue)) {
77 Resource converter = graph.getPossibleObject(value, L0.ConvertsToValueWith);
78 if(converter != null) {
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);
86 System.err.println("Undefined converter " + graph.getURI(converter));
91 } else if(graph.isInstanceOf(value, L0.Value)) {
93 Resource valueAccessor = graph.getPossibleObject(value, L0.valueAccessor);
94 if(valueAccessor != null) {
95 FunctionImpl1 fn = new FunctionImpl1() {
98 public Object apply(Object p0) {
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);
110 return new ImmutableComponentPropertyContent(pi, value, null, fn);
121 private void process(ReadGraph graph, Resource subject, ImmutableComponentPropertyContent result) throws DatabaseException {
123 Set<Resource> exclusions = graph.syncRequest(new ExcludedSubjects());
124 if(exclusions.contains(subject)) return;
126 for(PropertyInfo pi : graph.syncRequest(new UnescapedPropertyMapOfResource(subject)).values()) {
128 // This is somewhat awkward
129 if(parameter2.predicate.equals(pi.predicate)) continue;
132 if(pi.isHasProperty) {
133 ImmutableComponentPropertyContent pc = graph.syncRequest(new ImmutableComponentPropertyContentRequest(subject, pi));
135 if(result.properties == null) result.properties = new HashMap<>();
136 result.properties.put(pc.pi.name, pc);
139 } catch (DatabaseException e) {
146 public ImmutableComponentPropertyContent perform(ReadGraph graph) throws DatabaseException {
148 ImmutableComponentPropertyContent result = getPossibleContent(graph);
149 if(result == null) return null;
151 process(graph, result.pi.predicate, result);
152 process(graph, result.valueResource, result);