Author: pkluegl
Date: Fri Oct 21 11:33:26 2011
New Revision: 1187303

URL: http://svn.apache.org/viewvc?rev=1187303&view=rev
Log:
UIMA-2238
The place where the scripts are parsed is now configurable: by default scripts 
are parsed in the initialize method and the types are set in the process 
method. Removed all dependencies of the CAS and type system from the parsing 
code. The values of variables will be reset if the script is not parsed again.

Modified:
    
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerAutomataBlock.java
    
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerAutomataFactory.java
    
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerBlock.java
    
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerEnvironment.java
    
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerModule.java
    
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerScriptBlock.java
    
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerScriptFactory.java
    
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerStatement.java
    
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/action/MarkFastAction.java
    
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/engine/TextMarkerEngine.java
    
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/expression/ExpressionFactory.java
    
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/expression/bool/BooleanTypeExpression.java
    
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/expression/type/SimpleTypeExpression.java
    
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/parser/TextMarkerParser.g
    
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/parser/TextMarkerParser.java
    
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/TextMarkerTypeMatcher.java
    
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/ExpressionVerbalizer.java

Modified: 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerAutomataBlock.java
URL: 
http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerAutomataBlock.java?rev=1187303&r1=1187302&r2=1187303&view=diff
==============================================================================
--- 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerAutomataBlock.java
 (original)
+++ 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerAutomataBlock.java
 Fri Oct 21 11:33:26 2011
@@ -21,7 +21,6 @@ package org.apache.uima.textmarker;
 
 import java.util.List;
 
-import org.apache.uima.cas.CAS;
 import org.apache.uima.cas.Type;
 import org.apache.uima.cas.text.AnnotationFS;
 import org.apache.uima.textmarker.rule.RuleApply;
