]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/ConstantChildVariable.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / variable / ConstantChildVariable.java
diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/ConstantChildVariable.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/ConstantChildVariable.java
new file mode 100644 (file)
index 0000000..2bfa386
--- /dev/null
@@ -0,0 +1,161 @@
+package org.simantics.db.layer0.variable;\r
+\r
+import gnu.trove.map.hash.THashMap;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.NoSingleResultException;\r
+\r
+public class ConstantChildVariable extends AbstractChildVariable {\r
+\r
+    final Variable parent;\r
+    final String name;\r
+    final Map<String, ConstantPropertyVariable> properties;\r
+\r
+    public ConstantChildVariable(Variable parent, String name) {\r
+        this.parent = parent;\r
+        this.name = name;\r
+        this.properties = new THashMap<String, ConstantPropertyVariable>();\r
+    }\r
+\r
+    public ConstantChildVariable(Variable parent, String name, Collection<ConstantPropertyVariableBuilder> propertyBuilders) {\r
+       this(parent, name);\r
+       for(ConstantPropertyVariableBuilder b : propertyBuilders) {\r
+               ConstantPropertyVariable property = b.build(this);\r
+               properties.put(b.getName(), property);\r
+       }\r
+    }\r
+    \r
+    public ConstantChildVariable(Variable parent, String name, ConstantPropertyVariableBuilder ... propertyBuilders) {\r
+       this(parent, name);\r
+       for(ConstantPropertyVariableBuilder b : propertyBuilders) {\r
+               ConstantPropertyVariable property = b.build(this);\r
+               properties.put(b.getName(), property);\r
+       }\r
+    }\r
+\r
+    public ConstantChildVariable(Variable parent, String name, String[] propertyNames, Binding[] bindings, Object ... values) {\r
+\r
+       this(parent, name);\r
+\r
+       buildProperties(this, propertyNames, bindings, values, properties);\r
+\r
+    }\r
+\r
+    private Map<String, Variable> buildProperties(Variable parent, String[] propertyNames, Binding[] bindings, Object ... values) {\r
+\r
+        assert parent != null;\r
+        assert propertyNames.length == bindings.length;\r
+        assert propertyNames.length == values.length;\r
+\r
+       Map<String, Variable> result = new HashMap<String, Variable>();\r
+       \r
+       for(int i=0;i<values.length;i++) {\r
+               String property = propertyNames[i];\r
+               result.put(property, new ConstantPropertyVariable(parent, property, values[i], bindings[i]));\r
+       }\r
+       \r
+       return result;\r
+\r
+    }\r
+\r
+    @Override\r
+    public Map<String, Variable> collectDomainProperties(ReadGraph graph, Map<String, Variable> properties) throws DatabaseException {\r
+       if(properties == null) properties = new HashMap<String,Variable>(this.properties.size());\r
+       properties.putAll(this.properties);\r
+       return properties;\r
+    }\r
+    \r
+    @Override\r
+    public Variable getPossibleDomainProperty(ReadGraph graph, String name) throws DatabaseException {\r
+       return properties.get(name);\r
+    }\r
+    \r
+    @Override\r
+    public Variable getParent(ReadGraph graph) throws DatabaseException {\r
+       return parent;\r
+    }\r
+    \r
+//    @Override\r
+//    public Object getSerialized(ReadGraph graph) throws DatabaseException {\r
+//     return getName(graph);\r
+//    }\r
+    \r
+    @Override\r
+    public String getName(ReadGraph graph) throws DatabaseException {\r
+       return name;\r
+    }\r
+\r
+    @Override\r
+    public String getLabel(ReadGraph graph) throws DatabaseException {\r
+       Variable variable = getPossibleDomainProperty(graph, Variables.LABEL);\r
+       if(variable != null) return variable.getValue(graph);\r
+       else return name;\r
+    }\r
+\r
+    @Override\r
+    public Resource getRepresents(ReadGraph graph) throws DatabaseException {\r
+       return null;\r
+//     Variable variable = getPossibleDomainProperty(graph, Variables.REPRESENTS);\r
+//     if(variable != null) return variable.getValue(graph);\r
+//     else return null;\r
+    }\r
+\r
+    @Override\r
+    public Resource getType(ReadGraph graph) throws DatabaseException {\r
+       return getDomainProperty(graph, Variables.TYPE).getValue(graph);\r
+    }\r
+\r
+    @Override\r
+    public Resource getPossibleType(ReadGraph graph) throws DatabaseException {\r
+       Variable v = getPossibleDomainProperty(graph, Variables.TYPE);\r
+       return v != null ? v.<Resource>getPossibleValue(graph) : null;\r
+    }\r
+\r
+    @Override\r
+    public Resource getType(ReadGraph graph, Resource baseType) throws DatabaseException {\r
+       Resource type = getPossibleType(graph, baseType);\r
+       if (type != null)\r
+               return type;\r
+       throw new NoSingleResultException("variable " + getPossibleURI(graph) + " has no type");\r
+    }\r
+\r
+    @Override\r
+    public Resource getPossibleType(ReadGraph graph, Resource baseType) throws DatabaseException {\r
+       Variable typev = getPossibleDomainProperty(graph, Variables.TYPE);\r
+       if (typev == null)\r
+               return null;\r
+       Resource type = typev.getValue(graph);\r
+       return type != null && graph.isInheritedFrom(type, baseType) ? type : null;\r
+    }\r
+\r
+    @Override\r
+    public Collection<Variable> getProperties(ReadGraph graph, String classification) throws DatabaseException {\r
+       Collection<Variable> result = null;\r
+       Variable singleResult = null;\r
+       for(ConstantPropertyVariable property : properties.values()) {\r
+               if(property.getClassifications(graph).contains(classification)) {\r
+                       if(result == null && singleResult == null) singleResult = property;\r
+                       else if(result == null && singleResult != null) {\r
+                               result = new ArrayList<Variable>();\r
+                               result.add(singleResult);\r
+                               result.add(property);\r
+                       } else {\r
+                               result.add(property);\r
+                       }\r
+               }\r
+       }\r
+       if(result != null) return result;\r
+       if(singleResult != null) return Collections.singletonList(singleResult);\r
+       else return Collections.emptyList();\r
+    } \r
+\r
+}\r