Removed unnecessary dependencies on org.apache.log4j
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / SCLAction.java
1 package org.simantics.modeling;
2
3 import org.simantics.Simantics;
4 import org.simantics.db.ReadGraph;
5 import org.simantics.db.Resource;
6 import org.simantics.db.common.request.ResourceRead;
7 import org.simantics.db.exception.DatabaseException;
8 import org.simantics.db.layer0.adapter.ActionFactory;
9 import org.simantics.db.layer0.variable.Variable;
10 import org.simantics.db.layer0.variable.Variables;
11 import org.simantics.scl.runtime.function.Function1;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15 public class SCLAction implements ActionFactory {
16
17         private static final Logger LOGGER = LoggerFactory.getLogger(SCLAction.class);
18
19         final private Resource rule;
20     
21     public SCLAction(ReadGraph graph, Resource rule) throws DatabaseException {
22         this.rule = rule;
23     }
24     
25     static class RuleFunctionRequest extends ResourceRead<Function1<Resource,Object>> {
26
27                 protected RuleFunctionRequest(Resource rule) {
28                         super(rule);
29                 }
30
31                 @Override
32                 public Function1<Resource, Object> perform(ReadGraph graph) throws DatabaseException {
33                 Variable ruleVariable = Variables.getVariable(graph, resource);
34                 ModelingResources MOD = ModelingResources.getInstance(graph);
35                 return ruleVariable.getPossiblePropertyValue(graph, MOD.SCLAction_action);
36                 }
37         
38     }
39     
40     public static class SCLActionRunnable implements Runnable {
41         
42         public Resource rule;
43         public Resource target;
44         
45         public SCLActionRunnable(Resource rule, Resource target) {
46                 this.rule = rule;
47                 this.target = target;
48         }
49         
50                 @Override
51                 public void run() {
52                         Resource resource = (Resource)target;
53                         Simantics.getSession().markUndoPoint();
54                         try {
55                                 Function1<Resource, Object> function = Simantics.getSession().syncRequest(new RuleFunctionRequest(rule));
56                                 function.apply(resource);
57                         } catch (DatabaseException e) {
58                                 LOGGER.error("SCLActionRunnable failed to request/apply RuleFunction {}", rule, e);
59                         }
60                 }
61         
62     }
63
64         @Override
65         public Runnable create(final Object target) {
66                 return new SCLActionRunnable(rule, (Resource)target);
67         }
68
69 }