@@ -33,9 +32,8 @@ import org.apache.uima.textmarker.visito
 public class TextMarkerAutomataBlock extends TextMarkerBlock {
 
   public TextMarkerAutomataBlock(String id, TextMarkerRule rule,
-          List<TextMarkerStatement> elements, TextMarkerBlock parent, String 
defaultNamespace,
-          CAS cas) {
-    super(id, rule, elements, parent, defaultNamespace, cas);
+          List<TextMarkerStatement> elements, TextMarkerBlock parent, String 
defaultNamespace) {
+    super(id, rule, elements, parent, defaultNamespace);
   }
 
   @Override

Modified: 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerAutomataFactory.java
URL: 
http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerAutomataFactory.java?rev=1187303&r1=1187302&r2=1187303&view=diff
==============================================================================
--- 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerAutomataFactory.java
 (original)
+++ 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerAutomataFactory.java
 Fri Oct 21 11:33:26 2011
@@ -22,14 +22,13 @@ package org.apache.uima.textmarker;
 import java.util.List;
 
 import org.antlr.runtime.Token;
-import org.apache.uima.cas.CAS;
 import org.apache.uima.textmarker.rule.TextMarkerRuleElement;
 
 public class TextMarkerAutomataFactory extends TextMarkerScriptFactory {
 
   public TextMarkerBlock createAutomataBlock(Token id, TextMarkerRuleElement 
re,
-          List<TextMarkerStatement> body, TextMarkerBlock env, CAS cas) {
-    return createScriptBlock(id, re, body, env, cas);
+          List<TextMarkerStatement> body, TextMarkerBlock env) {
+    return createScriptBlock(id, re, body, env);
   }
 
 }

Modified: 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerBlock.java
URL: 
http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerBlock.java?rev=1187303&r1=1187302&r2=1187303&view=diff
==============================================================================
--- 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerBlock.java
 (original)
+++ 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerBlock.java
 Fri Oct 21 11:33:26 2011
@@ -21,7 +21,6 @@ package org.apache.uima.textmarker;
 
 import java.util.List;
 
-import org.apache.uima.cas.CAS;
 import org.apache.uima.textmarker.rule.TextMarkerRule;
 
 public abstract class TextMarkerBlock extends TextMarkerStatement {
@@ -39,12 +38,12 @@ public abstract class TextMarkerBlock ex
   private TextMarkerModule script;
 
   public TextMarkerBlock(String id, TextMarkerRule rule, 
List<TextMarkerStatement> elements,
-          TextMarkerBlock parent, String defaultNamespace, CAS cas) {
+          TextMarkerBlock parent, String defaultNamespace) {
     super(parent);
     this.id = id;
     this.rule = rule;
     this.elements = elements;
-    this.environment = new TextMarkerEnvironment(cas, this);
+    this.environment = new TextMarkerEnvironment(this);
     this.namespace = defaultNamespace;
   }
 
@@ -70,10 +69,15 @@ public abstract class TextMarkerBlock ex
   }
 
   public TextMarkerModule getScript() {
-    if (getParent() != null) {
+    if (script != null) {
+      return script;
+    } else if (getParent() != null) {
       return getParent().getScript();
     } else {
-      return script;
+      // may not happen!
+      assert (false);
+      return null;
+
     }
   }
 

Modified: 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerEnvironment.java
URL: 
http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerEnvironment.java?rev=1187303&r1=1187302&r2=1187303&view=diff
==============================================================================
--- 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerEnvironment.java
 (original)
+++ 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerEnvironment.java
 Fri Oct 21 11:33:26 2011
@@ -24,12 +24,16 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import org.apache.uima.cas.CAS;
+import org.apache.uima.cas.CASException;
 import org.apache.uima.cas.Type;
-import org.apache.uima.cas.TypeSystem;
+import org.apache.uima.jcas.cas.TOP;
+import org.apache.uima.jcas.tcas.DocumentAnnotation;
 import org.apache.uima.textmarker.action.AbstractTextMarkerAction;
 import org.apache.uima.textmarker.condition.AbstractTextMarkerCondition;
 import org.apache.uima.textmarker.expression.list.ListExpression;
@@ -47,6 +51,8 @@ import org.apache.uima.textmarker.resour
 
 public class TextMarkerEnvironment {
 
+  private final Object annotationTypeDummy = new Object();
+
   private Map<String, Type> types;
 
   private Map<String, TextMarkerWordList> wordLists;
@@ -71,10 +77,9 @@ public class TextMarkerEnvironment {
 
   private CAS cas;
 
-  public TextMarkerEnvironment(CAS cas, TextMarkerBlock owner) {
+  public TextMarkerEnvironment(TextMarkerBlock owner) {
     super();
     this.owner = owner;
-    this.cas = cas;
     types = new HashMap<String, Type>();
     namespaces = new HashMap<String, String>();
     wordLists = new HashMap<String, TextMarkerWordList>();
@@ -106,6 +111,26 @@ public class TextMarkerEnvironment {
     resourcePaths = getResourcePaths();
   }
 
+  public void initializeTypes(CAS cas) {
+    this.cas = cas;
+    Type topType = null;
+    try {
+      topType = cas.getJCas().getCasType(TOP.type);
+      if (topType != null) {
+        List<Type> list = 
cas.getTypeSystem().getProperlySubsumedTypes(topType);
+        for (Type type : list) {
+          addType(type);
+        }
+      }
+      Type documentType = cas.getJCas().getCasType(DocumentAnnotation.type);
+      addType("Document", documentType);
+      Type annotationType = 
cas.getJCas().getCasType(org.apache.uima.jcas.tcas.Annotation.type);
+      addType("Annotation", annotationType);
+    } catch (CASException e) {
+    }
+
+  }
+
   public String[] getResourcePaths() {
     if (resourcePaths == null) {
       TextMarkerBlock parent = owner.getParent();
@@ -153,15 +178,6 @@ public class TextMarkerEnvironment {
     return type;
   }
 
-  public void addType(String string) {
-    TypeSystem ts = cas.getTypeSystem();
-    string = expand(string);
-    Type type = ts.getType(string);
-    if (type != null) {
-      types.put(string, type);
-    }
-  }
-
   public void addType(String string, Type type) {
     namespaces.put(string, type.getName());
     types.put(type.getName(), type);
@@ -233,7 +249,11 @@ public class TextMarkerEnvironment {
     } else if (Boolean.class.equals(type)) {
       return false;
     } else if (Type.class.equals(type)) {
-      return cas.getAnnotationType();
+      if (cas == null) {
+        return annotationTypeDummy;
+      } else {
+        return cas.getAnnotationType();
+      }
     } else if (List.class.equals(type)) {
       return new ArrayList<Object>();
     }
@@ -292,7 +312,16 @@ public class TextMarkerEnvironment {
   }
 
   public <T> T getVariableValue(String name, Class<T> type) {
+    boolean containsKey = variableValues.containsKey(name);
     Object result = variableValues.get(name);
+    if (containsKey && result == null) {
+      // TODO find the problem with the null values!
+      // this might now work for word lists in another env.
+      return type.cast(getInitialValue(type));
+    }
+    if (result == annotationTypeDummy) {
+      return type.cast(cas.getAnnotationType());
+    }
     if (result != null) {
       return type.cast(result);
     } else if (owner.getParent() != null) {
@@ -355,4 +384,17 @@ public class TextMarkerEnvironment {
     return null;
   }
 
+  public void reset(CAS cas) {
+    this.cas = cas;
+    Set<Entry<String, Object>> entrySet = variableValues.entrySet();
+    for (Entry<String, Object> entry : entrySet) {
+      String key = entry.getKey();
+      Object initialValue = getInitialValue(variableTypes.get(key));
+      if (initialValue != null) {
+        // not for word lists
+        entry.setValue(initialValue);
+      }
+    }
+  }
+
 }

Modified: 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerModule.java
URL: 
http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerModule.java?rev=1187303&r1=1187302&r2=1187303&view=diff
==============================================================================
--- 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerModule.java
 (original)
+++ 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerModule.java
 Fri Oct 21 11:33:26 2011
@@ -15,7 +15,7 @@
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
-*/
+ */
 
 package org.apache.uima.textmarker;
 
@@ -25,7 +25,6 @@ import java.util.Map;
 import org.apache.uima.analysis_engine.AnalysisEngine;
 import org.apache.uima.textmarker.visitor.InferenceCrowd;
 
-
 public class TextMarkerModule extends TextMarkerElement {
 
   private final TextMarkerBlock rootBlock;
@@ -119,6 +118,10 @@ public class TextMarkerModule extends Te
     return scripts;
   }
 
+  public Map<String, TextMarkerBlock> getBlocks() {
+    return blocks;
+  }
+
   public Map<String, AnalysisEngine> getEngines() {
     return engines;
   }

Modified: 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerScriptBlock.java
URL: 
http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerScriptBlock.java?rev=1187303&r1=1187302&r2=1187303&view=diff
==============================================================================
--- 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerScriptBlock.java
 (original)
+++ 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerScriptBlock.java
 Fri Oct 21 11:33:26 2011
@@ -21,7 +21,6 @@ package org.apache.uima.textmarker;
 
 import java.util.List;
 
-import org.apache.uima.cas.CAS;
 import org.apache.uima.cas.Type;
 import org.apache.uima.cas.text.AnnotationFS;
 import org.apache.uima.textmarker.rule.RuleApply;
@@ -33,8 +32,8 @@ import org.apache.uima.textmarker.visito
 public class TextMarkerScriptBlock extends TextMarkerBlock {
 
   public TextMarkerScriptBlock(String id, TextMarkerRule rule, 
List<TextMarkerStatement> elements,
-          TextMarkerBlock parent, String defaultNamespace, CAS cas) {
-    super(id, rule, elements, parent, defaultNamespace, cas);
+          TextMarkerBlock parent, String defaultNamespace) {
+    super(id, rule, elements, parent, defaultNamespace);
   }
 
   @Override
@@ -53,7 +52,7 @@ public class TextMarkerScriptBlock exten
           continue;
         }
         List<Type> types = ((TextMarkerRuleElement) 
rule.getRuleElements().get(0)).getMatcher()
-                .getTypes(getParent(), stream);
+                .getTypes(getParent() == null ? this : getParent(), stream);
         for (Type eachType : types) {
           TextMarkerStream window = stream.getWindowStream(each, eachType);
           for (TextMarkerStatement element : getElements()) {

Modified: 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerScriptFactory.java
URL: 
http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerScriptFactory.java?rev=1187303&r1=1187302&r2=1187303&view=diff
==============================================================================
--- 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerScriptFactory.java
 (original)
+++ 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerScriptFactory.java
 Fri Oct 21 11:33:26 2011
@@ -23,11 +23,6 @@ import java.util.ArrayList;
 import java.util.List;
 
 import org.antlr.runtime.Token;
-import org.apache.uima.cas.CAS;
-import org.apache.uima.cas.CASException;
-import org.apache.uima.cas.CASRuntimeException;
-import org.apache.uima.cas.Type;
-import org.apache.uima.jcas.cas.TOP;
 import org.apache.uima.resource.metadata.TypeSystemDescription;
 import org.apache.uima.textmarker.action.AbstractTextMarkerAction;
 import org.apache.uima.textmarker.condition.AbstractTextMarkerCondition;
@@ -63,17 +58,17 @@ public class TextMarkerScriptFactory {
   }
 
   public TextMarkerScriptBlock createScriptBlock(Token id, 
TextMarkerRuleElement ruleElement,
-          List<TextMarkerStatement> body, TextMarkerBlock parent, CAS cas) {
+          List<TextMarkerStatement> body, TextMarkerBlock parent) {
     String text = id == null ? "root" : id.getText();
     String defaultNamespace = parent.getNamespace();
     if (id != null) {
       defaultNamespace = defaultNamespace + "." + text;
     }
-    return createScriptBlock(text, ruleElement, body, parent, 
defaultNamespace, cas);
+    return createScriptBlock(text, ruleElement, body, parent, 
defaultNamespace);
   }
 
   public TextMarkerScriptBlock createScriptBlock(String text, 
TextMarkerRuleElement ruleElement,
-          List<TextMarkerStatement> body, TextMarkerBlock parent, String 
defaultNamespace, CAS cas) {
+          List<TextMarkerStatement> body, TextMarkerBlock parent, String 
defaultNamespace) {
     TextMarkerRule rule = null;
     if (ruleElement != null) {
       rule = createRule(ruleElement, parent);
@@ -86,34 +81,17 @@ public class TextMarkerScriptFactory {
         }
       }
     }
-    return new TextMarkerScriptBlock(text, rule, elements, parent, 
defaultNamespace, cas);
+    return new TextMarkerScriptBlock(text, rule, elements, parent, 
defaultNamespace);
   }
 
-  public TextMarkerScriptBlock createRootScriptBlock(String module, String 
pack, CAS cas,
+  public TextMarkerScriptBlock createRootScriptBlock(String module, String 
pack,
           TypeSystemDescription localTSD) {
     String defaultNamespace = pack + "." + module;
-    TextMarkerScriptBlock result = createScriptBlock(module, null, null, null, 
defaultNamespace,
-            cas);
-    TextMarkerEnvironment environment = result.getEnvironment();
-    try {
-      Type topType = cas.getJCas().getCasType(TOP.type);
-      List<Type> list = cas.getTypeSystem().getProperlySubsumedTypes(topType);
-      for (Type type : list) {
-        if (localTSD == null || localTSD.getType(type.getName()) != null) {
-          environment.addType(type);
-        }
-      }
-    } catch (CASRuntimeException e) {
-      e.printStackTrace();
-    } catch (CASException e) {
-      e.printStackTrace();
-    }
-
+    TextMarkerScriptBlock result = createScriptBlock(module, null, null, null, 
defaultNamespace);
     List<RuleElement> ruleElements = new ArrayList<RuleElement>();
-
     RuleElementIsolator container = new RuleElementIsolator();
-    ruleElements.add(createRuleElement(new 
SimpleTypeExpression(cas.getDocumentAnnotation()
-            .getType()), null, null, null, container, result));
+    ruleElements.add(createRuleElement(new 
SimpleTypeExpression("uima.tcas.DocumentAnnotation"),
+            null, null, null, container, result));
     TextMarkerRule createRule = createRule(ruleElements, result);
     container.setContainer(createRule);
 
@@ -206,8 +184,8 @@ public class TextMarkerScriptFactory {
   }
 
   public TextMarkerBlock createAutomataBlock(Token id, TextMarkerRuleElement 
re,
-          List<TextMarkerStatement> body, TextMarkerBlock env, CAS cas) {
-    return createScriptBlock(id, re, body, env, cas);
+          List<TextMarkerStatement> body, TextMarkerBlock env) {
+    return createScriptBlock(id, re, body, env);
   }
 
 }

Modified: 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerStatement.java
URL: 
http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerStatement.java?rev=1187303&r1=1187302&r2=1187303&view=diff
==============================================================================
--- 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerStatement.java
 (original)
+++ 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/TextMarkerStatement.java
 Fri Oct 21 11:33:26 2011
@@ -15,7 +15,7 @@
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
-*/
+ */
 
 package org.apache.uima.textmarker;
 
@@ -23,19 +23,23 @@ import org.apache.uima.textmarker.visito
 
 public abstract class TextMarkerStatement extends TextMarkerElement {
 
-  private final TextMarkerBlock parent;
+  private TextMarkerBlock parent;
 
   public TextMarkerStatement(TextMarkerBlock parent) {
     super();
     this.parent = parent;
   }
 
+  public abstract ScriptApply apply(TextMarkerStream stream, InferenceCrowd 
crowd);
+
+  public abstract TextMarkerEnvironment getEnvironment();
+
   public TextMarkerBlock getParent() {
     return parent;
   }
 
-  public abstract ScriptApply apply(TextMarkerStream stream, InferenceCrowd 
crowd);
-
-  public abstract TextMarkerEnvironment getEnvironment();
+  public void setParent(TextMarkerBlock parent) {
+    this.parent = parent;
+  }
 
 }

Modified: 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/action/MarkFastAction.java
URL: 
http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/action/MarkFastAction.java?rev=1187303&r1=1187302&r2=1187303&view=diff
==============================================================================
--- 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/action/MarkFastAction.java
 (original)
+++ 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/action/MarkFastAction.java
 Fri Oct 21 11:33:26 2011
@@ -71,19 +71,6 @@ public class MarkFastAction extends Abst
         }
       }
     }
-
-    // if(list.contains(matchedAnnotation.getCoveredText(), ignore, 0)) {
-    // createAnnotation(match, stream, matchedAnnotation);
-    // } else {
-    // List<TextMarkerBasic> annotationsInWindow =
-    // stream.getBasicAnnotationsInWindow(matchedAnnotation,
-    // TextMarkerBasic.class);
-    // for (TextMarkerBasic textMarkerBasic : annotationsInWindow) {
-    // if(list.contains(textMarkerBasic.getCoveredText(), ignore, 0)) {
-    // createAnnotation(match, stream, textMarkerBasic);
-    // }
-    // }
-    // }
   }
 
   public WordListExpression getList() {

Modified: 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/engine/TextMarkerEngine.java
URL: 
http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/engine/TextMarkerEngine.java?rev=1187303&r1=1187302&r2=1187303&view=diff
==============================================================================
--- 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/engine/TextMarkerEngine.java
 (original)
+++ 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/engine/TextMarkerEngine.java
 Fri Oct 21 11:33:26 2011
@@ -53,6 +53,7 @@ import org.apache.uima.resource.metadata
 import org.apache.uima.resource.metadata.ConfigurationParameterDeclarations;
 import org.apache.uima.resource.metadata.TypeSystemDescription;
 import org.apache.uima.textmarker.FilterManager;
+import org.apache.uima.textmarker.TextMarkerBlock;
 import org.apache.uima.textmarker.TextMarkerModule;
 import org.apache.uima.textmarker.TextMarkerStream;
 import org.apache.uima.textmarker.extensions.IEngineLoader;
@@ -117,6 +118,8 @@ public class TextMarkerEngine extends JC
 
   public static final String DYNAMIC_ANCHORING = "dynamicAnchoring";
 
+  public static final String RELOAD_SCRIPT = "reloadScript";
+
   private String[] seeders;
 
   private String useBasics;
@@ -129,8 +132,6 @@ public class TextMarkerEngine extends JC
 
   private Boolean createStatisticInfo;
 
-  private String styleMapLocation;
-
   private Boolean withMatches;
 
   private String[] resourcePaths;
@@ -171,6 +172,10 @@ public class TextMarkerEngine extends JC
 
   private Boolean dynamicAnchoring;
 
+  private Boolean reloadScript;
+
+  private boolean initialized = false;
+
   @Override
   public void initialize(UimaContext aContext) throws 
ResourceInitializationException {
     super.initialize(aContext);
@@ -200,6 +205,7 @@ public class TextMarkerEngine extends JC
     defaultFilteredTypes = (String[]) 
aContext.getConfigParameterValue(DEFAULT_FILTERED_TYPES);
     defaultFilteredMarkups = (String[]) 
aContext.getConfigParameterValue(DEFAULT_FILTERED_MARKUPS);
     dynamicAnchoring = (Boolean) 
aContext.getConfigParameterValue(DYNAMIC_ANCHORING);
+    reloadScript = (Boolean) aContext.getConfigParameterValue(RELOAD_SCRIPT);
 
     removeBasics = removeBasics == null ? false : removeBasics;
     createDebugInfo = createDebugInfo == null ? false : createDebugInfo;
@@ -213,6 +219,7 @@ public class TextMarkerEngine extends JC
     defaultFilteredMarkups = defaultFilteredMarkups == null ? new String[0]
             : defaultFilteredMarkups;
     dynamicAnchoring = dynamicAnchoring == null ? false : dynamicAnchoring;
+    reloadScript = reloadScript == null ? false : reloadScript;
 
     this.context = aContext;
 
@@ -221,10 +228,6 @@ public class TextMarkerEngine extends JC
     verbalizer = new TextMarkerVerbalizer();
 
     localTSDMap = new HashMap<String, TypeSystemDescription>();
-  }
-
-  @Override
-  public void process(JCas cas) throws AnalysisEngineProcessException {
 
     if (!factory.isInitialized()) {
       initializeExtensionWithClassPath();
@@ -232,8 +235,28 @@ public class TextMarkerEngine extends JC
     if (!engineLoader.isInitialized()) {
       initializeEngineLoaderWithClassPath();
     }
-    initializeScript(cas.getCas());
-    TextMarkerStream stream = initializeStream(cas.getCas());
+    if (!reloadScript) {
+      try {
+        initializeScript();
+      } catch (AnalysisEngineProcessException e) {
+        throw new ResourceInitializationException(e);
+      }
+    }
+  }
+
+  @Override
+  public void process(JCas jcas) throws AnalysisEngineProcessException {
+    CAS cas = jcas.getCas();
+    if (reloadScript) {
+      initializeScript();
+    } else {
+      resetEnvironments(cas);
+    }
+    if (!initialized || reloadScript) {
+      initializeTypes(script, cas);
+      initialized = true;
+    }
+    TextMarkerStream stream = initializeStream(cas);
     stream.setDynamicAnchoring(dynamicAnchoring);
     InferenceCrowd crowd = initializeCrowd();
     try {
@@ -247,7 +270,7 @@ public class TextMarkerEngine extends JC
     if (removeBasics) {
       List<AnnotationFS> toRemove = new ArrayList<AnnotationFS>();
       Type type = cas.getTypeSystem().getType(BASIC_TYPE);
-      FSIterator<AnnotationFS> iterator = 
cas.getCas().getAnnotationIndex(type).iterator();
+      FSIterator<AnnotationFS> iterator = 
cas.getAnnotationIndex(type).iterator();
       while (iterator.isValid()) {
         AnnotationFS fs = iterator.get();
         toRemove.add(fs);
@@ -259,6 +282,43 @@ public class TextMarkerEngine extends JC
     }
   }
 
+  private void resetEnvironments(CAS cas) {
+    resetEnvironment(script, cas);
+    Collection<TextMarkerModule> scripts = script.getScripts().values();
+    for (TextMarkerModule module : scripts) {
+      resetEnvironment(module, cas);
+    }
+  }
+
+  private void resetEnvironment(TextMarkerModule module, CAS cas) {
+    TextMarkerBlock block = module.getBlock(null);
+    block.getEnvironment().reset(cas);
+    Collection<TextMarkerBlock> blocks = module.getBlocks().values();
+    for (TextMarkerBlock each : blocks) {
+      each.getEnvironment().reset(cas);
+    }
+  }
+
+  private void initializeTypes(TextMarkerModule script, CAS cas) {
+    // TODO find a better solution for telling everyone about the types!
+    TextMarkerBlock mainRootBlock = script.getBlock(null);
+    mainRootBlock.getEnvironment().initializeTypes(cas);
+    Collection<TextMarkerModule> values = script.getScripts().values();
+    for (TextMarkerModule eachModule : values) {
+      relinkEnvironments(eachModule, mainRootBlock);
+      // initializeTypes(eachModule, cas);
+    }
+  }
+
+  private void relinkEnvironments(TextMarkerModule script, TextMarkerBlock 
mainRootBlock) {
+    TextMarkerBlock block = script.getBlock(null);
+    block.setParent(mainRootBlock);
+    Collection<TextMarkerModule> innerScripts = script.getScripts().values();
+    for (TextMarkerModule textMarkerModule : innerScripts) {
+      relinkEnvironments(textMarkerModule, mainRootBlock);
+    }
+  }
+
   private void initializeExtensionWithClassPath() {
     if (additionalExtensions == null) {
       return;
@@ -383,14 +443,17 @@ public class TextMarkerEngine extends JC
     return stream;
   }
 
-  private void initializeScript(CAS cas) throws AnalysisEngineProcessException 
{
+  private void initializeScript() throws AnalysisEngineProcessException {
     String scriptLocation = locate(mainScript, scriptPaths, ".tm");
+    if (scriptLocation == null) {
+      // if someone loads an empty analysis engine and then reconfigures it
+      return;
+    }
     try {
-      script = loadScript(scriptLocation, cas, null);
+      script = loadScript(scriptLocation, null);
     } catch (Exception e) {
       throw new AnalysisEngineProcessException(e);
     }
-
     Map<String, TextMarkerModule> additionalScripts = new HashMap<String, 
TextMarkerModule>();
     Map<String, AnalysisEngine> additionalEngines = new HashMap<String, 
AnalysisEngine>();
 
@@ -409,10 +472,9 @@ public class TextMarkerEngine extends JC
 
     if (additionalScriptLocations != null) {
       for (String add : additionalScriptLocations) {
-        recursiveLoadScript(add, additionalScripts, additionalEngines, cas);
+        recursiveLoadScript(add, additionalScripts, additionalEngines);
       }
     }
-
     for (TextMarkerModule each : additionalScripts.values()) {
       each.setScriptDependencies(additionalScripts);
     }
@@ -490,16 +552,15 @@ public class TextMarkerEngine extends JC
   }
 
   private void recursiveLoadScript(String toLoad, Map<String, 
TextMarkerModule> additionalScripts,
-          Map<String, AnalysisEngine> additionalEngines, CAS cas)
-          throws AnalysisEngineProcessException {
+          Map<String, AnalysisEngine> additionalEngines) throws 
AnalysisEngineProcessException {
     String location = locate(toLoad, scriptPaths, ".tm");
     try {
       TypeSystemDescription localTSD = getLocalTSD(toLoad);
-      TextMarkerModule eachScript = loadScript(location, cas, localTSD);
+      TextMarkerModule eachScript = loadScript(location, localTSD);
       additionalScripts.put(toLoad, eachScript);
       for (String add : eachScript.getScripts().keySet()) {
         if (!additionalScripts.containsKey(add)) {
-          recursiveLoadScript(add, additionalScripts, additionalEngines, cas);
+          recursiveLoadScript(add, additionalScripts, additionalEngines);
         }
       }
       Set<String> engineKeySet = eachScript.getEngines().keySet();
@@ -549,14 +610,13 @@ public class TextMarkerEngine extends JC
     return result;
   }
 
-  private TextMarkerModule loadScript(String scriptLocation, CAS cas, 
TypeSystemDescription localTSD)
+  private TextMarkerModule loadScript(String scriptLocation, 
TypeSystemDescription localTSD)
           throws IOException, RecognitionException {
     File scriptFile = new File(scriptLocation);
     CharStream st = new ANTLRFileStream(scriptLocation, scriptEncoding);
     TextMarkerLexer lexer = new TextMarkerLexer(st);
     CommonTokenStream tokens = new CommonTokenStream(lexer);
     TextMarkerParser parser = new TextMarkerParser(tokens);
-    parser.setCAS(cas);
     parser.setLocalTSD(localTSD);
     parser.setExternalFactory(factory);
     parser.setResourcePaths(resourcePaths);

Modified: 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/expression/ExpressionFactory.java
URL: 
http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/expression/ExpressionFactory.java?rev=1187303&r1=1187302&r2=1187303&view=diff
==============================================================================
--- 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/expression/ExpressionFactory.java
 (original)
+++ 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/expression/ExpressionFactory.java
 Fri Oct 21 11:33:26 2011
@@ -23,7 +23,6 @@ import java.util.ArrayList;
 import java.util.List;
 
 import org.antlr.runtime.Token;
-import org.apache.uima.cas.Type;
 import org.apache.uima.textmarker.TextMarkerBlock;
 import org.apache.uima.textmarker.expression.bool.BooleanExpression;
 import org.apache.uima.textmarker.expression.bool.BooleanNumberExpression;
@@ -143,13 +142,7 @@ public class ExpressionFactory {
 
   public static TypeExpression createSimpleTypeExpression(Token typeToken, 
TextMarkerBlock parent) {
     String typeString = typeToken == null ? "uima.tcas.DocumentAnnotation" : 
typeToken.getText();
-    Type type = parent.getEnvironment().getType(typeString);
-    if (type == null) {
-      NullPointerException exception = new NullPointerException("Type " + 
typeString
-              + " is not defined in current type system");
-      throw exception;
-    }
-    return new SimpleTypeExpression(type);
+    return new SimpleTypeExpression(typeString);
   }
 
   public static TypeExpression createReferenceTypeExpression(Token varToken) {
@@ -158,8 +151,7 @@ public class ExpressionFactory {
   }
 
   public static TypeExpression createSimpleTypeExpression(String typeString, 
TextMarkerBlock parent) {
-    Type type = parent.getEnvironment().getType(typeString);
-    return new SimpleTypeExpression(type);
+    return new SimpleTypeExpression(typeString);
   }
 
   public static BooleanExpression createBooleanFunction(Token op, 
BooleanExpression e1,

Modified: 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/expression/bool/BooleanTypeExpression.java
URL: 
http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/expression/bool/BooleanTypeExpression.java?rev=1187303&r1=1187302&r2=1187303&view=diff
==============================================================================
--- 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/expression/bool/BooleanTypeExpression.java
 (original)
+++ 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/expression/bool/BooleanTypeExpression.java
 Fri Oct 21 11:33:26 2011
@@ -15,10 +15,11 @@
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
-*/
+ */
 
 package org.apache.uima.textmarker.expression.bool;
 
+import org.apache.uima.cas.Type;
 import org.apache.uima.textmarker.TextMarkerStatement;
 import org.apache.uima.textmarker.expression.type.TypeExpression;
 
@@ -39,8 +40,10 @@ public class BooleanTypeExpression exten
 
   @Override
   public boolean getBooleanValue(TextMarkerStatement parent) {
-    String first = getFristExpression().getType(parent).getName();
-    String second = getSecondExpression().getType(parent).getName();
+    Type type1 = getFristExpression().getType(parent);
+    String first = type1.getName();
+    Type type2 = getSecondExpression().getType(parent);
+    String second = type2.getName();
     return eval(first, getOperator(), second);
   }
 

Modified: 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/expression/type/SimpleTypeExpression.java
URL: 
http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/expression/type/SimpleTypeExpression.java?rev=1187303&r1=1187302&r2=1187303&view=diff
==============================================================================
--- 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/expression/type/SimpleTypeExpression.java
 (original)
+++ 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/expression/type/SimpleTypeExpression.java
 Fri Oct 21 11:33:26 2011
@@ -24,26 +24,30 @@ import org.apache.uima.textmarker.TextMa
 
 public class SimpleTypeExpression extends TypeExpression {
 
-  private final Type type;
+  private final String typeString;
 
-  public SimpleTypeExpression(Type type) {
+  public SimpleTypeExpression(String typeString) {
     super();
-    this.type = type;
+    this.typeString = typeString;
   }
 
   @Override
   public Type getType(TextMarkerStatement parent) {
-    return type;
+    return parent.getEnvironment().getType(typeString);
   }
 
   @Override
   public String toString() {
-    return type.getShortName();
+    return typeString;
   }
 
   @Override
   public String getStringValue(TextMarkerStatement parent) {
-    return type.getName();
+    return typeString;
+  }
+
+  public String getTypeString() {
+    return typeString;
   }
 
 }

Modified: 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/parser/TextMarkerParser.g
URL: 
http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/parser/TextMarkerParser.g?rev=1187303&r1=1187302&r2=1187303&view=diff
==============================================================================
--- 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/parser/TextMarkerParser.g
 (original)
+++ 
uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/parser/TextMarkerParser.g
 Fri Oct 21 11:33:26 2011
@@ -48,10 +48,6 @@ import org.antlr.runtime.RecognitionExce
 import org.antlr.runtime.RecognizerSharedState;
 import org.antlr.runtime.Token;
 import org.antlr.runtime.TokenStream;
-import org.apache.uima.cas.CAS;
-import org.apache.uima.cas.CASException;
-import org.apache.uima.cas.Type;
-import org.apache.uima.jcas.tcas.DocumentAnnotation;
 import org.apache.uima.resource.metadata.TypeSystemDescription;
 
 import org.apache.uima.textmarker.action.AbstractTextMarkerAction;
@@ -176,14 +172,10 @@ public void emitErrorMessage(String msg)
                return null;
        }
        
-       private CAS cas;
        private TypeSystemDescription localTSD;
        private String[] resourcePaths;
        
-       public void setCAS(CAS cas) {
-               this.cas = cas;
-       }
-       
+
        public void setResourcePaths(String[] resourcePaths) {
                this.resourcePaths = resourcePaths;
        }
@@ -211,17 +203,8 @@ List<TextMarkerStatement> stmts = new Ar
        :
        p = packageDeclaration  
        {
-       rootBlock = factory.createRootScriptBlock(moduleName, p, cas, localTSD);
-               
-        try {
-               Type documentType = 
cas.getJCas().getCasType(DocumentAnnotation.type);
-               rootBlock.getEnvironment().addType("Document", documentType);
-               Type annotationType = 
cas.getJCas().getCasType(org.apache.uima.jcas.tcas.Annotation.type);
-               rootBlock.getEnvironment().addType("Annotation", 
annotationType);
-               rootBlock.getEnvironment().setResourcePaths(resourcePaths);
-               } catch (CASException e) {
-                       e.printStackTrace();
-               }
+       rootBlock = factory.createRootScriptBlock(moduleName, p, localTSD);
+        rootBlock.getEnvironment().setResourcePaths(resourcePaths);
        rootBlock.setElements(stmts);
        module = new TextMarkerModule(rootBlock);
        rootBlock.setScript(module);
@@ -412,7 +395,7 @@ level--;
        LPAREN
        id = Identifier 
        RPAREN
-       {block = factory.createScriptBlock(id, re, body, 
$blockDeclaration[level - 1]::env, cas);}
+       {block = factory.createScriptBlock(id, re, body, 
$blockDeclaration[level - 1]::env);}
        {$blockDeclaration::env = block;
        container = new RuleElementIsolator();}
        re1 = ruleElementWithCA[container]
@@ -454,7 +437,7 @@ level--;
        LPAREN
        id = Identifier 
        RPAREN
-       {block = factory.createAutomataBlock(id, re, body, 
$blockDeclaration[level - 1]::env, cas);}
+       {block = factory.createAutomataBlock(id, re, body, 
$blockDeclaration[level - 1]::env);}
        {$blockDeclaration::env = block;
        container = new RuleElementIsolator();}
        re1 = ruleElementWithCA[container] {re = re1;}


Reply via email to