@L0.new
L0.HasResourceClass "org.simantics.action.ontology.ActionResource"
-ACT.Action <T L0.Entity
+ACT.Action <T L0.Action
ACT.DropAction <T L0.Entity
</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">
--- /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();
+ }
+ }
+
+}
public class NewFileDocument implements ActionFactory {
Resource relation;
String defaultName;
-
+
public NewFileDocument(ReadGraph graph, String relationUri, String defaultName) throws DatabaseException {
relation = graph.getResource(relationUri);
this.defaultName = defaultName;
}
-
+
@Override
public Runnable create(Object target) {
@Override
public void run() {
Simantics.getSession().asyncRequest(new WriteRequest() {
-
+
@Override
public void perform(WriteGraph graph) throws DatabaseException {
- graph.markUndoPoint();
-
- Layer0 l0 = Layer0.getInstance(graph);
-
- String name = NameUtils.findFreshName(graph, defaultName, resource, relation);
- DocumentResource doc = DocumentResource.getInstance(graph);
-
- Resource fileResource = graph.newResource();
- graph.claim(fileResource, l0.InstanceOf, doc.FileDocument);
- graph.claimLiteral(fileResource, l0.HasName, name);
- graph.claim(resource, relation, fileResource);
- try {
- GraphFileUtil.writeDataToGraph(graph, new byte[0], fileResource);
- } catch (IOException e) {
- throw new DatabaseException(e);
- }
+ create(graph, resource, relation, defaultName);
}
-
+
});
}
};
}
+
+ public static Resource create(WriteGraph graph, Resource resource, Resource relation, String defaultName) throws DatabaseException {
+
+ graph.markUndoPoint();
+
+ Layer0 l0 = Layer0.getInstance(graph);
+
+ String name = NameUtils.findFreshName(graph, defaultName, resource, relation);
+ DocumentResource doc = DocumentResource.getInstance(graph);
+
+ Resource fileResource = graph.newResource();
+ graph.claim(fileResource, l0.InstanceOf, doc.FileDocument);
+ graph.claimLiteral(fileResource, l0.HasName, name);
+ graph.claim(resource, relation, fileResource);
+ try {
+ GraphFileUtil.writeDataToGraph(graph, new byte[0], fileResource);
+ } catch (IOException e) {
+ throw new DatabaseException(e);
+ }
+
+ return fileResource;
+
+ }
+
}
>-- 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 ""
%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