]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/StandardGraphChildVariable.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / variable / StandardGraphChildVariable.java
diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/StandardGraphChildVariable.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/StandardGraphChildVariable.java
new file mode 100644 (file)
index 0000000..2e8b09c
--- /dev/null
@@ -0,0 +1,335 @@
+package org.simantics.db.layer0.variable;\r
+\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.Map;\r
+import java.util.Set;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.datatypes.literal.GUID;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;\r
+import org.simantics.db.exception.AssumptionException;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.exception.InvalidVariableException;\r
+import org.simantics.db.layer0.exception.VariableException;\r
+import org.simantics.db.layer0.function.All;\r
+import org.simantics.db.layer0.request.ClassificationsRequest;\r
+import org.simantics.db.layer0.request.VariableURI;\r
+import org.simantics.db.layer0.variable.RVI.RVIPart;\r
+import org.simantics.db.layer0.variable.RVI.ResourceRVIPart;\r
+import org.simantics.db.layer0.variable.RVI.StringRVIPart;\r
+import org.simantics.db.layer0.variable.Variables.Role;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.simulator.variable.exceptions.NodeManagerException;\r
+\r
+public class StandardGraphChildVariable extends AbstractChildVariable {\r
+\r
+       /*\r
+        * Extension points\r
+        * \r
+        */\r
+       public Variable getPossibleSpecialChild(ReadGraph graph, String name) throws DatabaseException {\r
+               return null;\r
+       }\r
+\r
+       public Map<String, Variable> collectSpecialChildren(ReadGraph graph, Map<String, Variable> children) throws DatabaseException {\r
+               return children;\r
+       }\r
+\r
+       /*\r
+        * Standard implementation\r
+        * \r
+        */\r
+       \r
+       final protected Variable parent;\r
+       final public Resource resource;\r
+       \r
+       transient private int hash;\r
+       \r
+       public StandardGraphChildVariable(Variable parent, VariableNode node, Resource resource) {\r
+               super(node);\r
+               this.parent = parent;\r
+               this.resource = resource;\r
+       }\r
+\r
+       @Override\r
+       public void validate(ReadGraph graph) throws DatabaseException {\r
+               if(!graph.hasStatement(resource)) throw new InvalidVariableException("The resource has been removed: " + resource);\r
+       }\r
+       \r
+       @Override\r
+       protected Variable getPossibleDomainProperty(ReadGraph graph, String name) throws DatabaseException {\r
+               VariableMap map = getPossiblePropertyVariableMap(graph);\r
+               if(map == null) return null;\r
+               try {\r
+                       return map.getVariable(graph, this, name);\r
+               } catch (DatabaseException e) {\r
+                       return null;\r
+               }\r
+       }\r
+\r
+       @Override\r
+       public Variable getPossibleChild(ReadGraph graph, String name) throws DatabaseException {\r
+               VariableMap map = getPossibleChildVariableMap(graph);\r
+               if(map == null) return getPossibleSpecialChild(graph, name);\r
+               try {\r
+                       Variable result = map.getVariable(graph, this, name);\r
+                       if(result != null) return result;\r
+                       else return getPossibleSpecialChild(graph, name);\r
+               } catch (DatabaseException e) {\r
+                       return null;\r
+               }\r
+       }\r
+\r
+       @Override\r
+       public Map<String, Variable> collectDomainProperties(ReadGraph graph, Map<String, Variable> properties) throws DatabaseException {\r
+               VariableMap map = getPossiblePropertyVariableMap(graph);\r
+               if(map == null) return properties;\r
+               return map.getVariables(graph, this, properties);\r
+       }\r
+\r
+       @Override\r
+       public Collection<Variable> getChildren(ReadGraph graph) throws DatabaseException {\r
+               Map<String, Variable> result = null;\r
+               VariableMap map = getPossibleChildVariableMap(graph);\r
+               if(map != null) result = map.getVariables(graph, this, result);\r
+               result = collectSpecialChildren(graph, result);\r
+               if(result == null) return Collections.emptyList();\r
+               else return result.values();\r
+       }\r
+\r
+       @SuppressWarnings("unchecked")\r
+       @Override\r
+       public String getName(ReadGraph graph) throws DatabaseException {\r
+           if(node != null) {\r
+               return node.support.manager.getName(node.node);\r
+           }\r
+               String unescapedName = graph.getPossibleRelatedValue(resource, graph.getService(Layer0.class).HasName, Bindings.STRING);\r
+               if(unescapedName == null) return "r" + resource.getResourceId();\r
+               return unescapedName;\r
+       }\r
+\r
+       @Override\r
+       public Variable getNameVariable(ReadGraph graph) throws DatabaseException {\r
+               Resource resource = (Resource) getPossibleRepresents(graph);\r
+               if (resource != null) {\r
+                       return new StandardGraphPropertyVariable(graph, this, null, resource, Layer0.getInstance(graph).HasName);\r
+               }\r
+               return super.getNameVariable(graph);\r
+       }\r
+       \r
+       \r
+       @Override\r
+       public String getLabel(ReadGraph graph) throws DatabaseException {\r
+           if(resource == null) return getName(graph);\r
+               String label = graph.getPossibleRelatedValue2(resource, graph.getService(Layer0.class).HasLabel);\r
+               return label != null ? label : getName(graph);\r
+       }\r
+\r
+       @Override\r
+       final protected Variable getLabelVariable(ReadGraph graph) throws DatabaseException {\r
+               Layer0 L0 = Layer0.getInstance(graph);\r
+               if(resource == null) return null;\r
+               return new StandardGraphPropertyVariable(graph, this, L0.HasLabel);\r
+       }\r
+       \r
+//     @Override\r
+//     final public Object getSerialized(ReadGraph graph) throws DatabaseException {\r
+//             Resource represents = getRepresents(graph);\r
+//             if(represents != null) return represents;\r
+//             else return getName(graph);\r
+//     }\r
+\r
+       @Override\r
+       public Variable getParent(ReadGraph graph) throws DatabaseException {\r
+               return parent;\r
+       }\r
+       \r
+       @Override\r
+       final public Resource getRepresents(ReadGraph graph) throws DatabaseException {\r
+           if(resource == null)\r
+               throw new VariableException("Variable is not represented by any resource (URI=" + getPossibleURI(graph) + ").");\r
+           return resource;\r
+//             Layer0X L0X = Layer0X.getInstance(graph);\r
+//             Resource represents = graph.getPossibleObject(resource, L0X.Represents);\r
+//             if(represents != null) return represents;\r
+//             else return resource;\r
+       }\r
+\r
+       @Override\r
+       public Resource getPossibleRepresents(ReadGraph graph)\r
+               throws DatabaseException {\r
+           return resource;\r
+       }\r
+       \r
+       @Override\r
+       final public String getURI(ReadGraph graph) throws DatabaseException {\r
+               \r
+               try {\r
+                       if(parent == null) return "http:/";\r
+                       String parentURI = graph.syncRequest(new VariableURI(parent), TransientCacheAsyncListener.<String>instance());\r
+                       return parentURI + Role.CHILD.getIdentifier() + encodeString(getName(graph));\r
+               } catch (AssumptionException e) {\r
+                       throw new InvalidVariableException(e);\r
+               }\r
+       }\r
+\r
+       /**\r
+        * All variables must have proper URIs, this is only for debugging purposes.\r
+        * \r
+        * @see org.simantics.db.layer0.variable.AbstractVariable#getPossibleURI(org.simantics.db.ReadGraph)\r
+        */\r
+       @Override\r
+       public String getPossibleURI(ReadGraph graph) throws DatabaseException {\r
+        try {\r
+            return getURI(graph);\r
+        } catch (DatabaseException e) {\r
+            return "<no uri>";\r
+        }\r
+       }\r
+\r
+       @Override\r
+       public int hashCode() {\r
+               if(hash == 0) {\r
+                       final int prime = 31;\r
+                       int result = 1;\r
+                       result = prime * result + (parent != null ? parent.hashCode() : 0);\r
+                       result = prime * result + (node != null ? node.hashCode() : 0);\r
+                       result = prime * result + (resource != null ? resource.hashCode() : 0);\r
+                       hash =result;\r
+               }\r
+               return hash;\r
+       }\r
+\r
+       @Override\r
+       public boolean equals(Object obj) {\r
+               if (this == obj)\r
+                       return true;\r
+               if (obj == null)\r
+                       return false;\r
+               if (getClass() != obj.getClass())\r
+                       return false;\r
+               \r
+               StandardGraphChildVariable other = (StandardGraphChildVariable) obj;\r
+               \r
+               if(node != null) { \r
+                       if(!node.equals(other.node)) return false;\r
+               } else if (other.node != null) return false;\r
+\r
+        if(resource != null) {\r
+               if(!resource.equals(other.resource)) return false;\r
+        } else if (other.resource != null) return false;\r
+               \r
+        if(parent != null) {\r
+               if(!parent.equals(other.parent)) return false;\r
+        } else if (other.parent != null) return false;\r
+        \r
+        return true;\r
+               \r
+       }\r
+       \r
+       @Override\r
+       public <T> T adapt(ReadGraph graph, Class<T> clazz) throws DatabaseException {\r
+               return graph.adapt(resource, clazz);\r
+       }\r
+\r
+    @Override\r
+    public <T> T adaptPossible(ReadGraph graph, Class<T> clazz) throws DatabaseException {\r
+        return graph.getPossibleAdapter(resource, clazz);\r
+    }\r
+\r
+       @Override\r
+       public RVIPart getRVIPart(ReadGraph graph) throws DatabaseException {\r
+           if(resource == null) return new StringRVIPart(Role.CHILD, getName(graph));\r
+        Layer0 L0 = Layer0.getInstance(graph);\r
+        GUID id = graph.getPossibleRelatedValue(resource, L0.identifier, GUID.BINDING);\r
+        if(id != null)\r
+               return new RVI.GuidRVIPart(Role.CHILD, resource, id.mostSignificant, id.leastSignificant);\r
+        else\r
+               return new ResourceRVIPart(Role.CHILD, resource);\r
+       }\r
+       \r
+    public String getIdentifier() {\r
+       return getClass().getSimpleName() + "[" + resource + "]";\r
+    }\r
+       \r
+       protected VariableMap getPossiblePropertyVariableMap(ReadGraph graph) throws DatabaseException {\r
+        if(resource == null) return All.standardChildDomainProperties;\r
+               return graph.getPossibleRelatedValue2(resource, Layer0.getInstance(graph).domainProperties, this);\r
+       }\r
+\r
+       protected VariableMap getPossibleChildVariableMap(ReadGraph graph) throws DatabaseException {\r
+           if(resource == null) return All.standardChildDomainChildren;\r
+               return graph.getPossibleRelatedValue2(resource, Layer0.getInstance(graph).domainChildren, this);\r
+       }\r
+       \r
+       @Override\r
+    public Map<String, Variable> collectDomainProperties(ReadGraph graph, String classification, Map<String, Variable> properties) throws DatabaseException {\r
+       VariableMap valueMap = getPossiblePropertyVariableMap(graph);\r
+               if(valueMap == null) return properties;\r
+               return valueMap.getVariables(graph, this, classification, properties);\r
+    }\r
+       \r
+    @Override\r
+    public Variable resolve(ReadGraph graph, RVIPart part) throws DatabaseException {\r
+        Resource represents = getPossibleRepresents(graph);\r
+        if (represents == null)\r
+            return StandardRVIResolver.INSTANCE.getVariable(graph, this, part);\r
+        RVIResolver resolver = graph.adapt(represents, RVIResolver.class);\r
+        return resolver.getVariable(graph, this, part);\r
+    }    \r
+\r
+    @Override\r
+    public Variable resolvePossible(ReadGraph graph, RVIPart part) throws DatabaseException {\r
+        try {\r
+            return resolve(graph, part);\r
+        } catch (DatabaseException e) {\r
+            return null;\r
+        }\r
+    }\r
+\r
+    @SuppressWarnings("unchecked")\r
+    public Set<String> getClassifications(ReadGraph graph) throws DatabaseException {\r
+        Resource represents = getPossibleRepresents(graph);\r
+        Set<String> result = (represents != null) ? graph.syncRequest(new ClassificationsRequest(graph.getPrincipalTypes(represents))) : Collections.<String>emptySet();\r
+        if (result.isEmpty()) {\r
+               Resource type = getPossibleType(graph);\r
+               if(type != null) result = graph.syncRequest(new ClassificationsRequest(Collections.singleton(type)));\r
+        }\r
+        if (result.isEmpty() && node != null) {\r
+            try {\r
+                result = node.support.manager.getClassifications(node.node);\r
+            } catch(NodeManagerException e) {\r
+                throw new DatabaseException(e);\r
+            }\r
+        }\r
+        return result;\r
+    }\r
+\r
+    @Override\r
+    public RVI getRVI(ReadGraph graph) throws DatabaseException {\r
+        Resource represents = getPossibleRepresents(graph);\r
+        if (represents == null)\r
+            return StandardRVIResolver.INSTANCE.getRVI(graph, this);\r
+        RVIResolver resolver = graph.adapt(represents, RVIResolver.class);\r
+        return resolver.getRVI(graph, this);\r
+    }    \r
+    \r
+    @Override\r
+    public RVI getPossibleRVI(ReadGraph graph) throws DatabaseException {\r
+        Resource represents = getPossibleRepresents(graph);\r
+        if (represents == null)\r
+            return StandardRVIResolver.INSTANCE.getPossibleRVI(graph, this);\r
+        RVIResolver resolver = graph.getPossibleAdapter(represents, RVIResolver.class);\r
+        if(resolver == null) return null;\r
+        return resolver.getPossibleRVI(graph, this);\r
+    }\r
+\r
+    @Override\r
+    public Resource getPossiblePredicateResource(ReadGraph graph) throws DatabaseException {\r
+       return null;\r
+    }\r
+       \r
+}\r