1 package org.simantics.db.layer0.variable;
\r
3 import java.util.Collection;
\r
4 import java.util.Collections;
\r
5 import java.util.Map;
\r
6 import java.util.Set;
\r
8 import org.simantics.databoard.Bindings;
\r
9 import org.simantics.databoard.binding.Binding;
\r
10 import org.simantics.databoard.type.ArrayType;
\r
11 import org.simantics.databoard.type.Datatype;
\r
12 import org.simantics.databoard.type.NumberType;
\r
13 import org.simantics.db.ReadGraph;
\r
14 import org.simantics.db.Resource;
\r
15 import org.simantics.db.common.request.PropertyMapOfResource;
\r
16 import org.simantics.db.exception.DatabaseException;
\r
17 import org.simantics.db.layer0.variable.RVI.RVIPart;
\r
18 import org.simantics.db.layer0.variable.RVI.StringRVIPart;
\r
19 import org.simantics.db.layer0.variable.Variables.Role;
\r
21 public abstract class AbstractPropertyVariable extends AbstractVariable {
\r
23 final protected static Binding datatype_binding = Bindings.getBindingUnchecked(Datatype.class);
\r
25 public abstract Resource getPropertyResource(ReadGraph graph) throws DatabaseException;
\r
26 public abstract Resource getContainerResource(ReadGraph graph) throws DatabaseException;
\r
27 public abstract Datatype getDatatype(ReadGraph graph) throws DatabaseException;
\r
28 public abstract Variable getPredicate(ReadGraph graph) throws DatabaseException;
\r
30 public AbstractPropertyVariable() {
\r
34 public AbstractPropertyVariable(VariableNode node) {
\r
38 public String getLabel(ReadGraph graph) throws DatabaseException {
\r
41 * This should work but doesn't. REPRESENTS incorrectly returns the predicate resource.
\r
46 Object value = getPossibleValue(graph);
\r
47 return value == null ? "<no value>" : value.toString();
\r
52 public String getUnit(ReadGraph graph) throws DatabaseException {
\r
53 Datatype dt = getPossibleDatatype(graph);
\r
56 else if (dt instanceof NumberType) {
\r
57 String result = ((NumberType) dt).getUnit();
\r
58 return result != null ? result : "";
\r
59 } else if (dt instanceof ArrayType) {
\r
60 ArrayType at = (ArrayType) dt;
\r
61 Datatype cdt = at.componentType();
\r
62 if (cdt instanceof NumberType) {
\r
63 String result = ((NumberType) cdt).getUnit();
\r
64 return result != null ? result : "";
\r
71 public Role getRole(ReadGraph graph) throws DatabaseException {
\r
72 return Role.PROPERTY;
\r
76 public Collection<Variable> getChildren(ReadGraph graph)
\r
77 throws DatabaseException {
\r
78 return Collections.emptyList();
\r
82 public Variable getPossibleChild(ReadGraph graph, String name)
\r
83 throws DatabaseException {
\r
88 protected Variable getPossibleDomainProperty(ReadGraph graph, String name)
\r
89 throws DatabaseException {
\r
94 public Map<String, Variable> collectDomainProperties(ReadGraph graph,
\r
95 Map<String, Variable> properties) throws DatabaseException {
\r
100 * @see org.simantics.db.layer0.variable.AbstractVariable#getRepresents(org.simantics.db.ReadGraph)
\r
102 * FIXME: change this method to throw exceptions if representation is
\r
103 * not found and leave the current logic to
\r
104 * {@link #getPossibleRepresents(ReadGraph)}.
\r
107 public Resource getRepresents(ReadGraph graph) throws DatabaseException {
\r
108 Variable parent = getParent(graph);
\r
111 Resource parentRepresents = parent.getPossibleRepresents(graph);
\r
112 if (parentRepresents == null)
\r
114 Resource predicate = graph.syncRequest(new PropertyMapOfResource(parentRepresents))
\r
115 .get(getName(graph));
\r
116 if (predicate == null)
\r
118 return graph.getPossibleObject(parentRepresents, predicate);
\r
122 public Resource getPossibleRepresents(ReadGraph graph) throws DatabaseException {
\r
123 Variable parent = getParent(graph);
\r
126 Resource parentRepresents = parent.getPossibleRepresents(graph);
\r
127 if (parentRepresents == null)
\r
129 Resource predicate = graph.syncRequest(new PropertyMapOfResource(parentRepresents))
\r
130 .get(getName(graph));
\r
131 if (predicate == null)
\r
133 return graph.getPossibleObject(parentRepresents, predicate);
\r
137 public Variable getPossibleExtraProperty(ReadGraph graph, String name) throws DatabaseException {
\r
138 /*if(Variables.DATATYPE.equals(name)) {
\r
139 Object value = getPossibleDatatype(graph);
\r
141 return new ConstantPropertyVariable(this, name, value, datatype_binding);
\r
142 } else if(Variables.UNIT.equals(name)) {
\r
143 Object value = getUnit(graph);
\r
144 return new ConstantPropertyVariable(this, name, value, Bindings.STRING);
\r
145 } else if(Variables.PREDICATE.equals(name)) {
\r
146 Object value = getPossiblePredicate(graph);
\r
148 return new ConstantPropertyVariable(this, name, value, null);
\r
154 public void collectExtraProperties(ReadGraph graph, Map<String, Variable> properties) throws DatabaseException {
\r
155 // addProperty(properties, Variables.PREDICATE, getPossiblePredicate(graph), null);
\r
156 // Datatype dt = getPossibleDatatype(graph);
\r
158 // addProperty(properties, Variables.DATATYPE, dt, datatype_binding);
\r
159 //addProperty(properties, Variables.UNIT, getUnit(graph), Bindings.STRING);
\r
164 public RVIPart getRVIPart(ReadGraph graph) throws DatabaseException {
\r
165 return new StringRVIPart(Role.PROPERTY, getName(graph));
\r
169 public Set<String> getClassifications(ReadGraph graph) throws DatabaseException {
\r
170 return Collections.emptySet();
\r