]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Variable optimizations for documents (Simupedia) 02/2402/1
authorAntti Villberg <antti.villberg@semantum.fi>
Wed, 31 Oct 2018 05:00:00 +0000 (07:00 +0200)
committerAntti Villberg <antti.villberg@semantum.fi>
Wed, 31 Oct 2018 05:00:00 +0000 (07:00 +0200)
More control over computational values served by ReadGraph.

gitlab #169

Change-Id: I304de13f97c25661fed2905e33887e315144591e

27 files changed:
bundles/org.simantics.db.common/src/org/simantics/db/common/request/AdaptValue.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/ReadGraphImpl.java
bundles/org.simantics.db.layer0/adapters.xml
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/ContextualRelatedValue.java [new file with mode: 0644]
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/ReflectionComputationalValue.java [new file with mode: 0644]
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/SCLComputationalValue.java [new file with mode: 0644]
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/scl/AbstractExpressionCompilationRequest.java
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/scl/CompileResourceValueRequest.java
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/scl/CompileValueRequest.java
bundles/org.simantics.db/src/org/simantics/db/ComputationalValue.java [new file with mode: 0644]
bundles/org.simantics.db/src/org/simantics/db/ConverterComputationalValue.java [new file with mode: 0644]
bundles/org.simantics.document.server/adapters.xml
bundles/org.simantics.document.server/src/org/simantics/document/server/HandlerSCLComputationalValue.java [new file with mode: 0644]
bundles/org.simantics.document.server/src/org/simantics/document/server/SCLComputationalValue.java [new file with mode: 0644]
bundles/org.simantics.document.server/src/org/simantics/document/server/request/ServerSCLHandlerValueRequest.java
bundles/org.simantics.document.server/src/org/simantics/document/server/request/ServerSCLValueRequest.java
bundles/org.simantics.layer0/graph/Layer0SCL.pgraph
bundles/org.simantics.layer0/graph/Layer0Values.pgraph
bundles/org.simantics.modeling/adapters.xml
bundles/org.simantics.modeling/src/org/simantics/modeling/SCLComputationalValue.java [new file with mode: 0644]
bundles/org.simantics.modeling/src/org/simantics/modeling/scl/CompileSCLQueryRequest.java
bundles/org.simantics.modeling/src/org/simantics/modeling/scl/CompileSCLValueRequest.java
bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/function/CompileSCLValueRequest.java
bundles/org.simantics.structural2/src/org/simantics/structural2/Functions.java
bundles/org.simantics.structural2/src/org/simantics/structural2/scl/AbstractCompileStructuralValueRequest.java
bundles/org.simantics.structural2/src/org/simantics/structural2/scl/CompileProceduralExpressionValueRequest.java
bundles/org.simantics.structural2/src/org/simantics/structural2/scl/CompileStructuralValueRequest.java

index 02877596b00d4423fac8387a31bdc73cdde76e87..b3eba37fbdb4a0e395a90ea75c21a82abd1ad089 100644 (file)
@@ -1,13 +1,12 @@
 package org.simantics.db.common.request;
 
+import org.simantics.db.ComputationalValue;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.common.utils.Functions;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.exception.RuntimeDatabaseException;
 import org.simantics.layer0.Layer0;
