From 66a5745bfefb159ce9d959bc29cbf148614e6fae Mon Sep 17 00:00:00 2001 From: jsimomaa Date: Mon, 30 Oct 2017 14:31:32 +0200 Subject: [PATCH] Add checking of expected type in SCL expressions to SCL issues view refs #7575 Change-Id: Idb20ac002995052c21d5f32215f2f4d7009c1a4c --- .../AbstractExpressionCompilationRequest.java | 8 ++++++ .../request/ServerSCLHandlerValueRequest.java | 25 +++++++++++++++++-- .../server/request/ServerSCLValueRequest.java | 25 +++++++++++++++++-- 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/scl/AbstractExpressionCompilationRequest.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/scl/AbstractExpressionCompilationRequest.java index f7b78278c..b12d0258e 100644 --- a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/scl/AbstractExpressionCompilationRequest.java +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/scl/AbstractExpressionCompilationRequest.java @@ -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> { 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; + } } diff --git a/bundles/org.simantics.document.server/src/org/simantics/document/server/request/ServerSCLHandlerValueRequest.java b/bundles/org.simantics.document.server/src/org/simantics/document/server/request/ServerSCLHandlerValueRequest.java index 2071b8323..286cae272 100644 --- a/bundles/org.simantics.document.server/src/org/simantics/document/server/request/ServerSCLHandlerValueRequest.java +++ b/bundles/org.simantics.document.server/src/org/simantics/document/server/request/ServerSCLHandlerValueRequest.java @@ -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 { + private static final Logger LOGGER = LoggerFactory.getLogger(ServerSCLHandlerValueRequest.class); + private final Variable context; private final Pair componentTypeAndRoot; private final Resource literal; + protected String possibleExpectedValueType; public static class CompilationContext extends AbstractExpressionCompilationContext { public final Map propertyMap; @@ -51,15 +58,16 @@ public class ServerSCLHandlerValueRequest extends AbstractExpressionCompilationR } } - private ServerSCLHandlerValueRequest(Variable context, Pair componentTypeAndRoot, Resource literal) { + private ServerSCLHandlerValueRequest(Variable context, Pair 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 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; diff --git a/bundles/org.simantics.document.server/src/org/simantics/document/server/request/ServerSCLValueRequest.java b/bundles/org.simantics.document.server/src/org/simantics/document/server/request/ServerSCLValueRequest.java index 25afd8689..5013f8814 100644 --- a/bundles/org.simantics.document.server/src/org/simantics/document/server/request/ServerSCLValueRequest.java +++ b/bundles/org.simantics.document.server/src/org/simantics/document/server/request/ServerSCLValueRequest.java @@ -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 { + private static final Logger LOGGER = LoggerFactory.getLogger(ServerSCLValueRequest.class); + private final Pair componentTypeAndRoot; private final Resource literal; + protected String possibleExpectedValueType; public static class CompilationContext extends AbstractExpressionCompilationContext { public final Map propertyMap; @@ -51,14 +58,15 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest< } } - private ServerSCLValueRequest(Pair componentTypeAndRoot, Resource literal) { + private ServerSCLValueRequest(Pair 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 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() { -- 2.47.1