1 package org.simantics.db.layer0.request;
3 import gnu.trove.map.hash.THashMap;
5 import java.util.Collection;
6 import java.util.Collections;
10 import org.simantics.databoard.Bindings;
11 import org.simantics.databoard.accessor.reference.ChildReference;
12 import org.simantics.databoard.accessor.reference.IndexReference;
13 import org.simantics.databoard.accessor.reference.NameReference;
14 import org.simantics.databoard.binding.Binding;
15 import org.simantics.databoard.type.Datatype;
16 import org.simantics.db.ReadGraph;
17 import org.simantics.db.Resource;
18 import org.simantics.db.common.request.ResourceRead;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.layer0.util.Layer0Utils;
21 import org.simantics.db.layer0.variable.ValueAccessor;
22 import org.simantics.db.layer0.variable.VariableBuilder;
23 import org.simantics.layer0.Layer0;
24 import org.simantics.operation.Layer0X;
25 import org.simantics.utils.datastructures.Pair;
27 final public class PropertyInfoRequest extends ResourceRead<PropertyInfo> {
29 public PropertyInfoRequest(Resource resource) {
34 public PropertyInfo perform(ReadGraph graph) throws DatabaseException {
36 Layer0 L0 = Layer0.getInstance(graph);
37 Layer0X L0X = Layer0X.getInstance(graph);
39 boolean isHasProperty = graph.isSubrelationOf(resource, L0.HasProperty);
41 String name = graph.getPossibleRelatedValue(resource, L0.HasName, Bindings.STRING);
42 if(name != null) name = name.intern();
44 Set<String> classifications = graph.sync(new ClassificationsRequest(graph.getPrincipalTypes(resource)));
45 VariableBuilder variableBuilder = graph.getPossibleAdapter(resource, VariableBuilder.class);
47 Datatype requiredDataType = graph.getPossibleRelatedValue(resource, L0X.RequiresDataType, Layer0Utils.datatype_binging);
49 String definedUnit = graph.getPossibleRelatedValue(resource, L0X.HasUnit, Bindings.STRING);
51 Resource literalRange = graph.getPossibleObject(resource, L0.HasRange);
52 String requiredValueType = graph.getPossibleRelatedValue(resource, L0.RequiresValueType, Bindings.STRING);
53 //System.out.println(name + " -> " + requiredValueType);
55 ValueAccessor accessor = graph.getPossibleRelatedValue2(resource, Layer0.getInstance(graph).valueAccessor, resource);
57 boolean hasEnumerationRange = graph.syncRequest(new HasEnumerationRange(resource));
59 Collection<Resource> ranges = graph.getObjects(resource, L0.HasRange);
60 if (!ranges.isEmpty()) {
61 for (Resource range : ranges) {
62 if (requiredValueType == null) {
63 // Check if the single range defines value type
64 Collection<Resource> valueTypes = graph.getAssertedObjects(range, L0.HasValueType);
65 if (valueTypes.size() > 0) {
66 for (Resource valueType : valueTypes) {
67 String vt = graph.getPossibleValue(valueType, Bindings.STRING);
68 if (vt != null && !vt.isEmpty()) {
69 requiredValueType = vt;
76 Collection<Resource> subliterals = graph.getObjects(range, L0.HasSubliteralPredicate);
77 if(!subliterals.isEmpty()) {
78 Map<String,Pair<Resource, ChildReference>> map = new THashMap<String,Pair<Resource, ChildReference>>();
79 for(Resource p : subliterals) {
80 String pN = graph.getPossibleRelatedValue(p, L0.HasName, Bindings.STRING);
81 if(pN == null) continue;
82 if(pN.startsWith("n-")) {
83 ChildReference r = new NameReference(pN.substring(2));
84 map.put(pN, Pair.make(p, r));
85 } else if (pN.startsWith("i-")) {
86 ChildReference r = new IndexReference(Integer.parseInt(pN.substring(2)));
87 map.put(pN, Pair.make(p, r));
91 return PropertyInfo.make(graph, resource, name, isHasProperty, classifications, variableBuilder, literalRange, requiredDataType, definedUnit, requiredValueType, map, accessor, hasEnumerationRange);
97 return PropertyInfo.make(graph, resource, name, isHasProperty, classifications, variableBuilder, literalRange, requiredDataType, definedUnit, requiredValueType, Collections.<String,Pair<Resource,ChildReference>>emptyMap(), accessor, hasEnumerationRange);
102 public boolean equals(Object object) {
105 else if (object == null)
107 else if (!(object instanceof PropertyInfoRequest))
109 PropertyInfoRequest r = (PropertyInfoRequest)object;
110 return r.resource.equals(resource);
114 public int hashCode() {
115 return resource.hashCode() + classHash;
118 private static int classHash = 31*PropertyInfo.class.hashCode();