]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Define actions already in L0 53/3053/4
authorAntti Villberg <antti.villberg@semantum.fi>
Fri, 2 Aug 2019 06:25:13 +0000 (09:25 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Fri, 2 Aug 2019 12:11:59 +0000 (12:11 +0000)
gitlab #327

Change-Id: Ia8ac62b8b6de90cc3e0b8e127a15d113f640270b

bundles/org.simantics.action.ontology/graph/Action.pgraph
bundles/org.simantics.db.layer0/adapters.xml
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/scl/SCLAction.java [new file with mode: 0644]
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/DatabaseExceptionUtils.java [new file with mode: 0644]
bundles/org.simantics.layer0/graph/Layer0SCL.pgraph
bundles/org.simantics.layer0/graph/Layer0Templates.pgraph

index 3b0cb14c086b65db2866ac393bb7bb55582ffbc1..11205dacc89eba7e0f6f59205376314b8493c4dc 100644 (file)
@@ -4,6 +4,6 @@ ACT = <http://www.simantics.org/Action-1.1> : L0.Ontology
     @L0.new
     L0.HasResourceClass "org.simantics.action.ontology.ActionResource"
     
-ACT.Action <T L0.Entity
+ACT.Action <T L0.Action
 
 ACT.DropAction <T L0.Entity
index 0aabf0b350e2c507f054766834058a560dc78fbf..11a3ff235c889257472f54e7d60e3af240b13f9f 100644 (file)
        </target>
 
        <target interface="org.simantics.db.layer0.adapter.ActionFactory">
-               <baseType uri="http://www.simantics.org/Action-0.0/Action" />
+               <baseType uri="http://www.simantics.org/Layer0-0.0/Action" />
+               <type uri="http://www.simantics.org/Layer0-0.0/SCLAction"
+                       class="org.simantics.db.layer0.scl.SCLAction">
+                       <graph/>
+                       <this/>
+               </type>
        </target>
 
        <target interface="org.simantics.db.layer0.migration.MigrationStep">
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);
+    }
+
+}
diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/DatabaseExceptionUtils.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/DatabaseExceptionUtils.java
new file mode 100644 (file)
index 0000000..2368713
--- /dev/null
@@ -0,0 +1,36 @@
+package org.simantics.db.layer0.util;
+
+import org.simantics.db.ReadGraph;
+import org.simantics.db.RequestProcessor;
+import org.simantics.db.Resource;
+import org.simantics.db.common.request.UniqueRead;
+import org.simantics.db.common.utils.NameUtils;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.internal.SimanticsInternal;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class DatabaseExceptionUtils {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(DatabaseExceptionUtils.class);
+
+    public static String showResource(Resource resource) {
+        return showResource(SimanticsInternal.getSession(), resource);
+    }
+
+    public static String showResource(RequestProcessor processor, Resource resource) {
+        try {
+            return processor.syncRequest(new UniqueRead<String>() {
+
+                @Override
+                public String perform(ReadGraph graph) throws DatabaseException {
+                    return NameUtils.getURIOrSafeNameInternal(graph, resource);
+                }
+            });
+        } catch (DatabaseException e) {
+            LOGGER.error("Unknown error while evaluating debug name for a resource " + resource, e);
+            return resource.toString();
+        }
+    }
+
+}
index ec31d0350e96b2f3c5eaa44590a643bbbea3f132..6421614359cc81df895bba4a87b157c26df3dead 100644 (file)
@@ -66,6 +66,11 @@ L0.SCLScript <T L0.Entity
     >-- L0.SCLScript.definition --> L0.String <R L0.HasProperty : L0.TotalFunction
     @L0.assert L0.SCLScript.definition ""
 
+L0.Action <T L0.Entity
+
+L0.SCLAction <T L0.Action
+  --> L0.SCLAction.action ==> "Resource -> <Proc> ()" <R L0.HasProperty : L0.TotalFunction 
+
 L0.Ontology
     >-- L0.Ontology.defaultLocalName --> L0.String <R L0.HasProperty : L0.TotalFunction
     @L0.assert L0.Ontology.defaultLocalName ""
index 74bff5f40d84e814a87eaf647273263e415b9202..0dbba3181589a58260003ff062c3e4da753ee895 100644 (file)
@@ -40,4 +40,13 @@ L0.tag : L0.Template
         %resource %tag %resource
 L0.defTag : L0.Template
     @template %resource
-        %resource <R L0.IsWeaklyRelatedTo : L0.Tag                
\ No newline at end of file
+        %resource <R L0.IsWeaklyRelatedTo : L0.Tag
+
+L0.SCLAction.valueType = "Resource -> <Proc> ()" : L0.String
+
+L0.sclAction : L0.Template
+    @template %action %expression
+        %action : L0.SCLAction
+          L0.SCLAction.action _ : L0.SCLValue
+            L0.SCLValue.expression %expression
+            L0.HasValueType L0.SCLAction.valueType