01: package org.osbl.agent.logic;
02:
03: import org.concern.Controller;
04: import org.concern.ControllerLookup;
05: import org.concern.LoaderException;
06: import org.concern.SubjectNotFoundException;
07: import org.concern.Work;
08: import org.concern.controller.LoaderFactory;
09: import org.conform.BeanMeta;
10: import org.conform.hibernate.HibernateEnvironment;
11: import org.osbl.agent.model.Rule;
12: import org.osbl.agent.model.RuleContext;
13:
14: /**
15: * The Class TaskRuntimeContext.
16: *
17: * @author Sebastian Nozzi.
18: */
19: public class TaskRuntimeContext extends RuntimeContext {
20:
21: /* (non-Javadoc)
22: * @see org.osbl.agent.logic.RuntimeContext#populateRuleContext(org.osbl.agent.model.RuleContext, java.lang.Object)
23: */
24: public void populateRuleContext(RuleContext rtc, Object concernWork) {
25:
26: Work task = (Work) concernWork;
27:
28: Controller controller = ControllerLookup.getInstance()
29: .getController(task.getProcess());
30: HibernateEnvironment.getInstance().beginTransaction();
31: org.concern.controller.Loader loader = new LoaderFactory()
32: .getLoader(controller);
33:
34: try {
35: task.setSubject(loader.load(task.getSubjectId()));
36: } catch (LoaderException e) {
37: // TODO Auto-generated catch block
38: e.printStackTrace();
39: } catch (SubjectNotFoundException e) {
40: // TODO Auto-generated catch block
41: e.printStackTrace();
42: } finally {
43: HibernateEnvironment.getInstance().endTransaction();
44: }
45:
46: rtc.setTargetObject(task.getSubject());
47: rtc.put(rtc.SUBJECT_ID, task.getSubjectId());
48: rtc.put(rtc.PROCESS_NAME_KEY, task.getProcess());
49:
50: BeanMeta beanMeta = new BeanMeta(task.getSubject().getClass());
51: rtc.put(rtc.BEAN_META, beanMeta);
52:
53: }
54:
55: /* (non-Javadoc)
56: * @see org.osbl.agent.logic.RuntimeContext#ruleIsApplicable(org.osbl.agent.model.Rule, java.lang.Object)
57: */
58: public boolean ruleIsApplicable(Rule ruleToRun, Object concernWork) {
59:
60: if (concernWork instanceof Work == false)
61: return false;
62:
63: Work work = (Work) concernWork;
64:
65: String activityNameInSubject = work.getActivity();
66:
67: String activityNameInRule = (String) ruleToRun
68: .getMetaInformation()
69: .get(RuleContext.ACTIVITY_NAME_KEY);
70:
71: String processNameInSubject = work.getProcess();
72:
73: String processNameInRule = (String) ruleToRun
74: .getMetaInformation().get(RuleContext.PROCESS_NAME_KEY);
75:
76: return processNameInSubject.equals(processNameInRule)
77: && activityNameInSubject.equals(activityNameInRule);
78: }
79:
80: }
|