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 [5/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/condition/TerminalTextMarkerCondition.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/condition/TerminalTextMarkerCondition.java?rev=1152824&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/condition/TerminalTextMarkerCondition.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/condition/TerminalTextMarkerCondition.java Mon Aug 1 15:24:44 2011
@@ -0,0 +1,5 @@
+package org.apache.uima.tm.textmarker.condition;
+
+public abstract class TerminalTextMarkerCondition extends AbstractTextMarkerCondition {
+
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/condition/TerminalTextMarkerCondition.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/condition/TerminalTextMarkerCondition.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/condition/TotalCountCondition.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/condition/TotalCountCondition.java?rev=1152824&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/condition/TotalCountCondition.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/condition/TotalCountCondition.java Mon Aug 1 15:24:44 2011
@@ -0,0 +1,61 @@
+package org.apache.uima.tm.textmarker.condition;
+
+import java.util.Iterator;
+
+import org.apache.uima.cas.Type;
+import org.apache.uima.tm.textmarker.kernel.TextMarkerStream;
+import org.apache.uima.tm.textmarker.kernel.expression.number.NumberExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.number.SimpleNumberExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.type.TypeExpression;
+import org.apache.uima.tm.textmarker.kernel.rule.EvaluatedCondition;
+import org.apache.uima.tm.textmarker.kernel.rule.TextMarkerRuleElement;
+import org.apache.uima.tm.textmarker.kernel.type.TextMarkerBasic;
+import org.apache.uima.tm.textmarker.kernel.visitor.InferenceCrowd;
+
+
+public class TotalCountCondition extends TypeSentiveCondition {
+ private final NumberExpression min;
+
+ private final NumberExpression max;
+
+ private final String var;
+
+ public TotalCountCondition(TypeExpression type, NumberExpression min, NumberExpression max,
+ String var) {
+ super(type);
+ this.min = min == null ? new SimpleNumberExpression(Integer.MIN_VALUE) : min;
+ this.max = max == null ? new SimpleNumberExpression(Integer.MAX_VALUE) : max;
+ this.var = var;
+ }
+
+ @Override
+ public EvaluatedCondition eval(TextMarkerBasic annotation, Type matchedType,
+ TextMarkerRuleElement element, TextMarkerStream stream, InferenceCrowd crowd) {
+ int count = 0;
+ Iterator<?> it = stream.getJCas().getAnnotationIndex(type.getType(element.getParent()))
+ .iterator();
+ while (it.hasNext()) {
+ it.next();
+ count++;
+ }
+ if (var != null) {
+ element.getParent().getEnvironment().setVariableValue(var, count);
+ }
+ boolean value = count >= min.getIntegerValue(element.getParent())
+ && count <= max.getIntegerValue(element.getParent());
+ return new EvaluatedCondition(this, value);
+ }
+
+ public NumberExpression getMin() {
+ return min;
+ }
+
+ public NumberExpression getMax() {
+ return max;
+ }
+
+ public String getVar() {
+ return var;
+ }
+
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/condition/TotalCountCondition.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/condition/TotalCountCondition.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/condition/TypeSentiveCondition.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/condition/TypeSentiveCondition.java?rev=1152824&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/condition/TypeSentiveCondition.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/condition/TypeSentiveCondition.java Mon Aug 1 15:24:44 2011
@@ -0,0 +1,35 @@
+package org.apache.uima.tm.textmarker.condition;
+
+import org.apache.uima.tm.textmarker.kernel.expression.list.TypeListExpression;
+import org.apache.uima.tm.textmarker.kernel.expression.type.TypeExpression;
+
+public abstract class TypeSentiveCondition extends TerminalTextMarkerCondition {
+
+ protected final TypeExpression type;
+
+ private final TypeListExpression list;
+
+ public TypeExpression getType() {
+ return type;
+ }
+
+ public TypeSentiveCondition(TypeExpression type) {
+ super();
+ this.type = type;
+ this.list = null;
+ }
+
+ public TypeSentiveCondition(TypeListExpression list) {
+ super();
+ this.type = null;
+ this.list = list;
+ }
+
+ public boolean isWorkingOnList() {
+ return getList() != null;
+ }
+
+ public TypeListExpression getList() {
+ return list;
+ }
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/condition/TypeSentiveCondition.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/condition/TypeSentiveCondition.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/condition/VariableCondition.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/condition/VariableCondition.java?rev=1152824&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/condition/VariableCondition.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/condition/VariableCondition.java Mon Aug 1 15:24:44 2011
@@ -0,0 +1,30 @@
+package org.apache.uima.tm.textmarker.condition;
+
+import org.apache.uima.cas.Type;
+import org.apache.uima.tm.textmarker.kernel.TextMarkerStream;
+import org.apache.uima.tm.textmarker.kernel.rule.EvaluatedCondition;
+import org.apache.uima.tm.textmarker.kernel.rule.TextMarkerRuleElement;
+import org.apache.uima.tm.textmarker.kernel.type.TextMarkerBasic;
+import org.apache.uima.tm.textmarker.kernel.visitor.InferenceCrowd;
+
+
+public class VariableCondition extends AbstractTextMarkerCondition {
+
+ private final String var;
+
+ public VariableCondition(String var) {
+ super();
+ this.var = var;
+ }
+
+ @Override
+ public EvaluatedCondition eval(TextMarkerBasic basic, Type matchedType,
+ TextMarkerRuleElement element, TextMarkerStream stream, InferenceCrowd crowd) {
+ return null;
+ }
+
+ public String getVar() {
+ return var;
+ }
+
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/condition/VariableCondition.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/condition/VariableCondition.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/condition/VoteCondition.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/condition/VoteCondition.java?rev=1152824&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/condition/VoteCondition.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/condition/VoteCondition.java Mon Aug 1 15:24:44 2011
@@ -0,0 +1,60 @@
+package org.apache.uima.tm.textmarker.condition;
+
+import java.util.List;
+
+import org.apache.uima.cas.Type;
+import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.tm.textmarker.kernel.TextMarkerStream;
+import org.apache.uima.tm.textmarker.kernel.expression.type.TypeExpression;
+import org.apache.uima.tm.textmarker.kernel.rule.EvaluatedCondition;
+import org.apache.uima.tm.textmarker.kernel.rule.TextMarkerRuleElement;
+import org.apache.uima.tm.textmarker.kernel.type.TextMarkerBasic;
+import org.apache.uima.tm.textmarker.kernel.visitor.InferenceCrowd;
+
+
+public class VoteCondition extends TerminalTextMarkerCondition {
+
+ private final TypeExpression type1;
+
+ private final TypeExpression type2;
+
+ public VoteCondition(TypeExpression type1, TypeExpression type2) {
+ super();
+ this.type1 = type1;
+ this.type2 = type2;
+ }
+
+ @Override
+ public EvaluatedCondition eval(TextMarkerBasic basic, Type matchedType,
+ TextMarkerRuleElement element, TextMarkerStream stream, InferenceCrowd crowd) {
+ int count1 = 0;
+ int count2 = 0;
+ int totalCount = 0;
+ if (matchedType == null)
+ return new EvaluatedCondition(this, false);
+ AnnotationFS annotation = basic.getType(matchedType.getName());
+ if (annotation != null) {
+ List<TextMarkerBasic> annotations = stream.getBasicsInWindow(annotation);
+ String name1 = type1.getType(element.getParent()).getName();
+ String name2 = type2.getType(element.getParent()).getName();
+ for (TextMarkerBasic each : annotations) {
+ totalCount++;
+ if (each.isAnchorOf(name1)) {
+ count1++;
+ }
+ if (each.isAnchorOf(name2)) {
+ count2++;
+ }
+ }
+ }
+ return new EvaluatedCondition(this, count1 > count2);
+ }
+
+ public TypeExpression getType1() {
+ return type1;
+ }
+
+ public TypeExpression getType2() {
+ return type2;
+ }
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/condition/VoteCondition.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/condition/VoteCondition.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/engine/StyleMapFactory.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/engine/StyleMapFactory.java?rev=1152824&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/engine/StyleMapFactory.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/engine/StyleMapFactory.java Mon Aug 1 15:24:44 2011
@@ -0,0 +1,360 @@
+package org.apache.uima.tm.textmarker.engine;
+
+import java.awt.Color;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.StringTokenizer;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.FactoryConfigurationError;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.apache.uima.cas.FSIterator;
+import org.apache.uima.cas.FeatureStructure;
+import org.apache.uima.cas.Type;
+import org.apache.uima.jcas.JCas;
+import org.apache.uima.tm.textmarker.kernel.TextMarkerStream;
+import org.apache.uima.tm.textmarker.kernel.type.TextMarkerColoring;
+import org.apache.uima.tools.stylemap.ColorParser;
+import org.apache.uima.tools.stylemap.StyleMapEntry;
+import org.apache.uima.tools.stylemap.StyleMapXmlParser;
+import org.apache.uima.util.FileUtils;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Text;
+import org.xml.sax.SAXException;
+
+
+public class StyleMapFactory {
+
+ private Map<String, String> colorNameMap;
+
+ public StyleMapFactory() {
+ initializeColorNameMap();
+ }
+
+ private void initializeColorNameMap() {
+ colorNameMap = new HashMap<String, String>();
+ colorNameMap.put("#000000", "black");
+ colorNameMap.put("#c0c0c0", "silver");
+ colorNameMap.put("#808080", "gray");
+ colorNameMap.put("#ffffff", "white");
+ colorNameMap.put("#800000", "maroon");
+ colorNameMap.put("#ff0000", "red");
+ colorNameMap.put("#800080", "purple");
+ colorNameMap.put("#ff00ff", "fuchsia");
+ colorNameMap.put("#008000", "green");
+ colorNameMap.put("#00ff00", "lime");
+ colorNameMap.put("#808000", "olive");
+ colorNameMap.put("#ffff00", "yellow");
+ colorNameMap.put("#000080", "navy");
+ colorNameMap.put("#0000ff", "blue");
+ colorNameMap.put("#00ffff", "aqua");
+ colorNameMap.put("#000000", "black");
+ colorNameMap.put("#add8e6", "lightblue");
+ colorNameMap.put("#90ee90", "lightgreen");
+ colorNameMap.put("#ffa500", "orange");
+ colorNameMap.put("#ffc0cb", "pink");
+ colorNameMap.put("#fa8072", "salmon");
+ colorNameMap.put("#00ffff", "cyan");
+ colorNameMap.put("#ee82ee", "violet");
+ colorNameMap.put("#d2b48c", "tan");
+ colorNameMap.put("#a52a2a", "brown");
+ colorNameMap.put("#ffffff", "white");
+ colorNameMap.put("#9370db", "mediumpurple");
+ // in other order for lookup
+ colorNameMap.put("black", "#000000");
+ colorNameMap.put("silver", "#c0c0c0");
+ colorNameMap.put("gray", "#808080");
+ colorNameMap.put("white", "#ffffff");
+ colorNameMap.put("maroon", "#800000");
+ colorNameMap.put("red", "#ff0000");
+ colorNameMap.put("purple", "#800080");
+ colorNameMap.put("fuchsia", "#ff00ff");
+ colorNameMap.put("green", "#008000");
+ colorNameMap.put("lime", "#00ff00");
+ colorNameMap.put("olive", "#808000");
+ colorNameMap.put("yellow", "#ffff00");
+ colorNameMap.put("navy", "#000080");
+ colorNameMap.put("blue", "#0000ff");
+ colorNameMap.put("aqua", "#00ffff");
+ colorNameMap.put("black", "#000000");
+ colorNameMap.put("lightblue", "#add8e6");
+ colorNameMap.put("lightgreen", "#90ee90");
+ colorNameMap.put("orange", "#ffa500");
+ colorNameMap.put("pink", "#ffc0cb");
+ colorNameMap.put("salmon", "#fa8072");
+ colorNameMap.put("cyan", "#00ffff");
+ colorNameMap.put("violet", "#ee82ee");
+ colorNameMap.put("tan", "#d2b48c");
+ colorNameMap.put("brown", "#a52a2a");
+ colorNameMap.put("white", "#ffffff");
+ colorNameMap.put("mediumpurple", "#9370db");
+ }
+
+ public void createStyleMap(String styleMapLocation, TextMarkerStream stream) throws IOException {
+ List<StyleMapEntry> styleList = createStyleList(stream);
+ String styleXml = createStyleXml(styleList);
+ writeStyleFile(styleXml, styleMapLocation);
+ }
+
+ private String createStyleXml(List<StyleMapEntry> styleList) {
+ StringBuffer result = new StringBuffer();
+ result.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
+ result.append("<styleMap>\n");
+
+ for (StyleMapEntry e : styleList) {
+ result.append("<rule>\n");
+ result.append("<pattern>");
+ result.append(e.getPattern());
+ result.append("</pattern>\n");
+ result.append("<label>");
+ String label = e.getLabel();
+ if (label != null) {
+ if ((label != null) && !label.equals(""))
+ result.append(label);
+ else
+ result.append(e.getAnnotationTypeName());
+ } else
+ result.append(e.getAnnotationTypeName());
+
+ result.append("</label>\n");
+ result.append("<style>");
+
+ String foregroundColor = "#" + Integer.toHexString(e.getForeground().getRGB()).substring(2);
+ String backgroundColor = "#" + Integer.toHexString(e.getBackground().getRGB()).substring(2);
+
+ if (colorNameMap.containsKey(foregroundColor)) {
+ result.append("color:" + colorNameMap.get(foregroundColor) + ";");
+ } else {
+ result.append("color:" + foregroundColor + ";");
+ }
+
+ if (colorNameMap.containsKey(backgroundColor)) {
+ result.append("background:" + colorNameMap.get(backgroundColor) + ";");
+ } else {
+ result.append("background:" + backgroundColor + ";");
+ }
+ Boolean ck = e.getChecked();
+ String ckString = ck.toString();
+
+ Boolean hid = (Boolean) Boolean.FALSE;
+ String hidString = hid.toString();
+ // this prevents hidden from being checked,
+ // becasue that is not a meaningful combination
+ if (hidString.equals("true")) {
+ ckString = "false";
+ }
+ result.append("checked:" + ckString + ";");
+ result.append("hidden:" + hidString + ";");
+
+ result.append("</style>\n");
+ result.append("</rule>\n");
+ }
+
+ result.append("</styleMap>\n");
+
+ return result.toString();
+ }
+
+ private void writeStyleFile(String output, String styleMapLocation) throws IOException {
+ File file = new File(styleMapLocation);
+ FileUtils.saveString2File(output, file);
+ }
+
+ private List<StyleMapEntry> createStyleList(TextMarkerStream stream) {
+ List<StyleMapEntry> result = new ArrayList<StyleMapEntry>();
+ JCas cas = stream.getJCas();
+ Type type = cas.getCasType(TextMarkerColoring.type);
+ FSIterator<FeatureStructure> iterator = cas.getFSIndexRepository().getAllIndexedFS(type);
+ while (iterator.hasNext()) {
+ TextMarkerColoring each = (TextMarkerColoring) iterator.next();
+ StyleMapEntry entry = new StyleMapEntry();
+ entry.setAnnotationTypeName(each.getTargetType());
+ String fgColor = each.getFgColor();
+ entry.setForeground(parseColorBackground(fgColor));
+ String bgColor = each.getBgColor();
+ entry.setBackground(parseColorBackground(bgColor));
+ entry.setChecked(each.getSelected());
+ result.add(entry);
+ }
+
+ return result;
+ }
+
+ private Color parseColorBackground(String color) {
+ if (color == null) {
+ return Color.BLACK;
+ }
+ if (color.startsWith("#")) {
+ return Color.decode(color);
+ } else {
+ String string = (String) colorNameMap.get(color);
+ if (string != null)
+ return Color.decode(string);
+ else
+ return Color.LIGHT_GRAY;
+ }
+ }
+
+ private Color parseColorForeground(String color) {
+ if (color.startsWith("#")) {
+ return Color.decode(color);
+ } else {
+ String string = (String) colorNameMap.get(color);
+ if (string != null)
+ return Color.decode(string);
+ else
+ return Color.BLACK;
+ }
+ }
+
+ public Map<String, StyleMapEntry> parseStyleMap(String styleFileString) {
+ Map<String, StyleMapEntry> result = new HashMap<String, StyleMapEntry>();
+ StyleMapXmlParser smxp = new StyleMapXmlParser(styleFileString);
+ ColorParser cp = new ColorParser();
+ for (int i = 0; i < smxp.annotType.size(); i++) {
+ String typeName = ((String) smxp.annotType.elementAt(i));
+ String labelString = ((String) smxp.styleLabel.elementAt(i));
+ String featureValue = ((String) smxp.featureValue.elementAt(i));
+ String styleColor = smxp.styleColor.elementAt(i).toString();
+ StyleMapEntry e = cp.parseAndAssignColors(typeName, featureValue, labelString, styleColor);
+ result.put(typeName, e);
+ }
+ return result;
+ }
+
+ public Map<String, StyleMapEntry> parseStyleMapDOM(String styleFileString) {
+ Map<String, StyleMapEntry> result = new HashMap<String, StyleMapEntry>();
+ if (styleFileString == null) {
+ return result;
+ }
+
+ File styleMapFile = new File(styleFileString);
+ Document parse = null;
+ try {
+ DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ FileInputStream stream = new FileInputStream(styleMapFile);
+ parse = db.parse(stream);
+ stream.close();
+
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ return null;
+
+ } catch (ParserConfigurationException e) {
+ e.printStackTrace();
+ return null;
+
+ } catch (FactoryConfigurationError e) {
+ e.printStackTrace();
+ return null;
+
+ } catch (SAXException e) {
+ e.printStackTrace();
+ return null;
+
+ } catch (IOException e) {
+ e.printStackTrace();
+ return null;
+ }
+
+ final Node root = parse.getDocumentElement();
+ final NodeList nodeList = root.getChildNodes();
+
+ for (int i = 0; i < nodeList.getLength(); ++i) {
+ final Node node = nodeList.item(i);
+ final String nodeName = node.getNodeName();
+ // "rule" node ?
+ if (!nodeName.equals("rule")) { //$NON-NLS-1$
+ continue;
+ }
+
+ // Collect type or pattern, label, and color text style
+ NodeList childrenList = node.getChildNodes();
+ String type = ""; //$NON-NLS-1$
+ String label = ""; //$NON-NLS-1$
+ String colorText = ""; //$NON-NLS-1$
+ for (int j = 0; j < childrenList.getLength(); ++j) {
+ final Node child = childrenList.item(j);
+ final String childName = child.getNodeName();
+
+ if (childName.equals("pattern")) { //$NON-NLS-1$
+ type = getTextValue(child);
+ } else if (childName.equals("label")) { //$NON-NLS-1$
+ label = getTextValue(child);
+ } else if (childName.equals("style")) { //$NON-NLS-1$
+ colorText = getTextValue(child);
+ }
+ }
+
+ StyleMapEntry styleMapEntry = getStyleMapEntry(type, label, colorText);
+ result.put(styleMapEntry.getAnnotationTypeName(), styleMapEntry);
+ }
+ return result;
+ }
+
+ protected StyleMapEntry getStyleMapEntry(String type, String label, String styleColor) {
+ StyleMapEntry result = new StyleMapEntry();
+ result.setAnnotationTypeName(type);
+ result.setLabel(label);
+ StringTokenizer token = new StringTokenizer(styleColor, ":;");
+ if (!token.hasMoreTokens()) {
+ return null; // No token
+ }
+
+ token.nextToken();
+ String fgString = token.nextToken().toLowerCase().trim();
+ result.setForeground(parseColorForeground(fgString));
+
+ token.nextToken();
+ String bgString = token.nextToken().toLowerCase().trim();
+ result.setBackground(parseColorBackground(bgString));
+
+ boolean checked = false; // default to Checked
+ if (token.hasMoreTokens()) {
+ String ck = token.nextToken();
+ String tf = token.nextToken();
+ if (ck.equals("checked")) {
+ if (tf.equals("false")) {
+ checked = false;
+ } else if (tf.equals("true")) {
+ checked = true;
+ }
+ }
+ }
+ result.setChecked(checked);
+
+ boolean hidden = false;
+ if (token.hasMoreTokens()) {
+ String ck = token.nextToken();
+ String tf = token.nextToken();
+ if (ck.equals("hidden")) {
+ if (tf.equals("true")) {
+ hidden = true;
+ }
+ }
+ }
+ result.setHidden(hidden);
+ return result;
+ }
+
+ private String getTextValue(final Node node) {
+ final Node first = node.getFirstChild();
+
+ if (first != null) {
+ return ((Text) first).getNodeValue().trim();
+
+ } else {
+ return null;
+ }
+ }
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/engine/StyleMapFactory.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/engine/StyleMapFactory.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/engine/TextMarkerEngine.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/engine/TextMarkerEngine.java?rev=1152824&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/engine/TextMarkerEngine.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/engine/TextMarkerEngine.java Mon Aug 1 15:24:44 2011
@@ -0,0 +1,562 @@
+package org.apache.uima.tm.textmarker.engine;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.antlr.runtime.ANTLRFileStream;
+import org.antlr.runtime.CharStream;
+import org.antlr.runtime.CommonTokenStream;
+import org.antlr.runtime.RecognitionException;
+import org.apache.uima.UIMAFramework;
+import org.apache.uima.UimaContext;
+import org.apache.uima.analysis_component.JCasAnnotator_ImplBase;
+import org.apache.uima.analysis_engine.AnalysisEngine;
+import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
+import org.apache.uima.cas.CAS;
+import org.apache.uima.cas.CASException;
+import org.apache.uima.cas.FSIterator;
+import org.apache.uima.cas.Feature;
+import org.apache.uima.cas.Type;
+import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.cas.text.AnnotationIndex;
+import org.apache.uima.jcas.JCas;
+import org.apache.uima.resource.ResourceInitializationException;
+import org.apache.uima.resource.ResourceManager;
+import org.apache.uima.resource.metadata.TypeSystemDescription;
+import org.apache.uima.tm.textmarker.kernel.FilterManager;
+import org.apache.uima.tm.textmarker.kernel.TextMarkerModule;
+import org.apache.uima.tm.textmarker.kernel.TextMarkerStream;
+import org.apache.uima.tm.textmarker.kernel.extensions.IEngineLoader;
+import org.apache.uima.tm.textmarker.kernel.extensions.ITextMarkerExtension;
+import org.apache.uima.tm.textmarker.kernel.extensions.TextMarkerEngineLoader;
+import org.apache.uima.tm.textmarker.kernel.extensions.TextMarkerExternalFactory;
+import org.apache.uima.tm.textmarker.kernel.type.TextMarkerBasic;
+import org.apache.uima.tm.textmarker.kernel.visitor.DebugInfoCollectorVisitor;
+import org.apache.uima.tm.textmarker.kernel.visitor.InferenceCrowd;
+import org.apache.uima.tm.textmarker.kernel.visitor.StatisticsVisitor;
+import org.apache.uima.tm.textmarker.kernel.visitor.TextMarkerInferenceVisitor;
+import org.apache.uima.tm.textmarker.kernel.visitor.TimeProfilerVisitor;
+import org.apache.uima.tm.textmarker.parser.TextMarkerLexer;
+import org.apache.uima.tm.textmarker.parser.TextMarkerParser;
+import org.apache.uima.tm.textmarker.seed.TextMarkerAnnotationSeeder;
+import org.apache.uima.tm.textmarker.verbalize.TextMarkerVerbalizer;
+import org.apache.uima.util.InvalidXMLException;
+import org.apache.uima.util.XMLInputSource;
+
+
+public class TextMarkerEngine extends JCasAnnotator_ImplBase {
+
+ public static final String BASIC_TYPE = "org.apache.uima.tm.textmarker.kernel.type.TextMarkerBasic";
+
+ public static final String SEEDERS = "seeders";
+
+ public static final String USE_BASICS = "useBasics";
+
+ public static final String REMOVE_BASICS = "removeBasics";
+
+ public static final String SCRIPT_PATHS = "scriptPaths";
+
+ public static final String DESCRIPTOR_PATHS = "descriptorPaths";
+
+ public static final String MAIN_SCRIPT = "mainScript";
+
+ public static final String ADDITIONAL_SCRIPTS = "additionalScripts";
+
+ public static final String ADDITIONAL_ENGINES = "additionalEngines";
+
+ public static final String ADDITIONAL_EXTENSIONS = "additionalExtensions";
+
+ public static final String ADDITIONAL_ENGINE_LOADERS = "additionalEngineLoaders";
+
+ public static final String CREATE_DEBUG_INFO = "debug";
+
+ public static final String CREATE_DEBUG_INFO_ONLY_FOR = "debugOnlyFor";
+
+ public static final String CREATE_PROFILING_INFO = "profile";
+
+ public static final String CREATE_STATISTIC_INFO = "statistics";
+
+ public static final String CREATE_MATCH_DEBUG_INFO = "debugWithMatches";
+
+ public static final String CREATE_STYLE_MAP = "style";
+
+ public static final String STYLE_MAP = "styleMap";
+
+ public static final String RESOURCE_PATHS = "resourcePaths";
+
+ public static final String SCRIPT_ENCODING = "scriptEncoding";
+
+ public static final String MODIFIED_SOFA = "modified";
+
+ public static final String DEFAULT_FILTERED_TYPES = "defaultFilteredTypes";
+
+ public static final String DEFAULT_FILTERED_MARKUPS = "defaultFilteredMarkups";
+
+ private String[] seeders;
+
+ private String useBasics;
+
+ private Boolean createDebugInfo;
+
+ private String[] createDebugOnlyFor;
+
+ private Boolean createProfilingInfo;
+
+ private Boolean createStatisticInfo;
+
+ private String styleMapLocation;
+
+ private Boolean createStyleMap;
+
+ private Boolean withMatches;
+
+ private String[] resourcePaths;
+
+ private String scriptEncoding;
+
+ private UimaContext context;
+
+ private TextMarkerModule script;
+
+ private StyleMapFactory styleMapFactory;
+
+ private String[] additionalScriptLocations;
+
+ private String[] additionalEngineLocations;
+
+ private String[] additionalExtensions;
+
+ private String[] additionalEngineLoaders;
+
+ private TextMarkerExternalFactory factory;
+
+ private TextMarkerEngineLoader engineLoader;
+
+ private String[] defaultFilteredTypes;
+
+ private String[] defaultFilteredMarkups;
+
+ private String mainScript;
+
+ private String[] scriptPaths;
+
+ private String[] descriptorPaths;
+
+ private TextMarkerVerbalizer verbalizer;
+
+ private Boolean removeBasics;
+
+ private Map<String, TypeSystemDescription> localTSDMap;
+
+ @Override
+ public void initialize(UimaContext aContext) throws ResourceInitializationException {
+ super.initialize(aContext);
+ if (aContext == null && context != null) {
+ aContext = context;
+ }
+ seeders = (String[]) aContext.getConfigParameterValue(SEEDERS);
+ useBasics = (String) aContext.getConfigParameterValue(USE_BASICS);
+ removeBasics = (Boolean) aContext.getConfigParameterValue(REMOVE_BASICS);
+ scriptPaths = (String[]) aContext.getConfigParameterValue(SCRIPT_PATHS);
+ descriptorPaths = (String[]) aContext.getConfigParameterValue(DESCRIPTOR_PATHS);
+ mainScript = (String) aContext.getConfigParameterValue(MAIN_SCRIPT);
+ additionalScriptLocations = (String[]) aContext.getConfigParameterValue(ADDITIONAL_SCRIPTS);
+ additionalEngineLocations = (String[]) aContext.getConfigParameterValue(ADDITIONAL_ENGINES);
+ additionalExtensions = (String[]) aContext.getConfigParameterValue(ADDITIONAL_EXTENSIONS);
+ additionalEngineLoaders = (String[]) aContext
+ .getConfigParameterValue(ADDITIONAL_ENGINE_LOADERS);
+
+ createDebugInfo = (Boolean) aContext.getConfigParameterValue(CREATE_DEBUG_INFO);
+ createDebugOnlyFor = (String[]) aContext.getConfigParameterValue(CREATE_DEBUG_INFO_ONLY_FOR);
+ createProfilingInfo = (Boolean) aContext.getConfigParameterValue(CREATE_PROFILING_INFO);
+ createStatisticInfo = (Boolean) aContext.getConfigParameterValue(CREATE_STATISTIC_INFO);
+ withMatches = (Boolean) aContext.getConfigParameterValue(CREATE_MATCH_DEBUG_INFO);
+ createStyleMap = (Boolean) aContext.getConfigParameterValue(CREATE_STYLE_MAP);
+ styleMapLocation = (String) aContext.getConfigParameterValue(STYLE_MAP);
+ resourcePaths = (String[]) aContext.getConfigParameterValue(RESOURCE_PATHS);
+ scriptEncoding = (String) aContext.getConfigParameterValue(SCRIPT_ENCODING);
+ defaultFilteredTypes = (String[]) aContext.getConfigParameterValue(DEFAULT_FILTERED_TYPES);
+ defaultFilteredMarkups = (String[]) aContext.getConfigParameterValue(DEFAULT_FILTERED_MARKUPS);
+
+ styleMapFactory = new StyleMapFactory();
+
+ removeBasics = removeBasics == null ? false : removeBasics;
+ createDebugInfo = createDebugInfo == null ? false : createDebugInfo;
+ createDebugOnlyFor = createDebugOnlyFor == null ? new String[0] : createDebugOnlyFor;
+ createProfilingInfo = createProfilingInfo == null ? false : createProfilingInfo;
+ createStatisticInfo = createStatisticInfo == null ? false : createStatisticInfo;
+ withMatches = withMatches == null ? true : withMatches;
+ createStyleMap = createStyleMap == null ? true : createStyleMap;
+ scriptEncoding = scriptEncoding == null ? "UTF-8" : scriptEncoding;
+ defaultFilteredTypes = defaultFilteredTypes == null ? new String[0] : defaultFilteredTypes;
+ defaultFilteredMarkups = defaultFilteredMarkups == null ? new String[0]
+ : defaultFilteredMarkups;
+
+ this.context = aContext;
+
+ factory = new TextMarkerExternalFactory();
+ engineLoader = new TextMarkerEngineLoader();
+ verbalizer = new TextMarkerVerbalizer();
+
+ localTSDMap = new HashMap<String, TypeSystemDescription>();
+ }
+
+ @Override
+ public void process(JCas cas) throws AnalysisEngineProcessException {
+ // TODO fix single/multi view stuff
+ if (cas.getViewName() == null) {
+ try {
+ cas = cas.getView(CAS.NAME_DEFAULT_SOFA);
+ } catch (CASException e) {
+ throw new AnalysisEngineProcessException(e);
+ }
+ }
+
+ if (!factory.isInitialized()) {
+ initializeExtensionWithClassPath();
+ }
+ if (!engineLoader.isInitialized()) {
+ initializeEngineLoaderWithClassPath();
+ }
+ initializeScript(cas.getCas());
+ TextMarkerStream stream = initializeStream(cas.getCas());
+
+ InferenceCrowd crowd = initializeCrowd();
+ try {
+ script.apply(stream, crowd);
+ } catch (Throwable e) {
+ throw new AnalysisEngineProcessException(AnalysisEngineProcessException.ANNOTATOR_EXCEPTION,
+ new Object[] {}, e);
+ }
+ crowd.finished(stream);
+
+ if (createStyleMap) {
+ try {
+ String locate = locate(styleMapLocation, descriptorPaths, ".xml", false);
+ if (locate != null) {
+ styleMapFactory.createStyleMap(locate, stream);
+ }
+ } catch (IOException e) {
+ throw new AnalysisEngineProcessException(e);
+ }
+ }
+ if (removeBasics) {
+ List<AnnotationFS> toRemove = new ArrayList<AnnotationFS>();
+ Type type = cas.getTypeSystem().getType(BASIC_TYPE);
+ FSIterator<AnnotationFS> iterator = cas.getCas().getAnnotationIndex(type).iterator();
+ while (iterator.isValid()) {
+ AnnotationFS fs = iterator.get();
+ toRemove.add(fs);
+ iterator.moveToNext();
+ }
+ for (AnnotationFS annotationFS : toRemove) {
+ cas.removeFsFromIndexes(annotationFS);
+ }
+ }
+ }
+
+ private void initializeExtensionWithClassPath() {
+ if (additionalExtensions == null) {
+ return;
+ }
+ for (String each : additionalExtensions) {
+ try {
+ Class<?> forName = Class.forName(each);
+ if (ITextMarkerExtension.class.isAssignableFrom(forName)) {
+ ITextMarkerExtension extension = (ITextMarkerExtension) forName.newInstance();
+ verbalizer.addExternalVerbalizers(extension);
+ for (String name : extension.getKnownExtensions()) {
+ factory.addExtension(name, extension);
+ }
+ }
+ } catch (Exception e) {
+ // System.out.println("EXTENSION ERROR: " + each);
+ }
+ }
+ }
+
+ private void initializeEngineLoaderWithClassPath() {
+ if (additionalEngineLoaders == null) {
+ return;
+ }
+ for (String each : additionalEngineLoaders) {
+ try {
+ Class<?> forName = Class.forName(each);
+ if (IEngineLoader.class.isAssignableFrom(forName)) {
+ IEngineLoader loader = (IEngineLoader) forName.newInstance();
+ for (String name : loader.getKnownEngines()) {
+ engineLoader.addLoader(name, loader);
+ }
+ }
+ } catch (Exception e) {
+ // System.out.println("LOADER ERROR: " + each);
+ }
+ }
+ }
+
+ private InferenceCrowd initializeCrowd() {
+ List<TextMarkerInferenceVisitor> visitors = new ArrayList<TextMarkerInferenceVisitor>();
+ if (createDebugInfo) {
+ visitors.add(new DebugInfoCollectorVisitor(createDebugInfo, withMatches, Arrays
+ .asList(createDebugOnlyFor), verbalizer));
+ }
+ if (createProfilingInfo) {
+ visitors.add(new TimeProfilerVisitor());
+ }
+ if (createStatisticInfo) {
+ visitors.add(new StatisticsVisitor(verbalizer));
+ }
+ return new InferenceCrowd(visitors);
+ }
+
+ private TextMarkerStream initializeStream(CAS cas) throws AnalysisEngineProcessException {
+ Collection<Type> filterTypes = new ArrayList<Type>();
+ Collection<String> filterTags = Arrays.asList(defaultFilteredMarkups);
+
+ for (String each : defaultFilteredTypes) {
+ Type type = cas.getTypeSystem().getType(each);
+ if (type != null) {
+ filterTypes.add(type);
+ }
+ }
+
+ FilterManager filter = new FilterManager(filterTypes, filterTags, cas);
+
+ Type basicType = cas.getTypeSystem().getType(BASIC_TYPE);
+ if (seeders != null) {
+ // not already contains that basics:
+ if (cas.getAnnotationIndex(basicType).size() <= 0) {
+
+ for (String seederClass : seeders) {
+ Class<?> loadClass = null;
+ try {
+ loadClass = Class.forName(seederClass);
+ } catch (ClassNotFoundException e) {
+ throw new AnalysisEngineProcessException(e);
+ }
+ Object newInstance = null;
+ try {
+ newInstance = loadClass.newInstance();
+ } catch (Exception e) {
+ throw new AnalysisEngineProcessException(e);
+ }
+
+ try {
+ TextMarkerAnnotationSeeder seeder = (TextMarkerAnnotationSeeder) newInstance;
+ seeder.seed(cas.getDocumentText(), cas);
+
+ } catch (Exception e) {
+ throw new AnalysisEngineProcessException(e);
+ }
+ }
+
+ }
+
+ } else if (useBasics != null) {
+ // thats the case if predefined annotation should be used for inference
+ Type givenType = cas.getTypeSystem().getType(useBasics);
+ FSIterator<AnnotationFS> iterator = cas.getAnnotationIndex(givenType).iterator();
+ while (iterator.isValid()) {
+ AnnotationFS fs = iterator.get();
+ AnnotationFS createAnnotation = cas.createAnnotation(basicType, fs.getBegin(), fs.getEnd());
+ cas.addFsToIndexes(createAnnotation);
+ iterator.moveToNext();
+ }
+ }
+
+ FSIterator<AnnotationFS> it = cas.getAnnotationIndex(basicType).iterator();
+
+ TextMarkerStream stream = new TextMarkerStream(cas, it, basicType, filter);
+
+ // TODO find a better solution for this!! -> feature request open
+ FSIterator<AnnotationFS> others = cas.getAnnotationIndex().iterator();
+ while (others.isValid()) {
+ AnnotationFS a = (AnnotationFS) others.get();
+ TextMarkerBasic anchor = stream.getFirstBasicInWindow(a, stream.getUnfilteredBasicIterator());
+ stream.addAnnotation(anchor, a);
+ others.moveToNext();
+ }
+ return stream;
+ }
+
+ private void initializeScript(CAS cas) throws AnalysisEngineProcessException {
+ String scriptLocation = locate(mainScript, scriptPaths, ".tm");
+ try {
+ script = loadScript(scriptLocation, cas, null);
+ } catch (Exception e) {
+ throw new AnalysisEngineProcessException(e);
+ }
+
+ Map<String, TextMarkerModule> additionalScripts = new HashMap<String, TextMarkerModule>();
+ Map<String, AnalysisEngine> additionalEngines = new HashMap<String, AnalysisEngine>();
+
+ if (additionalEngineLocations != null) {
+ for (String eachEngineLocation : additionalEngineLocations) {
+ String location = locate(eachEngineLocation, descriptorPaths, ".xml");
+ try {
+ AnalysisEngine eachEngine = engineLoader.loadEngine(location);
+ additionalEngines.put(eachEngineLocation, eachEngine);
+ } catch (Exception e) {
+ throw new AnalysisEngineProcessException(e);
+ }
+ }
+ }
+
+ if (additionalScriptLocations != null) {
+ for (String add : additionalScriptLocations) {
+ recursiveLoadScript(add, additionalScripts, additionalEngines, cas);
+ }
+ }
+
+ for (TextMarkerModule each : additionalScripts.values()) {
+ each.setScriptDependencies(additionalScripts);
+ }
+ script.setScriptDependencies(additionalScripts);
+
+ for (TextMarkerModule each : additionalScripts.values()) {
+ each.setEngineDependencies(additionalEngines);
+ }
+ script.setEngineDependencies(additionalEngines);
+ }
+
+ public static void addSourceDocumentInformation(CAS cas, File each) {
+ Type sdiType = cas.getTypeSystem()
+ .getType("org.apache.uima.examples.SourceDocumentInformation");
+ if (sdiType != null) {
+ if (cas.getAnnotationIndex(sdiType).size() == 0) {
+ AnnotationFS sdi = cas.createAnnotation(sdiType, cas.getDocumentAnnotation().getBegin(),
+ cas.getDocumentAnnotation().getEnd());
+ Feature uriFeature = sdiType.getFeatureByBaseName("uri");
+ sdi.setStringValue(uriFeature, each.toURI().getPath());
+ cas.addFsToIndexes(sdi);
+ }
+ }
+ }
+
+ public static void removeSourceDocumentInformation(CAS cas) {
+ Type sdiType = cas.getTypeSystem()
+ .getType("org.apache.uima.examples.SourceDocumentInformation");
+ if (sdiType != null) {
+ AnnotationIndex<AnnotationFS> annotationIndex = cas.getAnnotationIndex(sdiType);
+ List<AnnotationFS> toRemove = new ArrayList<AnnotationFS>();
+ for (AnnotationFS annotationFS : annotationIndex) {
+ toRemove.add(annotationFS);
+ }
+ for (AnnotationFS annotationFS : toRemove) {
+ cas.removeFsFromIndexes(annotationFS);
+ }
+ }
+ }
+
+ public static String locate(String name, String[] paths, String suffix) {
+ return locate(name, paths, suffix, true);
+ }
+
+ public static String locate(String name, String[] paths, String suffix, boolean mustExist) {
+ if (name == null) {
+ return null;
+ }
+ name = name.replaceAll("[.]", "/");
+ for (String each : paths) {
+ File file = new File(each, name + suffix);
+ if (!mustExist || file.exists()) {
+ return file.getAbsolutePath();
+ }
+ }
+ return null;
+ }
+
+ private void recursiveLoadScript(String toLoad, Map<String, TextMarkerModule> additionalScripts,
+ Map<String, AnalysisEngine> additionalEngines, CAS cas)
+ throws AnalysisEngineProcessException {
+ String location = locate(toLoad, scriptPaths, ".tm");
+ try {
+ TypeSystemDescription localTSD = getLocalTSD(toLoad);
+ TextMarkerModule eachScript = loadScript(location, cas, localTSD);
+ additionalScripts.put(toLoad, eachScript);
+ for (String add : eachScript.getScripts().keySet()) {
+ if (!additionalScripts.containsKey(add)) {
+ recursiveLoadScript(add, additionalScripts, additionalEngines, cas);
+ }
+ }
+ Set<String> engineKeySet = eachScript.getEngines().keySet();
+ for (String eachEngineLocation : engineKeySet) {
+ if (!additionalEngines.containsKey(eachEngineLocation)) {
+ String engineLocation = locate(eachEngineLocation, descriptorPaths, ".xml");
+ try {
+ AnalysisEngine eachEngine = engineLoader.loadEngine(engineLocation);
+ additionalEngines.put(eachEngineLocation, eachEngine);
+ } catch (Exception e) {
+ throw new AnalysisEngineProcessException(e);
+ }
+ }
+ }
+ } catch (IOException e) {
+ throw new AnalysisEngineProcessException(e);
+ } catch (RecognitionException e) {
+ throw new AnalysisEngineProcessException(e);
+ } catch (InvalidXMLException e) {
+ throw new AnalysisEngineProcessException(e);
+ }
+ }
+
+ private TypeSystemDescription getLocalTSD(String toLoad) throws InvalidXMLException, IOException {
+ TypeSystemDescription localTSD = localTSDMap.get(toLoad);
+ if (localTSD == null) {
+ String locateTSD = locate(toLoad, descriptorPaths, "TypeSystem.xml", true);
+ if (locateTSD != null) {
+ localTSD = UIMAFramework.getXMLParser().parseTypeSystemDescription(
+ new XMLInputSource(locateTSD));
+ ResourceManager resMgr = UIMAFramework.newDefaultResourceManager();
+ resMgr.setDataPath(getDataPath());
+ localTSD.resolveImports(resMgr);
+ localTSDMap.put(toLoad, localTSD);
+ }
+ }
+ return localTSD;
+ }
+
+ private String getDataPath() {
+ String result = "";
+ String sep = System.getProperty("path.separator");
+ for (String each : descriptorPaths) {
+ result += each + sep;
+ }
+ result = result.substring(0, result.length() - 1);
+ return result;
+ }
+
+ private TextMarkerModule loadScript(String scriptLocation, CAS cas, 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);
+ String name = scriptFile.getName();
+ int lastIndexOf = name.lastIndexOf(".tm");
+ name = name.substring(0, lastIndexOf);
+ TextMarkerModule script = parser.file_input(name);
+ return script;
+ }
+
+ public TextMarkerExternalFactory getFactory() {
+ return factory;
+ }
+
+ public TextMarkerEngineLoader getEngineLoader() {
+ return engineLoader;
+ }
+
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/engine/TextMarkerEngine.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/engine/TextMarkerEngine.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/engine/TextMarkerModifier.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/engine/TextMarkerModifier.java?rev=1152824&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/engine/TextMarkerModifier.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/engine/TextMarkerModifier.java Mon Aug 1 15:24:44 2011
@@ -0,0 +1,173 @@
+package org.apache.uima.tm.textmarker.engine;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.uima.UimaContext;
+import org.apache.uima.analysis_component.JCasAnnotator_ImplBase;
+import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
+import org.apache.uima.cas.CAS;
+import org.apache.uima.cas.CASException;
+import org.apache.uima.cas.FSIterator;
+import org.apache.uima.cas.Type;
+import org.apache.uima.jcas.JCas;
+import org.apache.uima.jcas.tcas.Annotation;
+import org.apache.uima.resource.ResourceInitializationException;
+import org.apache.uima.tm.textmarker.kernel.type.TextMarkerBasic;
+import org.apache.uima.tools.stylemap.StyleMapEntry;
+
+
+public class TextMarkerModifier extends JCasAnnotator_ImplBase {
+
+ private StyleMapFactory styleMapFactory;
+
+ private String styleMapLocation;
+
+ private UimaContext context;
+
+ private String[] enginePaths;
+
+ @Override
+ public void initialize(UimaContext aContext) throws ResourceInitializationException {
+ super.initialize(aContext);
+ if (aContext == null && context != null) {
+ aContext = context;
+ }
+ styleMapLocation = (String) aContext.getConfigParameterValue(TextMarkerEngine.STYLE_MAP);
+ enginePaths = (String[]) aContext.getConfigParameterValue(TextMarkerEngine.DESCRIPTOR_PATHS);
+ styleMapFactory = new StyleMapFactory();
+ this.context = aContext;
+ }
+
+ private String locate(String name, String[] paths, String suffix, boolean mustExist) {
+ if (name == null) {
+ return null;
+ }
+ name = name.replaceAll("[.]", "/");
+ for (String each : paths) {
+ File file = new File(each, name + suffix);
+ if (!mustExist || file.exists()) {
+ return file.getAbsolutePath();
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public void process(JCas cas) throws AnalysisEngineProcessException {
+ try {
+ String viewName = cas.getViewName();
+ if (viewName == null) {
+ cas = cas.getView(CAS.NAME_DEFAULT_SOFA);
+ }
+ JCas modifiedView = null;
+ Iterator<?> viewIterator = cas.getViewIterator();
+ while (viewIterator.hasNext()) {
+ JCas each = (JCas) viewIterator.next();
+ if (each.getViewName().equals(TextMarkerEngine.MODIFIED_SOFA)) {
+ modifiedView = each;
+ break;
+ }
+ }
+
+ if (modifiedView == null) {
+ try {
+ modifiedView = cas.createView(TextMarkerEngine.MODIFIED_SOFA);
+ } catch (Exception e) {
+ modifiedView = cas.getView(TextMarkerEngine.MODIFIED_SOFA);
+ }
+ } else {
+ modifiedView = cas.getView(TextMarkerEngine.MODIFIED_SOFA);
+ }
+ String locate = locate(styleMapLocation, enginePaths, ".xml", true);
+ // System.out.println(locate + ": " + styleMapLocation + " in " + Arrays.asList(enginePaths));
+ try {
+ String modifiedDocument = getModifiedDocument(cas, locate);
+ modifiedView.setDocumentText(modifiedDocument);
+ } catch (Exception e) {
+ throw new AnalysisEngineProcessException(e);
+ }
+ } catch (CASException e) {
+ throw new AnalysisEngineProcessException(e);
+ }
+ }
+
+ private String getModifiedDocument(JCas cas, String styleMapLocation) {
+ String startTag = "<span style='background:&bgcolor'>";
+ String endTag = "</span>";
+ Map<String, StyleMapEntry> styleMap = styleMapFactory.parseStyleMapDOM(styleMapLocation);
+ List<Type> coloredTypes = new ArrayList<Type>();
+ if (styleMap != null) {
+ coloredTypes = getColoredTypes(cas, styleMap);
+ }
+ List<String> tokens = new ArrayList<String>();
+ FSIterator<Annotation> iterator = cas.getAnnotationIndex(TextMarkerBasic.type).iterator();
+ while (iterator.isValid()) {
+ TextMarkerBasic each = (TextMarkerBasic) iterator.get();
+ String replace = each.getReplacement() == null ? each.getCoveredText() : each
+ .getReplacement();
+ Type type = getColorType(each, coloredTypes, cas);
+ if (type != null && !"".equals(replace)) {
+ StyleMapEntry entry = styleMap.get(type.getName());
+ String backgroundColor = "#"
+ + Integer.toHexString(entry.getBackground().getRGB()).substring(2);
+ String newStartTag = startTag.replaceAll("&bgcolor", backgroundColor);
+ tokens.add(newStartTag);
+ tokens.add(replace);
+ tokens.add(endTag);
+ } else {
+ tokens.add(replace);
+ }
+ iterator.moveToNext();
+ }
+ StringBuilder sb = new StringBuilder();
+ for (String string : tokens) {
+ sb.append(string);
+ }
+ return sb.toString();
+ }
+
+ private Type getColorType(TextMarkerBasic basic, List<Type> coloredTypes, JCas cas) {
+ if (coloredTypes.isEmpty()) {
+ return null;
+ }
+ List<Type> colorTypes = new ArrayList<Type>();
+ for (Type each : coloredTypes) {
+ if (cas.getTypeSystem().subsumes(each, basic.getType()) || basic.isPartOf(each.getName())) {
+ colorTypes.add(each);
+ }
+ }
+ Type best = null;
+ if (colorTypes.isEmpty()) {
+ } else if (colorTypes.size() == 1) {
+ best = colorTypes.get(0);
+ } else {
+ // best = colorTypes.get(0);
+ }
+ return best;
+ }
+
+ private List<Type> getColoredTypes(JCas cas, Map<String, StyleMapEntry> styleMap) {
+ List<Type> result = new ArrayList<Type>();
+ for (String string : styleMap.keySet()) {
+ if (string == null)
+ continue;
+ Type type = cas.getTypeSystem().getType(string);
+ if (type != null) {
+ result.add(type);
+ }
+ }
+ Collections.sort(result, new Comparator<Type>() {
+ public int compare(Type o1, Type o2) {
+ return o1.getShortName().compareTo(o2.getShortName());
+ }
+ });
+ return result;
+ }
+
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/engine/TextMarkerModifier.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/engine/TextMarkerModifier.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/BlockApply.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/BlockApply.java?rev=1152824&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/BlockApply.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/BlockApply.java Mon Aug 1 15:24:44 2011
@@ -0,0 +1,45 @@
+package org.apache.uima.tm.textmarker.kernel;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.uima.tm.textmarker.kernel.rule.RuleApply;
+
+
+public class BlockApply extends ScriptApply {
+
+ public BlockApply(TextMarkerStatement tme) {
+ super(tme);
+ }
+
+ private List<ScriptApply> innerApplies = new ArrayList<ScriptApply>();
+
+ private RuleApply ruleApply;
+
+ public void add(ScriptApply apply) {
+ innerApplies.add(apply);
+ }
+
+ public List<ScriptApply> getInnerApplies() {
+ return innerApplies;
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder result = new StringBuilder();
+ for (ScriptApply each : getInnerApplies()) {
+ result.append(each.toString());
+ result.append("\n");
+ }
+ return result.toString() + " : " + hashCode();
+ }
+
+ public RuleApply getRuleApply() {
+ return ruleApply;
+ }
+
+ public void setRuleApply(RuleApply ruleApply) {
+ this.ruleApply = ruleApply;
+ }
+
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/BlockApply.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/BlockApply.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/FilterManager.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/FilterManager.java?rev=1152824&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/FilterManager.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/FilterManager.java Mon Aug 1 15:24:44 2011
@@ -0,0 +1,234 @@
+package org.apache.uima.tm.textmarker.kernel;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.uima.cas.CAS;
+import org.apache.uima.cas.CASException;
+import org.apache.uima.cas.ConstraintFactory;
+import org.apache.uima.cas.FSIntConstraint;
+import org.apache.uima.cas.FSIterator;
+import org.apache.uima.cas.FSMatchConstraint;
+import org.apache.uima.cas.FSTypeConstraint;
+import org.apache.uima.cas.FeaturePath;
+import org.apache.uima.cas.Type;
+import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.tm.textmarker.kernel.constraint.BasicTypeConstraint;
+import org.apache.uima.tm.textmarker.kernel.constraint.MarkupConstraint;
+import org.apache.uima.tm.textmarker.kernel.constraint.NotConstraint;
+import org.apache.uima.tm.type.MARKUP;
+
+
+public class FilterManager {
+
+ private final Collection<Type> defaultFilterTypes;
+
+ private final Collection<String> defaultRetainTags;
+
+ private final FSMatchConstraint additionalWindow;
+
+ private final AnnotationFS windowAnnotation;
+
+ private final Type windowType;
+
+ private Collection<Type> currentFilterTypes;
+
+ private Collection<Type> currentRetainTypes;
+
+ private Collection<String> currentFilterTags;
+
+ private Collection<String> currentRetainTags;
+
+ private ConstraintFactory cf;
+
+ private Type markupType;
+
+ public FilterManager(Collection<Type> filterTypes, Collection<String> filterTags, CAS cas) {
+ super();
+ this.defaultFilterTypes = filterTypes;
+ this.defaultRetainTags = filterTags;
+
+ currentFilterTypes = new ArrayList<Type>();
+ currentRetainTypes = new ArrayList<Type>();
+ currentFilterTags = new ArrayList<String>();
+ currentRetainTags = new ArrayList<String>();
+
+ cf = cas.getConstraintFactory();
+
+ this.windowAnnotation = null;
+ this.windowType = null;
+ this.additionalWindow = null;
+ try {
+ markupType = cas.getJCas().getCasType(MARKUP.type);
+ } catch (CASException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public FilterManager(Collection<Type> defaultFilterTypes, Collection<String> defaultFilterTags,
+ Collection<Type> filterTypes, Collection<Type> retainTypes,
+ Collection<String> filterTags, Collection<String> retainTags,
+ AnnotationFS windowAnnotation, Type windowType, CAS cas) {
+ super();
+ this.defaultFilterTypes = defaultFilterTypes;
+ this.defaultRetainTags = defaultFilterTags;
+
+ currentFilterTypes = new ArrayList<Type>(filterTypes);
+ currentRetainTypes = new ArrayList<Type>(retainTypes);
+ currentFilterTags = new ArrayList<String>(filterTags);
+ currentRetainTags = new ArrayList<String>(retainTags);
+
+ cf = cas.getConstraintFactory();
+
+ this.windowAnnotation = windowAnnotation;
+ this.windowType = windowType;
+ this.additionalWindow = createWindowConstraint(windowAnnotation, cas);
+ try {
+ markupType = cas.getJCas().getCasType(MARKUP.type);
+ } catch (CASException e) {
+ e.printStackTrace();
+ }
+ }
+
+ private FSMatchConstraint createWindowConstraint(AnnotationFS windowAnnotation, CAS cas) {
+ if (windowAnnotation == null)
+ return null;
+ FeaturePath beginFP = cas.createFeaturePath();
+ Type type = windowAnnotation.getType();
+ beginFP.addFeature(type.getFeatureByBaseName("begin"));
+ FSIntConstraint intConstraint = cf.createIntConstraint();
+ intConstraint.geq(windowAnnotation.getBegin());
+ FSMatchConstraint beginConstraint = cf.embedConstraint(beginFP, intConstraint);
+
+ FeaturePath endFP = cas.createFeaturePath();
+ endFP.addFeature(type.getFeatureByBaseName("end"));
+ intConstraint = cf.createIntConstraint();
+ intConstraint.leq(windowAnnotation.getEnd());
+ FSMatchConstraint endConstraint = cf.embedConstraint(endFP, intConstraint);
+
+ FSMatchConstraint windowConstraint = cf.and(beginConstraint, endConstraint);
+ return windowConstraint;
+ }
+
+ public FSMatchConstraint getDefaultConstraint() {
+ return createCurrentConstraint(true);
+ }
+
+ private FSMatchConstraint createCurrentConstraint(boolean windowConstraint) {
+ Set<Type> filterTypes = new HashSet<Type>();
+ filterTypes.addAll(defaultFilterTypes);
+ filterTypes.addAll(currentFilterTypes);
+ filterTypes.removeAll(currentRetainTypes);
+
+ Set<String> retainTags = new HashSet<String>();
+ retainTags.addAll(defaultRetainTags);
+ retainTags.addAll(currentRetainTags);
+ retainTags.removeAll(currentFilterTags);
+
+ FSMatchConstraint typeConstraint = createTypeConstraint(filterTypes);
+ FSMatchConstraint markupConstraint = createTagConstraint(retainTags);
+
+ FSMatchConstraint constraint = cf.or(new NotConstraint(typeConstraint), markupConstraint);
+ if (additionalWindow != null && windowConstraint) {
+ constraint = cf.and(additionalWindow, constraint);
+ }
+ return constraint;
+ }
+
+ private FSMatchConstraint createTagConstraint(Collection<String> tags) {
+ FSTypeConstraint constraint = cf.createTypeConstraint();
+ constraint.add(markupType);
+ MarkupConstraint result = new MarkupConstraint(constraint);
+ for (String string : tags) {
+ result.addTag(string);
+ }
+ return result;
+ }
+
+ private FSMatchConstraint createTypeConstraint(Collection<Type> types) {
+ List<String> typeList = new ArrayList<String>();
+ for (Type each : types) {
+ typeList.add(each.getName());
+ }
+ BasicTypeConstraint result = new BasicTypeConstraint(cf.createTypeConstraint(), typeList, null);
+ for (Type each : types) {
+ result.add(each);
+ }
+ return result;
+ }
+
+ public void retainTypes(List<Type> list) {
+ currentRetainTypes = list;
+ }
+
+ public void filterTypes(List<Type> list) {
+ currentFilterTypes = list;
+ }
+
+ public void retainTags(List<String> list) {
+ currentRetainTags = list;
+ }
+
+ public void filterTags(List<String> list) {
+ currentFilterTags = list;
+ }
+
+ public Collection<Type> getDefaultFilterTypes() {
+ return defaultFilterTypes;
+ }
+
+ public Collection<String> getDefaultRetainTags() {
+ return defaultRetainTags;
+ }
+
+ public FSMatchConstraint getAdditionalWindow() {
+ return additionalWindow;
+ }
+
+ public Collection<Type> getCurrentFilterTypes() {
+ return currentFilterTypes;
+ }
+
+ public Collection<Type> getCurrentRetainTypes() {
+ return currentRetainTypes;
+ }
+
+ public Collection<String> getCurrentFilterTags() {
+ return currentFilterTags;
+ }
+
+ public Collection<String> getCurrentRetainTags() {
+ return currentRetainTags;
+ }
+
+ public AnnotationFS getWindowAnnotation() {
+ return windowAnnotation;
+ }
+
+ public Type getWindowType() {
+ return windowType;
+ }
+
+ public FSIterator<AnnotationFS> createFilteredIterator(CAS cas, FSIterator<AnnotationFS> basic,
+ Type basicType) {
+ if (windowAnnotation != null) {
+ FSIterator<AnnotationFS> windowIt = cas.getAnnotationIndex(basicType).subiterator(
+ windowAnnotation);
+ FSIterator<AnnotationFS> iterator = cas.createFilteredIterator(windowIt,
+ createCurrentConstraint(false));
+ // FSIterator<AnnotationFS> iterator = cas.createFilteredIterator(basic,
+ // createCurrentConstraint(false));
+ return iterator;
+ } else {
+ // FSIterator<AnnotationFS> iterator = cas.createFilteredIterator(basic,
+ // createCurrentConstraint(false));
+ FSIterator<AnnotationFS> iterator = cas.createFilteredIterator(cas.getAnnotationIndex(
+ basicType).iterator(), createCurrentConstraint(false));
+ return iterator;
+ }
+ }
+
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/FilterManager.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/FilterManager.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/ScriptApply.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/ScriptApply.java?rev=1152824&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/ScriptApply.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/ScriptApply.java Mon Aug 1 15:24:44 2011
@@ -0,0 +1,18 @@
+package org.apache.uima.tm.textmarker.kernel;
+
+public abstract class ScriptApply {
+
+ public static int count = 0;
+
+ private final TextMarkerStatement tme;
+
+ public ScriptApply(TextMarkerStatement tme) {
+ super();
+ this.tme = tme;
+ }
+
+ public TextMarkerStatement getElement() {
+ return tme;
+ }
+
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/ScriptApply.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/ScriptApply.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/TextMarkerAutomataBlock.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/TextMarkerAutomataBlock.java?rev=1152824&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerAutomataBlock.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerAutomataBlock.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 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);
+ }
+
+ @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/TextMarkerAutomataBlock.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerAutomataBlock.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/TextMarkerAutomataFactory.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/TextMarkerAutomataFactory.java?rev=1152824&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerAutomataFactory.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerAutomataFactory.java Mon Aug 1 15:24:44 2011
@@ -0,0 +1,17 @@
+package org.apache.uima.tm.textmarker.kernel;
+
+import java.util.List;
+
+import org.antlr.runtime.Token;
+import org.apache.uima.cas.CAS;
+import org.apache.uima.tm.textmarker.kernel.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);
+ }
+
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerAutomataFactory.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerAutomataFactory.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/TextMarkerBlock.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/TextMarkerBlock.java?rev=1152824&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerBlock.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerBlock.java Mon Aug 1 15:24:44 2011
@@ -0,0 +1,71 @@
+package org.apache.uima.tm.textmarker.kernel;
+
+import java.util.List;
+
+import org.apache.uima.cas.CAS;
+import org.apache.uima.tm.textmarker.kernel.rule.TextMarkerRule;
+
+
+public abstract class TextMarkerBlock extends TextMarkerStatement {
+
+ protected final String id;
+
+ protected TextMarkerEnvironment environment;
+
+ protected TextMarkerRule rule;
+
+ protected List<TextMarkerStatement> elements;
+
+ private String namespace;
+
+ private TextMarkerModule script;
+
+ public TextMarkerBlock(String id, TextMarkerRule rule, List<TextMarkerStatement> elements,
+ TextMarkerBlock parent, String defaultNamespace, CAS cas) {
+ super(parent);
+ this.id = id;
+ this.rule = rule;
+ this.elements = elements;
+ this.environment = new TextMarkerEnvironment(cas, this);
+ this.namespace = defaultNamespace;
+ }
+
+ public TextMarkerRule getRule() {
+ return rule;
+ }
+
+ @Override
+ public TextMarkerEnvironment getEnvironment() {
+ return environment;
+ }
+
+ public List<TextMarkerStatement> getElements() {
+ return elements;
+ }
+
+ public void setElements(List<TextMarkerStatement> elements) {
+ this.elements = elements;
+ }
+
+ public TextMarkerModule getScript() {
+ // TODO refactor!
+ if (getParent() != null) {
+ return getParent().getScript();
+ } else {
+ return script;
+ }
+ }
+
+ public void setScript(TextMarkerModule script) {
+ this.script = script;
+ }
+
+ public String getNamespace() {
+ return namespace;
+ }
+
+ public String getId() {
+ return id;
+ }
+
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerBlock.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerBlock.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/TextMarkerElement.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/TextMarkerElement.java?rev=1152824&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerElement.java (added)
+++ uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerElement.java Mon Aug 1 15:24:44 2011
@@ -0,0 +1,5 @@
+package org.apache.uima.tm.textmarker.kernel;
+
+public class TextMarkerElement {
+
+}
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerElement.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/org.apache.uima.tm.textmarker.engine/src/main/java/org/apache/uima/tm/textmarker/kernel/TextMarkerElement.java
------------------------------------------------------------------------------
svn:mime-type = text/plain