-import org.simantics.scl.reflection.ReflectionUtils;
-import org.simantics.scl.reflection.ValueNotFoundException;
 import org.simantics.scl.runtime.function.FunctionImpl3;
 
 /**
@@ -20,7 +19,7 @@ public class AdaptValue extends ResourceRead<Object> {
         super(resource);
     }
 
-    private static final FunctionImpl3<ReadGraph,Resource,Object,Object> functionApplication = new FunctionImpl3<ReadGraph,Resource,Object,Object>() {
+    public static final FunctionImpl3<ReadGraph,Resource,Object,Object> functionApplication = new FunctionImpl3<ReadGraph,Resource,Object,Object>() {
 
                @Override
                public Object apply(ReadGraph graph, Resource resource, Object context) {
@@ -36,12 +35,10 @@ public class AdaptValue extends ResourceRead<Object> {
     @Override
     public Object perform(ReadGraph graph) throws DatabaseException {
         String uri = graph.getURI(resource);        
-        try {
-                       if(Layer0.URIs.Functions_functionApplication.equals(uri)) return functionApplication;
-            return ReflectionUtils.getValue(uri).getValue();
-        } catch (ValueNotFoundException e) {
-            throw new DatabaseException("Couldn't adapt the value " + uri, e);
-        }
+        if(Layer0.URIs.Functions_functionApplication.equals(uri)) return functionApplication;
+        ComputationalValue ev = graph.adapt(resource, ComputationalValue.class);
+        return ev.getValue(graph, resource);
+
     }
 
 }
index 3feb22e00ebda688379b6ed7c8785b09fb8035ca..2ef40c29459d4be20009b30a73f54bd85b6fb08f 100644 (file)
@@ -45,6 +45,7 @@ import org.simantics.databoard.type.Datatype;
 import org.simantics.databoard.util.binary.BinaryFile;
 import org.simantics.databoard.util.binary.RandomAccessBinary;
 import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.ComputationalValue;
 import org.simantics.db.DevelopmentKeys;
 import org.simantics.db.ExternalValueSupport;
 import org.simantics.db.ReadGraph;
@@ -6102,12 +6103,18 @@ public class ReadGraphImpl implements AsyncReadGraph {
                        return getValue(r, binding);
                }
        } else if(types.contains(L0.ExternalValue)) {
-               try {
-                       return (T)ReflectionUtils.getValue(getURI(r)).getValue();
-               } catch(ValueNotFoundException e) {
-                       throw new DatabaseException(e);
-               } catch(ClassCastException e) {
-                       throw new DatabaseException(e);
+               ComputationalValue cv = syncRequest(new PossibleAdapter<ComputationalValue>(r, ComputationalValue.class), TransientCacheAsyncListener.instance());
+               if(cv != null) {
+                       return cv.getValue(this, r);
+               } else {
+                       // This should not even be possible since L0 defines an adapter for all values
+                       try {
+                               return (T)ReflectionUtils.getValue(getURI(r)).getValue();
+                       } catch(ValueNotFoundException e) {
+                               throw new DatabaseException(e);
+                       } catch(ClassCastException e) {
+                               throw new DatabaseException(e);
+                       }
                }
        }
        else {
index 9e85e9a1bc15ab584f3522933b012ae60c468be2..c2071e5c5400a00bb5b17c7367bae616dc4303d8 100644 (file)
                </resource>
        </target>
        
+    <target interface="org.simantics.db.ComputationalValue">
+        <!-- TODO: should be ExternalValue but handle all values to minimise regressions for the time being  -->
+        <type uri="http://www.simantics.org/Layer0-0.0/Value"
+            class="org.simantics.db.layer0.adapter.ReflectionComputationalValue">
+        </type>
+        <resource uri="http://www.simantics.org/Layer0-0.0/Functions/sclValue"
+            class="org.simantics.db.layer0.adapter.SCLComputationalValue">
+        </resource>
+    </target>
+       
 </adapters>
diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/ContextualRelatedValue.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/ContextualRelatedValue.java
new file mode 100644 (file)
index 0000000..b5f939a
--- /dev/null
@@ -0,0 +1,55 @@
+package org.simantics.db.layer0.adapter;
+
+import org.simantics.db.ConverterComputationalValue;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.exception.RuntimeDatabaseException;
+import org.simantics.db.layer0.variable.Variable;
+import org.simantics.scl.runtime.SCLContext;
+import org.simantics.scl.runtime.function.Function1;
+import org.simantics.scl.runtime.function.FunctionImpl3;
+
+abstract public class ContextualRelatedValue implements ConverterComputationalValue {
+
+    @SuppressWarnings("unchecked")
+       @Override
+    public <T> T getValue(ReadGraph graph, Resource resource) throws DatabaseException {
+        
+        return (T)new FunctionImpl3<ReadGraph, Resource, Object, Object>() {
+
+            @Override
+            public Object apply(ReadGraph graph, Resource converter, Object context) {
+                SCLContext sclContext = SCLContext.getCurrent();
+                Object oldGraph = sclContext.get("graph");
+                try {
+                    if(context instanceof Variable) {
+                        Variable variable = (Variable)context;
+                        try {
+                            Function1<Object,Object> fn = getFunction(graph, variable.getParent(graph).getRepresents(graph), variable.getRepresents(graph), variable.getPredicateResource(graph));
+                            sclContext.put("graph", graph);
+                            return fn.apply(variable);
+                        } catch (DatabaseException e) {
+                            throw new RuntimeDatabaseException(e);
+                        }
+                    } if (context instanceof Resource) {
+                        Resource resource = (Resource)context;
+                        try {
+                               Function1<Object,Object> fn = getFunction(graph, null, resource, null);
+                            return fn.apply(resource);
+                        } catch (DatabaseException e) {
+                            throw new RuntimeDatabaseException(e);
+                        }
+                    } else {
+                        throw new IllegalStateException("Unknown context " + context);
+                    }
+                } finally {
+                    sclContext.put("graph", oldGraph);
+                }
+            }
+
+        };
+        
+    }
+
+}
diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/ReflectionComputationalValue.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/ReflectionComputationalValue.java
new file mode 100644 (file)
index 0000000..809dbd0
--- /dev/null
@@ -0,0 +1,26 @@
+package org.simantics.db.layer0.adapter;
+
+import org.simantics.db.ComputationalValue;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.scl.reflection.ReflectionUtils;
+import org.simantics.scl.reflection.ValueNotFoundException;
+
+public class ReflectionComputationalValue implements ComputationalValue {
+
+       @SuppressWarnings("unchecked")
+       @Override
+       public <T> T getValue(ReadGraph graph, Resource resource) throws DatabaseException {
+               
+               try {
+                       return (T)ReflectionUtils.getValue(graph.getURI(resource)).getValue();
+               } catch(ValueNotFoundException e) {
+                       throw new DatabaseException(e);
+               } catch(ClassCastException e) {
+                       throw new DatabaseException(e);
+               }
+               
+       }
+
+}
diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/SCLComputationalValue.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/SCLComputationalValue.java
new file mode 100644 (file)
index 0000000..f91a83a
--- /dev/null
@@ -0,0 +1,24 @@
+package org.simantics.db.layer0.adapter;
+
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.scl.CompileResourceValueRequest;
+import org.simantics.db.layer0.scl.CompileValueRequest;
+import org.simantics.scl.runtime.function.Function1;
+
+public class SCLComputationalValue extends ContextualRelatedValue {
+
+       @Override
+       public Function1<Object,Object> getFunction(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException {
+           if(s != null && p != null && o != null) {
+               return CompileValueRequest.compile(graph, s, o, p);    
+           } else if (o != null) {
+            return CompileResourceValueRequest.compile(graph, o);    
+           } else {
+               throw new DatabaseException("Could not compile SCL expression: s=" + s+ " p=" + p + " o=" + o);
+           }
+               
+       }
+
+}
index d40f82d4dc1219055b83fe4c722edff93703d63e..21758a2e559d4191e2879a9a1baa0952edcc6449 100644 (file)
@@ -5,6 +5,7 @@ import java.util.List;
 
 import org.simantics.databoard.Bindings;
 import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.exception.RuntimeDatabaseException;
 import org.simantics.db.request.Read;
@@ -226,9 +227,9 @@ implements Read<Function1<EvaluationContext,Object>> {
             return base;
     }
     
-    protected static String resolveExpectedValueType(ReadGraph graph, org.simantics.db.layer0.variable.Variable context) throws DatabaseException {
+    protected static String resolveExpectedValueType(ReadGraph graph, Resource predicate) throws DatabaseException {
         Layer0 L0 = Layer0.getInstance(graph);
-        String valueType = graph.getPossibleRelatedValue(context.getPredicateResource(graph), L0.RequiresValueType, Bindings.STRING);
-        return valueType;
+        return graph.getPossibleRelatedValue(predicate, L0.RequiresValueType, Bindings.STRING);
     }
+    
 }
index c8d168f50320b547966e8bccb95d5473bf071ac5..e56c3d5cef0bf33b02be0a7a723cdf9c80c63064 100644 (file)
@@ -24,7 +24,7 @@ import org.simantics.scl.runtime.function.Function1;
  * 
  * @author Antti Villberg
  */
