]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/AbstractPropertyVariable.java
Merge commit 'ad8333027322fda6b9a8a524c7a7e15a54c52f38'
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / variable / AbstractPropertyVariable.java
1 package org.simantics.db.layer0.variable;\r
2 \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
7 \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
20 \r
21 public abstract class AbstractPropertyVariable extends AbstractVariable {\r
22 \r
23     final protected static Binding datatype_binding = Bindings.getBindingUnchecked(Datatype.class);\r
24 \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
29 \r
30     public AbstractPropertyVariable() {\r
31         this(null);\r
32     }\r
33 \r
34     public AbstractPropertyVariable(VariableNode node) {\r
35         super(node);\r
36     }\r
37     \r
38     public String getLabel(ReadGraph graph) throws DatabaseException {\r
39 \r
40         /*\r
41          * This should work but doesn't. REPRESENTS incorrectly returns the predicate resource.\r
42          *     \r
43          * \r
44          */\r
45         \r
46         Object value = getPossibleValue(graph);\r
47         return value == null ? "<no value>" : value.toString(); \r
48         \r
49     }\r
50     \r
51     \r
52     public String getUnit(ReadGraph graph) throws DatabaseException {\r
53         Datatype dt = getPossibleDatatype(graph);\r
54         if (dt == null)\r
55             return "";\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
65             }\r
66         }\r
67         return null;\r
68     }\r
69 \r
70     @Override\r
71     public Role getRole(ReadGraph graph) throws DatabaseException {\r
72         return Role.PROPERTY;\r
73     }\r
74     \r
75     @Override\r
76     public Collection<Variable> getChildren(ReadGraph graph)\r
77             throws DatabaseException {\r
78         return Collections.emptyList();\r
79     }\r
80     \r
81     @Override\r
82     public Variable getPossibleChild(ReadGraph graph, String name)\r
83             throws DatabaseException {\r
84         return null;\r
85     }\r
86     \r
87     @Override\r
88     protected Variable getPossibleDomainProperty(ReadGraph graph, String name)\r
89             throws DatabaseException {\r
90         return null;\r
91     }\r
92     \r
93     @Override\r
94     public Map<String, Variable> collectDomainProperties(ReadGraph graph,\r
95             Map<String, Variable> properties) throws DatabaseException {\r
96         return properties;\r
97     }\r
98 \r
99     /**\r
100      * @see org.simantics.db.layer0.variable.AbstractVariable#getRepresents(org.simantics.db.ReadGraph)\r
101      * \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
105      */\r
106     @Override\r
107     public Resource getRepresents(ReadGraph graph) throws DatabaseException {\r
108         Variable parent = getParent(graph);\r
109         if(parent == null)\r
110             return null;\r
111         Resource parentRepresents = parent.getPossibleRepresents(graph);\r
112         if (parentRepresents == null)\r
113             return null;\r
114         Resource predicate = graph.syncRequest(new PropertyMapOfResource(parentRepresents))\r
115                 .get(getName(graph));\r
116         if (predicate == null)\r
117             return null;\r
118         return graph.getPossibleObject(parentRepresents, predicate);\r
119     }\r
120 \r
121     @Override\r
122     public Resource getPossibleRepresents(ReadGraph graph) throws DatabaseException {\r
123         Variable parent = getParent(graph);\r
124         if(parent == null)\r
125             return null;\r
126         Resource parentRepresents = parent.getPossibleRepresents(graph);\r
127         if (parentRepresents == null)\r
128             return null;\r
129         Resource predicate = graph.syncRequest(new PropertyMapOfResource(parentRepresents))\r
130                 .get(getName(graph));\r
131         if (predicate == null)\r
132             return null;\r
133         return graph.getPossibleObject(parentRepresents, predicate);\r
134     }\r
135 \r
136     @Override\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
140             if (value != null)\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
147             if (value != null)\r
148                 return new ConstantPropertyVariable(this, name, value, null);\r
149         } */\r
150         return null;\r
151     }\r
152     \r
153     @Override\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
157 //        if(dt != null)\r
158 //            addProperty(properties, Variables.DATATYPE, dt, datatype_binding);\r
159         //addProperty(properties, Variables.UNIT, getUnit(graph), Bindings.STRING);\r
160     }\r
161     \r
162     \r
163     @Override\r
164     public RVIPart getRVIPart(ReadGraph graph) throws DatabaseException {\r
165         return new StringRVIPart(Role.PROPERTY, getName(graph));\r
166     }\r
167     \r
168     @Override\r
169     public Set<String> getClassifications(ReadGraph graph) throws DatabaseException {\r
170         return Collections.emptySet();\r
171     }\r
172     \r
173 }\r