1 package org.simantics.db.layer0.variable;
3 import java.util.Collection;
4 import java.util.Collections;
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;
21 public abstract class AbstractPropertyVariable extends AbstractVariable {
23 final protected static Binding datatype_binding = Bindings.getBindingUnchecked(Datatype.class);
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;
30 public AbstractPropertyVariable() {
34 public AbstractPropertyVariable(VariableNode node) {
38 public String getLabel(ReadGraph graph) throws DatabaseException {
41 * This should work but doesn't. REPRESENTS incorrectly returns the predicate resource.
46 Object value = getPossibleValue(graph);
47 return value == null ? "<no value>" : value.toString();
52 public String getUnit(ReadGraph graph) throws DatabaseException {
53 Datatype dt = getPossibleDatatype(graph);
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 : "";
71 public Role getRole(ReadGraph graph) throws DatabaseException {
76 public Collection<Variable> getChildren(ReadGraph graph)
77 throws DatabaseException {
78 return Collections.emptyList();
82 public Variable getPossibleChild(ReadGraph graph, String name)
83 throws DatabaseException {
88 protected Variable getPossibleDomainProperty(ReadGraph graph, String name)
89 throws DatabaseException {
94 public Map<String, Variable> collectDomainProperties(ReadGraph graph,
95 Map<String, Variable> properties) throws DatabaseException {
100 * @see org.simantics.db.layer0.variable.AbstractVariable#getRepresents(org.simantics.db.ReadGraph)
102 * FIXME: change this method to throw exceptions if representation is
103 * not found and leave the current logic to
104 * {@link #getPossibleRepresents(ReadGraph)}.
107 public Resource getRepresents(ReadGraph graph) throws DatabaseException {
108 Variable parent = getParent(graph);
111 Resource parentRepresents = parent.getPossibleRepresents(graph);
112 if (parentRepresents == null)
114 Resource predicate = graph.syncRequest(new PropertyMapOfResource(parentRepresents))
115 .get(getName(graph));
116 if (predicate == null)
118 return graph.getPossibleObject(parentRepresents, predicate);
122 public Resource getPossibleRepresents(ReadGraph graph) throws DatabaseException {
123 Variable parent = getParent(graph);
126 Resource parentRepresents = parent.getPossibleRepresents(graph);
127 if (parentRepresents == null)
129 Resource predicate = graph.syncRequest(new PropertyMapOfResource(parentRepresents))
130 .get(getName(graph));
131 if (predicate == null)
133 return graph.getPossibleObject(parentRepresents, predicate);
137 public Variable getPossibleExtraProperty(ReadGraph graph, String name) throws DatabaseException {
138 /*if(Variables.DATATYPE.equals(name)) {
139 Object value = getPossibleDatatype(graph);
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);
148 return new ConstantPropertyVariable(this, name, value, null);
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);
158 // addProperty(properties, Variables.DATATYPE, dt, datatype_binding);
159 //addProperty(properties, Variables.UNIT, getUnit(graph), Bindings.STRING);
164 public RVIPart getRVIPart(ReadGraph graph) throws DatabaseException {
165 return new StringRVIPart(Role.PROPERTY, getName(graph));
169 public Set<String> getClassifications(ReadGraph graph) throws DatabaseException {
170 return Collections.emptySet();