]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/function/All.java
Removed jsonValues since there already is Data/Json
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / function / All.java
index 6a144165247732a36e1d47aa08240c5fbf9c0f86..08ed3edf2650873f316c8ceb1228bcf89e1afee1 100644 (file)
@@ -3,6 +3,7 @@ package org.simantics.db.layer0.function;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -41,6 +42,7 @@ import org.simantics.db.common.validation.L0Validations;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.exception.DoesNotContainValueException;
 import org.simantics.db.exception.NoSingleResultException;
+import org.simantics.db.exception.RuntimeDatabaseException;
 import org.simantics.db.layer0.exception.MissingVariableValueException;
 import org.simantics.db.layer0.exception.PendingVariableException;
 import org.simantics.db.layer0.exception.VariableException;
@@ -48,6 +50,7 @@ import org.simantics.db.layer0.request.PossibleURI;
 import org.simantics.db.layer0.request.PropertyInfo;
 import org.simantics.db.layer0.request.PropertyInfoRequest;
 import org.simantics.db.layer0.request.UnescapedAssertedPropertyMapOfResource;
+import org.simantics.db.layer0.request.UnescapedMethodMapOfResource;
 import org.simantics.db.layer0.request.UnescapedPropertyMapOfResource;
 import org.simantics.db.layer0.scl.CompileResourceValueRequest;
 import org.simantics.db.layer0.scl.CompileValueRequest;
@@ -78,8 +81,10 @@ import org.simantics.db.service.UndoRedoSupport;
 import org.simantics.issues.ontology.IssueResource;
 import org.simantics.layer0.Layer0;
 import org.simantics.scl.reflection.annotations.SCLValue;
+import org.simantics.scl.runtime.SCLContext;
 import org.simantics.scl.runtime.function.Function4;
 import org.simantics.scl.runtime.function.FunctionImpl1;
+import org.simantics.scl.runtime.function.FunctionImpl2;
 import org.simantics.simulator.variable.exceptions.NodeManagerException;
 import org.simantics.utils.Development;
 import org.simantics.utils.datastructures.Pair;
@@ -596,6 +601,43 @@ public class All {
                
        };
        
+    @SCLValue(type = "VariableMap")
+       public static VariableMap methodsPropertyDomainProperties = new VariableMapImpl() {
+       
+               @Override
+               public Variable getVariable(ReadGraph graph, Variable context, String name) throws DatabaseException {
+                       Variable parent = context.getParent(graph);
+                       Resource container = parent.getPossibleRepresents(graph);
+                       Map<String,Resource> methods = graph.syncRequest(new UnescapedMethodMapOfResource(container));
+                       Resource predicate = methods.get(name);
+                       if(predicate != null) {
+                               Layer0 L0 = Layer0.getInstance(graph);
+                               PropertyInfo info = graph.syncRequest(new PropertyInfoRequest(L0.Entity_method));
+                               Resource value = graph.getSingleObject(container, predicate);
+                               return new StandardGraphPropertyVariable(context, null, container, info, value);
+                       }
+                       return null;
+               }
+
+               @Override
+               public Map<String, Variable> getVariables(ReadGraph graph, Variable context, Map<String, Variable> map) throws DatabaseException {
+                       Variable parent = context.getParent(graph);
+                       Resource container = parent.getPossibleRepresents(graph);
+                       Map<String,Resource> methods = graph.syncRequest(new UnescapedMethodMapOfResource(container));
+                       for(Map.Entry<String, Resource> entry : methods.entrySet()) {
+                               String name = entry.getKey();
+                               Resource predicate = entry.getValue();
+                               Layer0 L0 = Layer0.getInstance(graph);
+                               PropertyInfo info = graph.syncRequest(new PropertyInfoRequest(L0.Entity_method));
+                               Resource value = graph.getSingleObject(container, predicate);
+                               if(map == null) map = new HashMap<>();
+                               map.put(name, new StandardGraphPropertyVariable(context, null, container, info, value));
+                       }
+                       return map;
+               }
+               
+       };
+
        public static Variable getStandardPropertyDomainPropertyVariableFromValue(ReadGraph graph, Variable context, String name) throws DatabaseException {
 
                if(context instanceof StandardGraphPropertyVariable) {
@@ -1581,4 +1623,28 @@ public class All {
                return parent.node.support.manager.getPropertyURI(parent.node.node, node);
        }
     
+
+    @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")
+    public static Object defaultInstantiateUnder(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
+        return new FunctionImpl2<Resource, Resource, Resource>() {
+            public Resource apply(Resource container, Resource type) {
+                try {
+                    WriteGraph graph = (WriteGraph)SCLContext.getCurrent().get("graph");
+
+                    Layer0 L0 = Layer0.getInstance(graph);
+                    CommonDBUtils.selectClusterSet(graph, container);
+                    Resource result = graph.newResource();
+                    String name = NameUtils.findFreshInstanceName(graph, type, container);
+                    graph.claim(result, L0.InstanceOf, type);
+                    graph.addLiteral(result, L0.HasName, L0.NameOf, name, Bindings.STRING);
+                    graph.claim(container, L0.ConsistsOf, L0.PartOf, result);
+
+                    return result;
+                } catch (DatabaseException e) {
+                    throw new RuntimeDatabaseException(e);
+                }
+            }
+        };
+    }
+
 }
\ No newline at end of file