X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.layer0%2Fsrc%2Forg%2Fsimantics%2Fdb%2Flayer0%2Fscl%2FAbstractExpressionCompilationRequest.java;h=1bcd918771f3db8520507e6893b2de2376d61164;hb=99395a7780ef1626d3ae97183ef4ae717f32b215;hp=f7b78278c513ba3c7a0b20b018ec46e2ea161738;hpb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;p=simantics%2Fplatform.git
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..1bcd91877 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
@@ -1,14 +1,15 @@
package org.simantics.db.layer0.scl;
-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.Resource;
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;
@@ -34,6 +35,10 @@ import org.simantics.scl.compiler.types.util.MultiFunction;
import org.simantics.scl.runtime.SCLContext;
import org.simantics.scl.runtime.function.Function1;
import org.simantics.utils.datastructures.Pair;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import gnu.trove.map.hash.THashMap;
/**
*
This is a base implementation for compiling expressions stored into
@@ -60,6 +65,8 @@ import org.simantics.utils.datastructures.Pair;
public abstract class AbstractExpressionCompilationRequest
implements Read> {
+ private static final Logger LOGGER = LoggerFactory.getLogger(AbstractExpressionCompilationRequest.class);
+
protected static final Type RESOURCE = Types.con("Simantics/DB", "Resource");
protected static final Type VARIABLE = Types.con("Simantics/Variables", "Variable");
protected static Name PROPERTY_VALUE = Name.create("Simantics/Variables", "propertyValue");
@@ -97,7 +104,7 @@ implements Read> {
return DEFAULT_EXPECTED_EFFECT;
}
- private ExpressionEvaluator prepareEvaluator(final ReadGraph graph, final CompilationContext context, Type expectedType) throws DatabaseException {
+ protected ExpressionEvaluator prepareEvaluator(final ReadGraph graph, final CompilationContext context, Type expectedType) throws DatabaseException {
final Variable contextVariable = new Variable("context", getContextVariableType());
LocalEnvironment localEnvironment = new AbstractLocalEnvironment() {
THashMap> precalculatedVariables = new THashMap>();
@@ -140,13 +147,21 @@ implements Read> {
protected boolean parseAsBlock() {
return false;
}
+
+ /*
+ * Override this to provide location information in compilation error situations.
+ */
+ protected String getContextDescription(ReadGraph graph) throws DatabaseException {
+ return toString();
+ }
- private Function1 eval(ExpressionEvaluator evaluator, ReadGraph graph) throws DatabaseException {
+ @SuppressWarnings("unchecked")
+ protected Function1 eval(ExpressionEvaluator evaluator, ReadGraph graph) throws DatabaseException {
Object oldGraph = SCLContext.getCurrent().put("graph", graph);
try {
return (Function1)evaluator.eval();
} catch(RuntimeDatabaseException e) {
- e.printStackTrace();
+ LOGGER.error("Failed to evaluate SCL expression", e);
if(e.getCause() instanceof DatabaseException)
throw (DatabaseException)e.getCause();
else
@@ -155,17 +170,20 @@ implements Read> {
StringBuilder b = new StringBuilder();
b.append("Couldn't compile '");
b.append(evaluator.getExpressionText());
+ b.append("' in ");
+ b.append(getContextDescription(graph));
b.append("':\n");
StringBuilder b2 = new StringBuilder();
for(CompilationError error : e.getErrors()) {
b2.append(error.description);
b2.append('\n');
}
- System.err.println(b.toString() + b2.toString());
- throw new SCLDatabaseException(b.toString()+b2.toString(), b2.toString(), e.getErrors());
+ SCLDatabaseException exception = new SCLDatabaseException(b.toString()+b2.toString(), b2.toString(), e.getErrors());
+ LOGGER.info(exception.getMessage(), exception);
+ throw exception;
} catch(Throwable e) {
// Should not happen!
- e.printStackTrace();
+ LOGGER.error("This error should never happen", e);
throw new RuntimeException(e);
} finally {
SCLContext.getCurrent().put("graph", oldGraph);
@@ -184,12 +202,11 @@ implements Read> {
return concreteEffects;
} catch(MatchException e) {
// Should not happen!
- e.printStackTrace();
+ LOGGER.error("Failed to get expression effects", e);
throw new RuntimeException(e);
}
}
- @SuppressWarnings("unchecked")
@Override
public Function1 perform(final ReadGraph graph) throws DatabaseException {
CompilationContext context = getCompilationContext(graph);
@@ -223,4 +240,15 @@ implements Read> {
else
return base;
}
+
+ protected static String resolveExpectedValueType(ReadGraph graph, Resource predicate) throws DatabaseException {
+ Layer0 L0 = Layer0.getInstance(graph);
+ return graph.getPossibleRelatedValue(predicate, L0.RequiresValueType, Bindings.STRING);
+ }
+
+ @Override
+ public abstract int hashCode();
+
+ @Override
+ public abstract boolean equals(Object obj);
}