]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/scl/AbstractExpressionCompilationRequest.java
Remove apparent LOGGER.info used for transient debugging
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / scl / AbstractExpressionCompilationRequest.java
index d40f82d4dc1219055b83fe4c722edff93703d63e..16f88a340a63e7f9dfeb9c0d7ef751a7ff8e7048 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;
@@ -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<CompilationContext extends AbstractExpressionCompilationContext,EvaluationContext>
 implements Read<Function1<EvaluationContext,Object>> {
 
+    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<Function1<EvaluationContext,Object>> {
         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<String,Pair<Variable,Expression>> precalculatedVariables = new THashMap<String,Pair<Variable,Expression>>(); 
@@ -142,13 +147,21 @@ implements Read<Function1<EvaluationContext,Object>> {
     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<EvaluationContext, Object> eval(ExpressionEvaluator evaluator, ReadGraph graph) throws DatabaseException {
+    @SuppressWarnings("unchecked")
+    protected Function1<EvaluationContext, Object> eval(ExpressionEvaluator evaluator, ReadGraph graph) throws DatabaseException {
         Object oldGraph = SCLContext.getCurrent().put("graph", graph);
         try {
             return (Function1<EvaluationContext,Object>)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,19 @@ implements Read<Function1<EvaluationContext,Object>> {
             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()); 
+            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 +201,11 @@ implements Read<Function1<EvaluationContext,Object>> {
             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<EvaluationContext,Object> perform(final ReadGraph graph) throws DatabaseException {
         CompilationContext context = getCompilationContext(graph);
@@ -225,10 +239,15 @@ implements Read<Function1<EvaluationContext,Object>> {
         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);
 }