]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/AbstractPropertyVariable.java
Merge "Documented difference of RuntimeEnvironmentRequest and Runti...quest2" into...
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / variable / AbstractPropertyVariable.java
1 package org.simantics.db.layer0.variable;
2
3 import java.util.Collection;
4 import java.util.Collections;
5 import java.util.Map;
6 import java.util.Set;
7
8 import org.simantics.databoard.Bindings;
9 import org.simantics.databoard.binding.Binding;
10 import org.simantics.databoard.type.ArrayType;
11 import org.simantics.databoard.type.Datatype;
12 import org.simantics.databoard.type.NumberType;
13 import org.simantics.db.ReadGraph;
14 import org.simantics.db.Resource;
15 import org.simantics.db.common.request.PropertyMapOfResource;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.layer0.variable.RVI.RVIPart;
18 import org.simantics.db.layer0.variable.RVI.StringRVIPart;
19 import org.simantics.db.layer0.variable.Variables.Role;
20
21 public abstract class AbstractPropertyVariable extends AbstractVariable {
22
23     final protected static Binding datatype_binding = Bindings.getBindingUnchecked(Datatype.class);
24
25     public abstract Resource getPropertyResource(ReadGraph graph) throws DatabaseException;
26     public abstract Resource getContainerResource(ReadGraph graph) throws DatabaseException;
27     public abstract Datatype getDatatype(ReadGraph graph) throws DatabaseException;
28     public abstract Variable getPredicate(ReadGraph graph) throws DatabaseException;
29
30     public AbstractPropertyVariable() {
31         this(null);
32     }
33
34     public AbstractPropertyVariable(VariableNode node) {
35         super(node);
36     }
37     
38     public String getLabel(ReadGraph graph) throws DatabaseException {
39
40         /*
41          * This should work but doesn't. REPRESENTS incorrectly returns the predicate resource.
42          *     
43          * 
44          */
45         
46         Object value = getPossibleValue(graph);
47         return value == null ? "<no value>" : value.toString(); 
48         
49     }
50     
51     
52     public String getUnit(ReadGraph graph) throws DatabaseException {
53         Datatype dt = getPossibleDatatype(graph);
54         if (dt == null)
55             return "";
56         else if (dt instanceof NumberType) {
57                 String result = ((NumberType) dt).getUnit(); 
58             return result != null ? result : "";
59         } else if (dt instanceof ArrayType) {
60             ArrayType at = (ArrayType) dt;
61             Datatype cdt = at.componentType();
62             if (cdt instanceof NumberType) {
63                 String result = ((NumberType) cdt).getUnit(); 
64                 return result != null ? result : "";
65             }
66         }
67         return null;
68     }
69
70     @Override
71     public Role getRole(ReadGraph graph) throws DatabaseException {
72         return Role.PROPERTY;
73     }
74     
75     @Override
76     public Collection<Variable> getChildren(ReadGraph graph)
77             throws DatabaseException {
78         return Collections.emptyList();
79     }
80     
81     @Override
82     public Variable getPossibleChild(ReadGraph graph, String name)
83             throws DatabaseException {
84         return null;
85     }
86     
87     @Override
88     protected Variable getPossibleDomainProperty(ReadGraph graph, String name)
89             throws DatabaseException {
90         return null;
91     }
92     
93     @Override
94     public Map<String, Variable> collectDomainProperties(ReadGraph graph,
95             Map<String, Variable> properties) throws DatabaseException {
96         return properties;
97     }
98
99     /**
100      * @see org.simantics.db.layer0.variable.AbstractVariable#getRepresents(org.simantics.db.ReadGraph)
101      * 
102      * FIXME: change this method to throw exceptions if representation is
103      *      not found and leave the current logic to
104      *      {@link #getPossibleRepresents(ReadGraph)}.
105      */
106     @Override
107     public Resource getRepresents(ReadGraph graph) throws DatabaseException {
108         Variable parent = getParent(graph);
109         if(parent == null)
110             return null;
111         Resource parentRepresents = parent.getPossibleRepresents(graph);
112         if (parentRepresents == null)
113             return null;
114         Resource predicate = graph.syncRequest(new PropertyMapOfResource(parentRepresents))
115                 .get(getName(graph));
116         if (predicate == null)
117             return null;
118         return graph.getPossibleObject(parentRepresents, predicate);
119     }
120
121     @Override
122     public Resource getPossibleRepresents(ReadGraph graph) throws DatabaseException {
123         Variable parent = getParent(graph);
124         if(parent == null)
125             return null;
126         Resource parentRepresents = parent.getPossibleRepresents(graph);
127         if (parentRepresents == null)
128             return null;
129         Resource predicate = graph.syncRequest(new PropertyMapOfResource(parentRepresents))
130                 .get(getName(graph));
131         if (predicate == null)
132             return null;
133         return graph.getPossibleObject(parentRepresents, predicate);
134     }
135
136     @Override
137     public Variable getPossibleExtraProperty(ReadGraph graph, String name) throws DatabaseException {
138         /*if(Variables.DATATYPE.equals(name)) {
139             Object value = getPossibleDatatype(graph);
140             if (value != null)
141                 return new ConstantPropertyVariable(this, name, value, datatype_binding);
142         } else if(Variables.UNIT.equals(name)) {
143             Object value = getUnit(graph);
144             return new ConstantPropertyVariable(this, name, value, Bindings.STRING);
145         } else  if(Variables.PREDICATE.equals(name)) {
146             Object value = getPossiblePredicate(graph);
147             if (value != null)
148                 return new ConstantPropertyVariable(this, name, value, null);
149         } */
150         return null;
151     }
152     
153     @Override
154     public void collectExtraProperties(ReadGraph graph, Map<String, Variable> properties) throws DatabaseException {
155 //        addProperty(properties, Variables.PREDICATE, getPossiblePredicate(graph), null);
156 //        Datatype dt = getPossibleDatatype(graph);
157 //        if(dt != null)
158 //            addProperty(properties, Variables.DATATYPE, dt, datatype_binding);
159         //addProperty(properties, Variables.UNIT, getUnit(graph), Bindings.STRING);
160     }
161     
162     
163     @Override
164     public RVIPart getRVIPart(ReadGraph graph) throws DatabaseException {
165         return new StringRVIPart(Role.PROPERTY, getName(graph));
166     }
167     
168     @Override
169     public Set<String> getClassifications(ReadGraph graph) throws DatabaseException {
170         return Collections.emptySet();
171     }
172     
173 }