]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Add checking of expected type in SCL expressions to SCL issues view 59/1159/2
authorjsimomaa <jani.simomaa@gmail.com>
Mon, 30 Oct 2017 12:31:32 +0000 (14:31 +0200)
committerJani Simomaa <jani.simomaa@semantum.fi>
Mon, 30 Oct 2017 12:31:47 +0000 (14:31 +0200)
refs #7575

Change-Id: Idb20ac002995052c21d5f32215f2f4d7009c1a4c

bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/scl/AbstractExpressionCompilationRequest.java
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

index f7b78278c513ba3c7a0b20b018ec46e2ea161738..b12d0258ede32c4c48e60c287c35250496fa5c77 100644 (file)
@@ -5,10 +5,12 @@ import gnu.trove.map.hash.THashMap;
 import java.util.ArrayList;
 import java.util.List;
 
+import org.simantics.databoard.Bindings;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.exception.RuntimeDatabaseException;
 import org.simantics.db.request.Read;
+import org.simantics.layer0.Layer0;
 import org.simantics.scl.compiler.common.names.Name;
 import org.simantics.scl.compiler.constants.StringConstant;
 import org.simantics.scl.compiler.elaboration.expressions.EApply;
@@ -223,4 +225,10 @@ implements Read<Function1<EvaluationContext,Object>> {
         else
             return base;
     }
+    
+    protected static String resolveExpectedValueType(ReadGraph graph, org.simantics.db.layer0.variable.Variable context) throws DatabaseException {
+        Layer0 L0 = Layer0.getInstance(graph);
+        String valueType = graph.getPossibleRelatedValue(context.getPredicateResource(graph), L0.RequiresValueType, Bindings.STRING);
+        return valueType;
+    }
 }
index 2071b8323646596c9fa86c0d1c0fc734a9773b95..286cae2728c9f12660c71e5fe2dc38b934dd0568 100644 (file)
@@ -22,8 +22,10 @@ import org.simantics.scl.compiler.elaboration.expressions.EVariable;
 import org.simantics.scl.compiler.elaboration.expressions.Expression;
 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
 import org.simantics.scl.compiler.environment.Environment;
+import org.simantics.scl.compiler.environment.Environments;
 import org.simantics.scl.compiler.environment.specification.EnvironmentSpecification;
 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
+import org.simantics.scl.compiler.top.SCLExpressionCompilationException;
 import org.simantics.scl.compiler.types.TCon;
 import org.simantics.scl.compiler.types.Type;
 import org.simantics.scl.compiler.types.Types;
@@ -34,12 +36,17 @@ import org.simantics.structural2.scl.ComponentTypeProperty;
 import org.simantics.structural2.scl.FindPossibleComponentTypeRequest;
 import org.simantics.structural2.scl.ReadComponentTypeInterfaceRequest;
 import org.simantics.utils.datastructures.Pair;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class ServerSCLHandlerValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Variable> {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(ServerSCLHandlerValueRequest.class);
+
        private final Variable context;
        private final Pair<Resource,Resource> componentTypeAndRoot;
        private final Resource literal;
+       protected String possibleExpectedValueType;
 
        public static class CompilationContext extends AbstractExpressionCompilationContext {
                public final Map<String, ComponentTypeProperty> propertyMap;
@@ -51,15 +58,16 @@ public class ServerSCLHandlerValueRequest extends AbstractExpressionCompilationR
                }
        }
 
-       private ServerSCLHandlerValueRequest(Variable context, Pair<Resource,Resource> componentTypeAndRoot, Resource literal) {
+       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;
        }
 
        public ServerSCLHandlerValueRequest(ReadGraph graph, Variable context) throws DatabaseException {
-               this(context, getComponentTypeAndRoot(graph, context), context.getRepresents(graph));
+               this(context, getComponentTypeAndRoot(graph, context), context.getRepresents(graph), resolveExpectedValueType(graph, context));
        }
 
        private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Variable property)  throws DatabaseException {
@@ -220,6 +228,19 @@ public class ServerSCLHandlerValueRequest extends AbstractExpressionCompilationR
                        return null;
        }
 
+    @Override
+    protected Type getExpectedType(ReadGraph graph, CompilationContext context) throws DatabaseException {
+        if(possibleExpectedValueType != null) {
+            try {
+                return Environments.getType(context.runtimeEnvironment.getEnvironment(), possibleExpectedValueType);
+            } catch (SCLExpressionCompilationException e) {
+                LOGGER.error("Could not get type for " + String.valueOf(possibleExpectedValueType), e);
+            }
+        }
+        return super.getExpectedType(graph, context);
+    }
+
+
        @Override
        public int hashCode() {
                final int prime = 31;
index 25afd86893fd17a83475aa11f4ec9b2ea148ffb3..5013f88146bbb232d48d8f27e8883df40fd81421 100644 (file)
@@ -24,8 +24,10 @@ import org.simantics.scl.compiler.elaboration.expressions.EVariable;
 import org.simantics.scl.compiler.elaboration.expressions.Expression;
 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
 import org.simantics.scl.compiler.environment.Environment;
+import org.simantics.scl.compiler.environment.Environments;
 import org.simantics.scl.compiler.environment.specification.EnvironmentSpecification;
 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
+import org.simantics.scl.compiler.top.SCLExpressionCompilationException;
 import org.simantics.scl.compiler.types.Type;
 import org.simantics.scl.compiler.types.Types;
 import org.simantics.scl.compiler.types.kinds.Kinds;
@@ -35,11 +37,16 @@ import org.simantics.structural2.scl.ComponentTypeProperty;
 import org.simantics.structural2.scl.FindPossibleComponentTypeRequest;
 import org.simantics.structural2.scl.ReadComponentTypeInterfaceRequest;
 import org.simantics.utils.datastructures.Pair;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Variable> {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(ServerSCLValueRequest.class);
+    
        private final Pair<Resource,Resource> componentTypeAndRoot;
        private final Resource literal;
+    protected String possibleExpectedValueType;
 
        public static class CompilationContext extends AbstractExpressionCompilationContext {
                public final Map<String, ComponentTypeProperty> propertyMap;
@@ -51,14 +58,15 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<
                }
        }
 
-       private ServerSCLValueRequest(Pair<Resource,Resource> componentTypeAndRoot, Resource literal) {
+       private ServerSCLValueRequest(Pair<Resource,Resource> componentTypeAndRoot, Resource literal, String possibleExpectedValueType) {
                assert(literal != null);
                this.literal = literal;
                this.componentTypeAndRoot = componentTypeAndRoot;
+               this.possibleExpectedValueType = possibleExpectedValueType;
        }
 
        public ServerSCLValueRequest(ReadGraph graph, Variable context) throws DatabaseException {
-               this(getComponentTypeAndRoot(graph, context), context.getRepresents(graph));
+               this(getComponentTypeAndRoot(graph, context), context.getRepresents(graph), resolveExpectedValueType(graph, context));
        }
 
        private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Variable property)  throws DatabaseException {
@@ -192,6 +200,19 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<
                else
                        return null;
        }
+       
+    @Override
+    protected Type getExpectedType(ReadGraph graph, CompilationContext context) throws DatabaseException {
+        if(possibleExpectedValueType != null) {
+            try {
+                return Environments.getType(context.runtimeEnvironment.getEnvironment(), possibleExpectedValueType);
+            } catch (SCLExpressionCompilationException e) {
+                LOGGER.error("Could not get type for " + String.valueOf(possibleExpectedValueType), e);
+            }
+        }
+        return super.getExpectedType(graph, context);
+    }
+
 
        @Override
        public int hashCode() {