--- /dev/null
+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);
+ }
+
+}
--- /dev/null
+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();
+ }
+ }
+
+}
%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