]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling/src/org/simantics/modeling/scl/GraphPropertyRelation.java
(refs #7375) Fixed implementation of collectEffects
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / scl / GraphPropertyRelation.java
index d9803aedde5ef8e5c2198ed5ea75d6ab75c5f6cf..a4c1c4b2ace8530a593531a3b094ce6590162cc6 100644 (file)
@@ -3,6 +3,7 @@ package org.simantics.modeling.scl;
 import org.simantics.db.Resource;
 import org.simantics.scl.compiler.common.names.Name;
 import org.simantics.scl.compiler.common.names.Names;
+import org.simantics.scl.compiler.compilation.CompilationContext;
 import org.simantics.scl.compiler.elaboration.chr.plan.PlanContext;
 import org.simantics.scl.compiler.elaboration.expressions.EApply;
 import org.simantics.scl.compiler.elaboration.expressions.EExternalConstant;
@@ -15,6 +16,7 @@ import org.simantics.scl.compiler.elaboration.relations.SCLRelation;
 import org.simantics.scl.compiler.environment.Environment;
 import org.simantics.scl.compiler.errors.Locations;
 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
+import org.simantics.scl.compiler.types.TPred;
 import org.simantics.scl.compiler.types.TVar;
 import org.simantics.scl.compiler.types.Type;
 import org.simantics.scl.compiler.types.Types;
@@ -26,7 +28,7 @@ public class GraphPropertyRelation implements SCLRelation {
     Type[] parameterTypes;
 
     private static final Type RESOURCE = Types.con("Simantics/DB", "Resource");
-    
+
     public GraphPropertyRelation(Resource propertyRelation, Type valueType) {
         this.propertyRelation = propertyRelation;
         this.valueType = valueType;
@@ -37,7 +39,12 @@ public class GraphPropertyRelation implements SCLRelation {
     public TVar[] getTypeVariables() {
         return TVar.EMPTY_ARRAY;
     }
-    
+
+    @Override
+    public TPred[] getTypeConstraints() {
+        return new TPred[] {Types.pred(Types.SERIALIZABLE, valueType)};
+    }
+
     @Override
     public Type[] getParameterTypes() {
         return parameterTypes;
@@ -53,14 +60,14 @@ public class GraphPropertyRelation implements SCLRelation {
         default: throw new IllegalArgumentException();
         }
     }
-    
+
     @Override
     public int getRequiredVariablesMask() {
         return BF;
     }
-    
+
     private static final Name POSSIBLE_RELATED_VALUE = Name.create("Simantics/DB", "possibleRelatedValue");
-    
+
     @Override
     public void generate(long location, QueryCompilationContext context,
             Type[] typeParameters, Variable[] parameters, int boundVariables) {
@@ -78,8 +85,8 @@ public class GraphPropertyRelation implements SCLRelation {
             context.condition(new EApply(
                     context.getCompilationContext().getConstant(Names.Builtin_equals, valueType),
                     new Expression[] {
-                        new EVariable(temp),
-                        new EVariable(parameters[1])
+                            new EVariable(temp),
+                            new EVariable(parameters[1])
                     }
                     ));
             context.iterateMaybe(temp, possibleValue);
@@ -91,7 +98,7 @@ public class GraphPropertyRelation implements SCLRelation {
     }
 
     private static final Name CLAIM_RELATED_VALUE = Name.create("Simantics/DB", "claimRelatedValue");
-    
+
     @Override
     public Expression generateEnforce(long location, EnforcingContext context,
             Type[] typeParameters, Variable[] parameters) {
@@ -110,15 +117,50 @@ public class GraphPropertyRelation implements SCLRelation {
     public int getPhase() {
         return 0;
     }
-    
+
     @Override
-    public void generateEnforce(PlanContext context, CodeWriter w, long location, Expression[] parameters) {
-        throw new UnsupportedOperationException(getClass().getSimpleName() + " does not support enforce.");
+    public void generateIterate(PlanContext context, CodeWriter w, long location, int boundMask, Variable[] variables,
+            Expression[] expressions, Expression[] typeConstraintEvidenceParameters) {
+        CompilationContext compilationContext = context.context;
+        switch(boundMask) {
+        case BF:
+            context.iterateMaybe(location, w, variables[1],
+                    w.apply(location,
+                            compilationContext.environment.getValue(POSSIBLE_RELATED_VALUE).getValue().createSpecialization(valueType),
+                            typeConstraintEvidenceParameters[0].toVal(compilationContext, w),
+                            expressions[0].toVal(compilationContext, w),
+                            w.getModuleWriter().getExternalConstant(propertyRelation, Types.RESOURCE)));
+            break;
+        case BB:
+            context.checkEqualsJust(location, w, expressions[1].toVal(compilationContext, w),
+                    w.apply(location,
+                            compilationContext.environment.getValue(POSSIBLE_RELATED_VALUE).getValue().createSpecialization(valueType),
+                            typeConstraintEvidenceParameters[0].toVal(compilationContext, w),
+                            expressions[0].toVal(compilationContext, w),
+                            w.getModuleWriter().getExternalConstant(propertyRelation, Types.RESOURCE)));
+            break;
+        default: throw new IllegalArgumentException();
+        }
+    }
+
+    @Override
+    public void generateEnforce(PlanContext context, CodeWriter w, long location, Expression[] parameters, Expression[] typeConstraintEvidenceParameters) {
+        CompilationContext compilationContext = context.context;
+        w.apply(location,
+                compilationContext.environment.getValue(CLAIM_RELATED_VALUE).getValue().createSpecialization(valueType),
+                typeConstraintEvidenceParameters[0].toVal(compilationContext, w),
+                parameters[0].toVal(compilationContext, w),
+                w.getModuleWriter().getExternalConstant(propertyRelation, Types.RESOURCE),
+                parameters[1].toVal(compilationContext, w));
     }
     
     @Override
-    public void generateIterate(PlanContext context, CodeWriter w, long location, int boundMask, Variable[] variables,
-            Expression[] expressions) {
-        throw new UnsupportedOperationException(getClass().getSimpleName() + " does not support iterate.");
+    public Type getEnforceEffect() {
+        return Types.WRITE_GRAPH;
+    }
+
+    @Override
+    public Type getQueryEffect() {
+        return Types.READ_GRAPH;
     }
 }