--- /dev/null
+/**\r
+ * Engine class\r
+ * Maps token types to actions\r
+ *\r
+ * @author Daniel Jackson\r
+ * @version 0, 07/06/01\r
+ */\r
+\r
+//package tagger;\r
+//import java.util.*;\r
+\r
+public class Engine {\r
+ /**\r
+ * There are some very tricky concurrent modification issues with this class.\r
+ * Can't execute a register or unregister method during an execution of consume_token\r
+ * if the register or unregister affects the same list of actions associated with the token.\r
+ * This means that during a consume_token for some type, can't register or unregister for\r
+ * that type, or for the all types.\r
+ * Note that a version of the perform method of action takes an iterator argument to\r
+ * allow an action to remove itself.\r
+ */\r
+ \r
+ // array of Action lists indexed on token type\r
+ private LinkedList [] actions;\r
+\r
+ // actions performed for all token types\r
+ private LinkedList default_actions;\r
+\r
+ public Engine () {\r
+ actions = new LinkedList [Token.MAXTOKEN() + 1];\r
+ for (int i = 0; i < actions.length; i++)\r
+ actions[i] = new LinkedList ();\r
+ default_actions = new LinkedList ();\r
+ }\r
+\r
+ public void register_by_type (Action action, int type) {\r
+ register_by_type_front (action, type);\r
+ }\r
+\r
+ public void register_for_all (Action action) {\r
+ default_actions.addFirst (action);\r
+ }\r
+\r
+ public void unregister_for_all (Action action) {\r
+ default_actions.remove (action);\r
+ }\r
+\r
+ public void register_by_type_front (Action action, int type) {\r
+ Assert.assert_ (type >= 0);\r
+ Assert.assert_ (type <= Token.MAXTOKEN());\r
+ actions[type].addFirst (action);\r
+ }\r
+\r
+ public void register_by_type_back (Action action, int type) {\r
+ Assert.assert_ (type >= 0);\r
+ Assert.assert_ (type <= Token.MAXTOKEN());\r
+ actions[type].addLast (action);\r
+ }\r
+\r
+ public void unregister_by_type (Action action, int type) {\r
+ Assert.assert_ (type >= 0);\r
+ Assert.assert_ (type <= Token.MAXTOKEN());\r
+ actions[type].remove (action);\r
+ }\r
+\r
+ public void consume_token (Token token) {\r
+ perform_actions (default_actions, token);\r
+ perform_actions (actions[token.type], token);\r
+ }\r
+ \r
+ public static void perform_actions (LinkedList actions, Token token) {\r
+ Iterator i = actions.iterator ();\r
+ while (i.hasNext ()) {\r
+ Action a = (Action) i.next ();\r
+ a.perform (token, i);\r
+ }\r
+ }\r
+}\r