]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/ConstantChildVariable.java
Merge commit 'ad8333027322fda6b9a8a524c7a7e15a54c52f38'
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / variable / ConstantChildVariable.java
1 package org.simantics.db.layer0.variable;\r
2 \r
3 import gnu.trove.map.hash.THashMap;\r
4 \r
5 import java.util.ArrayList;\r
6 import java.util.Collection;\r
7 import java.util.Collections;\r
8 import java.util.HashMap;\r
9 import java.util.Map;\r
10 \r
11 import org.simantics.databoard.binding.Binding;\r
12 import org.simantics.db.ReadGraph;\r
13 import org.simantics.db.Resource;\r
14 import org.simantics.db.exception.DatabaseException;\r
15 import org.simantics.db.exception.NoSingleResultException;\r
16 \r
17 public class ConstantChildVariable extends AbstractChildVariable {\r
18 \r
19     final Variable parent;\r
20     final String name;\r
21     final Map<String, ConstantPropertyVariable> properties;\r
22 \r
23     public ConstantChildVariable(Variable parent, String name) {\r
24         this.parent = parent;\r
25         this.name = name;\r
26         this.properties = new THashMap<String, ConstantPropertyVariable>();\r
27     }\r
28 \r
29     public ConstantChildVariable(Variable parent, String name, Collection<ConstantPropertyVariableBuilder> propertyBuilders) {\r
30         this(parent, name);\r
31         for(ConstantPropertyVariableBuilder b : propertyBuilders) {\r
32                 ConstantPropertyVariable property = b.build(this);\r
33                 properties.put(b.getName(), property);\r
34         }\r
35     }\r
36     \r
37     public ConstantChildVariable(Variable parent, String name, ConstantPropertyVariableBuilder ... propertyBuilders) {\r
38         this(parent, name);\r
39         for(ConstantPropertyVariableBuilder b : propertyBuilders) {\r
40                 ConstantPropertyVariable property = b.build(this);\r
41                 properties.put(b.getName(), property);\r
42         }\r
43     }\r
44 \r
45     public ConstantChildVariable(Variable parent, String name, String[] propertyNames, Binding[] bindings, Object ... values) {\r
46 \r
47         this(parent, name);\r
48 \r
49         buildProperties(this, propertyNames, bindings, values, properties);\r
50 \r
51     }\r
52 \r
53     private Map<String, Variable> buildProperties(Variable parent, String[] propertyNames, Binding[] bindings, Object ... values) {\r
54 \r
55         assert parent != null;\r
56         assert propertyNames.length == bindings.length;\r
57         assert propertyNames.length == values.length;\r
58 \r
59         Map<String, Variable> result = new HashMap<String, Variable>();\r
60         \r
61         for(int i=0;i<values.length;i++) {\r
62                 String property = propertyNames[i];\r
63                 result.put(property, new ConstantPropertyVariable(parent, property, values[i], bindings[i]));\r
64         }\r
65         \r
66         return result;\r
67 \r
68     }\r
69 \r
70     @Override\r
71     public Map<String, Variable> collectDomainProperties(ReadGraph graph, Map<String, Variable> properties) throws DatabaseException {\r
72         if(properties == null) properties = new HashMap<String,Variable>(this.properties.size());\r
73         properties.putAll(this.properties);\r
74         return properties;\r
75     }\r
76     \r
77     @Override\r
78     public Variable getPossibleDomainProperty(ReadGraph graph, String name) throws DatabaseException {\r
79         return properties.get(name);\r
80     }\r
81     \r
82     @Override\r
83     public Variable getParent(ReadGraph graph) throws DatabaseException {\r
84         return parent;\r
85     }\r
86     \r
87 //    @Override\r
88 //    public Object getSerialized(ReadGraph graph) throws DatabaseException {\r
89 //      return getName(graph);\r
90 //    }\r
91     \r
92     @Override\r
93     public String getName(ReadGraph graph) throws DatabaseException {\r
94         return name;\r
95     }\r
96 \r
97     @Override\r
98     public String getLabel(ReadGraph graph) throws DatabaseException {\r
99         Variable variable = getPossibleDomainProperty(graph, Variables.LABEL);\r
100         if(variable != null) return variable.getValue(graph);\r
101         else return name;\r
102     }\r
103 \r
104     @Override\r
105     public Resource getRepresents(ReadGraph graph) throws DatabaseException {\r
106         return null;\r
107 //      Variable variable = getPossibleDomainProperty(graph, Variables.REPRESENTS);\r
108 //      if(variable != null) return variable.getValue(graph);\r
109 //      else return null;\r
110     }\r
111 \r
112     @Override\r
113     public Resource getType(ReadGraph graph) throws DatabaseException {\r
114         return getDomainProperty(graph, Variables.TYPE).getValue(graph);\r
115     }\r
116 \r
117     @Override\r
118     public Resource getPossibleType(ReadGraph graph) throws DatabaseException {\r
119         Variable v = getPossibleDomainProperty(graph, Variables.TYPE);\r
120         return v != null ? v.<Resource>getPossibleValue(graph) : null;\r
121     }\r
122 \r
123     @Override\r
124     public Resource getType(ReadGraph graph, Resource baseType) throws DatabaseException {\r
125         Resource type = getPossibleType(graph, baseType);\r
126         if (type != null)\r
127                 return type;\r
128         throw new NoSingleResultException("variable " + getPossibleURI(graph) + " has no type");\r
129     }\r
130 \r
131     @Override\r
132     public Resource getPossibleType(ReadGraph graph, Resource baseType) throws DatabaseException {\r
133         Variable typev = getPossibleDomainProperty(graph, Variables.TYPE);\r
134         if (typev == null)\r
135                 return null;\r
136         Resource type = typev.getValue(graph);\r
137         return type != null && graph.isInheritedFrom(type, baseType) ? type : null;\r
138     }\r
139 \r
140     @Override\r
141     public Collection<Variable> getProperties(ReadGraph graph, String classification) throws DatabaseException {\r
142         Collection<Variable> result = null;\r
143         Variable singleResult = null;\r
144         for(ConstantPropertyVariable property : properties.values()) {\r
145                 if(property.getClassifications(graph).contains(classification)) {\r
146                         if(result == null && singleResult == null) singleResult = property;\r
147                         else if(result == null && singleResult != null) {\r
148                                 result = new ArrayList<Variable>();\r
149                                 result.add(singleResult);\r
150                                 result.add(property);\r
151                         } else {\r
152                                 result.add(property);\r
153                         }\r
154                 }\r
155         }\r
156         if(result != null) return result;\r
157         if(singleResult != null) return Collections.singletonList(singleResult);\r
158         else return Collections.emptyList();\r
159     } \r
160 \r
161 }\r