]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/scl/SCLAction.java
Define actions already in L0
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / scl / SCLAction.java
diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/scl/SCLAction.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/scl/SCLAction.java
new file mode 100644 (file)
index 0000000..b2225f7
--- /dev/null
@@ -0,0 +1,73 @@
+package org.simantics.db.layer0.scl;
+
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.Session;
+import org.simantics.db.common.request.ResourceRead;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.adapter.ActionFactory;
+import org.simantics.db.layer0.internal.SimanticsInternal;
+import org.simantics.db.layer0.util.DatabaseExceptionUtils;
+import org.simantics.db.layer0.variable.Variable;
+import org.simantics.db.layer0.variable.Variables;
+import org.simantics.layer0.Layer0;
+import org.simantics.scl.runtime.function.Function1;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class SCLAction implements ActionFactory {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(SCLAction.class);
+
+    private final Resource rule;
+
+    public SCLAction(ReadGraph graph, Resource rule) throws DatabaseException {
+        this.rule = rule;
+    }
+
+    static class RuleFunctionRequest extends ResourceRead<Function1<Resource, Object>> {
+
+        protected RuleFunctionRequest(Resource rule) {
+            super(rule);
+        }
+
+        @Override
+        public Function1<Resource, Object> perform(ReadGraph graph) throws DatabaseException {
+            Variable ruleVariable = Variables.getVariable(graph, resource);
+            Layer0 L0 = Layer0.getInstance(graph);
+            return ruleVariable.getPossiblePropertyValue(graph, L0.SCLAction_action);
+        }
+
+    }
+
+    static class SCLActionRunnable implements Runnable {
+
+        public final Resource rule;
+        public final Resource target;
+
+        public SCLActionRunnable(Resource rule, Resource target) {
+            this.rule = rule;
+            this.target = target;
+        }
+
+        @Override
+        public void run() {
+            Session s = SimanticsInternal.getSession();
+            Resource resource = (Resource) target;
+            s.markUndoPoint();
+            try {
+                Function1<Resource, Object> function = s.syncRequest(new RuleFunctionRequest(rule));
+                function.apply(resource);
+            } catch (DatabaseException e) {
+                LOGGER.error("Error while executing action " + DatabaseExceptionUtils.showResource(s, resource), e);
+            }
+        }
+
+    }
+
+    @Override
+    public Runnable create(final Object target) {
+        return new SCLActionRunnable(rule, (Resource) target);
+    }
+
+}