You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by pk...@apache.org on 2012/02/13 18:45:05 UTC
svn commit: r1243619 [17/25] - in /uima/sandbox/trunk/TextMarker:
uimaj-ep-textmarker-addons/
uimaj-ep-textmarker-addons/src/main/java/org/apache/uima/textmarker/addons/
uimaj-ep-textmarker-addons/src/main/java/org/apache/uima/textmarker/explain/apply/...
Added: uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/resource/TextNode.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/resource/TextNode.java?rev=1243619&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/resource/TextNode.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/resource/TextNode.java Mon Feb 13 17:44:52 2012
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+*/
+
+package org.apache.uima.textmarker.resource;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class TextNode {
+
+ private char value;
+
+ private boolean isWordEnd;
+
+ private Map<Character, TextNode> children;
+
+ public TextNode() {
+ this.value = ' ';
+ this.isWordEnd = false;
+ this.children = new HashMap<Character, TextNode>();
+ }
+
+ public TextNode(char value, boolean isWordEnd) {
+ this.value = value;
+ this.isWordEnd = isWordEnd;
+ this.children = new HashMap<Character, TextNode>(5);
+ }
+
+ public boolean contains(char c) {
+ if (value == c) {
+ return true;
+ }
+ return false;
+ }
+
+ public void addChild(char c, boolean isWordEnd) {
+ children.put(c, new TextNode(c, isWordEnd));
+ }
+
+ public void addChild(TextNode n) {
+ children.put(n.getValue(), n);
+ }
+
+ public void setWordEnd(boolean b) {
+ this.isWordEnd = b;
+ }
+
+ public TextNode getChildNode(char c) {
+ return children.get(c);
+ }
+
+ public char getValue() {
+ return this.value;
+ }
+
+ public boolean isWordEnd() {
+ return this.isWordEnd;
+ }
+
+ public Map<Character, TextNode> getChildren() {
+ return this.children;
+ }
+}
Added: uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/resource/TreeWordList.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/resource/TreeWordList.java?rev=1243619&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/resource/TreeWordList.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/resource/TreeWordList.java Mon Feb 13 17:44:52 2012
@@ -0,0 +1,367 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.uima.textmarker.resource;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Scanner;
+
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+
+import org.apache.uima.cas.FSIterator;
+import org.apache.uima.cas.Type;
+import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.jcas.tcas.Annotation;
+import org.apache.uima.textmarker.TextMarkerStream;
+import org.apache.uima.textmarker.type.TextMarkerBasic;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+import org.xml.sax.XMLReader;
+
+public class TreeWordList implements TextMarkerWordList {
+ private TextNode root;
+
+ private String name;
+
+ /**
+ * Default constructor
+ */
+ public TreeWordList() {
+ this.root = null;
+ }
+
+ /**
+ * Constructs a TreeWordList from a file with path = filename
+ *
+ * @param filename
+ * path of the file to create a TextWordList from
+ */
+ public TreeWordList(String pathname) {
+ if (pathname.endsWith(".twl")) {
+ readXML(pathname, "UTF-8");
+ }
+ if (pathname.endsWith(".txt")) {
+ buildNewTree(pathname);
+ }
+ this.name = new File(pathname).getName();
+ }
+
+ public TreeWordList(List<String> data) {
+ buildNewTree(data);
+ name = "local";
+ }
+
+ public void buildNewTree(List<String> data) {
+ this.root = new TextNode();
+ for (String s : data) {
+ addWord(s);
+ }
+ }
+
+ /**
+ * Creates a new Tree in the existing treeWordList from a file with path pathname
+ *
+ * @param pathname
+ * Absolut path of the file containing the word for the treeWordList
+ */
+ public void buildNewTree(String pathname) {
+ try {
+ // reading the file
+ File f = new File(pathname);
+ FileInputStream fstream = new FileInputStream(f);
+ Scanner scan = new Scanner(fstream, "UTF-8");
+ // creating a new tree
+ this.root = new TextNode();
+ while (scan.hasNextLine()) {
+ String s = scan.nextLine().trim();
+
+ if (s.endsWith("=")) {
+ s = s.substring(0, s.length() - 1);
+ s = s.trim();
+ }
+ addWord(s);
+ }
+ fstream.close();
+
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Returns the root node of the tree
+ *
+ * @return the root node
+ */
+ public TextNode getRoot() {
+ return this.root;
+ }
+
+ /**
+ * Add a new String into the TreeWordList
+ *
+ * @param s
+ * The String to add
+ */
+ public void addWord(String s) {
+ // Create Nodes from all chars of the strings besides the last one
+ TextNode pointer = root;
+ for (Character each : s.toCharArray()) {
+ if (Character.isWhitespace(each))
+ continue;
+ TextNode childNode = pointer.getChildNode(each);
+ if (childNode == null) {
+ childNode = new TextNode(each, false);
+ pointer.addChild(childNode);
+ }
+ pointer = childNode;
+ }
+ pointer.setWordEnd(s.length() > 0);
+ }
+
+ /**
+ * Checks if TreeWordList contains String s
+ */
+ public boolean contains(String s, boolean ignoreCase, int size, char[] ignoreChars,
+ int maxIgnoreChars) {
+ TextNode pointer = root;
+ return recursiveContains(pointer, s, 0, ignoreCase && s.length() > size, false, ignoreChars,
+ maxIgnoreChars);
+ }
+
+ public boolean containsFragment(String s, boolean ignoreCase, int size, char[] ignoreChars,
+ int maxIgnoreChars) {
+ TextNode pointer = root;
+ return recursiveContains(pointer, s, 0, ignoreCase && s.length() > size, true, ignoreChars,
+ maxIgnoreChars);
+ }
+
+ private boolean recursiveContains(TextNode pointer, String text, int index, boolean ignoreCase,
+ boolean fragment, char[] ignoreChars, int maxIgnoreChars) {
+ if (pointer == null) {
+ return false;
+ }
+ if (index == text.length()) {
+ return fragment || pointer.isWordEnd();
+ }
+ char charAt = text.charAt(index);
+ boolean charAtIgnored = false;
+ if (ignoreChars != null) {
+ for (char each : ignoreChars) {
+ if (each == charAt) {
+ charAtIgnored = true;
+ break;
+ }
+ }
+ charAtIgnored &= index != 0;
+ }
+ int next = ++index;
+
+ if (ignoreCase) {
+ TextNode childNodeL = pointer.getChildNode(Character.toLowerCase(charAt));
+ TextNode childNodeU = pointer.getChildNode(Character.toUpperCase(charAt));
+ if (charAtIgnored && childNodeL == null && childNodeU == null) {
+ return recursiveContains(pointer, text, next, ignoreCase, fragment, ignoreChars,
+ maxIgnoreChars);
+ } else {
+ return recursiveContains(childNodeL, text, next, ignoreCase, fragment, ignoreChars,
+ maxIgnoreChars)
+ | recursiveContains(childNodeU, text, next, ignoreCase, fragment, ignoreChars,
+ maxIgnoreChars);
+ }
+ } else {
+ TextNode childNode = pointer.getChildNode(charAt);
+ if (charAtIgnored && childNode == null) {
+ return recursiveContains(pointer, text, next, ignoreCase, fragment, ignoreChars,
+ maxIgnoreChars);
+ } else {
+ return recursiveContains(childNode, text, next, ignoreCase, fragment, ignoreChars,
+ maxIgnoreChars);
+ }
+ }
+ }
+
+ public List<AnnotationFS> find(TextMarkerStream stream, boolean ignoreCase, int size,
+ char[] ignoreChars, int maxIgnoredChars) {
+ ArrayList<AnnotationFS> results = new ArrayList<AnnotationFS>();
+ stream.moveToFirst();
+ FSIterator<AnnotationFS> streamPointer = stream.copy();
+ while (stream.isValid()) {
+ TextMarkerBasic anchorBasic = (TextMarkerBasic) stream.get();
+ streamPointer.moveTo(anchorBasic);
+
+ List<TextMarkerBasic> basicsToAdd = new ArrayList<TextMarkerBasic>();
+ basicsToAdd.add(anchorBasic);
+ String text = anchorBasic.getCoveredText();
+ StringBuilder candidate = new StringBuilder(text);
+ // String lastCandidate = candidate.toString();
+ Annotation interResult = null;
+ while (streamPointer.isValid()) {
+ if (containsFragment(candidate.toString(), ignoreCase, size, ignoreChars, maxIgnoredChars)) {
+ streamPointer.moveToNext();
+ if (streamPointer.isValid()) {
+ TextMarkerBasic next = (TextMarkerBasic) streamPointer.get();
+ if (contains(candidate.toString(), ignoreCase, size, ignoreChars, maxIgnoredChars)) {
+ interResult = new Annotation(stream.getJCas(), basicsToAdd.get(0).getBegin(),
+ basicsToAdd.get(basicsToAdd.size() - 1).getEnd());
+ }
+ // lastCandidate = candidate.toString();
+ candidate.append(next.getCoveredText());
+ basicsToAdd.add(next);
+ } else {
+ tryToCreateAnnotation(stream, ignoreCase, size, results, basicsToAdd,
+ candidate.toString(), interResult, ignoreChars, maxIgnoredChars);
+ }
+ } else {
+ basicsToAdd.remove(basicsToAdd.size() - 1);
+ tryToCreateAnnotation(stream, ignoreCase, size, results, basicsToAdd,
+ candidate.toString(), interResult, ignoreChars, maxIgnoredChars);
+ break;
+ }
+
+ }
+ stream.moveToNext();
+ }
+ return results;
+ }
+
+ public List<AnnotationFS> find(TextMarkerStream stream, boolean ignoreCase, int size) {
+ return find(stream, ignoreCase, size, null, 0);
+ }
+
+ private void tryToCreateAnnotation(TextMarkerStream stream, boolean ignoreCase, int size,
+ ArrayList<AnnotationFS> results, List<TextMarkerBasic> basicsToAdd, String lastCandidate,
+ Annotation interResult, char[] ignoreChars, int maxIgnoredChars) {
+ if (basicsToAdd.size() >= 1
+ && contains(lastCandidate, ignoreCase, size, ignoreChars, maxIgnoredChars)) {
+
+ results.add(new Annotation(stream.getJCas(), basicsToAdd.get(0).getBegin(), basicsToAdd.get(
+ basicsToAdd.size() - 1).getEnd()));
+ } else if (interResult != null) {
+ results.add(interResult);
+ }
+ }
+
+ public void readXML(String path, String encoding) {
+ try {
+ FileInputStream input = new FileInputStream(path);
+ InputStreamReader stream = new InputStreamReader(input, encoding);
+ this.root = new TextNode();
+ XMLEventHandler handler = new XMLEventHandler(root);
+ SAXParserFactory factory = SAXParserFactory.newInstance();
+ SAXParser parser = factory.newSAXParser();
+ XMLReader reader = parser.getXMLReader();
+ // XMLReader reader = XMLReaderFactory.createXMLReader();
+ reader.setContentHandler(handler);
+ reader.setErrorHandler(handler);
+ reader.parse(new InputSource(stream));
+ } catch (SAXParseException spe) {
+ StringBuffer sb = new StringBuffer(spe.toString());
+ sb.append("\n Line number: " + spe.getLineNumber());
+ sb.append("\n Column number: " + spe.getColumnNumber());
+ sb.append("\n Public ID: " + spe.getPublicId());
+ sb.append("\n System ID: " + spe.getSystemId() + "\n");
+ System.out.println(sb.toString());
+ } catch (SAXException se) {
+ System.out.println("loadDOM threw " + se);
+ se.printStackTrace(System.out);
+ } catch (IOException e) {
+ e.printStackTrace();
+ } catch (ParserConfigurationException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public void createXMLFile(String path, String encoding) {
+ try {
+ FileOutputStream output = new FileOutputStream(path);
+ OutputStreamWriter writer = new OutputStreamWriter(output, encoding);
+ writer.write("<?xml version=\"1.0\" ?>");
+ writer.write("<root>");
+ for (TextNode child : root.getChildren().values()) {
+ writeNode(writer, child);
+ }
+ writer.write("</root>");
+ writer.close();
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public void writeNode(Writer writer, TextNode node) {
+ try {
+ String output = "<node char=\"" + node.getValue() + "\" isWordEnd=\""
+ + Boolean.toString(node.isWordEnd()) + "\">";
+ writer.write(output);
+ for (TextNode child : node.getChildren().values()) {
+ writeNode(writer, child);
+
+ }
+ writer.write("</node>");
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ @Override
+ public String toString() {
+ return name;
+ }
+
+ public List<AnnotationFS> find(TextMarkerStream stream, Map<String, Type> typeMap,
+ boolean ignoreCase, int ignoreLength, boolean edit, double distance, String ignoreToken) {
+ return null;
+ }
+
+ public List<String> contains(String string, boolean ignoreCase, int ignoreLength, boolean edit,
+ double distance, String ignoreToken) {
+ return null;
+ }
+
+ public List<String> containsFragment(String string, boolean ignoreCase, int ignoreLength,
+ boolean edit, double distance, String ignoreToken) {
+ return null;
+ }
+
+ public void startDocument() {
+
+ }
+
+ public void endDocument() {
+
+ }
+
+}
Added: uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/resource/TrieXMLEventHandler.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/resource/TrieXMLEventHandler.java?rev=1243619&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/resource/TrieXMLEventHandler.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/resource/TrieXMLEventHandler.java Mon Feb 13 17:44:52 2012
@@ -0,0 +1,113 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+*/
+
+package org.apache.uima.textmarker.resource;
+
+import java.util.Stack;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.helpers.DefaultHandler;
+
+public class TrieXMLEventHandler extends DefaultHandler {
+
+ private Stack<MultiTextNode> stack;
+
+ // boolean listeningState;
+
+ boolean inContent;
+
+ boolean inType;
+
+ public TrieXMLEventHandler(MultiTextNode root) {
+ super();
+ this.stack = new Stack<MultiTextNode>();
+ stack.add(root);
+ // this.listeningState = false;
+ }
+
+ @Override
+ public void startDocument() {
+ }
+
+ @Override
+ public void endDocument() {
+ }
+
+ @Override
+ public void startElement(String namespaceURI, String localName, String qualifiedName,
+ Attributes atts) {
+ if ("n".equals(localName) || "n".equals(qualifiedName)) {
+ // char c = atts.getValue("c").charAt(0);
+ // boolean e = Boolean.valueOf(atts.getValue("e"));
+ MultiTextNode newNode = new MultiTextNode();
+ // newNode.setWordEnd(e);
+ // stack.peek().addChild(newNode);
+ stack.add(newNode);
+ inContent = false;
+ inType = false;
+ }
+ if ("t".equals(localName) || "t".equals(qualifiedName)) {
+ inType = true;
+ inContent = false;
+ }
+ if ("c".equals(localName) || "c".equals(qualifiedName)) {
+ // listeningState = true;
+ inType = false;
+ inContent = true;
+ }
+ }
+
+ @Override
+ public void endElement(String namespaceURI, String localName, String qualifiedName) {
+ if ("n".equals(localName) || "n".equals(qualifiedName)) {
+ MultiTextNode pop = stack.pop();
+ stack.peek().addChild(pop);
+ }
+ if ("t".equals(localName) || "t".equals(qualifiedName)) {
+ inType = false;
+ }
+ if ("c".equals(localName) || "c".equals(qualifiedName)) {
+ inContent = false;
+ }
+ }
+
+ @Override
+ public void characters(char ch[], int start, int length) {
+ // if (listeningState == true) {
+ if (stack.isEmpty()) {
+ return;
+ }
+ MultiTextNode peek = stack.peek();
+ if (inType) {
+ StringBuilder type = new StringBuilder();
+ for (int i = start; i < start + length; i++) {
+ type.append(String.valueOf(ch[i]));
+ }
+ peek.addType(type.toString());
+ } else if (inContent) {
+ if (ch.length > 0) {
+ peek.setValue(ch[0]);
+ } else {
+ peek.setValue(' ');
+ }
+ }
+ // }
+
+ }
+}
Added: uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/resource/XMLEventHandler.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/resource/XMLEventHandler.java?rev=1243619&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/resource/XMLEventHandler.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/resource/XMLEventHandler.java Mon Feb 13 17:44:52 2012
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+*/
+
+package org.apache.uima.textmarker.resource;
+
+import java.util.Stack;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.helpers.DefaultHandler;
+
+public class XMLEventHandler extends DefaultHandler {
+
+ Stack<TextNode> stack;
+
+ public XMLEventHandler(TextNode root) {
+ super();
+ stack = new Stack<TextNode>();
+ stack.add(root);
+ }
+
+ @Override
+ public void startDocument() {
+ }
+
+ @Override
+ public void endDocument() {
+ }
+
+ @Override
+ public void startElement(String namespaceURI, String localName, String qualifiedName,
+ Attributes atts) {
+ if ("node".equals(localName) || "node".equals(qualifiedName)) {
+ char c = atts.getValue("char").charAt(0);
+ boolean isWordEnd = Boolean.valueOf(atts.getValue("isWordEnd"));
+ TextNode newNode = new TextNode(c, isWordEnd);
+ stack.peek().addChild(newNode);
+ stack.add(newNode);
+ }
+
+ }
+
+ @Override
+ public void endElement(String namespaceURI, String localName, String qualifiedName) {
+ if ("node".equals(localName) || "node".equals(qualifiedName)) {
+ stack.pop();
+ }
+ }
+
+}
Added: uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/AbstractRuleElement.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/AbstractRuleElement.java?rev=1243619&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/AbstractRuleElement.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/AbstractRuleElement.java Mon Feb 13 17:44:52 2012
@@ -0,0 +1,166 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.uima.textmarker.rule;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.uima.textmarker.TextMarkerBlock;
+import org.apache.uima.textmarker.TextMarkerElement;
+import org.apache.uima.textmarker.TextMarkerStream;
+import org.apache.uima.textmarker.action.AbstractTextMarkerAction;
+import org.apache.uima.textmarker.condition.AbstractTextMarkerCondition;
+import org.apache.uima.textmarker.rule.quantifier.NormalQuantifier;
+import org.apache.uima.textmarker.rule.quantifier.RuleElementQuantifier;
+import org.apache.uima.textmarker.visitor.InferenceCrowd;
+
+public abstract class AbstractRuleElement extends TextMarkerElement implements RuleElement {
+
+ protected RuleElementQuantifier quantifier;
+
+ protected List<AbstractTextMarkerCondition> conditions;
+
+ protected List<AbstractTextMarkerAction> actions;
+
+ private RuleElementContainer container;
+
+ protected TextMarkerBlock parent;
+
+ public AbstractRuleElement(RuleElementQuantifier quantifier,
+ List<AbstractTextMarkerCondition> conditions, List<AbstractTextMarkerAction> actions,
+ RuleElementContainer container, TextMarkerBlock parent) {
+ super();
+ this.quantifier = quantifier;
+ this.conditions = conditions;
+ this.actions = actions;
+ this.container = container;
+ this.parent = parent;
+ if (this.conditions == null) {
+ this.conditions = new ArrayList<AbstractTextMarkerCondition>();
+ }
+ if (this.actions == null) {
+ this.actions = new ArrayList<AbstractTextMarkerAction>();
+ }
+ if (this.quantifier == null) {
+ this.quantifier = new NormalQuantifier();
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ protected final InferenceCrowd emptyCrowd = new InferenceCrowd(Collections.EMPTY_LIST);
+
+ protected void doneMatching(RuleMatch ruleMatch, RuleApply ruleApply, TextMarkerStream stream,
+ InferenceCrowd crowd) {
+ if (!ruleMatch.isApplied()) {
+ ruleApply.add(ruleMatch);
+ if (ruleMatch.matchedCompletely()) {
+ ruleMatch.getRule().getRoot().applyRuleElements(ruleMatch, stream, crowd);
+ }
+ ruleMatch.setApplied(true);
+ }
+ }
+
+ public void apply(RuleMatch ruleMatch, TextMarkerStream stream, InferenceCrowd crowd) {
+ for (AbstractTextMarkerAction action : actions) {
+ crowd.beginVisit(action, null);
+ action.execute(ruleMatch, this, stream, crowd);
+ crowd.endVisit(action, null);
+ }
+ }
+
+ protected List<RuleElementMatch> getMatch(RuleMatch ruleMatch,
+ ComposedRuleElementMatch containerMatch) {
+ List<RuleElementMatch> matchInfo;
+ if (containerMatch != null) {
+ matchInfo = containerMatch.getInnerMatches().get(this);
+ } else {
+ matchInfo = ruleMatch.getMatchInfo(this).get(0);
+ }
+ return matchInfo;
+ }
+
+ public List<RuleElementMatch> evaluateMatches(List<RuleElementMatch> matches,
+ TextMarkerBlock parent) {
+ return quantifier.evaluateMatches(matches, parent, emptyCrowd);
+ }
+
+ public List<Integer> getSelfIndexList() {
+ List<Integer> result = new ArrayList<Integer>(1);
+ if (getContainer() == null) {
+ return null;
+ }
+ int indexOf = getContainer().getRuleElements().indexOf(this);
+ result.add(indexOf + 1);
+ return result;
+ }
+
+ public boolean hasAncestor(boolean after) {
+ RuleElementContainer c = getContainer();
+ if (c == null) {
+ return false;
+ }
+ RuleElement nextElement = c.getNextElement(after, this);
+ if (nextElement != null) {
+ return true;
+ }
+ if (c instanceof ComposedRuleElement) {
+ return ((ComposedRuleElement) c).hasAncestor(after);
+ }
+ return false;
+ }
+
+ public RuleElementQuantifier getQuantifier() {
+ return quantifier;
+ }
+
+ public TextMarkerBlock getParent() {
+ return parent;
+ }
+
+ public List<AbstractTextMarkerCondition> getConditions() {
+ return conditions;
+ }
+
+ public void setConditions(List<AbstractTextMarkerCondition> conditions) {
+ this.conditions = conditions;
+ }
+
+ public List<AbstractTextMarkerAction> getActions() {
+ return actions;
+ }
+
+ public void setActions(List<AbstractTextMarkerAction> actions) {
+ this.actions = actions;
+ }
+
+ public void setQuantifier(RuleElementQuantifier quantifier) {
+ this.quantifier = quantifier;
+ }
+
+ public TextMarkerRule getRule() {
+ return container.getRule();
+ }
+
+ public RuleElementContainer getContainer() {
+ return container;
+ }
+
+}
Added: uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/AnnotationComparator.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/AnnotationComparator.java?rev=1243619&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/AnnotationComparator.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/AnnotationComparator.java Mon Feb 13 17:44:52 2012
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.uima.textmarker.rule;
+
+import java.util.Comparator;
+
+import org.apache.uima.cas.text.AnnotationFS;
+
+public class AnnotationComparator implements Comparator<AnnotationFS> {
+
+ public int compare(AnnotationFS o1, AnnotationFS o2) {
+ if (o1.getBegin() < o2.getBegin()) {
+ return -1;
+ } else if (o1.getBegin() > o2.getBegin()) {
+ return 1;
+ } else if (o1.getEnd() > o2.getEnd()) {
+ return 1;
+ } else if (o1.getEnd() < o2.getEnd()) {
+ return -1;
+ } else {
+ return o1.getType().getName().compareTo(o2.getType().getName());
+ }
+
+ }
+}
Added: uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/ComposedRuleElement.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/ComposedRuleElement.java?rev=1243619&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/ComposedRuleElement.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/ComposedRuleElement.java Mon Feb 13 17:44:52 2012
@@ -0,0 +1,257 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.uima.textmarker.rule;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.textmarker.TextMarkerBlock;
+import org.apache.uima.textmarker.TextMarkerStream;
+import org.apache.uima.textmarker.action.AbstractTextMarkerAction;
+import org.apache.uima.textmarker.condition.AbstractTextMarkerCondition;
+import org.apache.uima.textmarker.rule.quantifier.RuleElementQuantifier;
+import org.apache.uima.textmarker.visitor.InferenceCrowd;
+
+public class ComposedRuleElement extends AbstractRuleElement implements RuleElementContainer {
+
+ protected List<RuleElement> elements;
+
+ protected RuleElementContainer caretaker;
+
+ public ComposedRuleElement(List<RuleElement> elements, RuleElementQuantifier quantifier,
+ List<AbstractTextMarkerCondition> conditions, List<AbstractTextMarkerAction> actions,
+ RuleElementContainer container, TextMarkerBlock parent) {
+ super(quantifier, conditions, actions, container, parent);
+ this.elements = elements;
+ this.caretaker = new RuleElementCaretaker(this);
+ }
+
+ public void apply(RuleMatch match, TextMarkerStream symbolStream, InferenceCrowd crowd) {
+ applyRuleElements(match, symbolStream, crowd);
+ super.apply(match, symbolStream, crowd);
+ }
+
+ public void startMatch(RuleMatch ruleMatch, RuleApply ruleApply,
+ ComposedRuleElementMatch containerMatch, RuleElement entryPoint, TextMarkerStream stream,
+ InferenceCrowd crowd) {
+ RuleElement anchorElement = getAnchoringRuleElement(stream);
+ ComposedRuleElementMatch composedMatch = createComposedMatch(ruleMatch, containerMatch);
+ anchorElement.startMatch(ruleMatch, ruleApply, composedMatch, entryPoint, stream, crowd);
+ }
+
+ protected ComposedRuleElementMatch createComposedMatch(RuleMatch ruleMatch,
+ ComposedRuleElementMatch containerMatch) {
+ ComposedRuleElementMatch composedMatch = new ComposedRuleElementMatch(this, containerMatch);
+ includeMatch(ruleMatch, containerMatch, composedMatch);
+ return composedMatch;
+ }
+
+ public void continueMatch(boolean after, AnnotationFS annotation, RuleMatch ruleMatch,
+ RuleApply ruleApply, ComposedRuleElementMatch containerMatch,
+ TextMarkerRuleElement sideStepOrigin, RuleElement entryPoint, TextMarkerStream stream,
+ InferenceCrowd crowd) {
+ RuleElement nextElement = getNextElement(after, this);
+ if (nextElement != null) {
+ ComposedRuleElementMatch composedMatch = createComposedMatch(ruleMatch, containerMatch);
+ nextElement.continueMatch(after, annotation, ruleMatch, ruleApply, composedMatch,
+ sideStepOrigin, entryPoint, stream, crowd);
+ } else {
+ fallback(after, false, annotation, ruleMatch, ruleApply, containerMatch, sideStepOrigin,
+ entryPoint, stream, crowd);
+ }
+ }
+
+ public void continueOwnMatch(boolean after, AnnotationFS annotation, RuleMatch ruleMatch,
+ RuleApply ruleApply, ComposedRuleElementMatch containerMatch,
+ TextMarkerRuleElement sideStepOrigin, RuleElement entryPoint, TextMarkerStream stream,
+ InferenceCrowd crowd) {
+ // TODO minimize recursion call for greedy rule elements
+ RuleElement nextElement = getNextElement(after, this);
+ if (nextElement != null) {
+ ComposedRuleElementMatch composedMatch = createComposedMatch(ruleMatch, containerMatch);
+ nextElement.continueMatch(after, annotation, ruleMatch, ruleApply, composedMatch,
+ sideStepOrigin, entryPoint, stream, crowd);
+ } else {
+ fallback(after, false, annotation, ruleMatch, ruleApply, containerMatch, sideStepOrigin,
+ entryPoint, stream, crowd);
+ }
+ }
+
+ public void fallbackContinue(boolean after, boolean failed, AnnotationFS annotation,
+ RuleMatch ruleMatch, RuleApply ruleApply, ComposedRuleElementMatch containerMatch,
+ TextMarkerRuleElement sideStepOrigin, RuleElement entryPoint, TextMarkerStream stream,
+ InferenceCrowd crowd) {
+ RuleElementContainer container = getContainer();
+ doMatch(containerMatch, stream, crowd);
+ if (this.equals(entryPoint)) {
+ return;
+ }
+ if (container == null) {
+ fallback(after, failed, annotation, ruleMatch, ruleApply, containerMatch, sideStepOrigin,
+ entryPoint, stream, crowd);
+ } else {
+ ComposedRuleElementMatch parentContainerMatch = containerMatch.getContainerMatch();
+ RuleElement nextElement = container.getNextElement(after, this);
+ List<RuleElementMatch> match = getMatch(ruleMatch, parentContainerMatch);
+ List<RuleElementMatch> evaluateMatches = quantifier.evaluateMatches(match, parent, crowd);
+ ruleMatch.setMatched(ruleMatch.matched() && evaluateMatches != null);
+ if (failed) {
+ if (nextElement != null) {
+ AnnotationFS backtrackedAnnotation = getBacktrackedAnnotation(evaluateMatches);
+ if (backtrackedAnnotation != null) {
+ nextElement.continueMatch(after, backtrackedAnnotation, ruleMatch, ruleApply,
+ parentContainerMatch, sideStepOrigin, null, stream, crowd);
+ } else {
+ fallback(after, failed, annotation, ruleMatch, ruleApply, parentContainerMatch,
+ sideStepOrigin, entryPoint, stream, crowd);
+ }
+ } else {
+ fallback(after, failed, annotation, ruleMatch, ruleApply, parentContainerMatch,
+ sideStepOrigin, entryPoint, stream, crowd);
+ }
+ } else {
+ if (quantifier.continueMatch(after, annotation, this, ruleMatch, parentContainerMatch,
+ stream, crowd)) {
+ continueOwnMatch(after, annotation, ruleMatch, ruleApply, parentContainerMatch,
+ sideStepOrigin, null, stream, crowd);
+ } else if (nextElement != null) {
+ nextElement.continueMatch(after, annotation, ruleMatch, ruleApply, parentContainerMatch,
+ sideStepOrigin, null, stream, crowd);
+ } else {
+ fallback(after, failed, annotation, ruleMatch, ruleApply, parentContainerMatch,
+ sideStepOrigin, entryPoint, stream, crowd);
+ }
+ }
+ }
+ }
+
+ private AnnotationFS getBacktrackedAnnotation(List<RuleElementMatch> evaluateMatches) {
+ if (evaluateMatches == null) {
+ return null;
+ }
+ // TODO both directions!
+ List<AnnotationFS> textsMatched = evaluateMatches.get(evaluateMatches.size() - 1)
+ .getTextsMatched();
+ AnnotationFS backtrackedAnnotation = textsMatched.get(textsMatched.size() - 1);
+ return backtrackedAnnotation;
+ }
+
+ private void fallback(boolean after, boolean failed, AnnotationFS annotation,
+ RuleMatch ruleMatch, RuleApply ruleApply, ComposedRuleElementMatch containerMatch,
+ TextMarkerRuleElement sideStepOrigin, RuleElement entryPoint, TextMarkerStream stream,
+ InferenceCrowd crowd) {
+ RuleElementContainer parentContainer = getContainer();
+ if (parentContainer instanceof ComposedRuleElement) {
+ ComposedRuleElement parentElement = (ComposedRuleElement) parentContainer;
+ parentElement.fallbackContinue(after, failed, annotation, ruleMatch, ruleApply,
+ containerMatch, sideStepOrigin, entryPoint, stream, crowd);
+ } else if (sideStepOrigin != null) {
+ // System.out.println("SIDESTEP: " + sideStepOrigin);
+ sideStepOrigin.continueSideStep(after, ruleMatch, ruleApply, containerMatch, entryPoint,
+ stream, crowd);
+ } else {
+ doneMatching(ruleMatch, ruleApply, stream, crowd);
+ }
+ }
+
+ private void includeMatch(RuleMatch ruleMatch, ComposedRuleElementMatch containerMatch,
+ ComposedRuleElementMatch composedMatch) {
+ if (containerMatch == null) {
+ ruleMatch.setRootMatch(composedMatch);
+ } else {
+ containerMatch.addInnerMatch(this, composedMatch, false);
+ }
+ }
+
+ private void doMatch(ComposedRuleElementMatch match, TextMarkerStream stream, InferenceCrowd crowd) {
+ List<AnnotationFS> textsMatched = match.getTextsMatched();
+ if (textsMatched == null || textsMatched.isEmpty()) {
+ return;
+ }
+ int begin = textsMatched.get(0).getBegin();
+ int end = textsMatched.get(textsMatched.size() - 1).getEnd();
+ AnnotationFS annotation = stream.getCas().createAnnotation(stream.getCas().getAnnotationType(),
+ begin, end);
+
+ List<EvaluatedCondition> evaluatedConditions = new ArrayList<EvaluatedCondition>(
+ conditions.size());
+ for (AbstractTextMarkerCondition condition : conditions) {
+ crowd.beginVisit(condition, null);
+ EvaluatedCondition eval = condition.eval(annotation, this, stream, crowd);
+ crowd.endVisit(condition, null);
+ evaluatedConditions.add(eval);
+ }
+ match.setConditionInfo(evaluatedConditions);
+ match.evaluateInnerMatches(true);
+ }
+
+ public Collection<AnnotationFS> getAnchors(TextMarkerStream stream) {
+ RuleElement anchorElement = getAnchoringRuleElement(stream);
+ Collection<AnnotationFS> anchors = anchorElement.getAnchors(stream);
+ return anchors;
+ }
+
+ public int estimateAnchors(TextMarkerStream stream) {
+ int result = 1;
+ for (RuleElement each : elements) {
+ result += each.estimateAnchors(stream);
+ }
+ return result;
+ }
+
+ public RuleElement getAnchoringRuleElement(TextMarkerStream stream) {
+ return caretaker.getAnchoringRuleElement(stream);
+ }
+
+ public List<RuleElement> getRuleElements() {
+ return elements;
+ }
+
+ public void setRuleElements(List<RuleElement> elements) {
+ this.elements = elements;
+ }
+
+ public RuleElement getFirstElement() {
+ return caretaker.getFirstElement();
+ }
+
+ public RuleElement getLastElement() {
+ return caretaker.getLastElement();
+ }
+
+ public void applyRuleElements(RuleMatch ruleMatch, TextMarkerStream stream, InferenceCrowd crowd) {
+ caretaker.applyRuleElements(ruleMatch, stream, crowd);
+ }
+
+ public String toString() {
+ String simpleName = getQuantifier().getClass().getSimpleName();
+ return "(" + (elements == null ? "null" : elements.toString()) + ")"
+ + (simpleName.equals("NormalQuantifier") ? "" : simpleName)
+ + (conditions.isEmpty() ? "" : "(" + conditions.toString() + ")" + "\\n")
+ + (actions.isEmpty() ? "" : "{" + actions.toString() + "}");
+ }
+
+ public RuleElement getNextElement(boolean after, RuleElement ruleElement) {
+ return caretaker.getNextElement(after, ruleElement);
+ }
+
+}
Added: uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/ComposedRuleElementMatch.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/ComposedRuleElementMatch.java?rev=1243619&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/ComposedRuleElementMatch.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/ComposedRuleElementMatch.java Mon Feb 13 17:44:52 2012
@@ -0,0 +1,221 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.uima.textmarker.rule;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.TreeMap;
+import java.util.TreeSet;
+
+import org.apache.uima.cas.text.AnnotationFS;
+
+public class ComposedRuleElementMatch extends RuleElementMatch {
+
+ private Map<RuleElement, List<RuleElementMatch>> innerMatches;
+
+ public ComposedRuleElementMatch(RuleElement ruleElement, ComposedRuleElementMatch containerMatch) {
+ super(ruleElement, containerMatch);
+ baseConditionMatched = false;
+ ComposedRuleElement cre = (ComposedRuleElement) ruleElement;
+ innerMatches = new TreeMap<RuleElement, List<RuleElementMatch>>(new RuleElementComparator(cre));
+ List<RuleElement> ruleElements = cre.getRuleElements();
+ for (RuleElement eachRuleElement : ruleElements) {
+ innerMatches.put(eachRuleElement, null);
+ }
+ }
+
+ private void setInnerMatches(Map<RuleElement, List<RuleElementMatch>> innerMatches) {
+ this.innerMatches = innerMatches;
+ // textsMatched = new ArrayList<AnnotationFS>();
+ // boolean matched = true;
+ // for (Entry<RuleElement, List<RuleElementMatch>> entry : innerMatches.entrySet()) {
+ // for (RuleElementMatch ruleElementMatch : entry.getValue()) {
+ // List<AnnotationFS> list = ruleElementMatch.getTextsMatched();
+ // textsMatched.addAll(list);
+ // matched &= ruleElementMatch.matched();
+ // }
+ // }
+ // baseConditionMatched = matched;
+ // conditionsMatched = matched;
+ }
+
+ public Map<RuleElement, List<RuleElementMatch>> getInnerMatches() {
+ return innerMatches;
+ }
+
+ public void addInnerMatch(RuleElement ruleElement, RuleElementMatch ruleElementMatch) {
+ addInnerMatch(ruleElement, ruleElementMatch, true);
+ }
+
+ public void addInnerMatch(RuleElement ruleElement, RuleElementMatch ruleElementMatch,
+ boolean included) {
+ List<RuleElementMatch> list = innerMatches.get(ruleElement);
+ if (list == null) {
+ list = new ArrayList<RuleElementMatch>();
+ innerMatches.put(ruleElement, list);
+ }
+ list.add(ruleElementMatch);
+ textsMatched.addAll(ruleElementMatch.getTextsMatched());
+
+ evaluateInnerMatches(included);
+ }
+
+ public void evaluateInnerMatches(boolean included) {
+ boolean allDone = true;
+ Set<Entry<RuleElement, List<RuleElementMatch>>> entrySet = innerMatches.entrySet();
+ for (Entry<RuleElement, List<RuleElementMatch>> entry : entrySet) {
+ RuleElement element = entry.getKey();
+ List<RuleElementMatch> value = entry.getValue();
+ allDone &= (element.getQuantifier().isOptional(element.getParent()) || value != null);
+ if (value != null && !value.isEmpty() && included) {
+ for (RuleElementMatch ruleElementMatch : value) {
+ allDone &= ruleElementMatch.matched();
+ }
+ }
+ }
+ baseConditionMatched = allDone;
+ }
+
+ public ComposedRuleElementMatch copy() {
+ ComposedRuleElementMatch copy = new ComposedRuleElementMatch(ruleElement, containerMatch);
+ copy.setBaseConditionMatched(baseConditionMatched);
+ copy.setConditions(conditions);
+ copy.setConditionsMatched(conditionsMatched);
+ copy.setTextsMatched(textsMatched);
+ Map<RuleElement, List<RuleElementMatch>> newMap = new TreeMap<RuleElement, List<RuleElementMatch>>(
+ new RuleElementComparator((ComposedRuleElement) ruleElement));
+ for (Entry<RuleElement, List<RuleElementMatch>> entry : innerMatches.entrySet()) {
+ List<RuleElementMatch> value = entry.getValue();
+ if (value != null) {
+ List<RuleElementMatch> newValue = new ArrayList<RuleElementMatch>();
+ for (RuleElementMatch each : value) {
+ newValue.add(each.copy());
+ }
+ newMap.put(entry.getKey(), newValue);
+ } else {
+ newMap.put(entry.getKey(), null);
+ }
+ }
+ copy.setInnerMatches(newMap);
+ return copy;
+ }
+
+ public ComposedRuleElementMatch copy(ComposedRuleElementMatch extendedContainerMatch) {
+ ComposedRuleElementMatch copy = new ComposedRuleElementMatch(ruleElement, containerMatch);
+ copy.setBaseConditionMatched(baseConditionMatched);
+ copy.setConditions(conditions);
+ copy.setConditionsMatched(conditionsMatched);
+ copy.setTextsMatched(textsMatched);
+ Map<RuleElement, List<RuleElementMatch>> newMap = new TreeMap<RuleElement, List<RuleElementMatch>>(
+ new RuleElementComparator((ComposedRuleElement) ruleElement));
+ for (Entry<RuleElement, List<RuleElementMatch>> entry : innerMatches.entrySet()) {
+ RuleElement key = entry.getKey();
+ List<RuleElementMatch> value = entry.getValue();
+ if (key.equals(extendedContainerMatch.getRuleElement())) {
+ // if (value.isEmpty()) {
+ extendedContainerMatch.setContainerMatch(copy);
+ if (value != null) {
+ List<RuleElementMatch> newValue = new ArrayList<RuleElementMatch>();
+ newValue.addAll(value);
+ newValue.set(newValue.size() - 1, extendedContainerMatch);
+ newMap.put(extendedContainerMatch.getRuleElement(), newValue);
+ }
+ // } else {
+ // List<RuleElementMatch> newValue = new ArrayList<RuleElementMatch>();
+ // newValue.addAll(value);
+ // newValue.add(extendedContainerMatch);
+ // newMap.put(extendedContainerMatch.getRuleElement(), newValue);
+ // }
+ } else {
+ if (value != null) {
+ List<RuleElementMatch> newValue = new ArrayList<RuleElementMatch>();
+ for (RuleElementMatch each : value) {
+ if (each instanceof ComposedRuleElementMatch) {
+ newValue.add(((ComposedRuleElementMatch) each).copy(extendedContainerMatch));
+ } else {
+ newValue.add(each.copy());
+ }
+ }
+ newMap.put(entry.getKey(), newValue);
+ } else {
+ newMap.put(entry.getKey(), null);
+ }
+ }
+ }
+ copy.setInnerMatches(newMap);
+ return copy;
+ }
+
+ public void update(ComposedRuleElementMatch extendedContainerMatch) {
+ for (Entry<RuleElement, List<RuleElementMatch>> entry : innerMatches.entrySet()) {
+ RuleElement key = entry.getKey();
+ List<RuleElementMatch> value = entry.getValue();
+ if (key.equals(extendedContainerMatch.getRuleElement())) {
+ extendedContainerMatch.setContainerMatch(this);
+ if (value != null) {
+ value.set(value.size() - 1, extendedContainerMatch);
+ }
+ } else {
+ if (value != null) {
+ for (RuleElementMatch each : value) {
+ if (each instanceof ComposedRuleElementMatch) {
+ ((ComposedRuleElementMatch)each).update(extendedContainerMatch);
+ }
+ }
+ }
+ }
+ }
+ }
+
+
+
+ public String toString() {
+ return "CREM" + innerMatches.toString();
+ }
+
+ public List<AnnotationFS> getTextsMatched() {
+ // TODO find a better solution for this
+ Collection<AnnotationFS> set = new TreeSet<AnnotationFS>(new AnnotationComparator());
+ Collection<List<RuleElementMatch>> values = innerMatches.values();
+ for (List<RuleElementMatch> list : values) {
+ if (list != null) {
+ for (RuleElementMatch ruleElementMatch : list) {
+ set.addAll(ruleElementMatch.getTextsMatched());
+ }
+ }
+ }
+ return Arrays.asList(set.toArray(new AnnotationFS[0]));
+ }
+
+ public void setConditionInfo(List<EvaluatedCondition> evaluatedConditions) {
+ conditions = evaluatedConditions;
+ for (EvaluatedCondition each : conditions) {
+ conditionsMatched &= each.isValue();
+ }
+
+ }
+
+
+}
Added: uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/EvaluatedCondition.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/EvaluatedCondition.java?rev=1243619&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/EvaluatedCondition.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/EvaluatedCondition.java Mon Feb 13 17:44:52 2012
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.uima.textmarker.rule;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.uima.textmarker.condition.AbstractTextMarkerCondition;
+import org.apache.uima.textmarker.condition.NotCondition;
+
+public class EvaluatedCondition {
+
+ private final AbstractTextMarkerCondition condition;
+
+ private final boolean value;
+
+ private final List<EvaluatedCondition> conditions;
+
+ private final List<EvaluatedCondition> noConditions = new ArrayList<EvaluatedCondition>(0);
+
+ public EvaluatedCondition(AbstractTextMarkerCondition condition, boolean value,
+ List<EvaluatedCondition> conditions) {
+ super();
+ this.condition = condition;
+ this.value = value;
+ this.conditions = conditions;
+ }
+
+ public EvaluatedCondition(AbstractTextMarkerCondition condition, boolean value) {
+ super();
+ this.condition = condition;
+ this.value = value;
+ this.conditions = noConditions;
+ }
+
+ public EvaluatedCondition(NotCondition condition, boolean value, EvaluatedCondition eval) {
+ super();
+ this.condition = condition;
+ this.value = value;
+ this.conditions = new ArrayList<EvaluatedCondition>();
+ conditions.add(eval);
+ }
+
+ public AbstractTextMarkerCondition getCondition() {
+ return condition;
+ }
+
+ public boolean isValue() {
+ return value;
+ }
+
+ public List<EvaluatedCondition> getConditions() {
+ return conditions;
+ }
+
+}
Added: uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/RuleApply.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/RuleApply.java?rev=1243619&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/RuleApply.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/RuleApply.java Mon Feb 13 17:44:52 2012
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.uima.textmarker.rule;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.uima.textmarker.ScriptApply;
+
+public class RuleApply extends ScriptApply {
+
+ private int tried = 0;
+
+ private int applied = 0;
+
+ private boolean acceptMatches;
+
+ private List<RuleMatch> list;
+
+ public RuleApply(TextMarkerRule rule) {
+ this(rule, false);
+ }
+
+ public RuleApply(TextMarkerRule rule, boolean remember) {
+ super(rule);
+ list = new ArrayList<RuleMatch>();
+ acceptMatches = remember;
+ }
+
+ public void add(RuleMatch match) {
+ if (match.matchedCompletely()) {
+ applied++;
+ }
+ tried++;
+ if (acceptMatches) {
+ list.add(match);
+ }
+ }
+
+ public void addAll(List<RuleMatch> matches) {
+ for (RuleMatch ruleMatch : matches) {
+ add(ruleMatch);
+ }
+ }
+
+ public List<RuleMatch> getList() {
+ return list;
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder sb = new StringBuilder();
+ sb.append(((TextMarkerRule) getElement()).getId() + "\t");
+ sb.append(applied + "/" + tried + ":\t");
+ sb.append(getElement().toString());
+ return sb.toString();
+ }
+
+ public int getTried() {
+ return tried;
+ }
+
+ public int getApplied() {
+ return applied;
+ }
+
+ public boolean isAcceptMatches() {
+ return acceptMatches;
+ }
+
+ public void setAcceptMatches(boolean acceptMatches) {
+ this.acceptMatches = acceptMatches;
+ }
+}
Added: uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/RuleElement.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/RuleElement.java?rev=1243619&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/RuleElement.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/RuleElement.java Mon Feb 13 17:44:52 2012
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.uima.textmarker.rule;
+
+import java.util.Collection;
+import java.util.List;
+
+import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.textmarker.TextMarkerBlock;
+import org.apache.uima.textmarker.TextMarkerStream;
+import org.apache.uima.textmarker.action.AbstractTextMarkerAction;
+import org.apache.uima.textmarker.condition.AbstractTextMarkerCondition;
+import org.apache.uima.textmarker.rule.quantifier.RuleElementQuantifier;
+import org.apache.uima.textmarker.visitor.InferenceCrowd;
+
+public interface RuleElement {
+
+ void apply(RuleMatch match, TextMarkerStream stream, InferenceCrowd crowd);
+
+ void startMatch(RuleMatch ruleMatch, RuleApply ruleApply,
+ ComposedRuleElementMatch containerMatch, RuleElement entryPoint, TextMarkerStream stream,
+ InferenceCrowd crowd);
+
+ void continueMatch(boolean after, AnnotationFS annotation, RuleMatch ruleMatch,
+ RuleApply ruleApply, ComposedRuleElementMatch containerMatch,
+ TextMarkerRuleElement sideStepOrigin, RuleElement entryPoint, TextMarkerStream stream,
+ InferenceCrowd crowd);
+
+ void continueOwnMatch(boolean after, AnnotationFS annotation, RuleMatch ruleMatch,
+ RuleApply ruleApply, ComposedRuleElementMatch containerMatch,
+ TextMarkerRuleElement sideStepOrigin, RuleElement entryPoint, TextMarkerStream stream,
+ InferenceCrowd crowd);
+
+
+ List<RuleElementMatch> evaluateMatches(List<RuleElementMatch> matches, TextMarkerBlock parent);
+
+ Collection<AnnotationFS> getAnchors(TextMarkerStream symbolStream);
+
+ TextMarkerBlock getParent();
+
+ TextMarkerRule getRule();
+
+ RuleElementContainer getContainer();
+
+ RuleElementQuantifier getQuantifier();
+
+ int estimateAnchors(TextMarkerStream stream);
+
+ List<Integer> getSelfIndexList();
+
+ List<AbstractTextMarkerCondition> getConditions();
+
+ List<AbstractTextMarkerAction> getActions();
+
+ boolean hasAncestor(boolean after);
+}
+
Added: uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/RuleElementCaretaker.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/RuleElementCaretaker.java?rev=1243619&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/RuleElementCaretaker.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/RuleElementCaretaker.java Mon Feb 13 17:44:52 2012
@@ -0,0 +1,113 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.uima.textmarker.rule;
+
+import java.util.List;
+
+import org.apache.uima.textmarker.TextMarkerStream;
+import org.apache.uima.textmarker.visitor.InferenceCrowd;
+
+public class RuleElementCaretaker implements RuleElementContainer {
+
+ private final RuleElementContainer container;
+
+ public RuleElementCaretaker(RuleElementContainer container) {
+ super();
+ this.container = container;
+ }
+
+ public RuleElement getElementAfter(RuleElement element) {
+ int indexOf = container.getRuleElements().indexOf(element);
+ if (indexOf < container.getRuleElements().size() - 1) {
+ RuleElement ruleElement = container.getRuleElements().get(indexOf + 1);
+ return ruleElement;
+ }
+ return null;
+ }
+
+ public RuleElement getElementBefore(RuleElement element) {
+ int indexOf = container.getRuleElements().indexOf(element);
+ if (indexOf > 0) {
+ RuleElement ruleElement = container.getRuleElements().get(indexOf - 1);
+ return ruleElement;
+ }
+ return null;
+ }
+
+ public List<RuleElement> getRuleElements() {
+ return container.getRuleElements();
+ }
+
+ public RuleElement getAnchoringRuleElement(TextMarkerStream stream) {
+ List<RuleElement> ruleElements = container.getRuleElements();
+ if (ruleElements.size() == 1) {
+ return ruleElements.get(0);
+ }
+
+ if (stream.isDynamicAnchoring()) {
+ // TODO activate dynamic anchoring
+ int min = Integer.MAX_VALUE;
+ RuleElement minElement = null;
+ int i = 1;
+ for (RuleElement each : ruleElements) {
+ int estimate = each.estimateAnchors(stream);
+ // TODO what about this formula?
+ double factor = stream.getIndexPenalty();
+ estimate = (int) (Math.log(estimate) * (i * factor));
+ if (estimate < min) {
+ min = estimate;
+ minElement = each;
+ }
+ i++;
+ }
+ return minElement;
+ }
+ return ruleElements.get(0);
+
+ }
+
+ public RuleElement getFirstElement() {
+ List<RuleElement> ruleElements = container.getRuleElements();
+ return ruleElements.get(0);
+ }
+
+ public RuleElement getLastElement() {
+ List<RuleElement> ruleElements = container.getRuleElements();
+ return ruleElements.get(ruleElements.size() - 1);
+ }
+
+ public void applyRuleElements(RuleMatch ruleMatch, TextMarkerStream stream, InferenceCrowd crowd) {
+ for (RuleElement eachElement : getRuleElements()) {
+ eachElement.apply(ruleMatch, stream, crowd);
+ }
+ }
+
+ public TextMarkerRule getRule() {
+ return container.getRule();
+ }
+
+ public RuleElement getNextElement(boolean after, RuleElement ruleElement) {
+ if (after) {
+ return getElementAfter(ruleElement);
+ } else {
+ return getElementBefore(ruleElement);
+ }
+ }
+}
Added: uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/RuleElementComparator.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/RuleElementComparator.java?rev=1243619&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/RuleElementComparator.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/RuleElementComparator.java Mon Feb 13 17:44:52 2012
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.uima.textmarker.rule;
+
+import java.util.Comparator;
+import java.util.List;
+
+public class RuleElementComparator implements Comparator<RuleElement> {
+ private final RuleElementContainer container;
+
+ public RuleElementComparator(RuleElementContainer container) {
+ this.container = container;
+ }
+
+ public int compare(RuleElement re1, RuleElement re2) {
+ List<RuleElement> elements = container.getRuleElements();
+ int i1 = elements.indexOf(re1);
+ int i2 = elements.indexOf(re2);
+ if (i1 == i2)
+ return 0;
+ return i1 < i2 ? -1 : 1;
+ }
+
+}
Added: uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/RuleElementContainer.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/RuleElementContainer.java?rev=1243619&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/RuleElementContainer.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/RuleElementContainer.java Mon Feb 13 17:44:52 2012
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.uima.textmarker.rule;
+
+import java.util.List;
+
+import org.apache.uima.textmarker.TextMarkerStream;
+import org.apache.uima.textmarker.visitor.InferenceCrowd;
+
+public interface RuleElementContainer {
+
+ List<RuleElement> getRuleElements();
+
+ // RuleElement getElementAfter(RuleElement element);
+ //
+ // RuleElement getElementBefore(RuleElement element);
+
+ RuleElement getFirstElement();
+
+ RuleElement getLastElement();
+
+ RuleElement getAnchoringRuleElement(TextMarkerStream stream);
+
+ void applyRuleElements(RuleMatch ruleMatch, TextMarkerStream stream, InferenceCrowd crowd);
+
+ TextMarkerRule getRule();
+
+ RuleElement getNextElement(boolean after, RuleElement ruleElement);
+
+}
Added: uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/RuleElementIsolator.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/RuleElementIsolator.java?rev=1243619&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/RuleElementIsolator.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/RuleElementIsolator.java Mon Feb 13 17:44:52 2012
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.uima.textmarker.rule;
+
+import java.util.List;
+
+import org.apache.uima.textmarker.TextMarkerStream;
+import org.apache.uima.textmarker.visitor.InferenceCrowd;
+
+public class RuleElementIsolator implements RuleElementContainer {
+
+ private RuleElementContainer container;
+
+ public RuleElementIsolator() {
+ super();
+ }
+
+ public RuleElementIsolator(RuleElementContainer container) {
+ super();
+ this.container = container;
+ }
+
+ public List<RuleElement> getRuleElements() {
+ return container.getRuleElements();
+ }
+
+ public RuleElement getAnchoringRuleElement(TextMarkerStream stream) {
+ return getRuleElement();
+ }
+
+ public RuleElement getFirstElement() {
+ return getRuleElement();
+ }
+
+ public RuleElement getLastElement() {
+ return getRuleElement();
+ }
+
+ public void applyRuleElements(RuleMatch ruleMatch, TextMarkerStream stream, InferenceCrowd crowd) {
+ getRuleElement().apply(ruleMatch, stream, crowd);
+ }
+
+ public RuleElementContainer getContainer() {
+ return container;
+ }
+
+ public void setContainer(TextMarkerRule container) {
+ this.container = container.getRoot();
+ }
+
+ private RuleElement getRuleElement() {
+ return container.getRuleElements().get(0);
+ }
+
+ public TextMarkerRule getRule() {
+ return container.getRule();
+ }
+
+ public RuleElement getNextElement(boolean after, RuleElement textMarkerRuleElement) {
+ return null;
+ }
+
+}
Added: uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/RuleElementMatch.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/RuleElementMatch.java?rev=1243619&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/RuleElementMatch.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/rule/RuleElementMatch.java Mon Feb 13 17:44:52 2012
@@ -0,0 +1,141 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.uima.textmarker.rule;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.uima.cas.text.AnnotationFS;
+
+public class RuleElementMatch {
+
+ protected final RuleElement ruleElement;
+
+ protected List<AnnotationFS> textsMatched;
+
+ protected boolean conditionsMatched = true;
+
+ protected boolean baseConditionMatched = true;
+
+ protected List<EvaluatedCondition> conditions;
+
+ protected ComposedRuleElementMatch containerMatch;
+
+ private boolean ruleAnchor = false;
+
+ public RuleElementMatch(RuleElement ruleElement, ComposedRuleElementMatch containerMatch) {
+ super();
+ this.ruleElement = ruleElement;
+ this.containerMatch = containerMatch;
+ textsMatched = new ArrayList<AnnotationFS>();
+ }
+
+ public void setMatchInfo(boolean baseCondition, List<AnnotationFS> texts,
+ List<EvaluatedCondition> conditionList) {
+ baseConditionMatched = baseCondition;
+ textsMatched = texts;
+ conditions = conditionList;
+ conditionsMatched = baseConditionMatched;
+ if (baseConditionMatched) {
+ for (EvaluatedCondition each : conditions) {
+ conditionsMatched &= each.isValue();
+ }
+ }
+ if (containerMatch != null) {
+ containerMatch.addInnerMatch(ruleElement, this);
+ }
+ }
+
+ public boolean matched() {
+ return baseConditionMatched && conditionsMatched;
+ }
+
+ public RuleElement getRuleElement() {
+ return ruleElement;
+ }
+
+ public List<AnnotationFS> getTextsMatched() {
+ return textsMatched;
+ }
+
+ public void setTextsMatched(List<AnnotationFS> textsMatched) {
+ this.textsMatched = textsMatched;
+ }
+
+ public List<EvaluatedCondition> getConditions() {
+ return conditions;
+ }
+
+ public void setConditions(List<EvaluatedCondition> conditions) {
+ this.conditions = conditions;
+ }
+
+ public void setConditionsMatched(boolean conditionsMatched) {
+ this.conditionsMatched = conditionsMatched;
+ }
+
+ public void setBaseConditionMatched(boolean baseConditionMatched) {
+ this.baseConditionMatched = baseConditionMatched;
+ }
+
+ public void setContainerMatch(ComposedRuleElementMatch containerMatch) {
+ this.containerMatch = containerMatch;
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder sb = new StringBuilder();
+ sb.append(matched() + " : ");
+ for (AnnotationFS each : textsMatched) {
+ sb.append(each.getCoveredText());
+ }
+ String string = sb.toString();
+ if (string.length() > 20) {
+ string = string.substring(0, 20) + "...";
+ }
+ return string;
+ }
+
+ public boolean isBaseConditionMatched() {
+ return baseConditionMatched;
+ }
+
+ public ComposedRuleElementMatch getContainerMatch() {
+ return containerMatch;
+ }
+
+ public RuleElementMatch copy() {
+ RuleElementMatch copy = new RuleElementMatch(ruleElement, containerMatch);
+ copy.setBaseConditionMatched(baseConditionMatched);
+ copy.setConditions(conditions);
+ copy.setConditionsMatched(conditionsMatched);
+ copy.setTextsMatched(textsMatched);
+ copy.setRuleAnchor(ruleAnchor);
+ return copy;
+ }
+
+ public boolean isRuleAnchor() {
+ return ruleAnchor;
+ }
+
+ public void setRuleAnchor(boolean ruleAnchor) {
+ this.ruleAnchor = ruleAnchor;
+ }
+}