You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by jo...@apache.org on 2011/08/01 17:25:50 UTC
svn commit: r1152824 [6/21] - in
/uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker:
./ action/ batch/ condition/ engine/ kernel/ kernel/constraint/
kernel/expression/ kernel/expression/bool/...
Added: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerEnvironment.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerEnvironment.java?rev=1152824&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerEnvironment.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerEnvironment.java Mon Aug 1 15:24:44 2011
@@ -0,0 +1,340 @@
+package org.apache.uima.tm.textmarker.kernel;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import org.apache.uima.cas.CAS;
+import org.apache.uima.cas.Type;
+import org.apache.uima.cas.TypeSystem;
+import org.apache.uima.tm.textmarker.action.AbstractTextMarkerAction;
+import org.apache.uima.tm.textmarker.condition.AbstractTextMarkerCondition;
+import org.apache.uima.tm.textmarker.kernel.expression.list.ListExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.list.SimpleBooleanListExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.list.SimpleNumberListExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.list.SimpleStringListExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.list.SimpleTypeListExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.resource.LiteralWordListExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.resource.LiteralWordTableExpression;
+import org.apache.uima.tm.textmarker.resource.CSVTable;
+import org.apache.uima.tm.textmarker.resource.TextMarkerTable;
+import org.apache.uima.tm.textmarker.resource.TextMarkerWordList;
+import org.apache.uima.tm.textmarker.resource.TreeWordList;
+import org.apache.uima.tm.textmarker.resource.trie.MultiTreeWordList;
+
+
+public class TextMarkerEnvironment {
+
+ private Map<String, Type> types;
+
+ private Map<String, TextMarkerWordList> wordLists;
+
+ private Map<String, CSVTable> tables;
+
+ private TextMarkerBlock owner;
+
+ private Map<String, String> namespaces;
+
+ private Map<String, Object> variableValues;
+
+ private Map<String, Class<?>> variableTypes;
+
+ private Map<String, Class<?>> availableTypes;
+
+ private Map<String, Class<?>> variableGenericTypes;
+
+ private Map<String, Class<?>> availableListTypes;
+
+ private String[] resourcePaths = null;
+
+ private CAS cas;
+
+ public TextMarkerEnvironment(CAS cas, TextMarkerBlock owner) {
+ super();
+ this.owner = owner;
+ this.cas = cas;
+ types = new HashMap<String, Type>();
+ namespaces = new HashMap<String, String>();
+ wordLists = new HashMap<String, TextMarkerWordList>();
+ tables = new HashMap<String, CSVTable>();
+ variableValues = new HashMap<String, Object>();
+ variableTypes = new HashMap<String, Class<?>>();
+ variableGenericTypes = new HashMap<String, Class<?>>();
+ availableTypes = new HashMap<String, Class<?>>();
+ availableTypes.put("INT", Integer.class);
+ availableTypes.put("STRING", String.class);
+ availableTypes.put("DOUBLE", Double.class);
+ availableTypes.put("BOOLEAN", Boolean.class);
+ availableTypes.put("TYPE", Type.class);
+ availableTypes.put("CONDITION", AbstractTextMarkerCondition.class);
+ availableTypes.put("ACTION", AbstractTextMarkerAction.class);
+ availableTypes.put("WORDLIST", TextMarkerWordList.class);
+ availableTypes.put("WORDTABLE", TextMarkerTable.class);
+ availableTypes.put("BOOLEANLIST", List.class);
+ availableTypes.put("INTLIST", List.class);
+ availableTypes.put("DOUBLELIST", List.class);
+ availableTypes.put("STRINGLIST", List.class);
+ availableTypes.put("TYPELIST", List.class);
+ availableListTypes = new HashMap<String, Class<?>>();
+ availableListTypes.put("BOOLEANLIST", Boolean.class);
+ availableListTypes.put("INTLIST", Integer.class);
+ availableListTypes.put("DOUBLELIST", Double.class);
+ availableListTypes.put("STRINGLIST", String.class);
+ availableListTypes.put("TYPELIST", Type.class);
+ resourcePaths = getResourcePaths();
+ }
+
+ public String[] getResourcePaths() {
+ if (resourcePaths == null) {
+ TextMarkerBlock parent = owner.getParent();
+ if (parent != null) {
+ return parent.getEnvironment().getResourcePaths();
+ }
+ }
+ return resourcePaths;
+ }
+
+ public void setResourcePaths(String[] resourcePaths) {
+ this.resourcePaths = resourcePaths;
+ }
+
+ public boolean ownsType(String match) {
+ match = expand(match);
+ return types.keySet().contains(match);
+ }
+
+ private String expand(String string) {
+ String complete;
+ if (string.indexOf(".") == -1) {
+ complete = namespaces.get(string);
+ if (complete == null) {
+ complete = string;
+ }
+ } else {
+ complete = string;
+ String[] split = complete.split("\\p{Punct}");
+ String name = split[split.length - 1];
+ namespaces.put(name, complete);
+ }
+ return complete;
+ }
+
+ public Type getType(String match) {
+ String expanded = expand(match);
+ Type type = types.get(expanded);
+ if (type == null) {
+ TextMarkerBlock parent = owner.getParent();
+ if (parent != null) {
+ type = parent.getEnvironment().getType(match);
+ }
+ }
+ 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);
+ }
+
+ public void addType(Type type) {
+ addType(type.getShortName(), type);
+ }
+
+ public TextMarkerWordList getWordList(String list) {
+ TextMarkerWordList result = wordLists.get(list);
+ if (result == null) {
+ boolean found = false;
+ for (String eachPath : resourcePaths) {
+ File file = new File(eachPath, list);
+ if (!file.exists()) {
+ continue;
+ }
+ found = true;
+ if (file.getName().endsWith("mtwl")) {
+ wordLists.put(list, new MultiTreeWordList(file.getAbsolutePath()));
+ } else {
+ wordLists.put(list, new TreeWordList(file.getAbsolutePath()));
+ }
+ break;
+ }
+ if (!found) {
+ Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Can't find " + list + "!");
+ }
+ }
+ return wordLists.get(list);
+ }
+
+ public TextMarkerTable getWordTable(String table) {
+ TextMarkerTable result = tables.get(table);
+ if (result == null) {
+ boolean found = false;
+ for (String eachPath : resourcePaths) {
+ File file = new File(eachPath, table);
+ if (!file.exists()) {
+ continue;
+ }
+ found = true;
+ tables.put(table, new CSVTable(file.getAbsolutePath()));
+ break;
+ }
+ if (!found) {
+ Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Can't find " + table + "!");
+ }
+ }
+ return tables.get(table);
+ }
+
+ public void addVariable(String name, Class<?> type, Class<?> generic) {
+ variableTypes.put(name, type);
+ if (generic != null) {
+ variableGenericTypes.put(name, generic);
+ }
+ variableValues.put(name, getInitialValue(type));
+ }
+
+ private Object getInitialValue(Class<?> type) {
+ if (Integer.class.equals(type)) {
+ return 0;
+ } else if (String.class.equals(type)) {
+ return "";
+ } else if (Double.class.equals(type)) {
+ return 0;
+ } else if (Boolean.class.equals(type)) {
+ return false;
+ } else if (Type.class.equals(type)) {
+ return cas.getAnnotationType();
+ } else if (List.class.equals(type)) {
+ return new ArrayList<Object>();
+ }
+
+ return null;
+ }
+
+ public void addVariable(String name, String type) {
+ addVariable(name, availableTypes.get(type), availableListTypes.get(type));
+ }
+
+ public boolean ownsVariable(String name) {
+ return variableTypes.containsKey(name);
+ }
+
+ public boolean ownsVariableOfType(String name, String type) {
+ Class<?> varclass = variableTypes.get(name);
+ Class<?> aclass = availableTypes.get(type);
+ boolean list = true;
+ if (aclass.equals(List.class)) {
+ Class<?> vt = variableGenericTypes.get(name);
+ Class<?> at = availableListTypes.get(type);
+ list = vt != null && vt.equals(at);
+ }
+ return list && varclass != null && varclass.equals(aclass);
+ }
+
+ public boolean isVariable(String name) {
+ return ownsVariable(name) || owner.getParent().getEnvironment().isVariable(name);
+ }
+
+ public boolean isVariableOfType(String name, String type) {
+ return ownsVariableOfType(name, type)
+ || (owner.getParent() != null && owner.getParent().getEnvironment().isVariableOfType(
+ name, type));
+ }
+
+ public Class<?> getVariableType(String name) {
+ Class<?> result = variableTypes.get(name);
+ if (result != null) {
+ return result;
+ } else if (owner.getParent() != null) {
+ return owner.getParent().getEnvironment().getVariableType(name);
+ }
+ return null;
+ }
+
+ public Class<?> getVariableGenericType(String name) {
+ Class<?> result = variableGenericTypes.get(name);
+ if (result != null) {
+ return result;
+ } else if (owner.getParent() != null) {
+ return owner.getParent().getEnvironment().getVariableGenericType(name);
+ }
+ return null;
+ }
+
+ public <T> T getVariableValue(String name, Class<T> type) {
+ Object result = variableValues.get(name);
+ if (result != null) {
+ return type.cast(result);
+ } else if (owner.getParent() != null) {
+ return owner.getParent().getEnvironment().getVariableValue(name, type);
+ }
+ return null;
+ }
+
+ public Object getVariableValue(String name) {
+ return getVariableValue(name, Object.class);
+ }
+
+ public void setVariableValue(String var, Object value) {
+ if (ownsVariable(var)) {
+ Class<?> clazz = variableTypes.get(var);
+ if (clazz.equals(TextMarkerWordList.class) && value instanceof LiteralWordListExpression) {
+ LiteralWordListExpression lle = (LiteralWordListExpression) value;
+ String path = lle.getText();
+ TextMarkerWordList wordList = getWordList(path);
+ variableValues.put(var, wordList);
+ } else if (clazz.equals(TextMarkerWordList.class)) {
+ TextMarkerWordList list = getWordList((String) value);
+ variableValues.put(var, list);
+ } else if (clazz.equals(TextMarkerTable.class) && value instanceof LiteralWordTableExpression) {
+ LiteralWordTableExpression lte = (LiteralWordTableExpression) value;
+ String path = lte.getText();
+ TextMarkerTable table = getWordTable(path);
+ variableValues.put(var, table);
+ } else if (clazz.equals(TextMarkerTable.class)) {
+ TextMarkerTable table = getWordTable((String) value);
+ variableValues.put(var, table);
+ } else if (clazz.equals(List.class) && value instanceof ListExpression) {
+ List list = getList((ListExpression) value);
+ variableValues.put(var, list);
+ } else {
+ if (value == null) {
+ value = getInitialValue(clazz);
+ }
+ variableValues.put(var, value);
+ }
+ } else if (owner.getParent() != null) {
+ owner.getParent().getEnvironment().setVariableValue(var, value);
+ }
+ }
+
+ private List getList(ListExpression value) {
+ if (value instanceof SimpleBooleanListExpression) {
+ SimpleBooleanListExpression e = (SimpleBooleanListExpression) value;
+ return e.getList();
+ } else if (value instanceof SimpleNumberListExpression) {
+ SimpleNumberListExpression e = (SimpleNumberListExpression) value;
+ return e.getList();
+ } else if (value instanceof SimpleStringListExpression) {
+ SimpleStringListExpression e = (SimpleStringListExpression) value;
+ return e.getList();
+ } else if (value instanceof SimpleTypeListExpression) {
+ SimpleTypeListExpression e = (SimpleTypeListExpression) value;
+ return e.getList();
+ }
+ return null;
+ }
+
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerEnvironment.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerEnvironment.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerModule.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerModule.java?rev=1152824&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerModule.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerModule.java Mon Aug 1 15:24:44 2011
@@ -0,0 +1,107 @@
+package org.apache.uima.tm.textmarker.kernel;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.uima.analysis_engine.AnalysisEngine;
+import org.apache.uima.tm.textmarker.kernel.visitor.InferenceCrowd;
+
+
+public class TextMarkerModule extends TextMarkerElement {
+
+ private final TextMarkerBlock rootBlock;
+
+ private Map<String, TextMarkerModule> scripts;
+
+ private Map<String, AnalysisEngine> engines;
+
+ private Map<String, TextMarkerBlock> blocks;
+
+ public TextMarkerModule(TextMarkerBlock rootBlock) {
+ super();
+ this.rootBlock = rootBlock;
+ scripts = new HashMap<String, TextMarkerModule>();
+ engines = new HashMap<String, AnalysisEngine>();
+ blocks = new HashMap<String, TextMarkerBlock>();
+ }
+
+ public ScriptApply apply(TextMarkerStream stream, InferenceCrowd crowd) {
+ crowd.beginVisit(this, null);
+ ScriptApply result = rootBlock.apply(stream, crowd);
+ crowd.endVisit(this, result);
+ return result;
+ }
+
+ public TextMarkerBlock getBlock(String id) {
+ if (id == null || id.equals(rootBlock.getId())) {
+ return rootBlock;
+ }
+ return blocks.get(id);
+ }
+
+ public TextMarkerModule getScript(String name) {
+ if (name.equals(rootBlock.getId())) {
+ return this;
+ }
+ TextMarkerModule result = scripts.get(name);
+ if (result == null) {
+ for (String each : scripts.keySet()) {
+ String[] split = each.split("\\.");
+ String last = split[split.length - 1];
+ if (last.equals(name)) {
+ return scripts.get(each);
+ }
+ }
+ }
+ return result;
+ }
+
+ public void addScript(String name, TextMarkerModule script) {
+ scripts.put(name, script);
+ }
+
+ public void addBlock(String id, TextMarkerBlock block) {
+ blocks.put(id, block);
+ }
+
+ public void setScriptDependencies(Map<String, TextMarkerModule> additionalScripts) {
+ for (String eachTarget : scripts.keySet()) {
+ TextMarkerModule textMarkerModule = additionalScripts.get(eachTarget);
+ addScript(eachTarget, textMarkerModule);
+ }
+ }
+
+ public AnalysisEngine getEngine(String name) {
+ AnalysisEngine result = engines.get(name);
+ if (result == null) {
+ for (String each : engines.keySet()) {
+ String[] split = each.split("\\.");
+ String last = split[split.length - 1];
+ if (last.equals(name)) {
+ return engines.get(each);
+ }
+ }
+ }
+ return result;
+ }
+
+ public void addEngine(String name, AnalysisEngine engine) {
+ engines.put(name, engine);
+ }
+
+ public void setEngineDependencies(Map<String, AnalysisEngine> additionalEngines) {
+ for (String eachTarget : engines.keySet()) {
+ AnalysisEngine engine = additionalEngines.get(eachTarget);
+ addEngine(eachTarget, engine);
+ }
+ }
+
+ public Map<String, TextMarkerModule> getScripts() {
+ return scripts;
+ }
+
+ public Map<String, AnalysisEngine> getEngines() {
+ return engines;
+ }
+
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerModule.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerModule.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerScriptBlock.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerScriptBlock.java?rev=1152824&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerScriptBlock.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerScriptBlock.java Mon Aug 1 15:24:44 2011
@@ -0,0 +1,55 @@
+package org.apache.uima.tm.textmarker.kernel;
+
+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.tm.textmarker.kernel.rule.RuleApply;
+import org.apache.uima.tm.textmarker.kernel.rule.RuleMatch;
+import org.apache.uima.tm.textmarker.kernel.rule.TextMarkerRule;
+import org.apache.uima.tm.textmarker.kernel.visitor.InferenceCrowd;
+
+
+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);
+ }
+
+ @Override
+ public ScriptApply apply(TextMarkerStream stream, InferenceCrowd crowd) {
+ BlockApply result = new BlockApply(this);
+ crowd.beginVisit(this, result);
+ RuleApply apply = rule.apply(stream, crowd, true);
+ for (RuleMatch eachMatch : apply.getList()) {
+ if (eachMatch.matched()) {
+ AnnotationFS each = eachMatch.getMatchedAnnotation(stream, null);
+ if (each == null) {
+ continue;
+ }
+ Type type = rule.getElements().get(0).getMatcher().getType(getParent(), stream);
+ TextMarkerStream window = stream.getWindowStream(each, type);
+ for (TextMarkerStatement element : getElements()) {
+ if (element != null) {
+ element.apply(window, crowd);
+ }
+ }
+ }
+ }
+ crowd.endVisit(this, result);
+ return result;
+ }
+
+ @Override
+ public String toString() {
+ String ruleString = rule == null ? "Document" : rule.toString();
+ return "BLOCK(" + id + ") " + ruleString + " containing " + elements.size() + " Elements";
+ }
+
+ public void setMatchRule(TextMarkerRule rule) {
+ this.rule = rule;
+ }
+
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerScriptBlock.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerScriptBlock.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerScriptFactory.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerScriptFactory.java?rev=1152824&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerScriptFactory.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerScriptFactory.java Mon Aug 1 15:24:44 2011
@@ -0,0 +1,180 @@
+package org.apache.uima.tm.textmarker.kernel;
+
+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.tm.textmarker.action.AbstractTextMarkerAction;
+import org.apache.uima.tm.textmarker.condition.AbstractTextMarkerCondition;
+import org.apache.uima.tm.textmarker.kernel.expression.number.NumberExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.string.StringExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.type.SimpleTypeExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.type.TypeExpression;
+import org.apache.uima.tm.textmarker.kernel.rule.ComposedRuleElement;
+import org.apache.uima.tm.textmarker.kernel.rule.RuleElement;
+import org.apache.uima.tm.textmarker.kernel.rule.TextMarkerLiteralMatcher;
+import org.apache.uima.tm.textmarker.kernel.rule.TextMarkerRule;
+import org.apache.uima.tm.textmarker.kernel.rule.TextMarkerRuleElement;
+import org.apache.uima.tm.textmarker.kernel.rule.TextMarkerTypeMatcher;
+import org.apache.uima.tm.textmarker.kernel.rule.quantifier.MinMaxGreedy;
+import org.apache.uima.tm.textmarker.kernel.rule.quantifier.MinMaxReluctant;
+import org.apache.uima.tm.textmarker.kernel.rule.quantifier.NormalQuantifier;
+import org.apache.uima.tm.textmarker.kernel.rule.quantifier.PlusGreedy;
+import org.apache.uima.tm.textmarker.kernel.rule.quantifier.PlusReluctant;
+import org.apache.uima.tm.textmarker.kernel.rule.quantifier.QuestionGreedy;
+import org.apache.uima.tm.textmarker.kernel.rule.quantifier.QuestionReluctant;
+import org.apache.uima.tm.textmarker.kernel.rule.quantifier.RuleElementQuantifier;
+import org.apache.uima.tm.textmarker.kernel.rule.quantifier.StarGreedy;
+import org.apache.uima.tm.textmarker.kernel.rule.quantifier.StarReluctant;
+
+
+public class TextMarkerScriptFactory {
+
+ private int idCounter = 0;
+
+ public TextMarkerScriptFactory() {
+ super();
+ }
+
+ public TextMarkerScriptBlock createScriptBlock(Token id, TextMarkerRuleElement ruleElement,
+ List<TextMarkerStatement> body, TextMarkerBlock parent, CAS cas) {
+ 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);
+ }
+
+ public TextMarkerScriptBlock createScriptBlock(String text, TextMarkerRuleElement ruleElement,
+ List<TextMarkerStatement> body, TextMarkerBlock parent, String defaultNamespace, CAS cas) {
+ TextMarkerRule rule = null;
+ if (ruleElement != null) {
+ rule = createRule(ruleElement, parent);
+ }
+ List<TextMarkerStatement> elements = new ArrayList<TextMarkerStatement>();
+ if (body != null) {
+ for (TextMarkerStatement each : body) {
+ if (each != null) {
+ elements.add(each);
+ }
+ }
+ }
+ return new TextMarkerScriptBlock(text, rule, elements, parent, defaultNamespace, cas);
+ }
+
+ public TextMarkerScriptBlock createRootScriptBlock(String module, String pack, CAS cas,
+ 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();
+ }
+ result.setMatchRule(createRule(getDocumentRuleElement(result, cas), result));
+ return result;
+ }
+
+ private List<RuleElement> getDocumentRuleElement(TextMarkerBlock parent, CAS cas) {
+ List<RuleElement> result = new ArrayList<RuleElement>();
+
+ result.add(createRuleElement(new SimpleTypeExpression(cas.getDocumentAnnotation().getType()),
+ null, null, null, parent));
+ return result;
+ }
+
+ public TextMarkerRule createRule(RuleElement element, TextMarkerBlock parent) {
+ List<RuleElement> elements = new ArrayList<RuleElement>();
+ elements.add(element);
+ return new TextMarkerRule(elements, parent, idCounter++);
+ }
+
+ public TextMarkerRule createRule(List<RuleElement> elements, TextMarkerBlock parent) {
+ return new TextMarkerRule(elements, parent, idCounter++);
+ }
+
+ public TextMarkerRuleElement createRuleElement(TypeExpression typeExpression,
+ RuleElementQuantifier quantifier, List<AbstractTextMarkerCondition> conditions,
+ List<AbstractTextMarkerAction> actions, TextMarkerBlock parent) {
+ if (quantifier == null) {
+ quantifier = new NormalQuantifier();
+ }
+ TextMarkerTypeMatcher matcher = new TextMarkerTypeMatcher(typeExpression);
+ return new TextMarkerRuleElement(matcher, quantifier, conditions, actions, parent);
+ }
+
+ public TextMarkerRuleElement createRuleElement(StringExpression stringExpression,
+ RuleElementQuantifier quantifier, List<AbstractTextMarkerCondition> conditions,
+ List<AbstractTextMarkerAction> actions, TextMarkerBlock parent) {
+ if (quantifier == null) {
+ quantifier = new NormalQuantifier();
+ }
+ TextMarkerLiteralMatcher matcher = new TextMarkerLiteralMatcher(stringExpression);
+ return new TextMarkerRuleElement(matcher, quantifier, conditions, actions, parent);
+ }
+
+ public ComposedRuleElement createComposedRuleElement(List<RuleElement> res,
+ RuleElementQuantifier quantifier, TextMarkerBlock parent) {
+ if (quantifier == null) {
+ quantifier = new NormalQuantifier();
+ }
+ return new ComposedRuleElement(res, quantifier, parent);
+ }
+
+ public static RuleElementQuantifier createStarGreedyQuantifier() {
+ return new StarGreedy();
+ }
+
+ public static RuleElementQuantifier createPlusReluctantQuantifier() {
+ return new PlusReluctant();
+ }
+
+ public static RuleElementQuantifier createStarReluctantQuantifier() {
+ return new StarReluctant();
+ }
+
+ public static RuleElementQuantifier createMinMaxGreedyQuantifier(NumberExpression min,
+ NumberExpression max, Token comma) {
+ return new MinMaxGreedy(min, max, comma != null);
+ }
+
+ public static RuleElementQuantifier createMinMaxReluctantQuantifier(NumberExpression min,
+ NumberExpression max, Token comma) {
+ return new MinMaxReluctant(min, max, comma != null);
+ }
+
+ public static RuleElementQuantifier createPlusGreedyQuantifier() {
+ return new PlusGreedy();
+ }
+
+ public static RuleElementQuantifier createQuestionReluctantQuantifier() {
+ return new QuestionReluctant();
+ }
+
+ public static RuleElementQuantifier createQuestionGreedyQuantifier() {
+ return new QuestionGreedy();
+ }
+
+ public TextMarkerBlock createAutomataBlock(Token id, TextMarkerRuleElement re,
+ List<TextMarkerStatement> body, TextMarkerBlock env, CAS cas) {
+ return null;
+ }
+
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerScriptFactory.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerScriptFactory.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerStatement.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerStatement.java?rev=1152824&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerStatement.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerStatement.java Mon Aug 1 15:24:44 2011
@@ -0,0 +1,22 @@
+package org.apache.uima.tm.textmarker.kernel;
+
+import org.apache.uima.tm.textmarker.kernel.visitor.InferenceCrowd;
+
+public abstract class TextMarkerStatement extends TextMarkerElement {
+
+ private final TextMarkerBlock parent;
+
+ public TextMarkerStatement(TextMarkerBlock parent) {
+ super();
+ this.parent = parent;
+ }
+
+ public TextMarkerBlock getParent() {
+ return parent;
+ }
+
+ public abstract ScriptApply apply(TextMarkerStream stream, InferenceCrowd crowd);
+
+ public abstract TextMarkerEnvironment getEnvironment();
+
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerStatement.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerStatement.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerStream.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerStream.java?rev=1152824&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerStream.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerStream.java Mon Aug 1 15:24:44 2011
@@ -0,0 +1,551 @@
+package org.apache.uima.tm.textmarker.kernel;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.NoSuchElementException;
+
+import org.apache.uima.cas.CAS;
+import org.apache.uima.cas.CASException;
+import org.apache.uima.cas.ConstraintFactory;
+import org.apache.uima.cas.FSIterator;
+import org.apache.uima.cas.FSMatchConstraint;
+import org.apache.uima.cas.FeatureStructure;
+import org.apache.uima.cas.Type;
+import org.apache.uima.cas.TypeSystem;
+import org.apache.uima.cas.impl.FSIteratorImplBase;
+import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.jcas.JCas;
+import org.apache.uima.jcas.tcas.Annotation;
+import org.apache.uima.jcas.tcas.DocumentAnnotation;
+import org.apache.uima.tm.textmarker.kernel.rule.RuleElementMatch;
+import org.apache.uima.tm.textmarker.kernel.type.TextMarkerAnnotation;
+import org.apache.uima.tm.textmarker.kernel.type.TextMarkerBasic;
+import org.apache.uima.tm.textmarker.kernel.type.TextMarkerFrame;
+
+
+public class TextMarkerStream extends FSIteratorImplBase<AnnotationFS> {
+
+ private final CAS cas;
+
+ private final FSIterator<AnnotationFS> basicIt;
+
+ private FSIterator<AnnotationFS> currentIt;
+
+ private AnnotationFS documentAnnotation;
+
+ private TextMarkerBasic firstBasic;
+
+ private Type documentAnnotationType;
+
+ private Type basicType;
+
+ private final List<TextMarkerBasic> basics;
+
+ private FilterManager filter;
+
+ private Map<Integer, TextMarkerBasic> pointerMap = new HashMap<Integer, TextMarkerBasic>();
+
+ protected TextMarkerStream(CAS cas, FSIterator<AnnotationFS> basic,
+ FSIterator<AnnotationFS> current, Type basicType, FilterManager filter) {
+ super();
+ this.cas = cas;
+ this.filter = filter;
+ this.basicType = basicType;
+ AnnotationFS additionalWindow = filter.getWindowAnnotation();
+ if (additionalWindow != null) {
+ this.basicIt = cas.getAnnotationIndex(basicType).subiterator(additionalWindow);
+ } else {
+ this.basicIt = basic;
+ }
+ if (current == null) {
+ // TODO use here a subiterator??
+ currentIt = filter.createFilteredIterator(cas, basicIt, basicType);
+ // currentIt = cas.createFilteredIterator(basic, filter.getDefaultConstraint());
+ } else {
+ currentIt = current;
+ }
+ // really an if? sub it of basic should fix this
+ if (additionalWindow == null) {
+ documentAnnotation = (DocumentAnnotation) getJCas().getDocumentAnnotationFs();
+ documentAnnotationType = getCas().getDocumentAnnotation().getType();
+ basicIt.moveToFirst();
+ if (basicIt.isValid()) {
+ firstBasic = (TextMarkerBasic) basicIt.get();
+ }
+ } else {
+ documentAnnotation = additionalWindow;
+ documentAnnotationType = filter.getWindowType();
+ firstBasic = getFirstBasicInWindow(additionalWindow, basic);
+ }
+ // really faster???
+ basics = new ArrayList<TextMarkerBasic>();
+ FSIterator<AnnotationFS> iterator = cas.getAnnotationIndex(basicType).subiterator(
+ documentAnnotation);
+ while (iterator.isValid()) {
+ basics.add((TextMarkerBasic) iterator.get());
+ iterator.moveToNext();
+ }
+ }
+
+ public TextMarkerStream(CAS cas, FSIterator<AnnotationFS> basic, Type basicType,
+ FilterManager filter) {
+ this(cas, basic, null, basicType, filter);
+ }
+
+ public void addAnnotation(TextMarkerBasic anchor, AnnotationFS annotation) {
+ if (anchor == null)
+ return;
+ Type type = annotation.getType();
+ String name = type.getName();
+ TypeSystem typeSystem = cas.getTypeSystem();
+ Type parent = type;
+ while (parent != null) {
+ anchor.setAnnotation(parent.getName(), annotation, parent == type);
+ parent = typeSystem.getParent(parent);
+ }
+ List<TextMarkerBasic> basicAnnotationsInWindow = getAllBasicsInWindow(annotation);
+ for (TextMarkerBasic basic : basicAnnotationsInWindow) {
+ basic.addPartOf(name);
+ }
+ }
+
+ public void removeAnnotation(TextMarkerBasic anchor, Type type) {
+ TypeSystem typeSystem = cas.getTypeSystem();
+ String name = type.getName();
+ AnnotationFS expandAnchor = expandAnchor(anchor, type, true);
+ if (expandAnchor == null) {
+ // there is no annotation to remove
+ return;
+ }
+ List<TextMarkerBasic> basicAnnotationsInWindow = getAllBasicsInWindow(expandAnchor);
+ for (TextMarkerBasic basic : basicAnnotationsInWindow) {
+ basic.removePartOf(name);
+ }
+ Type parent = type;
+ while (parent != null) {
+ anchor.removeAnnotation(parent.getName(), parent == type);
+ parent = typeSystem.getParent(parent);
+ }
+ if (!(expandAnchor instanceof TextMarkerBasic)) {
+ cas.removeFsFromIndexes(expandAnchor);
+ }
+ }
+
+ public FSIterator<AnnotationFS> getFilteredBasicIterator(FSMatchConstraint constraint) {
+ ConstraintFactory cf = cas.getConstraintFactory();
+ FSMatchConstraint matchConstraint = cf.and(constraint, filter.getDefaultConstraint());
+ return cas.createFilteredIterator(basicIt, matchConstraint);
+ }
+
+ public TextMarkerStream getWindowStream(AnnotationFS windowAnnotation, Type windowType) {
+ if (windowAnnotation.getBegin() == documentAnnotation.getBegin()
+ && windowAnnotation.getEnd() == documentAnnotation.getEnd()) {
+ return this;
+ }
+
+ FilterManager filterManager = new FilterManager(filter.getDefaultFilterTypes(), filter
+ .getDefaultRetainTags(), filter.getCurrentFilterTypes(),
+ filter.getCurrentRetainTypes(), filter.getCurrentFilterTags(), filter
+ .getCurrentRetainTags(), windowAnnotation, windowType, cas);
+ // TODO can I use the old currentIT here? check this please!
+
+ TextMarkerStream stream = new TextMarkerStream(cas, basicIt, basicType, filterManager);
+ return stream;
+ }
+
+ public FSIterator<AnnotationFS> copy() {
+ return new TextMarkerStream(cas, basicIt.copy(), currentIt.copy(), basicType, filter);
+ }
+
+ public AnnotationFS get() throws NoSuchElementException {
+ return currentIt.get();
+ }
+
+ public boolean isValid() {
+ return currentIt.isValid();
+ }
+
+ public void moveTo(FeatureStructure fs) {
+ try {
+ currentIt.moveTo(fs);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ public void moveToFirst() {
+ currentIt.moveToFirst();
+ }
+
+ public void moveToLast() {
+ currentIt.moveToLast();
+ }
+
+ public void moveToNext() {
+ currentIt.moveToNext();
+ }
+
+ public void moveToPrevious() {
+ currentIt.moveToPrevious();
+ }
+
+ public List<AnnotationFS> getOverappingAnnotations(AnnotationFS window, Type type) {
+ List<AnnotationFS> result = new ArrayList<AnnotationFS>();
+ AnnotationFS newWindow = cas.createAnnotation(type, window.getBegin(), window.getEnd() - 1);
+ FSIterator<AnnotationFS> iterator = cas.getAnnotationIndex(type).iterator(newWindow);
+ if (!iterator.isValid()) {
+ iterator.moveToLast();
+ }
+ while (iterator.isValid()) {
+ FeatureStructure fs = iterator.get();
+ if (fs instanceof AnnotationFS) {
+ AnnotationFS a = (AnnotationFS) fs;
+ if (a.getEnd() >= window.getEnd() && a.getBegin() <= window.getBegin()) {
+ result.add(a);
+ }
+ }
+ iterator.moveToPrevious();
+ }
+ return result;
+ }
+
+ public List<Annotation> getAnnotationsFollowing(Annotation annotation) {
+ List<Annotation> result = new ArrayList<Annotation>();
+ moveTo(annotation);
+ while (currentIt.isValid()) {
+ currentIt.moveToNext();
+ if (currentIt.isValid()) {
+ Annotation nextAnnotation = (Annotation) currentIt.get();
+ if (nextAnnotation.getBegin() == annotation.getEnd()) {
+ result.add(nextAnnotation);
+ } else if (nextAnnotation.getBegin() >= annotation.getEnd()) {
+ break;
+ }
+ }
+ }
+ return result;
+ }
+
+ public CAS getCas() {
+ return cas;
+ }
+
+ public JCas getJCas() {
+ try {
+ return cas.getJCas();
+ } catch (CASException e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ public List<AnnotationFS> getAllofType(Type type) {
+ List<AnnotationFS> result = new ArrayList<AnnotationFS>();
+ FSIterator<AnnotationFS> iterator = cas.getAnnotationIndex(type).iterator();
+ while (iterator.isValid()) {
+ FeatureStructure featureStructure = iterator.get();
+ result.add((AnnotationFS) featureStructure);
+ iterator.moveToNext();
+ }
+ return result;
+ }
+
+ public List<AnnotationFS> getAnnotationsInWindow2(AnnotationFS windowAnnotation, Type type) {
+ List<AnnotationFS> result = new ArrayList<AnnotationFS>();
+ if (windowAnnotation instanceof TextMarkerBasic) {
+ TextMarkerBasic basic = (TextMarkerBasic) windowAnnotation;
+ AnnotationFS a = basic.getType(type.getName());
+ if (a != null) {
+ result.add(a);
+ }
+ return result;
+ }
+ windowAnnotation = cas.createAnnotation(type, windowAnnotation.getBegin(), windowAnnotation
+ .getEnd() + 1);
+ FSIterator<AnnotationFS> completeIt = getCas().getAnnotationIndex(type).iterator();
+ if (getDocumentAnnotation().getEnd() < windowAnnotation.getEnd()) {
+ completeIt.moveToLast();
+ } else {
+ completeIt.moveTo(windowAnnotation);
+ }
+ while (completeIt.isValid()
+ && ((Annotation) completeIt.get()).getBegin() >= windowAnnotation.getBegin()) {
+ completeIt.moveToPrevious();
+ }
+
+ if (completeIt.isValid()) {
+ completeIt.moveToNext();
+ } else {
+ completeIt.moveToFirst();
+ }
+
+ while (completeIt.isValid()
+ && ((Annotation) completeIt.get()).getBegin() < windowAnnotation.getBegin()) {
+ completeIt.moveToNext();
+ }
+
+ while (completeIt.isValid()
+ && ((Annotation) completeIt.get()).getBegin() >= windowAnnotation.getBegin()) {
+ Annotation annotation = (Annotation) completeIt.get();
+ if (getCas().getTypeSystem().subsumes(type, annotation.getType())
+ && annotation.getEnd() <= windowAnnotation.getEnd()) {
+ result.add(annotation);
+ }
+ completeIt.moveToNext();
+ }
+ return result;
+ }
+
+ public List<AnnotationFS> getAnnotationsInWindow(AnnotationFS windowAnnotation, Type type) {
+ List<AnnotationFS> result = new ArrayList<AnnotationFS>();
+ List<AnnotationFS> inWindow = getAnnotationsInWindow2(windowAnnotation, type);
+ result = inWindow;
+ return result;
+ }
+
+ public List<TextMarkerBasic> getAllBasicsInWindow(AnnotationFS windowAnnotation) {
+ List<TextMarkerBasic> result = new ArrayList<TextMarkerBasic>();
+ if (windowAnnotation instanceof TextMarkerBasic) {
+ result.add((TextMarkerBasic) windowAnnotation);
+ return result;
+ } else if (windowAnnotation.getBegin() <= documentAnnotation.getBegin()
+ && windowAnnotation.getEnd() >= documentAnnotation.getEnd()) {
+ return basics;
+ }
+ TextMarkerFrame frame = new TextMarkerFrame(getJCas(), windowAnnotation.getBegin(),
+ windowAnnotation.getEnd());
+ FSIterator<AnnotationFS> iterator = cas.getAnnotationIndex(basicType).subiterator(frame);
+ while (iterator.isValid()) {
+ result.add((TextMarkerBasic) iterator.get());
+ iterator.moveToNext();
+ }
+ return result;
+ }
+
+ public List<TextMarkerBasic> getBasicsInWindow(AnnotationFS windowAnnotation) {
+ List<TextMarkerBasic> result = new ArrayList<TextMarkerBasic>();
+ if (windowAnnotation instanceof TextMarkerBasic) {
+ result.add((TextMarkerBasic) windowAnnotation);
+ return result;
+ }
+ FSMatchConstraint defaultConstraint = filter.getDefaultConstraint();
+ FSIterator<AnnotationFS> iterator = cas.createFilteredIterator(cas
+ .getAnnotationIndex(basicType).subiterator(windowAnnotation), defaultConstraint);
+
+ while (iterator.isValid()) {
+ result.add((TextMarkerBasic) iterator.get());
+ iterator.moveToNext();
+ }
+ return result;
+ }
+
+ public TextMarkerBasic getFirstBasicInWindow(AnnotationFS windowAnnotation) {
+ return getFirstBasicInWindow(windowAnnotation, currentIt);
+ }
+
+ public TextMarkerBasic getFirstBasicInWindow(AnnotationFS windowAnnotation,
+ FSIterator<AnnotationFS> it) {
+ if (windowAnnotation instanceof TextMarkerBasic) {
+ return (TextMarkerBasic) windowAnnotation;
+ }
+ it.moveTo(windowAnnotation);
+ if (it.isValid()) {
+ return (TextMarkerBasic) it.get();
+ }
+ return null;
+ }
+
+ public List<TextMarkerBasic> getAnnotationsOverlappingWindow(TextMarkerBasic basic, Type type) {
+ AnnotationFS expand = expandPartOf(basic, type);
+ if (expand != null) {
+ return getBasicsInWindow(expand);
+ } else {
+ return new ArrayList<TextMarkerBasic>();
+ }
+ }
+
+ private AnnotationFS expandPartOf(TextMarkerBasic currentBasic, Type type) {
+ if (type == null) {
+ return currentBasic;
+ }
+ AnnotationFS result = currentBasic.getType(type.getName());
+ if (result != null) {
+ return result;
+ }
+ if (currentBasic.isPartOf(type.getName())) {
+ moveTo(currentBasic);
+ while (isValid()) {
+ TextMarkerBasic each = (TextMarkerBasic) get();
+ if (each.isAnchorOf(type.getName())) {
+ AnnotationFS annotation = each.getType(type.getName());
+ if (annotation.getBegin() <= currentBasic.getBegin()
+ && annotation.getEnd() >= currentBasic.getEnd()) {
+ return annotation;
+ }
+ }
+ moveToPrevious();
+ }
+ }
+ return null;
+ }
+
+ public AnnotationFS expandAnchor(TextMarkerBasic currentBasic, Type type) {
+ return expandAnchor(currentBasic, type, false);
+ }
+
+ public AnnotationFS expandAnchor(TextMarkerBasic currentBasic, Type type, boolean ret) {
+ if (type == null || currentBasic == null) {
+ return currentBasic;
+ }
+ AnnotationFS result = currentBasic.getType(type.getName());
+ if (result != null) {
+ return result;
+ }
+ if (type.getName().equals(getDocumentAnnotationType().getName())) {
+ return documentAnnotation;
+ }
+ if (!ret) {
+ return currentBasic;
+ } else {
+ return null;
+ }
+ }
+
+ public FSIterator<AnnotationFS> getUnfilteredBasicIterator() {
+ return basicIt;
+ }
+
+ public AnnotationFS getDocumentAnnotation() {
+ return documentAnnotation;
+ }
+
+ public TextMarkerAnnotation getCorrectTMA(List<AnnotationFS> annotationsInWindow,
+ TextMarkerAnnotation heuristicAnnotation) {
+ for (AnnotationFS annotation : annotationsInWindow) {
+ if (annotation instanceof TextMarkerAnnotation) {
+ TextMarkerAnnotation tma = (TextMarkerAnnotation) annotation;
+ if (tma.getBegin() == heuristicAnnotation.getBegin()
+ && tma.getEnd() == heuristicAnnotation.getEnd()
+ && tma.getAnnotation().getType().equals(
+ heuristicAnnotation.getAnnotation().getType())) {
+ return tma;
+ }
+ }
+ }
+ return null;
+ }
+
+ public void retainTypes(List<Type> list) {
+ filter.retainTypes(list);
+ FSMatchConstraint defaultConstraint = filter.getDefaultConstraint();
+ currentIt = cas.createFilteredIterator(basicIt, defaultConstraint);
+ }
+
+ public void filterTypes(List<Type> list) {
+ filter.filterTypes(list);
+ FSMatchConstraint defaultConstraint = filter.getDefaultConstraint();
+ currentIt = cas.createFilteredIterator(basicIt, defaultConstraint);
+ }
+
+ public void retainTags(List<String> list) {
+ filter.retainTags(list);
+ FSMatchConstraint defaultConstraint = filter.getDefaultConstraint();
+ currentIt = cas.createFilteredIterator(basicIt, defaultConstraint);
+ }
+
+ public void filterTags(List<String> list) {
+ filter.filterTags(list);
+ FSMatchConstraint defaultConstraint = filter.getDefaultConstraint();
+ currentIt = cas.createFilteredIterator(basicIt, defaultConstraint);
+ }
+
+ public FilterManager getFilter() {
+ return filter;
+ }
+
+ public TextMarkerBasic getFirstBasicOfAll() {
+ return firstBasic;
+ }
+
+ public Type getDocumentAnnotationType() {
+ return documentAnnotationType;
+ }
+
+ public TextMarkerBasic nextAnnotation(RuleElementMatch match) {
+ if (!match.matched()) {
+ return null;
+ }
+ List<AnnotationFS> textsMatched = match.getTextsMatched();
+ AnnotationFS lastAnnotation = null;
+ if (textsMatched != null && !textsMatched.isEmpty()) {
+ lastAnnotation = textsMatched.get(textsMatched.size() - 1);
+ }
+ if (lastAnnotation == null || lastAnnotation.getEnd() >= getDocumentAnnotation().getEnd()) {
+ return null;
+ }
+ // if (getNextBasic(lastAnnotation) == null) {
+ // System.out.println();
+ // }
+ // if (getNextBasic(lastAnnotation) != null
+ // && getNextBasic(lastAnnotation).getBegin() == lastAnnotation.getBegin()) {
+ // System.out.println();
+ // }
+ TextMarkerBasic nextBasic = getNextBasic(lastAnnotation);
+ // TextMarkerBasic nextBasic2 = getNextBasic2(lastAnnotation);
+ // if (nextBasic != nextBasic2) {
+ // String string = nextBasic == null ? "null" : nextBasic.getCoveredText();
+ // String string2 = nextBasic == null ? "null" : (nextBasic.getBegin() + "");
+ // System.out.println("nextBasic.getBegin() != nextBasic2.getBegin() " + string + " "
+ // + nextBasic2.getCoveredText());
+ // System.out.println(lastAnnotation.getBegin() + "=" + string2 + "=" + nextBasic2.getBegin());
+ // }
+ return nextBasic;
+ }
+
+ public TextMarkerBasic getNextBasic(AnnotationFS previous) {
+ TextMarkerBasic pointer = pointerMap.get(previous.getEnd());
+ if (pointer == null) {
+ // FIXME: hotfix for ML stuff
+ // pointer = new TextMarkerFrame(getJCas(), previous.getEnd()-1, previous.getEnd());
+ pointer = (TextMarkerBasic) cas.createAnnotation(basicType, previous.getEnd() - 1, previous
+ .getEnd());
+ pointerMap.put(previous.getEnd(), pointer);
+ }
+ currentIt.moveTo(pointer);
+ if (currentIt.isValid()) {
+ TextMarkerBasic basic = (TextMarkerBasic) currentIt.get();
+ if (basic.getBegin() == previous.getBegin()) {
+ // if (basic.getBegin() >= previous.getBegin() || basic.getEnd() <= previous.getEnd()) {
+ currentIt.moveToNext();
+ if (currentIt.isValid()) {
+ return (TextMarkerBasic) currentIt.get();
+ } else {
+ return null;
+ }
+ } else {
+ return basic;
+ }
+ }
+ return null;
+ }
+
+ public TextMarkerBasic getNextBasic2(AnnotationFS previous) {
+ AnnotationFS pointer = cas
+ .createAnnotation(basicType, previous.getEnd() - 1, previous.getEnd());
+ currentIt.moveTo(pointer);
+ if (currentIt.isValid()) {
+ TextMarkerBasic basic = (TextMarkerBasic) currentIt.get();
+ return basic;
+ }
+ return null;
+ }
+
+ public TextMarkerStream getCompleteStream() {
+ FilterManager defaultFilter = new FilterManager(filter.getDefaultFilterTypes(), filter
+ .getDefaultRetainTags(), getCas());
+ return new TextMarkerStream(getCas(), basicIt, basicType, defaultFilter);
+ }
+
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerStream.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerStream.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/constraint/BasicTypeConstraint.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/constraint/BasicTypeConstraint.java?rev=1152824&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/constraint/BasicTypeConstraint.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/constraint/BasicTypeConstraint.java Mon Aug 1 15:24:44 2011
@@ -0,0 +1,82 @@
+package org.apache.uima.tm.textmarker.kernel.constraint;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.uima.cas.FSTypeConstraint;
+import org.apache.uima.cas.FeatureStructure;
+import org.apache.uima.cas.Type;
+import org.apache.uima.tm.textmarker.kernel.type.TextMarkerBasic;
+
+
+public class BasicTypeConstraint implements FSTypeConstraint {
+ private static final long serialVersionUID = 1115953538613617791L;
+
+ private final FSTypeConstraint constraint;
+
+ private final List<String> types;
+
+ private final List<String> partOfs;
+
+ public BasicTypeConstraint(FSTypeConstraint constraint, List<String> types, List<String> partOfs) {
+ super();
+ this.constraint = constraint;
+ this.types = types;
+ this.partOfs = partOfs;
+ }
+
+ public BasicTypeConstraint(FSTypeConstraint constraint, String type, String partOf) {
+ super();
+ this.constraint = constraint;
+ if (type != null) {
+ this.types = new ArrayList<String>();
+ this.types.add(type);
+ } else {
+ this.types = null;
+ }
+ if (partOf != null) {
+ this.partOfs = new ArrayList<String>();
+ this.partOfs.add(partOf);
+ } else {
+ this.partOfs = null;
+ }
+ }
+
+ public void add(Type type) {
+ constraint.add(type);
+ }
+
+ public boolean match(FeatureStructure fs) {
+ boolean matched = constraint.match(fs);
+ if (matched)
+ return true;
+ boolean result = false;
+ if (fs instanceof TextMarkerBasic) {
+ TextMarkerBasic tmb = (TextMarkerBasic) fs;
+ if (types != null) {
+ for (String each : types) {
+ result |= tmb.isAnchorOf(each);
+ if (result)
+ break;
+ }
+ }
+ if (partOfs != null) {
+ for (String each : partOfs) {
+ result |= tmb.isAnchorOf(each);
+ if (result)
+ break;
+ }
+ }
+ }
+ return result;
+ }
+
+ @Override
+ public String toString() {
+ return "(BASIC " + constraint.toString() + " with " + types + " and " + partOfs + ")";
+ }
+
+ public void add(String type) {
+ constraint.add(type);
+ }
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/constraint/BasicTypeConstraint.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/constraint/BasicTypeConstraint.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/constraint/MarkupConstraint.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/constraint/MarkupConstraint.java?rev=1152824&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/constraint/MarkupConstraint.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/constraint/MarkupConstraint.java Mon Aug 1 15:24:44 2011
@@ -0,0 +1,66 @@
+package org.apache.uima.tm.textmarker.kernel.constraint;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.uima.cas.FSTypeConstraint;
+import org.apache.uima.cas.FeatureStructure;
+import org.apache.uima.cas.Type;
+import org.apache.uima.jcas.tcas.Annotation;
+
+public class MarkupConstraint implements FSTypeConstraint {
+ private static final long serialVersionUID = 1115953538613617791L;
+
+ private final static Pattern tagPattern = Pattern.compile("<(/)?([A-Za-z�������_0-9:!=\\-]+)([^>]*)>"); // <font
+
+ // color='red'>
+ // -->
+ // group(1)=font
+
+ private final FSTypeConstraint constraint;
+
+ private List<String> tags = new ArrayList<String>();
+
+ public MarkupConstraint(FSTypeConstraint constraint) {
+ super();
+ this.constraint = constraint;
+ }
+
+ public void addTag(String tag) {
+ tags.add(tag);
+ }
+
+ public void removeTag(String tag) {
+ tags.add(tag);
+ }
+
+ public boolean match(FeatureStructure fs) {
+ boolean result = constraint.match(fs);
+ if (fs instanceof Annotation) {
+ Annotation annotation = (Annotation) fs;
+ String text = annotation.getCoveredText();
+ Matcher m = tagPattern.matcher(text);
+ if (m.find()) {
+ String lowerCase = m.group(2).toLowerCase();
+ // String group = m.group(2);
+ result &= tags.contains(lowerCase);
+ }
+ }
+ return result;
+ }
+
+ public void add(Type type) {
+ constraint.add(type);
+ }
+
+ public void add(String type) {
+ constraint.add(type);
+ }
+
+ @Override
+ public String toString() {
+ return constraint.toString() + "with " + tags;
+ }
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/constraint/MarkupConstraint.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/constraint/MarkupConstraint.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/constraint/NotConstraint.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/constraint/NotConstraint.java?rev=1152824&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/constraint/NotConstraint.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/constraint/NotConstraint.java Mon Aug 1 15:24:44 2011
@@ -0,0 +1,24 @@
+package org.apache.uima.tm.textmarker.kernel.constraint;
+
+import org.apache.uima.cas.FSMatchConstraint;
+import org.apache.uima.cas.FeatureStructure;
+
+public class NotConstraint implements FSMatchConstraint {
+ private static final long serialVersionUID = 1115953538613617791L;
+
+ private final FSMatchConstraint constraint;
+
+ public NotConstraint(FSMatchConstraint constraint) {
+ super();
+ this.constraint = constraint;
+ }
+
+ public boolean match(FeatureStructure fs) {
+ return !constraint.match(fs);
+ }
+
+ @Override
+ public String toString() {
+ return "NOT " + constraint.toString();
+ }
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/constraint/NotConstraint.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/constraint/NotConstraint.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/constraint/NotMatchConstraint.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/constraint/NotMatchConstraint.java?rev=1152824&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/constraint/NotMatchConstraint.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/constraint/NotMatchConstraint.java Mon Aug 1 15:24:44 2011
@@ -0,0 +1,25 @@
+package org.apache.uima.tm.textmarker.kernel.constraint;
+
+import org.apache.uima.cas.FSMatchConstraint;
+import org.apache.uima.cas.FeatureStructure;
+
+public class NotMatchConstraint implements FSMatchConstraint {
+ private static final long serialVersionUID = 1115953538613617791L;
+
+ private final FSMatchConstraint constraint;
+
+ public NotMatchConstraint(FSMatchConstraint constraint) {
+ super();
+ this.constraint = constraint;
+ }
+
+ public boolean match(FeatureStructure fs) {
+ return !constraint.match(fs);
+ }
+
+ @Override
+ public String toString() {
+ return "NOT " + constraint.toString();
+ }
+
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/constraint/NotMatchConstraint.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/constraint/NotMatchConstraint.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/ExpressionFactory.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/ExpressionFactory.java?rev=1152824&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/ExpressionFactory.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/ExpressionFactory.java Mon Aug 1 15:24:44 2011
@@ -0,0 +1,209 @@
+package org.apache.uima.tm.textmarker.kernel.expression;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.antlr.runtime.Token;
+import org.apache.uima.cas.Type;
+import org.apache.uima.tm.textmarker.kernel.TextMarkerBlock;
+import org.apache.uima.tm.textmarker.kernel.expression.bool.BooleanExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.bool.BooleanNumberExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.bool.BooleanTypeExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.bool.ReferenceBooleanExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.bool.SimpleBooleanExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.bool.SimpleBooleanFunction;
+import org.apache.uima.tm.textmarker.kernel.expression.list.BooleanListExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.list.NumberListExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.list.ReferenceBooleanListExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.list.ReferenceNumberListExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.list.ReferenceStringListExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.list.ReferenceTypeListExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.list.SimpleBooleanListExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.list.SimpleNumberListExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.list.SimpleStringListExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.list.SimpleTypeListExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.list.StringListExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.list.TypeListExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.number.ComposedDoubleExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.number.NegativeNumberExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.number.NumberExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.number.ReferenceDoubleExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.number.SimpleNumberExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.resource.LiteralWordListExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.resource.LiteralWordTableExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.resource.ReferenceWordListExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.resource.ReferenceWordTableExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.resource.WordListExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.resource.WordTableExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.string.ComposedStringExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.string.ReferenceStringExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.string.SimpleStringExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.string.StringExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.type.ReferenceTypeExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.type.SimpleTypeExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.type.TypeExpression;
+
+
+public class ExpressionFactory {
+
+ private ExpressionFactory() {
+ }
+
+ public static NumberExpression createIntegerExpression(Token number, Token minus) {
+ Integer valueOf = Integer.valueOf(number.getText());
+ SimpleNumberExpression simpleNumberExpression = new SimpleNumberExpression(valueOf);
+ if (minus != null) {
+ return new NegativeNumberExpression(simpleNumberExpression);
+ } else {
+ return simpleNumberExpression;
+ }
+ }
+
+ public static NumberExpression createDoubleExpression(Token number, Token minus) {
+ Double valueOf = Double.valueOf(number.getText());
+ SimpleNumberExpression simpleNumberExpression = new SimpleNumberExpression(valueOf);
+ if (minus != null) {
+ return new NegativeNumberExpression(simpleNumberExpression);
+ } else {
+ return simpleNumberExpression;
+ }
+ }
+
+ public static NumberExpression createReferenceNumberExpression(Token var, Token minus) {
+ ReferenceDoubleExpression simpleNumberExpression = new ReferenceDoubleExpression(var.getText());
+ if (minus != null) {
+ return new NegativeNumberExpression(simpleNumberExpression);
+ } else {
+ return simpleNumberExpression;
+ }
+ }
+
+ public static NumberExpression createComposedNumberExpression(List<NumberExpression> expressions,
+ List<Token> opTokens) {
+ List<String> ops = new ArrayList<String>();
+ for (Token token : opTokens) {
+ ops.add(token.getText());
+ }
+ return new ComposedDoubleExpression(expressions, ops);
+ }
+
+ public static NumberExpression createComposedNumberExpression(NumberExpression expression,
+ Token opToken) {
+ List<String> ops = new ArrayList<String>();
+ List<NumberExpression> exprList = new ArrayList<NumberExpression>();
+ ops.add(opToken.getText());
+ exprList.add(expression);
+ return new ComposedDoubleExpression(exprList, ops);
+ }
+
+ public static StringExpression createSimpleStringExpression(Token token) {
+ String text = token.getText();
+ String substring = text.substring(1, text.length() - 1);
+ return new SimpleStringExpression(substring);
+ }
+
+ public static StringExpression createComposedStringExpression(List<StringExpression> expressions) {
+ return new ComposedStringExpression(expressions);
+ }
+
+ public static StringExpression createReferenceStringExpression(Token var) {
+ return new ReferenceStringExpression(var.getText());
+ }
+
+ public static BooleanExpression createBooleanNumberExpression(NumberExpression e1, Token op,
+ NumberExpression e2) {
+ return new BooleanNumberExpression(e1, op.getText(), e2);
+ }
+
+ public static BooleanExpression createSimpleBooleanExpression(Token v) {
+ return new SimpleBooleanExpression(Boolean.valueOf(v.getText()));
+ }
+
+ public static BooleanExpression createReferenceBooleanExpression(Token id) {
+ return new ReferenceBooleanExpression(id.getText());
+ }
+
+ 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);
+ }
+
+ public static TypeExpression createReferenceTypeExpression(Token varToken) {
+ String varString = varToken == null ? "" : varToken.getText();
+ return new ReferenceTypeExpression(varString);
+ }
+
+ public static TypeExpression createSimpleTypeExpression(String typeString, TextMarkerBlock parent) {
+ Type type = parent.getEnvironment().getType(typeString);
+ return new SimpleTypeExpression(type);
+ }
+
+ public static BooleanExpression createBooleanFunction(Token op, BooleanExpression e1,
+ BooleanExpression e2) {
+ return new SimpleBooleanFunction(op.getText(), e1, e2);
+ }
+
+ public static WordTableExpression createReferenceWordTableExpression(Token id) {
+ return new ReferenceWordTableExpression(id.getText());
+ }
+
+ public static WordListExpression createReferenceWordListExpression(Token id) {
+ return new ReferenceWordListExpression(id.getText());
+ }
+
+ public static WordListExpression createLiteralWordListExpression(Token path) {
+ return new LiteralWordListExpression(path.getText());
+ }
+
+ public static WordTableExpression createLiteralWordTableExpression(Token path) {
+ return new LiteralWordTableExpression(path.getText());
+ }
+
+ public static BooleanExpression createBooleanTypeExpression(TypeExpression e1, Token op,
+ TypeExpression e2) {
+ return new BooleanTypeExpression(e1, op.getText(), e2);
+ }
+
+ public static BooleanListExpression createReferenceBooleanListExpression(Token var) {
+ return new ReferenceBooleanListExpression(var.getText());
+ }
+
+ public static StringListExpression createReferenceStringListExpression(Token var) {
+ return new ReferenceStringListExpression(var.getText());
+ }
+
+ public static TypeListExpression createReferenceTypeListExpression(Token var) {
+ return new ReferenceTypeListExpression(var.getText());
+ }
+
+ public static NumberListExpression createReferenceDoubleListExpression(Token var) {
+ return new ReferenceNumberListExpression(var.getText());
+ }
+
+ public static NumberListExpression createReferenceIntListExpression(Token var) {
+ return new ReferenceNumberListExpression(var.getText());
+ }
+
+ public static BooleanListExpression createBooleanListExpression(List<BooleanExpression> list) {
+ return new SimpleBooleanListExpression(list);
+ }
+
+ public static NumberListExpression createNumberListExpression(List<NumberExpression> list) {
+ return new SimpleNumberListExpression(list);
+ }
+
+ public static TypeListExpression createTypeListExpression(List<TypeExpression> list) {
+ return new SimpleTypeListExpression(list);
+ }
+
+ public static StringListExpression createStringListExpression(List<StringExpression> list) {
+ return new SimpleStringListExpression(list);
+ }
+
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/ExpressionFactory.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/ExpressionFactory.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/TextMarkerExpression.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/TextMarkerExpression.java?rev=1152824&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/TextMarkerExpression.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/TextMarkerExpression.java Mon Aug 1 15:24:44 2011
@@ -0,0 +1,7 @@
+package org.apache.uima.tm.textmarker.kernel.expression;
+
+import org.apache.uima.tm.textmarker.kernel.TextMarkerElement;
+
+public class TextMarkerExpression extends TextMarkerElement {
+
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/TextMarkerExpression.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/TextMarkerExpression.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/bool/BooleanExpression.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/bool/BooleanExpression.java?rev=1152824&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/bool/BooleanExpression.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/bool/BooleanExpression.java Mon Aug 1 15:24:44 2011
@@ -0,0 +1,10 @@
+package org.apache.uima.tm.textmarker.kernel.expression.bool;
+
+import org.apache.uima.tm.textmarker.kernel.TextMarkerStatement;
+import org.apache.uima.tm.textmarker.kernel.expression.string.StringExpression;
+
+public abstract class BooleanExpression extends StringExpression {
+
+ public abstract boolean getBooleanValue(TextMarkerStatement parent);
+
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/bool/BooleanExpression.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/bool/BooleanExpression.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/bool/BooleanFunctionExpression.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/bool/BooleanFunctionExpression.java?rev=1152824&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/bool/BooleanFunctionExpression.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/bool/BooleanFunctionExpression.java Mon Aug 1 15:24:44 2011
@@ -0,0 +1,5 @@
+package org.apache.uima.tm.textmarker.kernel.expression.bool;
+
+public abstract class BooleanFunctionExpression extends BooleanExpression {
+
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/bool/BooleanFunctionExpression.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/bool/BooleanFunctionExpression.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/bool/BooleanNumberExpression.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/bool/BooleanNumberExpression.java?rev=1152824&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/bool/BooleanNumberExpression.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/bool/BooleanNumberExpression.java Mon Aug 1 15:24:44 2011
@@ -0,0 +1,62 @@
+package org.apache.uima.tm.textmarker.kernel.expression.bool;
+
+import org.apache.uima.tm.textmarker.kernel.TextMarkerStatement;
+import org.apache.uima.tm.textmarker.kernel.expression.number.NumberExpression;
+
+public class BooleanNumberExpression extends BooleanExpression {
+
+ private final NumberExpression e1;
+
+ private final String op;
+
+ private final NumberExpression e2;
+
+ public BooleanNumberExpression(NumberExpression e1, String op, NumberExpression e2) {
+ super();
+ this.e1 = e1;
+ this.op = op;
+ this.e2 = e2;
+ }
+
+ @Override
+ public boolean getBooleanValue(TextMarkerStatement parent) {
+ double doubleValue1 = getFristExpression().getDoubleValue(parent);
+ double doubleValue2 = getSecondExpression().getDoubleValue(parent);
+ return eval(doubleValue1, getOperator(), doubleValue2);
+ }
+
+ private boolean eval(double t1, String op, double t2) {
+ if ("==".equals(op)) {
+ return t1 == t2;
+ } else if ("!=".equals(op)) {
+ return t1 != t2;
+ } else if ("<".equals(op)) {
+ return t1 < t2;
+ } else if ("<=".equals(op)) {
+ return t1 <= t2;
+ } else if (">".equals(op)) {
+ return t1 > t2;
+ } else if (">=".equals(op)) {
+ return t1 >= t2;
+ }
+ return false;
+ }
+
+ public NumberExpression getFristExpression() {
+ return e1;
+ }
+
+ public String getOperator() {
+ return op;
+ }
+
+ public NumberExpression getSecondExpression() {
+ return e2;
+ }
+
+ @Override
+ public String getStringValue(TextMarkerStatement parent) {
+ return e1.getStringValue(parent) + " " + op + " " + e2.getStringValue(parent);
+ }
+
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/bool/BooleanNumberExpression.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/bool/BooleanNumberExpression.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/bool/BooleanTypeExpression.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/bool/BooleanTypeExpression.java?rev=1152824&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/bool/BooleanTypeExpression.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/bool/BooleanTypeExpression.java Mon Aug 1 15:24:44 2011
@@ -0,0 +1,54 @@
+package org.apache.uima.tm.textmarker.kernel.expression.bool;
+
+import org.apache.uima.tm.textmarker.kernel.TextMarkerStatement;
+import org.apache.uima.tm.textmarker.kernel.expression.type.TypeExpression;
+
+public class BooleanTypeExpression extends BooleanExpression {
+
+ private final TypeExpression e1;
+
+ private final String op;
+
+ private final TypeExpression e2;
+
+ public BooleanTypeExpression(TypeExpression e1, String op, TypeExpression e2) {
+ super();
+ this.e1 = e1;
+ this.op = op;
+ this.e2 = e2;
+ }
+
+ @Override
+ public boolean getBooleanValue(TextMarkerStatement parent) {
+ String first = getFristExpression().getType(parent).getName();
+ String second = getSecondExpression().getType(parent).getName();
+ return eval(first, getOperator(), second);
+ }
+
+ private boolean eval(String t1, String op, String t2) {
+ if ("==".equals(op)) {
+ return t1.equals(t2);
+ } else if ("!=".equals(op)) {
+ return !t1.equals(t2);
+ }
+ return false;
+ }
+
+ public TypeExpression getFristExpression() {
+ return e1;
+ }
+
+ public String getOperator() {
+ return op;
+ }
+
+ public TypeExpression getSecondExpression() {
+ return e2;
+ }
+
+ @Override
+ public String getStringValue(TextMarkerStatement parent) {
+ return e1.getStringValue(parent) + " " + op + " " + e2.getStringValue(parent);
+ }
+
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/bool/BooleanTypeExpression.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/bool/BooleanTypeExpression.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/bool/ReferenceBooleanExpression.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/bool/ReferenceBooleanExpression.java?rev=1152824&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/bool/ReferenceBooleanExpression.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/bool/ReferenceBooleanExpression.java Mon Aug 1 15:24:44 2011
@@ -0,0 +1,32 @@
+package org.apache.uima.tm.textmarker.kernel.expression.bool;
+
+import org.apache.uima.tm.textmarker.kernel.TextMarkerStatement;
+
+public class ReferenceBooleanExpression extends BooleanExpression {
+
+ private final String var;
+
+ public ReferenceBooleanExpression(String var) {
+ super();
+ this.var = var;
+ }
+
+ @Override
+ public boolean getBooleanValue(TextMarkerStatement parent) {
+ Boolean variableValue = parent.getEnvironment().getVariableValue(var, Boolean.class);
+ if (variableValue == null) {
+ return false;
+ }
+ return variableValue;
+ }
+
+ public String getVar() {
+ return var;
+ }
+
+ @Override
+ public String getStringValue(TextMarkerStatement parent) {
+ return getBooleanValue(parent) ? "true" : "false";
+ }
+
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/bool/ReferenceBooleanExpression.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/expression/bool/ReferenceBooleanExpression.java
------------------------------------------------------------------------------
svn:mime-type = text/plain