-public class CompileResourceValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Resource> {
+public class CompileResourceValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Object> {
 
     public static class CompilationContext extends AbstractExpressionCompilationContext {
         public CompilationContext(RuntimeEnvironment runtimeEnvironment) {
@@ -42,8 +42,8 @@ public class CompileResourceValueRequest extends AbstractExpressionCompilationRe
         SCLContext sclContext = SCLContext.getCurrent();
         Object oldGraph = sclContext.get("graph");
         try {
-            Function1<Resource,Object> exp = graph.syncRequest(new CompileResourceValueRequest(literal),
-                    TransientCacheListener.<Function1<Resource,Object>>instance());
+            Function1<Object,Object> exp = graph.syncRequest(new CompileResourceValueRequest(literal),
+                    TransientCacheListener.instance());
             sclContext.put("graph", graph);
             return exp.apply(literal);
         } catch (DatabaseException e) {
@@ -55,6 +55,10 @@ public class CompileResourceValueRequest extends AbstractExpressionCompilationRe
         }
     }
 
+    public static Function1<Object,Object> compile(ReadGraph graph, Resource literal) throws DatabaseException {
+        return graph.syncRequest(new CompileResourceValueRequest(literal), TransientCacheListener.instance());
+    }
+
     @Override
     protected String getExpressionText(ReadGraph graph)
             throws DatabaseException {
index e1804d790fc327c6713cc5e7bdf41b78b84c8b91..caa60900cfbd8d99729f2cef4684e78467bf1e35 100644 (file)
@@ -25,7 +25,7 @@ import org.simantics.scl.runtime.function.Function1;
  * 
  * @author Tuukka Lehtonen
  */
-public class CompileValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Variable> {
+public class CompileValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Object> {
 
     public static class CompilationContext extends AbstractExpressionCompilationContext {
         public CompilationContext(RuntimeEnvironment runtimeEnvironment) {
@@ -53,8 +53,8 @@ public class CompileValueRequest extends AbstractExpressionCompilationRequest<Co
         SCLContext sclContext = SCLContext.getCurrent();
         Object oldGraph = sclContext.get("graph");
         try {
-            Function1<Variable,Object> exp = graph.syncRequest(new CompileValueRequest(graph, context),
-                    TransientCacheListener.<Function1<Variable,Object>>instance());
+            Function1<Object,Object> exp = graph.syncRequest(new CompileValueRequest(graph, context),
+                    TransientCacheListener.instance());
             sclContext.put("graph", graph);
             return exp.apply(context);
         } catch (DatabaseException e) {
@@ -66,6 +66,23 @@ public class CompileValueRequest extends AbstractExpressionCompilationRequest<Co
         }
     }
 
+    public static Function1<Object,Object> compile(ReadGraph graph, Resource component, Resource literal, Resource predicate) throws DatabaseException {
+        SCLContext sclContext = SCLContext.getCurrent();
+        Object oldGraph = sclContext.get("graph");
+        try {
+            Function1<Object,Object> exp = graph.syncRequest(new CompileValueRequest(component, literal, predicate),
+                    TransientCacheListener.instance());
+            sclContext.put("graph", graph);
+            return exp;
+        } catch (DatabaseException e) {
+            throw (DatabaseException)e;
+        } catch (Throwable t) {
+            throw new DatabaseException(t);
+        } finally {
+            sclContext.put("graph", oldGraph);
+        }
+    }
+    
     @Override
     protected String getExpressionText(ReadGraph graph)
             throws DatabaseException {
diff --git a/bundles/org.simantics.db/src/org/simantics/db/ComputationalValue.java b/bundles/org.simantics.db/src/org/simantics/db/ComputationalValue.java
new file mode 100644 (file)
index 0000000..48a3bf2
--- /dev/null
@@ -0,0 +1,9 @@
+package org.simantics.db;
+
+import org.simantics.db.exception.DatabaseException;
+
+public interface ComputationalValue {
+
+       public <T> T getValue(ReadGraph graph, Resource resource) throws DatabaseException;
+       
+}
diff --git a/bundles/org.simantics.db/src/org/simantics/db/ConverterComputationalValue.java b/bundles/org.simantics.db/src/org/simantics/db/ConverterComputationalValue.java
new file mode 100644 (file)
index 0000000..15da827
--- /dev/null
@@ -0,0 +1,16 @@
+package org.simantics.db;
+
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.scl.runtime.function.Function1;
+
+public interface ConverterComputationalValue extends ComputationalValue {
+       
+    /*
+     * This computes the expression function that shall be called with given context as defined in ReadGraph.getRelatedValue2 
+     * Context can be Resource (literal) or Variable. 
+     * With Resource context this gets called with o != null and s==null and p==null 
+     * With Variable property context this gets called with (represents of parent, represents, predicate resource) 
+     */
+       Function1<Object,Object> getFunction(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException;
+       
+}
index 7a14760250c6d15b15bbf93d9be342004a35b114..76622e38b458e8b0835a930b63ed3b00a7a05b72 100644 (file)
     <target interface="org.simantics.db.layer0.variable.VariableBuilder">
         <type uri="http://www.simantics.org/Modeling-1.2/SCLCommandSession" class="org.simantics.document.server.state.StateVariableBuilder" />
     </target>
+    <target interface="org.simantics.db.ComputationalValue">
+        <resource uri="http://www.simantics.org/Documentation-0.0/Functions/sclValue"
+            class="org.simantics.document.server.SCLComputationalValue">
+        </resource>
+    </target>
+    <target interface="org.simantics.db.ComputationalValue">
+        <resource uri="http://www.simantics.org/Documentation-0.0/Functions/sclHandlerValue"
+            class="org.simantics.document.server.HandlerSCLComputationalValue">
+        </resource>
+    </target>
 </adapters>
\ No newline at end of file
diff --git a/bundles/org.simantics.document.server/src/org/simantics/document/server/HandlerSCLComputationalValue.java b/bundles/org.simantics.document.server/src/org/simantics/document/server/HandlerSCLComputationalValue.java
new file mode 100644 (file)
index 0000000..2e54138
--- /dev/null
@@ -0,0 +1,17 @@
+package org.simantics.document.server;
+
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.adapter.ContextualRelatedValue;
+import org.simantics.document.server.request.ServerSCLHandlerValueRequest;
+import org.simantics.scl.runtime.function.Function1;
+
+public class HandlerSCLComputationalValue extends ContextualRelatedValue {
+
+       @Override
+       public Function1<Object,Object> getFunction(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException {
+               return ServerSCLHandlerValueRequest.compile(graph, s, o, p);
+       }
+
+}
diff --git a/bundles/org.simantics.document.server/src/org/simantics/document/server/SCLComputationalValue.java b/bundles/org.simantics.document.server/src/org/simantics/document/server/SCLComputationalValue.java
new file mode 100644 (file)
index 0000000..028c30f
--- /dev/null
@@ -0,0 +1,17 @@
+package org.simantics.document.server;
+
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.adapter.ContextualRelatedValue;
+import org.simantics.document.server.request.ServerSCLValueRequest;
+import org.simantics.scl.runtime.function.Function1;
+
+public class SCLComputationalValue extends ContextualRelatedValue {
+
+       @Override
+       public Function1<Object,Object> getFunction(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException {
+               return ServerSCLValueRequest.compile(graph, s, o, p);
+       }
+
+}
index 4a4164303613c1a2c9d963693804d727305cdb2a..e114f745f208e6181849c259d2a6f3afc0d31d74 100644 (file)
@@ -8,12 +8,14 @@ import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
 import org.simantics.db.common.request.IndexRoot;
+import org.simantics.db.common.request.PossibleTypedParent;
+import org.simantics.db.common.request.UnaryRead;
 import org.simantics.db.exception.DatabaseException;
-import org.simantics.db.layer0.request.VariableRead;
 import org.simantics.db.layer0.scl.AbstractExpressionCompilationContext;
 import org.simantics.db.layer0.scl.AbstractExpressionCompilationRequest;
 import org.simantics.db.layer0.util.RuntimeEnvironmentRequest2;
 import org.simantics.db.layer0.variable.Variable;
+import org.simantics.document.base.ontology.DocumentationResource;
 import org.simantics.document.server.request.ServerSCLHandlerValueRequest.CompilationContext;
 import org.simantics.layer0.Layer0;
 import org.simantics.scl.compiler.elaboration.expressions.EApply;
@@ -35,9 +37,8 @@ import org.simantics.structural2.scl.FindPossibleComponentTypeRequest;
 import org.simantics.structural2.scl.ReadComponentTypeInterfaceRequest;
 import org.simantics.utils.datastructures.Pair;
 
-public class ServerSCLHandlerValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Variable> {
+public class ServerSCLHandlerValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Object> {
 
-       private final Variable context;
        private final Pair<Resource,Resource> componentTypeAndRoot;
        private final Resource literal;
        protected String possibleExpectedValueType;
@@ -52,18 +53,21 @@ public class ServerSCLHandlerValueRequest extends AbstractExpressionCompilationR
                }
        }
 
-       private ServerSCLHandlerValueRequest(Variable context, Pair<Resource,Resource> componentTypeAndRoot, Resource literal, String possibleExpectedValueType) {
-               assert(literal != null);
-               this.context = context;
-               this.literal = literal;
-               this.componentTypeAndRoot = componentTypeAndRoot;
-               this.possibleExpectedValueType = possibleExpectedValueType;
+       private ServerSCLHandlerValueRequest(Pair<Resource,Resource> componentTypeAndRoot, Resource literal, String possibleExpectedValueType) {
+           assert(literal != null);
+           this.literal = literal;
+           this.componentTypeAndRoot = componentTypeAndRoot;
+           this.possibleExpectedValueType = possibleExpectedValueType;
        }
 
-       public ServerSCLHandlerValueRequest(ReadGraph graph, Variable context) throws DatabaseException {
-               this(context, getComponentTypeAndRoot(graph, context), context.getRepresents(graph), resolveExpectedValueType(graph, context));
-       }
+    public ServerSCLHandlerValueRequest(ReadGraph graph, Variable context) throws DatabaseException {
+        this(getComponentTypeAndRoot(graph, context), context.getRepresents(graph), resolveExpectedValueType(graph, context.getPredicateResource(graph)));
+    }
 
+    public ServerSCLHandlerValueRequest(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException {
+        this(getComponentTypeAndRoot(graph, s), o, resolveExpectedValueType(graph, p));
+    }
+       
        private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Variable property)  throws DatabaseException {
                Variable parent = property.getParent(graph);
                Resource represents = parent.getRepresents(graph);
@@ -79,6 +83,21 @@ public class ServerSCLHandlerValueRequest extends AbstractExpressionCompilationR
                return Pair.make(parent.getType(graph), root);
        }
 
+    private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Resource component)  throws DatabaseException {
+        if(component != null) {
+            Resource type = graph.syncRequest(new FindPossibleComponentTypeRequest(component));
+            if(type != null) {
+                Resource root = graph.syncRequest(new IndexRoot(type));
+                return Pair.make(type, root);
+            } else {
+                Resource doc = graph.syncRequest(new PossibleTypedParent(component, DocumentationResource.getInstance(graph).Document));
+                Resource componentType = graph.getSingleType(doc); 
+                Resource root = graph.syncRequest(new IndexRoot(doc));
+                return Pair.make(componentType, root);
+            }
+        }
+        throw new IllegalStateException();
+    }
 
     public static List<TCon> getEffects(ReadGraph graph, Variable context) throws DatabaseException {
         try {
@@ -95,8 +114,8 @@ public class ServerSCLHandlerValueRequest extends AbstractExpressionCompilationR
                SCLContext sclContext = SCLContext.getCurrent();
         Object oldGraph = sclContext.get("graph");
                try {
-                       Function1<Variable,Object> exp = graph.syncRequest(new ServerSCLHandlerValueRequest(graph, context),
-                                       TransientCacheListener.<Function1<Variable,Object>>instance());
+                       Function1<Object,Object> exp = graph.syncRequest(new ServerSCLHandlerValueRequest(graph, context),
+                                       TransientCacheListener.instance());
                        sclContext.put("graph", graph);
                        return exp.apply(context);
                } catch (DatabaseException e) {
@@ -108,6 +127,14 @@ public class ServerSCLHandlerValueRequest extends AbstractExpressionCompilationR
                }
        }
 
+       public static Function1<Object, Object> compile(ReadGraph graph, Variable context) throws DatabaseException {
+           return graph.syncRequest(new ServerSCLHandlerValueRequest(graph, context), TransientCacheListener.instance());
+       }
+
+    public static Function1<Object, Object> compile(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException {
+        return graph.syncRequest(new ServerSCLHandlerValueRequest(graph, s, o, p), TransientCacheListener.<Function1<Object,Object>>instance());
+    }
+       
        @Override
        protected String getExpressionText(ReadGraph graph)
                        throws DatabaseException {
@@ -127,30 +154,23 @@ public class ServerSCLHandlerValueRequest extends AbstractExpressionCompilationR
 
        @Override
        protected CompilationContext getCompilationContext(ReadGraph graph) throws DatabaseException {
-               
-               return graph.syncRequest(new VariableRead<CompilationContext>(context) {
-                       
-                       @Override
-                       public CompilationContext perform(ReadGraph graph) throws DatabaseException {
-                               
-                               Pair<Resource,Resource> parameter = getComponentTypeAndRoot(graph, variable);
-                               RuntimeEnvironment runtimeEnvironment = graph.syncRequest(getRuntimeEnvironmentRequest(parameter.first, parameter.second));
-                               
-                               Map<String, ComponentTypeProperty> propertyMap =
-                                               graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),
-                                                               TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
+           
+           return graph.syncRequest(new UnaryRead<Pair<Resource,Resource>,CompilationContext>(componentTypeAndRoot) {
+
+               @Override
+               public CompilationContext perform(ReadGraph graph) throws DatabaseException {
+
+                   RuntimeEnvironment runtimeEnvironment = graph.syncRequest(getRuntimeEnvironmentRequest(parameter.first, parameter.second));
+                   Map<String, ComponentTypeProperty> propertyMap =
+                           graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),
+                                   TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
+                   return new CompilationContext(runtimeEnvironment, propertyMap);
+
+
+               }
+
+           });
 
-//                             Map<String, ComponentTypeProperty> result = new HashMap<String,ComponentTypeProperty>(propertyMap); 
-//                             for(DataDefinition dd : Functions.dataDefinitions(graph, variable)) {
-//                                     result.put(dd.target, null);
-//                             }
-                               
-                               return new CompilationContext(runtimeEnvironment, propertyMap);
-                               
-                       }
-                       
-               });
-               
        }
 
        @Override
@@ -227,44 +247,22 @@ public class ServerSCLHandlerValueRequest extends AbstractExpressionCompilationR
         return super.getExpectedType(graph, context);
     }
 
-       @Override
-       public int hashCode() {
-               final int prime = 31;
-               int result = 1;
-               result = prime * result + ((context == null) ? 0 : context.hashCode());
-               return result;
-       }
-
-       @Override
-       public boolean equals(Object obj) {
-               if (this == obj)
-                       return true;
-               if (obj == null)
-                       return false;
-               if (getClass() != obj.getClass())
-                       return false;
-               ServerSCLHandlerValueRequest other = (ServerSCLHandlerValueRequest) obj;
-               if (context == null) {
-                       if (other.context != null)
-                               return false;
-               } else if (!context.equals(other.context))
-                       return false;
-               return true;
-       }
+    @Override
+    public int hashCode() {
+        return 31*(31*getClass().hashCode() + literal.hashCode()) + componentTypeAndRoot.hashCode();
+    }
 
-//     @Override
-//     public int hashCode() {
-//             return 31*(31*getClass().hashCode() + literal.hashCode()) + componentTypeAndRoot.hashCode();
-//     }
-//
-//     @Override
-//     public boolean equals(Object obj) {
-//             if(this == obj)
-//                     return true;
-//             if(obj == null || obj.getClass() != getClass())
-//                     return false;
-//             ServerSCLHandlerValueRequest other = (ServerSCLHandlerValueRequest)obj;
-//             return literal.equals(other.literal) && componentTypeAndRoot.equals(other.componentTypeAndRoot);
-//     }
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (getClass() != obj.getClass())
+            return false;
+        ServerSCLHandlerValueRequest other = (ServerSCLHandlerValueRequest) obj;
+        return literal.equals(other.literal) && componentTypeAndRoot.equals(other.componentTypeAndRoot);
+    }
+    
        
 }
index f9181d271073964b6082fef5119a679e8c371075..b3d31b02a457c30b06c517af6f026d53504a8048 100644 (file)
@@ -7,12 +7,14 @@ import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
 import org.simantics.db.common.request.IndexRoot;
+import org.simantics.db.common.request.PossibleTypedParent;
 import org.simantics.db.common.request.UnaryRead;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.scl.AbstractExpressionCompilationContext;
 import org.simantics.db.layer0.scl.AbstractExpressionCompilationRequest;
 import org.simantics.db.layer0.util.RuntimeEnvironmentRequest2;
 import org.simantics.db.layer0.variable.Variable;
+import org.simantics.document.base.ontology.DocumentationResource;
 import org.simantics.document.server.request.ServerSCLValueRequest.CompilationContext;
 import org.simantics.layer0.Layer0;
 import org.simantics.scl.compiler.common.names.Name;
@@ -40,7 +42,7 @@ import org.simantics.utils.datastructures.Pair;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Variable> {
+public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Object> {
 
        private final Pair<Resource,Resource> componentTypeAndRoot;
        private final Resource literal;
@@ -64,7 +66,11 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<
        }
 
        public ServerSCLValueRequest(ReadGraph graph, Variable context) throws DatabaseException {
-               this(getComponentTypeAndRoot(graph, context), context.getRepresents(graph), resolveExpectedValueType(graph, context));
+               this(getComponentTypeAndRoot(graph, context), context.getRepresents(graph), resolveExpectedValueType(graph, context.getPredicateResource(graph)));
+       }
+
+       public ServerSCLValueRequest(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException {
+           this(getComponentTypeAndRoot(graph, s), o, resolveExpectedValueType(graph, p));
        }
 
        private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Variable property)  throws DatabaseException {
@@ -82,11 +88,35 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<
                return Pair.make(parent.getType(graph), root);
        }
 
+    private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Resource component)  throws DatabaseException {
+        if(component != null) {
+            Resource type = graph.syncRequest(new FindPossibleComponentTypeRequest(component));
+            if(type != null) {
+                Resource root = graph.syncRequest(new IndexRoot(type));
+          //      System.err.println("getComponentTypeAndRoot3 " + graph.getPossibleURI(component) + " => " + graph.getPossibleURI(type) + " " + graph.getPossibleURI(root));
+                return Pair.make(type, root);
+            } else {
+                Resource doc = graph.syncRequest(new PossibleTypedParent(component, DocumentationResource.getInstance(graph).Document));
+                if(doc != null) {
+                    Resource componentType = graph.getSingleType(doc);
+                    Resource root = graph.syncRequest(new IndexRoot(doc));
+                    return Pair.make(componentType, root);
+                } else {
+                    System.err.println("component = " + component);
+                    Resource root = graph.syncRequest(new IndexRoot(component));
+//                  Resource componentType = graph.getSingleType(doc);
+                    return Pair.make(null, root);
+                }
+            }
+        }
+        throw new IllegalStateException();
+    }
+       
        public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
                SCLContext sclContext = SCLContext.getCurrent();
         Object oldGraph = sclContext.get("graph");
                try {
-                       Function1<Variable,Object> exp = compile(graph, context);
+                       Function1<Object,Object> exp = compile(graph, context);
                        sclContext.put("graph", graph);
                        return exp.apply(context);
                } catch (DatabaseException e) {
@@ -98,10 +128,14 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<
                }
        }
        
-       public static Function1<Variable, Object> compile(ReadGraph graph, Variable context) throws DatabaseException {
-           return graph.syncRequest(new ServerSCLValueRequest(graph, context), TransientCacheListener.<Function1<Variable,Object>>instance());
+       public static Function1<Object, Object> compile(ReadGraph graph, Variable context) throws DatabaseException {
+           return graph.syncRequest(new ServerSCLValueRequest(graph, context), TransientCacheListener.instance());
        }
 
+    public static Function1<Object, Object> compile(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException {
+        return graph.syncRequest(new ServerSCLValueRequest(graph, s, o, p), TransientCacheListener.instance());
+    }
+       
        @Override
        protected String getExpressionText(ReadGraph graph)
                        throws DatabaseException {
@@ -223,8 +257,8 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<
                return literal.equals(other.literal) && componentTypeAndRoot.equals(other.componentTypeAndRoot);
        }
 
-    public static Function1<Variable, Object> validate(ReadGraph graph, Variable context) throws DatabaseException {
-        return graph.syncRequest(new ServerSCLValueValidationRequest(graph, context), TransientCacheListener.<Function1<Variable,Object>>instance());
+    public static Function1<Object, Object> validate(ReadGraph graph, Variable context) throws DatabaseException {
+        return graph.syncRequest(new ServerSCLValueValidationRequest(graph, context), TransientCacheListener.instance());
     }
     
     public static class ServerSCLValueValidationRequest extends ServerSCLValueRequest {
index 783f4f401f271affbd497959b5ec60e2806ee6bd..ec31d0350e96b2f3c5eaa44590a643bbbea3f132 100644 (file)
@@ -10,7 +10,12 @@ L0.Functions.entityLabel : L0.ExternalValue
 L0.Functions.listResources : L0.ExternalValue    
 L0.Functions.resourceAsValue : L0.ExternalValue    
 L0.Functions.functionApplication : L0.ExternalValue
+
+// This was replaced by L0.Functions.sclValue to make things more uniform
 L0.Functions.computeExpression : L0.ExternalValue
+    @L0.tag L0.Deprecated
+    
+L0.Functions.sclValue : L0.ExternalValue
 L0.Functions.composedPropertyValue : L0.ExternalValue
 
 L0.Functions.standardValueAccessor : L0.ExternalValue
index 16f01a7bebfba9553314e5f12cff27648a3470c8..358aaa7e914a92ad6b89e465139fa3e6f8f8559a 100644 (file)
@@ -28,7 +28,7 @@ L0.SCLValue <T L0.Value : L0.SCLValueType
     @L0.tag L0.Abstract
     >-- L0.SCLValue.expression --> L0.String <R L0.HasProperty : L0.TotalFunction
     >-- L0.SCLValue.environment --> L0.SCLValue.Environment <R L0.IsRelatedTo : L0.TotalFunction
-    @L0.assert L0.ConvertsToValueWith L0.Functions.computeExpression
+    @L0.assert L0.ConvertsToValueWith L0.Functions.sclValue
 
 L0.SCLValueType <T L0.Entity
     >-- L0.SCLValueType.validator ==> "Variable -> <ReadGraph> String" <R L0.HasProperty
index ff53cb579b533673483a2cfc2e33da8093905d52..6b735a7daae95b970f963621b21bdd32e8981355 100644 (file)
                </resource>
        </target>
 
+    <target interface="org.simantics.db.ComputationalValue">
+        <resource uri="http://www.simantics.org/Modeling-0.0/Functions/sclValue"
+            class="org.simantics.modeling.SCLComputationalValue">
+        </resource>
+    </target>
+
 </adapters>
\ No newline at end of file
diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/SCLComputationalValue.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/SCLComputationalValue.java
new file mode 100644 (file)
index 0000000..faae653
--- /dev/null
@@ -0,0 +1,17 @@
+package org.simantics.modeling;
+
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.adapter.ContextualRelatedValue;
+import org.simantics.scl.runtime.function.Function1;
+import org.simantics.structural2.scl.CompileStructuralValueRequest;
+
+public class SCLComputationalValue extends ContextualRelatedValue {
+
+       @Override
+       public Function1<Object,Object> getFunction(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException {
+               return CompileStructuralValueRequest.compile(graph, s, o, p);
+       }
+
+}
index 8fed128d68f4683c7b172d0b5779c3276d214fec..1cef75ddfec4662394c60850f930fc2ec77a0259 100644 (file)
@@ -38,8 +38,8 @@ public class CompileSCLQueryRequest extends CompileStructuralValueRequest {
         SCLContext sclContext = SCLContext.getCurrent();
         Object oldGraph = sclContext.get("graph");
         try {
-            Function1<Variable,Object> exp = graph.syncRequest(new CompileSCLQueryRequest(graph, context),
-                    TransientCacheListener.<Function1<Variable,Object>>instance());
+            Function1<Object,Object> exp = graph.syncRequest(new CompileSCLQueryRequest(graph, context),
+                    TransientCacheListener.<Function1<Object,Object>>instance());
             sclContext.put("graph", graph);
             return exp.apply(context);
         } catch (DatabaseException e) {
index bfd12fbe12a087d0fcacbb6b9c1fd49695394f37..9c97017cc92075095ea2322727e138d80d0e7270 100644 (file)
@@ -19,8 +19,8 @@ public class CompileSCLValueRequest extends CompileStructuralValueRequest {
         SCLContext sclContext = SCLContext.getCurrent();
         Object oldGraph = sclContext.get("graph");
         try {
-            Function1<Variable,Object> exp = graph.syncRequest(new CompileSCLValueRequest(graph, context),
-                    TransientCacheListener.<Function1<Variable,Object>>instance());
+            Function1<Object,Object> exp = graph.syncRequest(new CompileSCLValueRequest(graph, context),
+                    TransientCacheListener.instance());
             sclContext.put("graph", graph);
             return exp.apply(context);
         } catch (DatabaseException e) {
index 736c0396f4debcccbb2003dc800f905dc9dcb1d7..45e4d2d16aa55ad10dba61e3fd796c1641d28732 100644 (file)
@@ -29,8 +29,8 @@ public class CompileSCLValueRequest extends CompileStructuralValueRequest {
         SCLContext sclContext = SCLContext.getCurrent();
         Object oldGraph = sclContext.get("graph");
         try {
-            Function1<Variable,Object> exp = graph.syncRequest(new CompileSCLValueRequest(graph, context),
-                    TransientCacheListener.<Function1<Variable,Object>>instance());
+            Function1<Object,Object> exp = graph.syncRequest(new CompileSCLValueRequest(graph, context),
+                    TransientCacheListener.instance());
             sclContext.put("graph", graph);
             return exp.apply(context);
         } catch (DatabaseException e) {
index f429fdc91d104fabcaf53d6fd6c5752bdb1699ad..d89026a79454e8e661a0967958f664460bc49085 100644 (file)
@@ -846,12 +846,12 @@ public class Functions {
         SCLContext sclContext = SCLContext.getCurrent();
         Object oldGraph = sclContext.get("graph");
         try {
-            Function1<Variable,Object> exp = graph.syncRequest(new CompileStructuralValueRequest(graph, context) {
+            Function1<Object,Object> exp = graph.syncRequest(new CompileStructuralValueRequest(graph, context) {
                 protected String getExpressionText(ReadGraph graph) throws DatabaseException {
                     return expression;
                 }
             },
-            TransientCacheListener.<Function1<Variable,Object>>instance());
+            TransientCacheListener.instance());
             sclContext.put("graph", graph);
             return exp.apply(context);
         } catch (DatabaseException e) {
index 283e1b5ddcadd6f836fef90a811ce8392bac97a7..0f1e19eaea0071abed9069d0ac3bc3542cb56f8a 100644 (file)
@@ -14,7 +14,6 @@ import org.simantics.db.layer0.scl.AbstractExpressionCompilationContext;
 import org.simantics.db.layer0.scl.AbstractExpressionCompilationRequest;
 import org.simantics.db.layer0.util.RuntimeEnvironmentRequest;
 import org.simantics.db.layer0.util.RuntimeEnvironmentRequest2;
-import org.simantics.db.layer0.variable.Variable;
 import org.simantics.layer0.Layer0;
 import org.simantics.scl.compiler.elaboration.expressions.EApply;
 import org.simantics.scl.compiler.elaboration.expressions.EConstant;
@@ -34,7 +33,7 @@ import org.simantics.structural2.scl.AbstractCompileStructuralValueRequest.Compi
  * 
  * @author Antti Villberg
  */
-abstract public class AbstractCompileStructuralValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Variable> {
+abstract public class AbstractCompileStructuralValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Object> {
     
     protected final Resource relation;
     
index fa0676e7d788748283b2059dcb00504e33106711..d9ce13d5969c653a3932c486ea2dc3009b21fa8b 100644 (file)
@@ -31,8 +31,8 @@ public class CompileProceduralExpressionValueRequest extends AbstractCompileStru
         SCLContext sclContext = SCLContext.getCurrent();
         Object oldGraph = sclContext.get("graph");
         try {
-            Function1<Variable,Object> exp = graph.syncRequest(new CompileProceduralExpressionValueRequest(graph, expression, context),
-                    TransientCacheListener.<Function1<Variable,Object>>instance());
+            Function1<Object,Object> exp = graph.syncRequest(new CompileProceduralExpressionValueRequest(graph, expression, context),
+                    TransientCacheListener.instance());
             sclContext.put("graph", graph);
             return exp.apply(context);
         } catch (DatabaseException e) {
index 3b54c0f62c871ceff88fa8ef5cc3ac4dbc95b955..7a8eb715f26362fdc8726d680e3d485fd340d873 100644 (file)
@@ -39,7 +39,7 @@ public class CompileStructuralValueRequest extends AbstractCompileStructuralValu
         Object oldGraph = sclContext.get("graph");
         CompileStructuralValueRequest request = new CompileStructuralValueRequest(graph, context);
         try {
-            Function1<Variable,Object> exp = graph.syncRequest(request, TransientCacheListener.<Function1<Variable,Object>>instance());
+            Function1<Object,Object> exp = graph.syncRequest(request, TransientCacheListener.instance());
             sclContext.put("graph", graph);
             return exp.apply(context);
         } catch (Throwable t) {
@@ -49,6 +49,10 @@ public class CompileStructuralValueRequest extends AbstractCompileStructuralValu
         }
     }
     
+    public static Function1<Object, Object> compile(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException {
+        return graph.syncRequest(new CompileStructuralValueRequest(s, o, p), TransientCacheListener.instance());
+    }
+    
     @Override
     protected String getExpressionText(ReadGraph graph)
             throws DatabaseException {