From: Antti Villberg Date: Fri, 2 Aug 2019 06:25:13 +0000 (+0300) Subject: Define actions already in L0 X-Git-Tag: v1.43.0~136^2~125^2 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F53%2F3053%2F4;p=simantics%2Fplatform.git Define actions already in L0 gitlab #327 Change-Id: Ia8ac62b8b6de90cc3e0b8e127a15d113f640270b --- diff --git a/bundles/org.simantics.action.ontology/graph/Action.pgraph b/bundles/org.simantics.action.ontology/graph/Action.pgraph index 3b0cb14c0..11205dacc 100644 --- a/bundles/org.simantics.action.ontology/graph/Action.pgraph +++ b/bundles/org.simantics.action.ontology/graph/Action.pgraph @@ -4,6 +4,6 @@ ACT = : L0.Ontology @L0.new L0.HasResourceClass "org.simantics.action.ontology.ActionResource" -ACT.Action - + + + + + 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 index 000000000..b2225f7c9 --- /dev/null +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/scl/SCLAction.java @@ -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> { + + protected RuleFunctionRequest(Resource rule) { + super(rule); + } + + @Override + public Function1 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 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 index 000000000..236871354 --- /dev/null +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/DatabaseExceptionUtils.java @@ -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() { + + @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(); + } + } + +} diff --git a/bundles/org.simantics.layer0/graph/Layer0SCL.pgraph b/bundles/org.simantics.layer0/graph/Layer0SCL.pgraph index ec31d0350..642161435 100644 --- a/bundles/org.simantics.layer0/graph/Layer0SCL.pgraph +++ b/bundles/org.simantics.layer0/graph/Layer0SCL.pgraph @@ -66,6 +66,11 @@ L0.SCLScript -- L0.SCLScript.definition --> L0.String L0.SCLAction.action ==> "Resource -> ()" -- L0.Ontology.defaultLocalName --> L0.String