1 20 21 package net.innig.macker.rule; 22 23 import net.innig.macker.event.MackerEvent; 24 import net.innig.macker.event.MackerEventListener; 25 import net.innig.macker.event.MackerIsMadException; 26 import net.innig.macker.event.ListenerException; 27 import net.innig.macker.structure.ClassManager; 28 29 import java.util.*; 30 31 public class EvaluationContext 32 { 33 public EvaluationContext(ClassManager classManager, RuleSet ruleSet) 34 { 35 this.classManager = classManager; 36 this.ruleSet = ruleSet; 37 varValues = new HashMap(); 38 listeners = new HashSet(); 39 } 40 41 public EvaluationContext(RuleSet ruleSet, EvaluationContext parent) 42 { 43 this(parent.getClassManager(), ruleSet); 44 this.parent = parent; 45 } 46 47 public EvaluationContext(EvaluationContext parent) 48 { this(parent.getRuleSet(), parent); } 49 50 public EvaluationContext getParent() 51 { return parent; } 52 53 public ClassManager getClassManager() 54 { return classManager; } 55 56 public RuleSet getRuleSet() 57 { return ruleSet; } 58 59 public void setVariableValue(String name, String value) 60 throws UndeclaredVariableException 61 { varValues.put(name, (value == null) ? "" : VariableParser.parse(this, value)); } 62 63 public String getVariableValue(String name) 64 throws UndeclaredVariableException 65 { 66 String value = (String ) varValues.get(name); 67 if(value != null) 68 return value; 69 if(parent != null) 70 return parent.getVariableValue(name); 71 throw new UndeclaredVariableException(name); 72 } 73 74 public void setVariables(Map vars) 75 { varValues.putAll(vars); } 76 77 public void addListener(MackerEventListener listener) 78 { listeners.add(listener); } 79 80 public void removeListener(MackerEventListener listener) 81 { listeners.remove(listener); } 82 83 public void broadcastStarted() 84 throws ListenerException 85 { broadcastStarted(getRuleSet()); } 86 87 protected void broadcastStarted(RuleSet targetRuleSet) 88 throws ListenerException 89 { 90 for(Iterator i = listeners.iterator(); i.hasNext(); ) 91 ((MackerEventListener) i.next()).mackerStarted(targetRuleSet); 92 if(getParent() != null) 93 getParent().broadcastStarted(targetRuleSet); 94 } 95 96 public void broadcastFinished() 97 throws MackerIsMadException, ListenerException 98 { broadcastFinished(getRuleSet()); } 99 100 protected void broadcastFinished(RuleSet targetRuleSet) 101 throws MackerIsMadException, ListenerException 102 { 103 for(Iterator i = listeners.iterator(); i.hasNext(); ) 104 ((MackerEventListener) i.next()).mackerFinished(targetRuleSet); 105 if(getParent() != null) 106 getParent().broadcastFinished(targetRuleSet); 107 } 108 109 public void broadcastAborted() 110 { broadcastAborted(getRuleSet()); } 111 112 protected void broadcastAborted(RuleSet targetRuleSet) 113 { 114 for(Iterator i = listeners.iterator(); i.hasNext(); ) 115 ((MackerEventListener) i.next()).mackerAborted(targetRuleSet); 116 if(getParent() != null) 117 getParent().broadcastAborted(targetRuleSet); 118 } 119 120 public void broadcastEvent(MackerEvent event) 121 throws MackerIsMadException, ListenerException 122 { broadcastEvent(event, getRuleSet()); } 123 124 protected void broadcastEvent(MackerEvent event, RuleSet targetRuleSet) 125 throws MackerIsMadException, ListenerException 126 { 127 for(Iterator i = listeners.iterator(); i.hasNext(); ) 128 ((MackerEventListener) i.next()).handleMackerEvent(targetRuleSet, event); 129 if(getParent() != null) 130 getParent().broadcastEvent(event, targetRuleSet); 131 } 132 133 private RuleSet ruleSet; 134 private EvaluationContext parent; 135 private Map varValues; 136 private Set listeners; 137 private ClassManager classManager; 138 } 139 140 141 142 | Popular Tags |