KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > innig > macker > rule > EvaluationContext


1 /*______________________________________________________________________________
2  *
3  * Macker http://innig.net/macker/
4  *
5  * Copyright 2002-2003 Paul Cantrell
6  *
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License version 2, as published by the
9  * Free Software Foundation. See the file LICENSE.html for more information.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the license for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
17  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
18  *______________________________________________________________________________
19  */

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 JavaDoc name, String JavaDoc value)
60         throws UndeclaredVariableException
61         { varValues.put(name, (value == null) ? "" : VariableParser.parse(this, value)); }
62     
63     public String JavaDoc getVariableValue(String JavaDoc name)
64         throws UndeclaredVariableException
65         {
66         String JavaDoc value = (String JavaDoc) 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/*<String,String>*/ 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