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=d40f82d4dc1219055b83fe4c722edff93703d63e;hpb=7fe2a02ad295ec7406f44e8f86b7d25deedceb8b;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 d40f82d4d..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 @@ -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; @@ -34,6 +35,8 @@ 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; @@ -62,6 +65,8 @@ import gnu.trove.map.hash.THashMap; 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"); @@ -99,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>(); @@ -142,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 @@ -157,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); @@ -186,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); @@ -225,10 +240,15 @@ implements Read> { else 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); } + + @Override + public abstract int hashCode(); + + @Override + public abstract boolean equals(Object obj); }