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 [4/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/TextMarkerAutomataBlock.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerAutomataBlock.java?rev=1243619&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerAutomataBlock.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerAutomataBlock.java Mon Feb 13 17:44:52 2012
@@ -0,0 +1,76 @@
+/*
+ * 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;
+
+import java.util.List;
+
+import org.apache.uima.cas.Type;
+import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.textmarker.rule.RuleApply;
+import org.apache.uima.textmarker.rule.RuleMatch;
+import org.apache.uima.textmarker.rule.TextMarkerRule;
+import org.apache.uima.textmarker.rule.TextMarkerRuleElement;
+import org.apache.uima.textmarker.visitor.InferenceCrowd;
+
+public class TextMarkerAutomataBlock extends TextMarkerBlock {
+
+ public TextMarkerAutomataBlock(String id, TextMarkerRule rule,
+ List<TextMarkerStatement> elements, TextMarkerBlock parent, String defaultNamespace) {
+ super(id, rule, elements, parent, defaultNamespace);
+ }
+
+ @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.getMatchedAnnotations(stream, null, null).get(0);
+ if (each == null) {
+ continue;
+ }
+ List<Type> types = ((TextMarkerRuleElement) rule.getRuleElements().get(0)).getMatcher()
+ .getTypes(getParent(), stream);
+ for (Type eachType : types) {
+ TextMarkerStream window = stream.getWindowStream(each, eachType);
+ 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 "RULES(" + id + ") " + ruleString + " containing " + elements.size() + " Elements";
+ }
+
+ public void setMatchRule(TextMarkerRule rule) {
+ this.rule = rule;
+ }
+
+}
Added: uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerAutomataFactory.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerAutomataFactory.java?rev=1243619&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerAutomataFactory.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerAutomataFactory.java Mon Feb 13 17:44:52 2012
@@ -0,0 +1,34 @@
+/*
+ * 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;
+
+import java.util.List;
+
+import org.antlr.runtime.Token;
+import org.apache.uima.textmarker.rule.TextMarkerRuleElement;
+
+public class TextMarkerAutomataFactory extends TextMarkerScriptFactory {
+
+ public TextMarkerBlock createAutomataBlock(Token id, TextMarkerRuleElement re,
+ List<TextMarkerStatement> body, TextMarkerBlock env) {
+ return createScriptBlock(id, re, body, env);
+ }
+
+}
Added: uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerBlock.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerBlock.java?rev=1243619&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerBlock.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerBlock.java Mon Feb 13 17:44:52 2012
@@ -0,0 +1,96 @@
+/*
+ * 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;
+
+import java.util.List;
+
+import org.apache.uima.textmarker.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) {
+ super(parent);
+ this.id = id;
+ this.rule = rule;
+ this.elements = elements;
+ this.environment = new TextMarkerEnvironment(this);
+ this.namespace = defaultNamespace;
+ }
+
+ public TextMarkerRule getRule() {
+ return rule;
+ }
+
+ public void setRule(TextMarkerRule rule) {
+ this.rule = 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() {
+ if (script != null) {
+ return script;
+ } else if (getParent() != null) {
+ return getParent().getScript();
+ } else {
+ // may not happen!
+ assert (false);
+ return null;
+
+ }
+ }
+
+ public void setScript(TextMarkerModule script) {
+ this.script = script;
+ }
+
+ public String getNamespace() {
+ return namespace;
+ }
+
+ public String getId() {
+ return id;
+ }
+
+}
Added: uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerElement.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerElement.java?rev=1243619&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerElement.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerElement.java Mon Feb 13 17:44:52 2012
@@ -0,0 +1,24 @@
+/*
+ * 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;
+
+public class TextMarkerElement {
+
+}
Added: uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerEnvironment.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerEnvironment.java?rev=1243619&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerEnvironment.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerEnvironment.java Mon Feb 13 17:44:52 2012
@@ -0,0 +1,514 @@
+/*
+ * 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;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import org.apache.uima.cas.CAS;
+import org.apache.uima.cas.CASException;
+import org.apache.uima.cas.Type;
+import org.apache.uima.jcas.cas.TOP;
+import org.apache.uima.jcas.tcas.DocumentAnnotation;
+import org.apache.uima.textmarker.action.AbstractTextMarkerAction;
+import org.apache.uima.textmarker.condition.AbstractTextMarkerCondition;
+import org.apache.uima.textmarker.expression.bool.BooleanExpression;
+import org.apache.uima.textmarker.expression.list.ListExpression;
+import org.apache.uima.textmarker.expression.list.SimpleBooleanListExpression;
+import org.apache.uima.textmarker.expression.list.SimpleNumberListExpression;
+import org.apache.uima.textmarker.expression.list.SimpleStringListExpression;
+import org.apache.uima.textmarker.expression.list.SimpleTypeListExpression;
+import org.apache.uima.textmarker.expression.number.NumberExpression;
+import org.apache.uima.textmarker.expression.resource.LiteralWordListExpression;
+import org.apache.uima.textmarker.expression.resource.LiteralWordTableExpression;
+import org.apache.uima.textmarker.expression.string.StringExpression;
+import org.apache.uima.textmarker.resource.CSVTable;
+import org.apache.uima.textmarker.resource.MultiTreeWordList;
+import org.apache.uima.textmarker.resource.TextMarkerTable;
+import org.apache.uima.textmarker.resource.TextMarkerWordList;
+import org.apache.uima.textmarker.resource.TreeWordList;
+
+public class TextMarkerEnvironment {
+
+ private final Object annotationTypeDummy = new Object();
+
+ private Map<String, Type> types;
+
+ private Map<String, TextMarkerWordList> wordLists;
+
+ private Map<String, CSVTable> tables;
+
+ private TextMarkerBlock owner;
+
+ private Map<String, String> namespaces;
+
+ private Map<String, Object> variableValues;
+
+ private Map<String, Class<?>> variableTypes;
+
+ private Map<String, Class<?>> availableTypes;
+
+ private Map<String, Class<?>> variableGenericTypes;
+
+ private Map<String, Class<?>> availableListTypes;
+
+ private String[] resourcePaths = null;
+
+ private CAS cas;
+
+ private Map<String, Object> initializedVariables;
+
+ public TextMarkerEnvironment(TextMarkerBlock owner) {
+ super();
+ this.owner = owner;
+ types = new HashMap<String, Type>();
+ namespaces = new HashMap<String, String>();
+ wordLists = new HashMap<String, TextMarkerWordList>();
+ tables = new HashMap<String, CSVTable>();
+ variableValues = new HashMap<String, Object>();
+ variableTypes = new HashMap<String, Class<?>>();
+ variableGenericTypes = new HashMap<String, Class<?>>();
+ availableTypes = new HashMap<String, Class<?>>();
+ availableTypes.put("INT", Integer.class);
+ availableTypes.put("STRING", String.class);
+ availableTypes.put("DOUBLE", Double.class);
+ availableTypes.put("FLOAT", Float.class);
+ availableTypes.put("BOOLEAN", Boolean.class);
+ availableTypes.put("TYPE", Type.class);
+ availableTypes.put("CONDITION", AbstractTextMarkerCondition.class);
+ availableTypes.put("ACTION", AbstractTextMarkerAction.class);
+ availableTypes.put("WORDLIST", TextMarkerWordList.class);
+ availableTypes.put("WORDTABLE", TextMarkerTable.class);
+ availableTypes.put("BOOLEANLIST", List.class);
+ availableTypes.put("INTLIST", List.class);
+ availableTypes.put("DOUBLELIST", List.class);
+ availableTypes.put("FLOATLIST", List.class);
+ availableTypes.put("STRINGLIST", List.class);
+ availableTypes.put("TYPELIST", List.class);
+ availableListTypes = new HashMap<String, Class<?>>();
+ availableListTypes.put("BOOLEANLIST", Boolean.class);
+ availableListTypes.put("INTLIST", Integer.class);
+ availableListTypes.put("DOUBLELIST", Double.class);
+ availableListTypes.put("FLOATLIST", Float.class);
+ availableListTypes.put("STRINGLIST", String.class);
+ availableListTypes.put("TYPELIST", Type.class);
+ resourcePaths = getResourcePaths();
+ initializedVariables = new HashMap<String, Object>();
+ }
+
+ public void initializeTypes(CAS cas) {
+ this.cas = cas;
+ Type topType = null;
+ try {
+ topType = cas.getJCas().getCasType(TOP.type);
+ if (topType != null) {
+ List<Type> list = cas.getTypeSystem().getProperlySubsumedTypes(topType);
+ for (Type type : list) {
+ addType(type);
+ }
+ }
+ Type documentType = cas.getJCas().getCasType(DocumentAnnotation.type);
+ addType("Document", documentType);
+ Type annotationType = cas.getJCas().getCasType(org.apache.uima.jcas.tcas.Annotation.type);
+ addType("Annotation", annotationType);
+ } catch (CASException e) {
+ }
+
+ }
+
+ public String[] getResourcePaths() {
+ if (resourcePaths == null) {
+ TextMarkerBlock parent = owner.getParent();
+ if (parent != null) {
+ return parent.getEnvironment().getResourcePaths();
+ }
+ }
+ return resourcePaths;
+ }
+
+ public void setResourcePaths(String[] resourcePaths) {
+ this.resourcePaths = resourcePaths;
+ }
+
+ public boolean ownsType(String match) {
+ match = expand(match);
+ return types.keySet().contains(match);
+ }
+
+ private String expand(String string) {
+ String complete;
+ if (string.indexOf(".") == -1) {
+ complete = namespaces.get(string);
+ if (complete == null) {
+ complete = string;
+ }
+ } else {
+ complete = string;
+ String[] split = complete.split("\\p{Punct}");
+ String name = split[split.length - 1];
+ namespaces.put(name, complete);
+ }
+ return complete;
+ }
+
+ public Type getType(String match) {
+ String expanded = expand(match);
+ Type type = types.get(expanded);
+ if (type == null) {
+ TextMarkerBlock parent = owner.getParent();
+ if (parent != null) {
+ type = parent.getEnvironment().getType(match);
+ }
+ }
+ return type;
+ }
+
+ public void addType(String string, Type type) {
+ namespaces.put(string, type.getName());
+ types.put(type.getName(), type);
+ }
+
+ public void addType(Type type) {
+ addType(type.getShortName(), type);
+ }
+
+ public TextMarkerWordList getWordList(String list) {
+ TextMarkerWordList result = wordLists.get(list);
+ if (result == null) {
+ boolean found = false;
+ for (String eachPath : resourcePaths) {
+ File file = new File(eachPath, list);
+ if (!file.exists()) {
+ continue;
+ }
+ found = true;
+ if (file.getName().endsWith("mtwl")) {
+ wordLists.put(list, new MultiTreeWordList(file.getAbsolutePath()));
+ } else {
+ wordLists.put(list, new TreeWordList(file.getAbsolutePath()));
+ }
+ break;
+ }
+ if (!found) {
+ Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Can't find " + list + "!");
+ }
+ }
+ return wordLists.get(list);
+ }
+
+ public TextMarkerTable getWordTable(String table) {
+ TextMarkerTable result = tables.get(table);
+ if (result == null) {
+ boolean found = false;
+ for (String eachPath : resourcePaths) {
+ File file = new File(eachPath, table);
+ if (!file.exists()) {
+ continue;
+ }
+ found = true;
+ tables.put(table, new CSVTable(file.getAbsolutePath()));
+ break;
+ }
+ if (!found) {
+ Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Can't find " + table + "!");
+ }
+ }
+ return tables.get(table);
+ }
+
+ public void addVariable(String name, Class<?> type, Class<?> generic) {
+ variableTypes.put(name, type);
+ if (generic != null) {
+ variableGenericTypes.put(name, generic);
+ }
+ variableValues.put(name, getInitialValue(name, type));
+ }
+
+ private Object getInitialValue(String name, Class<?> type) {
+ Object init = initializedVariables.get(name);
+ if (init != null) {
+ return init;
+ }
+ if (Integer.class.equals(type)) {
+ return 0;
+ } else if (Double.class.equals(type)) {
+ return 0d;
+ } else if (Float.class.equals(type)) {
+ return 0f;
+ } else if (String.class.equals(type)) {
+ return "";
+ } else if (Boolean.class.equals(type)) {
+ return false;
+ } else if (Type.class.equals(type)) {
+ if (cas == null) {
+ return annotationTypeDummy;
+ } else {
+ return cas.getAnnotationType();
+ }
+ } else if (List.class.equals(type)) {
+ return new ArrayList<Object>();
+ }
+ return null;
+ }
+
+ public void addVariable(String name, String type) {
+ addVariable(name, availableTypes.get(type), availableListTypes.get(type));
+ }
+
+ public boolean ownsVariable(String name) {
+ return variableTypes.containsKey(name);
+ }
+
+ public boolean ownsVariableOfType(String name, String type) {
+ Class<?> varclass = variableTypes.get(name);
+ Class<?> aclass = availableTypes.get(type);
+ boolean list = true;
+ if (aclass.equals(List.class)) {
+ Class<?> vt = variableGenericTypes.get(name);
+ Class<?> at = availableListTypes.get(type);
+ list = vt != null && vt.equals(at);
+ }
+ return list && varclass != null && varclass.equals(aclass);
+ }
+
+ public boolean isVariable(String name) {
+ return ownsVariable(name) || owner.getParent().getEnvironment().isVariable(name);
+ }
+
+ public boolean isVariableOfType(String name, String type) {
+ return ownsVariableOfType(name, type)
+ || (owner.getParent() != null && owner.getParent().getEnvironment()
+ .isVariableOfType(name, type));
+ }
+
+ public Class<?> getVariableType(String name) {
+ Class<?> result = variableTypes.get(name);
+ if (result != null) {
+ return result;
+ } else if (owner.getParent() != null) {
+ return owner.getParent().getEnvironment().getVariableType(name);
+ }
+ return null;
+ }
+
+ public Class<?> getVariableGenericType(String name) {
+ Class<?> result = variableGenericTypes.get(name);
+ if (result != null) {
+ return result;
+ } else if (owner.getParent() != null) {
+ return owner.getParent().getEnvironment().getVariableGenericType(name);
+ }
+ return null;
+ }
+
+ public <T> T getVariableValue(String name, Class<T> type) {
+ boolean containsKey = variableValues.containsKey(name);
+ Object result = variableValues.get(name);
+ if (containsKey && result == null) {
+ // TODO find the problem with the null values!
+ // this might now work for word lists in another env.
+ return type.cast(getInitialValue(name, type));
+ }
+ if (result == annotationTypeDummy) {
+ return type.cast(cas.getAnnotationType());
+ }
+ if (result != null) {
+ return type.cast(result);
+ } else if (owner.getParent() != null) {
+ return owner.getParent().getEnvironment().getVariableValue(name, type);
+ }
+ return null;
+ }
+
+ public Object getVariableValue(String name) {
+ return getVariableValue(name, Object.class);
+ }
+
+// public Object getLiteralValue(String var, Object value) {
+// if (ownsVariable(var)) {
+// Class<?> clazz = variableTypes.get(var);
+// if (value instanceof NumberExpression) {
+// NumberExpression ne = (NumberExpression) value;
+// if (clazz.equals(Integer.class)) {
+// return ne.getIntegerValue(owner);
+// } else if (clazz.equals(Double.class)) {
+// return ne.getDoubleValue(owner);
+// } else if (clazz.equals(Float.class)) {
+// return ne.getFloatValue(owner);
+// } else if (clazz.equals(String.class)) {
+// return ne.getStringValue(owner);
+// }
+// } else if (clazz.equals(String.class) && value instanceof StringExpression) {
+// StringExpression se = (StringExpression) value;
+// return se.getStringValue(owner);
+// } else if (clazz.equals(Boolean.class) && value instanceof BooleanExpression) {
+// BooleanExpression be = (BooleanExpression) value;
+// return be.getBooleanValue(owner);
+// } else if(clazz.equals(TextMarkerWordList.class)||clazz.equals(TextMarkerTable.class) ) {
+// // TODO: refactor this: lists are handled by setVariableValue... resources should be ae resources in future!
+// return value;
+// }
+// return null;
+// } else {
+// return owner.getParent().getEnvironment().getLiteralValue(var, value);
+// }
+// }
+
+ public Object getLiteralValue(String var, Object value) {
+ if (ownsVariable(var)) {
+ Class<?> clazz = variableTypes.get(var);
+ if (value instanceof NumberExpression) {
+ NumberExpression ne = (NumberExpression) value;
+ if (clazz.equals(Integer.class)) {
+ return ne.getIntegerValue(owner);
+ } else if (clazz.equals(Double.class)) {
+ return ne.getDoubleValue(owner);
+ } else if (clazz.equals(Float.class)) {
+ return ne.getFloatValue(owner);
+ } else if (clazz.equals(String.class)) {
+ return ne.getStringValue(owner);
+ }
+ } else if (clazz.equals(String.class) && value instanceof StringExpression) {
+ StringExpression se = (StringExpression) value;
+ return se.getStringValue(owner);
+ } else if (clazz.equals(Boolean.class) && value instanceof BooleanExpression) {
+ BooleanExpression be = (BooleanExpression) value;
+ return be.getBooleanValue(owner);
+ }
+ if (clazz.equals(TextMarkerWordList.class) && value instanceof LiteralWordListExpression) {
+ LiteralWordListExpression lle = (LiteralWordListExpression) value;
+ String path = lle.getText();
+ TextMarkerWordList wordList = getWordList(path);
+ return wordList;
+ } else if (clazz.equals(TextMarkerWordList.class)) {
+ TextMarkerWordList list = getWordList((String) value);
+ return list;
+ } else if (clazz.equals(TextMarkerTable.class) && value instanceof LiteralWordTableExpression) {
+ LiteralWordTableExpression lte = (LiteralWordTableExpression) value;
+ String path = lte.getText();
+ TextMarkerTable table = getWordTable(path);
+ return table;
+ } else if (clazz.equals(TextMarkerTable.class)) {
+ TextMarkerTable table = getWordTable((String) value);
+ return table;
+ } else if (clazz.equals(List.class) && value instanceof ListExpression) {
+ List list = getList((ListExpression) value);
+ return list;
+ }
+
+ return null;
+ } else {
+ return owner.getParent().getEnvironment().getLiteralValue(var, value);
+ }
+ }
+
+
+ public void setInitialVariableValue(String var, Object value) {
+ if (ownsVariable(var)) {
+ initializedVariables.put(var, value);
+ setVariableValue(var, value);
+ } else if (owner.getParent() != null) {
+ owner.getParent().getEnvironment().setInitialVariableValue(var, value);
+ }
+ }
+
+ public void setVariableValue(String var, Object value) {
+ if (ownsVariable(var)) {
+ Class<?> clazz = variableTypes.get(var);
+ if (value == null) {
+ value = getInitialValue(var, clazz);
+ }
+ variableValues.put(var, value);
+ } else if (owner.getParent() != null) {
+ owner.getParent().getEnvironment().setVariableValue(var, value);
+ }
+ }
+
+// public void setVariableValue(String var, Object value) {
+// if (ownsVariable(var)) {
+// Class<?> clazz = variableTypes.get(var);
+// if (clazz.equals(TextMarkerWordList.class) && value instanceof LiteralWordListExpression) {
+// LiteralWordListExpression lle = (LiteralWordListExpression) value;
+// String path = lle.getText();
+// TextMarkerWordList wordList = getWordList(path);
+// variableValues.put(var, wordList);
+// } else if (clazz.equals(TextMarkerWordList.class)) {
+// TextMarkerWordList list = getWordList((String) value);
+// variableValues.put(var, list);
+// } else if (clazz.equals(TextMarkerTable.class) && value instanceof LiteralWordTableExpression) {
+// LiteralWordTableExpression lte = (LiteralWordTableExpression) value;
+// String path = lte.getText();
+// TextMarkerTable table = getWordTable(path);
+// variableValues.put(var, table);
+// } else if (clazz.equals(TextMarkerTable.class)) {
+// TextMarkerTable table = getWordTable((String) value);
+// variableValues.put(var, table);
+// } else if (clazz.equals(List.class) && value instanceof ListExpression) {
+// List list = getList((ListExpression) value);
+// variableValues.put(var, list);
+// } else {
+// if (value == null) {
+// value = getInitialValue(var, clazz);
+// }
+// variableValues.put(var, value);
+// }
+// } else if (owner.getParent() != null) {
+// owner.getParent().getEnvironment().setVariableValue(var, value);
+// }
+// }
+
+ private List getList(ListExpression value) {
+ if (value instanceof SimpleBooleanListExpression) {
+ SimpleBooleanListExpression e = (SimpleBooleanListExpression) value;
+ return e.getList();
+ } else if (value instanceof SimpleNumberListExpression) {
+ SimpleNumberListExpression e = (SimpleNumberListExpression) value;
+ return e.getList();
+ } else if (value instanceof SimpleStringListExpression) {
+ SimpleStringListExpression e = (SimpleStringListExpression) value;
+ return e.getList();
+ } else if (value instanceof SimpleTypeListExpression) {
+ SimpleTypeListExpression e = (SimpleTypeListExpression) value;
+ return e.getList();
+ }
+ return null;
+ }
+
+ public void reset(CAS cas) {
+ this.cas = cas;
+ Set<Entry<String, Object>> entrySet = variableValues.entrySet();
+ for (Entry<String, Object> entry : entrySet) {
+ String key = entry.getKey();
+ Object initialValue = getInitialValue(key, variableTypes.get(key));
+ if (initialValue != null) {
+ // not for word lists
+ entry.setValue(initialValue);
+ }
+ }
+ }
+
+}
Added: uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerModule.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerModule.java?rev=1243619&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerModule.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerModule.java Mon Feb 13 17:44:52 2012
@@ -0,0 +1,129 @@
+/*
+ * 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;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.uima.analysis_engine.AnalysisEngine;
+import org.apache.uima.textmarker.visitor.InferenceCrowd;
+
+public class TextMarkerModule extends TextMarkerElement {
+
+ private final TextMarkerBlock rootBlock;
+
+ private Map<String, TextMarkerModule> scripts;
+
+ private Map<String, AnalysisEngine> engines;
+
+ private Map<String, TextMarkerBlock> blocks;
+
+ public TextMarkerModule(TextMarkerBlock rootBlock) {
+ super();
+ this.rootBlock = rootBlock;
+ scripts = new HashMap<String, TextMarkerModule>();
+ engines = new HashMap<String, AnalysisEngine>();
+ blocks = new HashMap<String, TextMarkerBlock>();
+ }
+
+ public ScriptApply apply(TextMarkerStream stream, InferenceCrowd crowd) {
+ crowd.beginVisit(this, null);
+ ScriptApply result = rootBlock.apply(stream, crowd);
+ crowd.endVisit(this, result);
+ return result;
+ }
+
+ public TextMarkerBlock getBlock(String id) {
+ if (id == null || id.equals(rootBlock.getId())) {
+ return rootBlock;
+ }
+ return blocks.get(id);
+ }
+
+ public TextMarkerModule getScript(String name) {
+ if (name.equals(rootBlock.getId())) {
+ return this;
+ }
+ TextMarkerModule result = scripts.get(name);
+ if (result == null) {
+ for (String each : scripts.keySet()) {
+ String[] split = each.split("\\.");
+ String last = split[split.length - 1];
+ if (last.equals(name)) {
+ return scripts.get(each);
+ }
+ }
+ }
+ return result;
+ }
+
+ public void addScript(String name, TextMarkerModule script) {
+ scripts.put(name, script);
+ }
+
+ public void addBlock(String id, TextMarkerBlock block) {
+ blocks.put(id, block);
+ }
+
+ public void setScriptDependencies(Map<String, TextMarkerModule> additionalScripts) {
+ for (String eachTarget : scripts.keySet()) {
+ TextMarkerModule textMarkerModule = additionalScripts.get(eachTarget);
+ addScript(eachTarget, textMarkerModule);
+ }
+ }
+
+ public AnalysisEngine getEngine(String name) {
+ AnalysisEngine result = engines.get(name);
+ if (result == null) {
+ for (String each : engines.keySet()) {
+ String[] split = each.split("\\.");
+ String last = split[split.length - 1];
+ if (last.equals(name)) {
+ return engines.get(each);
+ }
+ }
+ }
+ return result;
+ }
+
+ public void addEngine(String name, AnalysisEngine engine) {
+ engines.put(name, engine);
+ }
+
+ public void setEngineDependencies(Map<String, AnalysisEngine> additionalEngines) {
+ for (String eachTarget : engines.keySet()) {
+ AnalysisEngine engine = additionalEngines.get(eachTarget);
+ addEngine(eachTarget, engine);
+ }
+ }
+
+ public Map<String, TextMarkerModule> getScripts() {
+ return scripts;
+ }
+
+ public Map<String, TextMarkerBlock> getBlocks() {
+ return blocks;
+ }
+
+ public Map<String, AnalysisEngine> getEngines() {
+ return engines;
+ }
+
+}
Added: uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerScriptBlock.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerScriptBlock.java?rev=1243619&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerScriptBlock.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerScriptBlock.java Mon Feb 13 17:44:52 2012
@@ -0,0 +1,76 @@
+/*
+ * 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;
+
+import java.util.List;
+
+import org.apache.uima.cas.Type;
+import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.textmarker.rule.RuleApply;
+import org.apache.uima.textmarker.rule.RuleMatch;
+import org.apache.uima.textmarker.rule.TextMarkerRule;
+import org.apache.uima.textmarker.rule.TextMarkerRuleElement;
+import org.apache.uima.textmarker.visitor.InferenceCrowd;
+
+public class TextMarkerScriptBlock extends TextMarkerBlock {
+
+ public TextMarkerScriptBlock(String id, TextMarkerRule rule, List<TextMarkerStatement> elements,
+ TextMarkerBlock parent, String defaultNamespace) {
+ super(id, rule, elements, parent, defaultNamespace);
+ }
+
+ @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()) {
+ List<AnnotationFS> matchedAnnotations = eachMatch.getMatchedAnnotations(stream, null, null);
+ if (matchedAnnotations == null || matchedAnnotations.isEmpty()) {
+ continue;
+ }
+ AnnotationFS each = matchedAnnotations.get(0);
+ if (each == null) {
+ continue;
+ }
+ List<Type> types = ((TextMarkerRuleElement) rule.getRuleElements().get(0)).getMatcher()
+ .getTypes(getParent() == null ? this : getParent(), stream);
+ for (Type eachType : types) {
+ TextMarkerStream window = stream.getWindowStream(each, eachType);
+ 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";
+ }
+
+}
Added: uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerScriptFactory.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerScriptFactory.java?rev=1243619&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerScriptFactory.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerScriptFactory.java Mon Feb 13 17:44:52 2012
@@ -0,0 +1,191 @@
+/*
+ * 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;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.antlr.runtime.Token;
+import org.apache.uima.resource.metadata.TypeSystemDescription;
+import org.apache.uima.textmarker.action.AbstractTextMarkerAction;
+import org.apache.uima.textmarker.condition.AbstractTextMarkerCondition;
+import org.apache.uima.textmarker.expression.number.NumberExpression;
+import org.apache.uima.textmarker.expression.string.StringExpression;
+import org.apache.uima.textmarker.expression.type.SimpleTypeExpression;
+import org.apache.uima.textmarker.expression.type.TypeExpression;
+import org.apache.uima.textmarker.rule.ComposedRuleElement;
+import org.apache.uima.textmarker.rule.RuleElement;
+import org.apache.uima.textmarker.rule.RuleElementContainer;
+import org.apache.uima.textmarker.rule.RuleElementIsolator;
+import org.apache.uima.textmarker.rule.TextMarkerDisjunctiveTypeMatcher;
+import org.apache.uima.textmarker.rule.TextMarkerLiteralMatcher;
+import org.apache.uima.textmarker.rule.TextMarkerRule;
+import org.apache.uima.textmarker.rule.TextMarkerRuleElement;
+import org.apache.uima.textmarker.rule.TextMarkerTypeMatcher;
+import org.apache.uima.textmarker.rule.quantifier.MinMaxGreedy;
+import org.apache.uima.textmarker.rule.quantifier.MinMaxReluctant;
+import org.apache.uima.textmarker.rule.quantifier.PlusGreedy;
+import org.apache.uima.textmarker.rule.quantifier.PlusReluctant;
+import org.apache.uima.textmarker.rule.quantifier.QuestionGreedy;
+import org.apache.uima.textmarker.rule.quantifier.QuestionReluctant;
+import org.apache.uima.textmarker.rule.quantifier.RuleElementQuantifier;
+import org.apache.uima.textmarker.rule.quantifier.StarGreedy;
+import org.apache.uima.textmarker.rule.quantifier.StarReluctant;
+
+public class TextMarkerScriptFactory {
+
+ private static int idCounter = 0;
+
+ public TextMarkerScriptFactory() {
+ super();
+ }
+
+ public TextMarkerScriptBlock createScriptBlock(Token id, TextMarkerRuleElement ruleElement,
+ List<TextMarkerStatement> body, TextMarkerBlock parent) {
+ String text = id == null ? "root" : id.getText();
+ String defaultNamespace = parent.getNamespace();
+ if (id != null) {
+ defaultNamespace = defaultNamespace + "." + text;
+ }
+ return createScriptBlock(text, ruleElement, body, parent, defaultNamespace);
+ }
+
+ public TextMarkerScriptBlock createScriptBlock(String text, TextMarkerRuleElement ruleElement,
+ List<TextMarkerStatement> body, TextMarkerBlock parent, String defaultNamespace) {
+ TextMarkerRule rule = null;
+ if (ruleElement != null) {
+ rule = createRule(ruleElement, parent);
+ }
+ List<TextMarkerStatement> elements = new ArrayList<TextMarkerStatement>();
+ if (body != null) {
+ for (TextMarkerStatement each : body) {
+ if (each != null) {
+ elements.add(each);
+ }
+ }
+ }
+ return new TextMarkerScriptBlock(text, rule, elements, parent, defaultNamespace);
+ }
+
+ public TextMarkerScriptBlock createRootScriptBlock(String module, String pack,
+ TypeSystemDescription localTSD) {
+ String defaultNamespace = pack + "." + module;
+ TextMarkerScriptBlock result = createScriptBlock(module, null, null, null, defaultNamespace);
+ List<RuleElement> ruleElements = new ArrayList<RuleElement>();
+ RuleElementIsolator container = new RuleElementIsolator();
+ ruleElements.add(createRuleElement(new SimpleTypeExpression("uima.tcas.DocumentAnnotation"),
+ null, null, null, container, result));
+ TextMarkerRule createRule = createRule(ruleElements, result);
+ container.setContainer(createRule);
+
+ result.setRule(createRule);
+ return result;
+ }
+
+ public TextMarkerRule createRule(RuleElement element, TextMarkerBlock parent) {
+ List<RuleElement> elements = new ArrayList<RuleElement>();
+ elements.add(element);
+ return createRule(elements, parent);
+ }
+
+ public TextMarkerRule createRule(List<RuleElement> elements, TextMarkerBlock parent) {
+ // System.out.println("Rule: " + idCounter);
+ return new TextMarkerRule(elements, parent, idCounter++);
+ }
+
+ public TextMarkerRuleElement createRuleElement(TypeExpression typeExpression,
+ RuleElementQuantifier quantifier, List<AbstractTextMarkerCondition> conditions,
+ List<AbstractTextMarkerAction> actions, RuleElementContainer container,
+ TextMarkerBlock parent) {
+
+ TextMarkerTypeMatcher matcher = new TextMarkerTypeMatcher(typeExpression);
+ return new TextMarkerRuleElement(matcher, quantifier, conditions, actions, container, parent);
+ }
+
+ public TextMarkerRuleElement createRuleElement(List<TypeExpression> typeExprs,
+ RuleElementQuantifier quantifier, List<AbstractTextMarkerCondition> conditions,
+ List<AbstractTextMarkerAction> actions, RuleElementContainer container,
+ TextMarkerBlock parent) {
+ TextMarkerDisjunctiveTypeMatcher matcher = new TextMarkerDisjunctiveTypeMatcher(typeExprs);
+ return new TextMarkerRuleElement(matcher, quantifier, conditions, actions, container, parent);
+ }
+
+ public TextMarkerRuleElement createRuleElement(StringExpression stringExpression,
+ RuleElementQuantifier quantifier, List<AbstractTextMarkerCondition> conditions,
+ List<AbstractTextMarkerAction> actions, RuleElementContainer container,
+ TextMarkerBlock parent) {
+
+ TextMarkerLiteralMatcher matcher = new TextMarkerLiteralMatcher(stringExpression);
+ return new TextMarkerRuleElement(matcher, quantifier, conditions, actions, container, parent);
+ }
+
+ public ComposedRuleElement createComposedRuleElement(List<RuleElement> res,
+ RuleElementQuantifier quantifier, List<AbstractTextMarkerCondition> conditions,
+ List<AbstractTextMarkerAction> actions, RuleElementContainer container,
+ TextMarkerBlock parent) {
+
+ return new ComposedRuleElement(res, quantifier, conditions, actions, container, parent);
+ }
+
+ public ComposedRuleElement createComposedRuleElement(RuleElementContainer container,
+ TextMarkerBlock parent) {
+ return new ComposedRuleElement(null, null, null, null, container, parent);
+ }
+
+ public static RuleElementQuantifier createStarGreedyQuantifier() {
+ return new StarGreedy();
+ }
+
+ public static RuleElementQuantifier createPlusReluctantQuantifier() {
+ return new PlusReluctant();
+ }
+
+ public static RuleElementQuantifier createStarReluctantQuantifier() {
+ return new StarReluctant();
+ }
+
+ public static RuleElementQuantifier createMinMaxGreedyQuantifier(NumberExpression min,
+ NumberExpression max, Token comma) {
+ return new MinMaxGreedy(min, max, comma != null);
+ }
+
+ public static RuleElementQuantifier createMinMaxReluctantQuantifier(NumberExpression min,
+ NumberExpression max, Token comma) {
+ return new MinMaxReluctant(min, max, comma != null);
+ }
+
+ public static RuleElementQuantifier createPlusGreedyQuantifier() {
+ return new PlusGreedy();
+ }
+
+ public static RuleElementQuantifier createQuestionReluctantQuantifier() {
+ return new QuestionReluctant();
+ }
+
+ public static RuleElementQuantifier createQuestionGreedyQuantifier() {
+ return new QuestionGreedy();
+ }
+
+ public TextMarkerBlock createAutomataBlock(Token id, TextMarkerRuleElement re,
+ List<TextMarkerStatement> body, TextMarkerBlock env) {
+ return createScriptBlock(id, re, body, env);
+ }
+
+}
Added: uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerStatement.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerStatement.java?rev=1243619&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerStatement.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerStatement.java Mon Feb 13 17:44:52 2012
@@ -0,0 +1,45 @@
+/*
+ * 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;
+
+import org.apache.uima.textmarker.visitor.InferenceCrowd;
+
+public abstract class TextMarkerStatement extends TextMarkerElement {
+
+ private TextMarkerBlock parent;
+
+ public TextMarkerStatement(TextMarkerBlock parent) {
+ super();
+ this.parent = parent;
+ }
+
+ public abstract ScriptApply apply(TextMarkerStream stream, InferenceCrowd crowd);
+
+ public abstract TextMarkerEnvironment getEnvironment();
+
+ public TextMarkerBlock getParent() {
+ return parent;
+ }
+
+ public void setParent(TextMarkerBlock parent) {
+ this.parent = parent;
+ }
+
+}
Added: uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerStream.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerStream.java?rev=1243619&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerStream.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/TextMarkerStream.java Mon Feb 13 17:44:52 2012
@@ -0,0 +1,640 @@
+/*
+ * 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;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.NavigableSet;
+import java.util.NoSuchElementException;
+import java.util.TreeMap;
+import java.util.TreeSet;
+
+import org.apache.uima.cas.CAS;
+import org.apache.uima.cas.CASException;
+import org.apache.uima.cas.ConstraintFactory;
+import org.apache.uima.cas.FSIterator;
+import org.apache.uima.cas.FSMatchConstraint;
+import org.apache.uima.cas.FeatureStructure;
+import org.apache.uima.cas.Type;
+import org.apache.uima.cas.TypeSystem;
+import org.apache.uima.cas.impl.FSIteratorImplBase;
+import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.cas.text.AnnotationIndex;
+import org.apache.uima.jcas.JCas;
+import org.apache.uima.jcas.tcas.Annotation;
+import org.apache.uima.jcas.tcas.DocumentAnnotation;
+import org.apache.uima.textmarker.rule.RuleElementMatch;
+import org.apache.uima.textmarker.type.TextMarkerAnnotation;
+import org.apache.uima.textmarker.type.TextMarkerBasic;
+
+public class TextMarkerStream extends FSIteratorImplBase<AnnotationFS> {
+
+ private final CAS cas;
+
+ private FSIterator<AnnotationFS> basicIt;
+
+ private FSIterator<AnnotationFS> currentIt;
+
+ private AnnotationFS documentAnnotation;
+
+ private Type documentAnnotationType;
+
+ private Type basicType;
+
+ private final TreeSet<TextMarkerBasic> basics;
+
+ private TreeMap<Integer, TextMarkerBasic> beginAnchors;
+
+ private TreeMap<Integer, TextMarkerBasic> endAnchors;
+
+ private FilterManager filter;
+
+ private Map<Integer, TextMarkerBasic> pointerMap = new HashMap<Integer, TextMarkerBasic>();
+
+ private boolean dynamicAnchoring;
+
+ private double indexPenalty = 5;
+
+ private double anchoringFactor;
+
+ protected TextMarkerStream(CAS cas, FSIterator<AnnotationFS> current, Type basicType,
+ FilterManager filter) {
+ super();
+ this.cas = cas;
+ this.filter = filter;
+ this.basicType = basicType;
+ AnnotationFS additionalWindow = filter.getWindowAnnotation();
+ updateIterators(cas, basicType, filter, additionalWindow);
+ // really an if? sub it of basic should fix this
+ if (additionalWindow == null) {
+ documentAnnotation = (DocumentAnnotation) getJCas().getDocumentAnnotationFs();
+ documentAnnotationType = getCas().getDocumentAnnotation().getType();
+ basicIt.moveToFirst();
+ } else {
+ documentAnnotation = additionalWindow;
+ documentAnnotationType = filter.getWindowType();
+ }
+ // // really faster???
+ // TODO this needs to be changed!! use collection of prior stream
+ org.apache.uima.textmarker.rule.AnnotationComparator comparator = new org.apache.uima.textmarker.rule.AnnotationComparator();
+ basics = new TreeSet<TextMarkerBasic>(comparator);
+ beginAnchors = new TreeMap<Integer, TextMarkerBasic>();
+ endAnchors = new TreeMap<Integer, TextMarkerBasic>();
+ FSIterator<AnnotationFS> iterator = cas.getAnnotationIndex(basicType).subiterator(
+ documentAnnotation);
+ while (iterator.isValid()) {
+ TextMarkerBasic e = (TextMarkerBasic) iterator.get();
+ beginAnchors.put(e.getBegin(), e);
+ endAnchors.put(e.getEnd(), e);
+ basics.add(e);
+ iterator.moveToNext();
+ }
+
+ }
+
+ private void updateIterators(AnnotationFS additionalWindow) {
+ updateIterators(cas, basicType, filter, additionalWindow);
+ }
+
+ private void updateIterators(CAS cas, Type basicType, FilterManager filter,
+ AnnotationFS additionalWindow) {
+ if (additionalWindow != null) {
+ this.basicIt = cas.getAnnotationIndex(basicType).subiterator(additionalWindow);
+ } else {
+ this.basicIt = cas.getAnnotationIndex(basicType).iterator();
+ }
+ currentIt = filter.createFilteredIterator(cas, basicType);
+ }
+
+ public TextMarkerStream(CAS cas, Type basicType, FilterManager filter) {
+ this(cas, null, basicType, filter);
+ }
+
+ public void initalizeBasics() {
+ AnnotationIndex<AnnotationFS> basicIndex = cas.getAnnotationIndex(basicType);
+ AnnotationIndex<AnnotationFS> annotationIndex = cas.getAnnotationIndex();
+ if (basicIndex.size() == 0) {
+ TreeSet<Integer> anchors = new TreeSet<Integer>();
+ for (AnnotationFS a : annotationIndex) {
+ anchors.add(a.getBegin());
+ anchors.add(a.getEnd());
+ }
+ while (anchors.size() >= 2) {
+ Integer first = anchors.pollFirst();
+ Integer second = anchors.first();
+ TextMarkerBasic newTMB = new TextMarkerBasic(getJCas(), first, second);
+ beginAnchors.put(first, newTMB);
+ endAnchors.put(second, newTMB);
+ basics.add(newTMB);
+ cas.addFsToIndexes(newTMB);
+ }
+ }
+ for (AnnotationFS a : annotationIndex) {
+ if (!a.getType().equals(basicType)) {
+ addAnnotation(a, false);
+ }
+ }
+ updateIterators(documentAnnotation);
+ }
+
+ public void addAnnotation(AnnotationFS annotation) {
+ addAnnotation(annotation, false);
+ }
+
+ public void addAnnotation(AnnotationFS annotation, boolean update) {
+ Type type = annotation.getType();
+ TypeSystem typeSystem = cas.getTypeSystem();
+ Type parent = type;
+ boolean modified = checkSpan(annotation);
+ if (modified) {
+ updateIterators(filter.getWindowAnnotation());
+ }
+ TextMarkerBasic beginAnchor = getBeginAnchor(annotation.getBegin());
+ TextMarkerBasic endAnchor = getEndAnchor(annotation.getEnd());
+ while (parent != null) {
+ beginAnchor.addBegin(annotation, parent);
+ endAnchor.addEnd(annotation, parent);
+ parent = typeSystem.getParent(parent);
+ }
+ Collection<TextMarkerBasic> basicAnnotationsInWindow = getAllBasicsInWindow(annotation);
+ for (TextMarkerBasic basic : basicAnnotationsInWindow) {
+ basic.addPartOf(type);
+ }
+ }
+
+ private boolean checkSpan(AnnotationFS annotation) {
+ boolean result = false;
+ int begin = annotation.getBegin();
+ int end = annotation.getEnd();
+ TextMarkerBasic beginAnchor = getBeginAnchor(begin);
+ TextMarkerBasic endAnchor = getEndAnchor(end);
+ if (beginAnchor != null && endAnchor != null) {
+ result = false;
+ } else {
+ if (beginAnchor == null) {
+ result |= checkAnchor(begin);
+ }
+ if (endAnchor == null) {
+ result |= checkAnchor(end);
+ }
+ }
+ return result;
+ }
+
+ private boolean checkAnchor(int anchor) {
+ Entry<Integer, TextMarkerBasic> floorEntry = endAnchors.floorEntry(anchor);
+ Entry<Integer, TextMarkerBasic> ceilingEntry = endAnchors.ceilingEntry(anchor);
+ if (floorEntry != null && ceilingEntry != null) {
+ TextMarkerBasic floor = floorEntry.getValue();
+ TextMarkerBasic ceiling = ceilingEntry.getValue();
+ cas.removeFsFromIndexes(floor);
+ floor.setEnd(anchor);
+ AnnotationFS createAnnotation = cas.createAnnotation(basicType, anchor, ceiling.getBegin());
+ cas.addFsToIndexes(floor);
+ cas.addFsToIndexes(createAnnotation);
+ return true;
+ } else {
+ // TODO this should never happen! test it and remove the assert!
+ assert (false);
+ }
+ return false;
+ }
+
+ public void removeAnnotation(AnnotationFS annotationFS) {
+ removeAnnotation(annotationFS, annotationFS.getType());
+ }
+
+ public void removeAnnotation(AnnotationFS annotation, Type type) {
+ TypeSystem typeSystem = cas.getTypeSystem();
+ Collection<TextMarkerBasic> basicAnnotationsInWindow = getAllBasicsInWindow(annotation);
+ for (TextMarkerBasic basic : basicAnnotationsInWindow) {
+ basic.removePartOf(type);
+ }
+ Type parent = type;
+ TextMarkerBasic beginAnchor = getBeginAnchor(annotation.getBegin());
+ TextMarkerBasic endAnchor = getEndAnchor(annotation.getEnd());
+ while (parent != null) {
+ beginAnchor.removeBegin(annotation, parent);
+ endAnchor.removeEnd(annotation, parent);
+ parent = typeSystem.getParent(parent);
+ }
+ if (!(annotation instanceof TextMarkerBasic)) {
+ cas.removeFsFromIndexes(annotation);
+ }
+
+ }
+
+ public FSIterator<AnnotationFS> getFilteredBasicIterator(FSMatchConstraint constraint) {
+ ConstraintFactory cf = cas.getConstraintFactory();
+ FSMatchConstraint matchConstraint = cf.and(constraint, filter.getDefaultConstraint());
+ return cas.createFilteredIterator(basicIt, matchConstraint);
+ }
+
+ public TextMarkerStream getWindowStream(AnnotationFS windowAnnotation, Type windowType) {
+ if (windowAnnotation.getBegin() == documentAnnotation.getBegin()
+ && windowAnnotation.getEnd() == documentAnnotation.getEnd()) {
+ return this;
+ }
+
+ FilterManager filterManager = new FilterManager(filter.getDefaultFilterTypes(),
+ filter.getCurrentFilterTypes(), filter.getCurrentRetainTypes(), windowAnnotation,
+ windowType, cas);
+ TextMarkerStream stream = new TextMarkerStream(cas, basicIt, basicType, filterManager);
+ return stream;
+ }
+
+ public FSIterator<AnnotationFS> copy() {
+ return new TextMarkerStream(cas, currentIt.copy(), basicType, filter);
+ }
+
+ public AnnotationFS get() throws NoSuchElementException {
+ return currentIt.get();
+ }
+
+ public boolean isValid() {
+ return currentIt.isValid();
+ }
+
+ public void moveTo(FeatureStructure fs) {
+ try {
+ currentIt.moveTo(fs);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ public void moveToFirst() {
+ currentIt.moveToFirst();
+ }
+
+ public void moveToLast() {
+ currentIt.moveToLast();
+ }
+
+ public void moveToNext() {
+ currentIt.moveToNext();
+ }
+
+ public void moveToPrevious() {
+ currentIt.moveToPrevious();
+ }
+
+ public List<AnnotationFS> getOverappingAnnotations(AnnotationFS window, Type type) {
+ List<AnnotationFS> result = new ArrayList<AnnotationFS>();
+ AnnotationFS newWindow = cas.createAnnotation(type, window.getBegin(), window.getEnd() - 1);
+ FSIterator<AnnotationFS> iterator = cas.getAnnotationIndex(type).iterator(newWindow);
+ if (!iterator.isValid()) {
+ iterator.moveToLast();
+ }
+ while (iterator.isValid()) {
+ FeatureStructure fs = iterator.get();
+ if (fs instanceof AnnotationFS) {
+ AnnotationFS a = (AnnotationFS) fs;
+ if (a.getEnd() >= window.getEnd() && a.getBegin() <= window.getBegin()) {
+ result.add(a);
+ }
+ }
+ iterator.moveToPrevious();
+ }
+ return result;
+ }
+
+ public List<Annotation> getAnnotationsFollowing(Annotation annotation) {
+ List<Annotation> result = new ArrayList<Annotation>();
+ moveTo(annotation);
+ while (currentIt.isValid()) {
+ currentIt.moveToNext();
+ if (currentIt.isValid()) {
+ Annotation nextAnnotation = (Annotation) currentIt.get();
+ if (nextAnnotation.getBegin() == annotation.getEnd()) {
+ result.add(nextAnnotation);
+ } else if (nextAnnotation.getBegin() >= annotation.getEnd()) {
+ break;
+ }
+ }
+ }
+ return result;
+ }
+
+ public CAS getCas() {
+ return cas;
+ }
+
+ public JCas getJCas() {
+ try {
+ return cas.getJCas();
+ } catch (CASException e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ public List<AnnotationFS> getAllofType(Type type) {
+ List<AnnotationFS> result = new ArrayList<AnnotationFS>();
+ FSIterator<AnnotationFS> iterator = cas.getAnnotationIndex(type).iterator();
+ while (iterator.isValid()) {
+ FeatureStructure featureStructure = iterator.get();
+ result.add((AnnotationFS) featureStructure);
+ iterator.moveToNext();
+ }
+ return result;
+ }
+
+ public List<AnnotationFS> getAnnotationsInWindow2(AnnotationFS windowAnnotation, Type type) {
+ List<AnnotationFS> result = new ArrayList<AnnotationFS>();
+ // if (windowAnnotation instanceof TextMarkerBasic) {
+ // TextMarkerBasic basic = (TextMarkerBasic) windowAnnotation;
+ // AnnotationFS a = basic.getType(type.getName());
+ // if (a != null) {
+ // result.add(a);
+ // }
+ // return result;
+ // }
+ windowAnnotation = cas.createAnnotation(type, windowAnnotation.getBegin(),
+ windowAnnotation.getEnd() + 1);
+ FSIterator<AnnotationFS> completeIt = getCas().getAnnotationIndex(type).iterator();
+ if (getDocumentAnnotation().getEnd() < windowAnnotation.getEnd()) {
+ completeIt.moveToLast();
+ } else {
+ completeIt.moveTo(windowAnnotation);
+ }
+ while (completeIt.isValid()
+ && ((Annotation) completeIt.get()).getBegin() >= windowAnnotation.getBegin()) {
+ completeIt.moveToPrevious();
+ }
+
+ if (completeIt.isValid()) {
+ completeIt.moveToNext();
+ } else {
+ completeIt.moveToFirst();
+ }
+
+ while (completeIt.isValid()
+ && ((Annotation) completeIt.get()).getBegin() < windowAnnotation.getBegin()) {
+ completeIt.moveToNext();
+ }
+
+ while (completeIt.isValid()
+ && ((Annotation) completeIt.get()).getBegin() >= windowAnnotation.getBegin()) {
+ Annotation annotation = (Annotation) completeIt.get();
+ if (getCas().getTypeSystem().subsumes(type, annotation.getType())
+ && annotation.getEnd() <= windowAnnotation.getEnd()) {
+ result.add(annotation);
+ }
+ completeIt.moveToNext();
+ }
+ return result;
+ }
+
+ public List<AnnotationFS> getAnnotationsInWindow(AnnotationFS windowAnnotation, Type type) {
+ List<AnnotationFS> result = new ArrayList<AnnotationFS>();
+ List<AnnotationFS> inWindow = getAnnotationsInWindow2(windowAnnotation, type);
+ result = inWindow;
+ return result;
+ }
+
+ public Collection<TextMarkerBasic> getAllBasicsInWindow(AnnotationFS windowAnnotation) {
+
+ TextMarkerBasic beginAnchor = getBeginAnchor(windowAnnotation.getBegin());
+ TextMarkerBasic endAnchor = getEndAnchor(windowAnnotation.getEnd());
+ NavigableSet<TextMarkerBasic> subSet = basics.subSet(beginAnchor, true, endAnchor, true);
+ return subSet;
+ // List<TextMarkerBasic> result = new ArrayList<TextMarkerBasic>();
+ // if (windowAnnotation instanceof TextMarkerBasic) {
+ // result.add((TextMarkerBasic) windowAnnotation);
+ // return result;
+ // } else if (windowAnnotation.getBegin() <= documentAnnotation.getBegin()
+ // && windowAnnotation.getEnd() >= documentAnnotation.getEnd()) {
+ // return basics;
+ // }
+ // TextMarkerFrame frame = new TextMarkerFrame(getJCas(), windowAnnotation.getBegin(),
+ // windowAnnotation.getEnd());
+ // FSIterator<AnnotationFS> iterator = cas.getAnnotationIndex(basicType).subiterator(frame);
+ // while (iterator.isValid()) {
+ // result.add((TextMarkerBasic) iterator.get());
+ // iterator.moveToNext();
+ // }
+ // return result;
+ }
+
+ public List<TextMarkerBasic> getBasicsInWindow(AnnotationFS windowAnnotation) {
+ List<TextMarkerBasic> result = new ArrayList<TextMarkerBasic>();
+ if (windowAnnotation instanceof TextMarkerBasic) {
+ result.add((TextMarkerBasic) windowAnnotation);
+ return result;
+ }
+ FSMatchConstraint defaultConstraint = filter.getDefaultConstraint();
+ FSIterator<AnnotationFS> iterator = cas.createFilteredIterator(cas
+ .getAnnotationIndex(basicType).subiterator(windowAnnotation), defaultConstraint);
+
+ while (iterator.isValid()) {
+ result.add((TextMarkerBasic) iterator.get());
+ iterator.moveToNext();
+ }
+ return result;
+ }
+
+ public TextMarkerBasic getFirstBasicInWindow(AnnotationFS windowAnnotation) {
+ return getFirstBasicInWindow(windowAnnotation, currentIt);
+ }
+
+ public TextMarkerBasic getFirstBasicInWindow(AnnotationFS windowAnnotation,
+ FSIterator<AnnotationFS> it) {
+ if (windowAnnotation instanceof TextMarkerBasic) {
+ return (TextMarkerBasic) windowAnnotation;
+ }
+ it.moveTo(windowAnnotation);
+ if (it.isValid()) {
+ return (TextMarkerBasic) it.get();
+ }
+ return null;
+ }
+
+ public List<TextMarkerBasic> getAnnotationsOverlappingWindow(AnnotationFS annotation) {
+ if (annotation != null) {
+ return getBasicsInWindow(annotation);
+ } else {
+ return new ArrayList<TextMarkerBasic>();
+ }
+ }
+
+ public FSIterator<AnnotationFS> getUnfilteredBasicIterator() {
+ return basicIt;
+ }
+
+ public AnnotationFS getDocumentAnnotation() {
+ return documentAnnotation;
+ }
+
+ public TextMarkerAnnotation getCorrectTMA(List<AnnotationFS> annotationsInWindow,
+ TextMarkerAnnotation heuristicAnnotation) {
+ for (AnnotationFS annotation : annotationsInWindow) {
+ if (annotation instanceof TextMarkerAnnotation) {
+ TextMarkerAnnotation tma = (TextMarkerAnnotation) annotation;
+ if (tma.getBegin() == heuristicAnnotation.getBegin()
+ && tma.getEnd() == heuristicAnnotation.getEnd()
+ && tma.getAnnotation().getType()
+ .equals(heuristicAnnotation.getAnnotation().getType())) {
+ return tma;
+ }
+ }
+ }
+ return null;
+ }
+
+ public void retainTypes(List<Type> list) {
+ filter.retainTypes(list);
+ FSMatchConstraint defaultConstraint = filter.getDefaultConstraint();
+ currentIt = cas.createFilteredIterator(basicIt, defaultConstraint);
+ }
+
+ public void filterTypes(List<Type> list) {
+ filter.filterTypes(list);
+ FSMatchConstraint defaultConstraint = filter.getDefaultConstraint();
+ currentIt = cas.createFilteredIterator(basicIt, defaultConstraint);
+ }
+
+ public FilterManager getFilter() {
+ return filter;
+ }
+
+ public TextMarkerBasic getFirstBasicOfAll() {
+ return basics.first();
+ }
+
+ public Type getDocumentAnnotationType() {
+ return documentAnnotationType;
+ }
+
+ public TextMarkerBasic nextAnnotation(RuleElementMatch match) {
+ if (!match.matched()) {
+ return null;
+ }
+ List<AnnotationFS> textsMatched = match.getTextsMatched();
+ AnnotationFS lastAnnotation = null;
+ if (textsMatched != null && !textsMatched.isEmpty()) {
+ lastAnnotation = textsMatched.get(textsMatched.size() - 1);
+ }
+ if (lastAnnotation == null || lastAnnotation.getEnd() >= getDocumentAnnotation().getEnd()) {
+ return null;
+ }
+ // if (getNextBasic(lastAnnotation) == null) {
+ // System.out.println();
+ // }
+ // if (getNextBasic(lastAnnotation) != null
+ // && getNextBasic(lastAnnotation).getBegin() == lastAnnotation.getBegin()) {
+ // System.out.println();
+ // }
+ TextMarkerBasic nextBasic = getNextBasic(lastAnnotation);
+ // TextMarkerBasic nextBasic2 = getNextBasic2(lastAnnotation);
+ // if (nextBasic != nextBasic2) {
+ // String string = nextBasic == null ? "null" : nextBasic.getCoveredText();
+ // String string2 = nextBasic == null ? "null" : (nextBasic.getBegin() + "");
+ // System.out.println("nextBasic.getBegin() != nextBasic2.getBegin() " + string + " "
+ // + nextBasic2.getCoveredText());
+ // System.out.println(lastAnnotation.getBegin() + "=" + string2 + "=" + nextBasic2.getBegin());
+ // }
+ return nextBasic;
+ }
+
+ public TextMarkerBasic getNextBasic(AnnotationFS previous) {
+ TextMarkerBasic pointer = pointerMap.get(previous.getEnd());
+ if (pointer == null) {
+ // FIXME: hotfix for ML stuff
+ // pointer = new TextMarkerFrame(getJCas(), previous.getEnd()-1, previous.getEnd());
+ pointer = (TextMarkerBasic) cas.createAnnotation(basicType, previous.getEnd() - 1,
+ previous.getEnd());
+ pointerMap.put(previous.getEnd(), pointer);
+ }
+ currentIt.moveTo(pointer);
+ if (currentIt.isValid()) {
+ TextMarkerBasic basic = (TextMarkerBasic) currentIt.get();
+ if (basic.getBegin() == previous.getBegin()) {
+ // if (basic.getBegin() >= previous.getBegin() || basic.getEnd() <= previous.getEnd()) {
+ currentIt.moveToNext();
+ if (currentIt.isValid()) {
+ return (TextMarkerBasic) currentIt.get();
+ } else {
+ return null;
+ }
+ } else {
+ return basic;
+ }
+ }
+ return null;
+ }
+
+ public TextMarkerBasic getNextBasic2(AnnotationFS previous) {
+ AnnotationFS pointer = cas
+ .createAnnotation(basicType, previous.getEnd() - 1, previous.getEnd());
+ currentIt.moveTo(pointer);
+ if (currentIt.isValid()) {
+ TextMarkerBasic basic = (TextMarkerBasic) currentIt.get();
+ return basic;
+ }
+ return null;
+ }
+
+ public TextMarkerStream getCompleteStream() {
+ FilterManager defaultFilter = new FilterManager(filter.getDefaultFilterTypes(), getCas());
+ return new TextMarkerStream(getCas(), basicIt, basicType, defaultFilter);
+ }
+
+ public int getHistogram(Type type) {
+ return cas.getAnnotationIndex(type).size();
+ }
+
+ public double getIndexPenalty() {
+ return indexPenalty;
+ }
+
+ public TextMarkerBasic getEndAnchor(int end) {
+ return endAnchors.get(end);
+ }
+
+ public TextMarkerBasic getBeginAnchor(int begin) {
+ return beginAnchors.get(begin);
+ }
+
+ public boolean isDynamicAnchoring() {
+ return dynamicAnchoring;
+ }
+
+ public void setDynamicAnchoring(boolean dynamicAnchoring) {
+ this.dynamicAnchoring = dynamicAnchoring;
+ }
+
+ public void setIndexPenalty(double indexPenalty) {
+ this.indexPenalty = indexPenalty;
+ }
+
+ public double getAnchoringFactor() {
+ return anchoringFactor;
+ }
+
+ public void setAnchoringFactor(double anchoringFactor) {
+ this.anchoringFactor = anchoringFactor;
+ }
+
+}
Added: uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/UIMAConstants.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/UIMAConstants.java?rev=1243619&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/UIMAConstants.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/UIMAConstants.java Mon Feb 13 17:44:52 2012
@@ -0,0 +1,40 @@
+/*
+ * 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;
+
+public class UIMAConstants {
+
+ public static final String TYPE_INTEGER = "uima.cas.Integer";
+
+ public static final String TYPE_FLOAT = "uima.cas.Float";
+
+ public static final String TYPE_BOOLEAN = "uima.cas.Boolean";
+
+ public static final String TYPE_BYTE = "uima.cas.Byte";
+
+ public static final String TYPE_SHORT = "uima.cas.Short";
+
+ public static final String TYPE_LONG = "uima.cas.Long";
+
+ public static final String TYPE_DOUBLE = "uima.cas.Double";
+
+ public static final String TYPE_STRING = "uima.cas.String";
+
+}
Added: uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/action/AbstractMarkAction.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/action/AbstractMarkAction.java?rev=1243619&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/action/AbstractMarkAction.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/action/AbstractMarkAction.java Mon Feb 13 17:44:52 2012
@@ -0,0 +1,85 @@
+/*
+ * 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.action;
+
+import java.util.ArrayList;
+import java.util.List;
+
+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.expression.number.NumberExpression;
+import org.apache.uima.textmarker.expression.type.TypeExpression;
+import org.apache.uima.textmarker.rule.RuleElement;
+import org.apache.uima.textmarker.type.TextMarkerBasic;
+
+public abstract class AbstractMarkAction extends TypeSensitiveAction {
+
+ public AbstractMarkAction(TypeExpression type) {
+ super(type);
+ }
+
+ protected void createAnnotation(AnnotationFS matchedAnnotation, RuleElement element,
+ TextMarkerStream stream) {
+ TextMarkerBasic first = stream.getFirstBasicInWindow(matchedAnnotation);
+ createAnnotation(first, element, stream, matchedAnnotation);
+ }
+
+ protected Annotation createAnnotation(TextMarkerBasic anchor, RuleElement element,
+ TextMarkerStream stream, AnnotationFS matchedAnnotation) {
+ Type t = type.getType(element.getParent());
+ AnnotationFS newAnnotationFS = stream.getCas().createAnnotation(t,
+ matchedAnnotation.getBegin(), matchedAnnotation.getEnd());
+ Annotation newAnnotation = null;
+ if (newAnnotationFS instanceof Annotation) {
+ newAnnotation = (Annotation) newAnnotationFS;
+ newAnnotation.addToIndexes();
+ } else {
+ return null;
+ }
+ stream.addAnnotation(newAnnotation);
+ return newAnnotation;
+ }
+
+ @Override
+ public String toString() {
+ return super.toString() + "," + type.getClass().getSimpleName();
+ }
+
+ protected List<Integer> getIndexList(RuleElement element, List<NumberExpression> list) {
+ List<Integer> indexList = new ArrayList<Integer>();
+ if (list == null || list.isEmpty()) {
+ int self = element.getContainer().getRuleElements().indexOf(element) + 1;
+ indexList.add(self);
+ return indexList;
+ }
+ int last = Integer.MAX_VALUE - 1;
+ for (NumberExpression each : list) {
+ int value = each.getIntegerValue(element.getParent());
+ for (int i = Math.min(value, last + 1); i < value; i++) {
+ indexList.add(i);
+ }
+ indexList.add(value);
+ }
+ return indexList;
+ }
+
+}
Added: uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/action/AbstractStructureAction.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/action/AbstractStructureAction.java?rev=1243619&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/action/AbstractStructureAction.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/action/AbstractStructureAction.java Mon Feb 13 17:44:52 2012
@@ -0,0 +1,121 @@
+/*
+ * 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.action;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.apache.uima.cas.Feature;
+import org.apache.uima.cas.Type;
+import org.apache.uima.cas.TypeSystem;
+import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.jcas.JCas;
+import org.apache.uima.jcas.cas.FSArray;
+import org.apache.uima.jcas.cas.TOP;
+import org.apache.uima.textmarker.TextMarkerStream;
+import org.apache.uima.textmarker.UIMAConstants;
+import org.apache.uima.textmarker.expression.TextMarkerExpression;
+import org.apache.uima.textmarker.expression.bool.BooleanExpression;
+import org.apache.uima.textmarker.expression.number.NumberExpression;
+import org.apache.uima.textmarker.expression.string.StringExpression;
+import org.apache.uima.textmarker.expression.type.TypeExpression;
+import org.apache.uima.textmarker.rule.RuleElement;
+import org.apache.uima.textmarker.utils.UIMAUtils;
+
+public abstract class AbstractStructureAction extends AbstractTextMarkerAction {
+
+ public AbstractStructureAction() {
+ super();
+ }
+
+ protected void fillFeatures(TOP structure, Map<StringExpression, TextMarkerExpression> features,
+ AnnotationFS matchedAnnotation, RuleElement element, TextMarkerStream stream) {
+ Map<String, TextMarkerExpression> map = new HashMap<String, TextMarkerExpression>();
+ for (Entry<StringExpression, TextMarkerExpression> each : features.entrySet()) {
+ String value = each.getKey().getStringValue(element.getParent());
+ map.put(value, each.getValue());
+ }
+
+ TypeSystem typeSystem = stream.getCas().getTypeSystem();
+ JCas jcas = stream.getJCas();
+ List<?> featuresList = structure.getType().getFeatures();
+ for (int i = 0; i < featuresList.size(); i++) {
+ Feature targetFeature = (Feature) featuresList.get(i);
+ String name = targetFeature.getName();
+ String shortFName = name.substring(name.indexOf(":") + 1, name.length());
+ Object valueObject = map.get(shortFName);
+ Type range = targetFeature.getRange();
+ if (valueObject != null) {
+ if (valueObject instanceof StringExpression && range.getName().equals(UIMAConstants.TYPE_STRING)) {
+ structure.setStringValue(targetFeature,
+ ((StringExpression) valueObject).getStringValue(element.getParent()));
+ } else if (valueObject instanceof TypeExpression
+ && range.getName().equals(UIMAConstants.TYPE_STRING)) {
+ TypeExpression type = (TypeExpression) valueObject;
+ List<AnnotationFS> annotationsInWindow = stream.getAnnotationsInWindow(matchedAnnotation,
+ type.getType(element.getParent()));
+ if (annotationsInWindow != null && !annotationsInWindow.isEmpty()) {
+ AnnotationFS annotation = annotationsInWindow.get(0);
+ structure.setStringValue(targetFeature, annotation.getCoveredText());
+ }
+ } else if (valueObject instanceof NumberExpression) {
+ if(range.getName().equals(UIMAConstants.TYPE_INTEGER)) {
+ structure.setIntValue(targetFeature,
+ ((NumberExpression) valueObject).getIntegerValue(element.getParent()));
+ } else if(range.getName().equals(UIMAConstants.TYPE_DOUBLE)) {
+ structure.setDoubleValue(targetFeature,
+ ((NumberExpression) valueObject).getDoubleValue(element.getParent()));
+ } else if(range.getName().equals(UIMAConstants.TYPE_FLOAT)) {
+ structure.setFloatValue(targetFeature,
+ ((NumberExpression) valueObject).getFloatValue(element.getParent()));
+ } else if(range.getName().equals(UIMAConstants.TYPE_BYTE)) {
+ structure.setByteValue(targetFeature, (byte)
+ ((NumberExpression) valueObject).getIntegerValue(element.getParent()));
+ } else if(range.getName().equals(UIMAConstants.TYPE_SHORT)) {
+ structure.setShortValue(targetFeature, (short)
+ ((NumberExpression) valueObject).getIntegerValue(element.getParent()));
+ } else if(range.getName().equals(UIMAConstants.TYPE_LONG)) {
+ structure.setLongValue(targetFeature, (long)
+ ((NumberExpression) valueObject).getIntegerValue(element.getParent()));
+ }
+ } else if (valueObject instanceof BooleanExpression
+ && range.getName().equals(UIMAConstants.TYPE_BOOLEAN)) {
+ structure.setBooleanValue(targetFeature,
+ ((BooleanExpression) valueObject).getBooleanValue(element.getParent()));
+ } else if (valueObject instanceof TypeExpression) {
+ TypeExpression type = (TypeExpression) valueObject;
+ List<AnnotationFS> annotationsInWindow = stream.getAnnotationsInWindow(matchedAnnotation,
+ type.getType(element.getParent()));
+ if (typeSystem.subsumes(jcas.getCasType(FSArray.type), range)) {
+ structure
+ .setFeatureValue(targetFeature, UIMAUtils.toFSArray(jcas, annotationsInWindow));
+ } else if (typeSystem.subsumes(range, type.getType(element.getParent()))
+ && !annotationsInWindow.isEmpty()) {
+ AnnotationFS annotation = annotationsInWindow.get(0);
+ structure.setFeatureValue(targetFeature, annotation);
+ }
+ }
+ }
+
+ }
+ }
+}
Added: uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/action/AbstractTextMarkerAction.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/action/AbstractTextMarkerAction.java?rev=1243619&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/action/AbstractTextMarkerAction.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/action/AbstractTextMarkerAction.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.action;
+
+import org.apache.uima.textmarker.TextMarkerElement;
+import org.apache.uima.textmarker.TextMarkerStream;
+import org.apache.uima.textmarker.rule.RuleElement;
+import org.apache.uima.textmarker.rule.RuleMatch;
+import org.apache.uima.textmarker.visitor.InferenceCrowd;
+
+public abstract class AbstractTextMarkerAction extends TextMarkerElement {
+
+ public AbstractTextMarkerAction() {
+ super();
+ }
+
+ public abstract void execute(RuleMatch match, RuleElement element, TextMarkerStream stream,
+ InferenceCrowd crowd);
+
+ @Override
+ public String toString() {
+ return getClass().getSimpleName();
+ }
+
+}