You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by jo...@apache.org on 2011/08/12 12:44:18 UTC
svn commit: r1157040 [22/23] - in
/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine: ./ .settings/
META-INF/ desc/ lib/ src/ src/main/ src/main/java/ src/main/java/org/
src/main/java/org/apache/ src/main/java/org/apache/uima/
src/main/java/org/...
Added: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/utils/UIMAUtils.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/utils/UIMAUtils.java?rev=1157040&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/utils/UIMAUtils.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/utils/UIMAUtils.java Fri Aug 12 10:44:04 2011
@@ -0,0 +1,70 @@
+/*
+ * 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.utils;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.uima.cas.FeatureStructure;
+import org.apache.uima.jcas.JCas;
+import org.apache.uima.jcas.cas.DoubleArray;
+import org.apache.uima.jcas.cas.FSArray;
+import org.apache.uima.jcas.cas.IntegerArray;
+import org.apache.uima.jcas.cas.StringArray;
+
+public class UIMAUtils {
+
+ public static FSArray toFSArray(JCas jCas, List<? extends FeatureStructure> fsList) {
+ FSArray fsArray = new FSArray(jCas, fsList.size());
+ fsArray.copyFromArray(fsList.toArray(new FeatureStructure[fsList.size()]), 0, 0, fsList.size());
+ return fsArray;
+ }
+
+ public static StringArray toStringArray(JCas jCas, String[] sArray) {
+ StringArray uimaSArray = new StringArray(jCas, sArray.length);
+ uimaSArray.copyFromArray(sArray, 0, 0, sArray.length);
+ return uimaSArray;
+ }
+
+ public static DoubleArray toDoubleArray(JCas jCas, double[] sArray) {
+ DoubleArray uimaSArray = new DoubleArray(jCas, sArray.length);
+ uimaSArray.copyFromArray(sArray, 0, 0, sArray.length);
+ return uimaSArray;
+ }
+
+ public static IntegerArray toIntegerArray(JCas jCas, int[] sArray) {
+ IntegerArray uimaSArray = new IntegerArray(jCas, sArray.length);
+ uimaSArray.copyFromArray(sArray, 0, 0, sArray.length);
+ return uimaSArray;
+ }
+
+ public static <T extends FeatureStructure> List<T> toList(FSArray fsArray, Class<T> cls) {
+ List<T> list = new ArrayList<T>();
+ if (fsArray == null) {
+ return list;
+ }
+ for (FeatureStructure fs : fsArray.toArray()) {
+ list.add(cls.cast(fs));
+ }
+ return list;
+
+ }
+
+}
Propchange: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/utils/UIMAUtils.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/utils/UIMAUtils.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/ActionVerbalizer.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/ActionVerbalizer.java?rev=1157040&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/ActionVerbalizer.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/ActionVerbalizer.java Fri Aug 12 10:44:04 2011
@@ -0,0 +1,344 @@
+/*
+ * 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.verbalize;
+
+import java.util.Map.Entry;
+
+import org.apache.uima.textmarker.action.AbstractTextMarkerAction;
+import org.apache.uima.textmarker.action.AddAction;
+import org.apache.uima.textmarker.action.AssignAction;
+import org.apache.uima.textmarker.action.CallAction;
+import org.apache.uima.textmarker.action.ColorAction;
+import org.apache.uima.textmarker.action.CreateAction;
+import org.apache.uima.textmarker.action.DelAction;
+import org.apache.uima.textmarker.action.ExpandAction;
+import org.apache.uima.textmarker.action.FillAction;
+import org.apache.uima.textmarker.action.FilterMarkupAction;
+import org.apache.uima.textmarker.action.FilterTypeAction;
+import org.apache.uima.textmarker.action.GatherAction;
+import org.apache.uima.textmarker.action.GetAction;
+import org.apache.uima.textmarker.action.GetFeatureAction;
+import org.apache.uima.textmarker.action.LogAction;
+import org.apache.uima.textmarker.action.MarkAction;
+import org.apache.uima.textmarker.action.MarkFastAction;
+import org.apache.uima.textmarker.action.MarkLastAction;
+import org.apache.uima.textmarker.action.MarkOnceAction;
+import org.apache.uima.textmarker.action.MergeAction;
+import org.apache.uima.textmarker.action.RemoveAction;
+import org.apache.uima.textmarker.action.RemoveDuplicateAction;
+import org.apache.uima.textmarker.action.ReplaceAction;
+import org.apache.uima.textmarker.action.RetainMarkupAction;
+import org.apache.uima.textmarker.action.RetainTypeAction;
+import org.apache.uima.textmarker.action.SetFeatureAction;
+import org.apache.uima.textmarker.action.TransferAction;
+import org.apache.uima.textmarker.action.TrieAction;
+import org.apache.uima.textmarker.action.UnmarkAction;
+import org.apache.uima.textmarker.expression.TextMarkerExpression;
+import org.apache.uima.textmarker.expression.string.StringExpression;
+
+
+public class ActionVerbalizer {
+
+ private TextMarkerVerbalizer verbalizer;
+
+ public ActionVerbalizer(TextMarkerVerbalizer verbalizer) {
+ super();
+ this.verbalizer = verbalizer;
+ }
+
+ public String verbalizeName(AbstractTextMarkerAction action) {
+ if (action instanceof AddAction) {
+ return "ADD";
+ } else if (action instanceof AssignAction) {
+ return "ASSIGN";
+ } else if (action instanceof CallAction) {
+ return "CALL";
+ } else if (action instanceof ColorAction) {
+ return "COLOR";
+ } else if (action instanceof CreateAction) {
+ return "CREATE";
+ } else if (action instanceof DelAction) {
+ return "DEL";
+ } else if (action instanceof FillAction) {
+ return "FILL";
+ } else if (action instanceof FilterMarkupAction) {
+ return "FILTERMARKUP";
+ } else if (action instanceof FilterTypeAction) {
+ return "FILTERTYPE";
+ } else if (action instanceof LogAction) {
+ return "LOG";
+ } else if (action instanceof MarkOnceAction) {
+ return "MARKONCE";
+ } else if (action instanceof ExpandAction) {
+ return "EXPAND";
+ } else if (action instanceof MarkAction) {
+ MarkAction a = (MarkAction) action;
+ if (a.getScore() != null) {
+ return "MARKSCORE";
+ }
+ return "MARK";
+ } else if (action instanceof MarkFastAction) {
+ return "MARKFAST";
+ } else if (action instanceof MarkLastAction) {
+ return "MARKLAST";
+ } else if (action instanceof ReplaceAction) {
+ return "REPLACE";
+ } else if (action instanceof RetainMarkupAction) {
+ return "RETAINMARKUP";
+ } else if (action instanceof RetainTypeAction) {
+ return "RETAINTYPE";
+ } else if (action instanceof SetFeatureAction) {
+ String name = "SETFEATURE";
+ return name;
+ } else if (action instanceof GetFeatureAction) {
+ String name = "GETFEATURE";
+ return name;
+ } else if (action instanceof UnmarkAction) {
+ return "UNMARK";
+ } else if (action instanceof TransferAction) {
+ return "TRANSFER";
+ } else if (action instanceof TrieAction) {
+ return "TRIE";
+ } else if (action instanceof GatherAction) {
+ return "GATHER";
+ } else if (action instanceof MergeAction) {
+ return "MERGE";
+ } else if (action instanceof GetAction) {
+ return "GET";
+ } else if (action instanceof RemoveAction) {
+ return "REMOVE";
+ } else if (action instanceof RemoveDuplicateAction) {
+ return "REMOVEDUPLICATE";
+ }
+ return action.getClass().getSimpleName();
+ }
+
+ public String verbalize(AbstractTextMarkerAction action) {
+ if (action instanceof AssignAction) {
+ AssignAction a = (AssignAction) action;
+ return "ASSIGN(" + a.getVar() + "," + verbalizer.verbalize(a.getExpression()) + ")";
+ } else if (action instanceof CallAction) {
+ CallAction a = (CallAction) action;
+ return "CALL(" + a.getNamespace() + ")";
+ } else if (action instanceof ColorAction) {
+ ColorAction a = (ColorAction) action;
+ return "COLOR(" + verbalizer.verbalize(a.getType()) + ","
+ + verbalizer.verbalize(a.getBgColor()) + "," + verbalizer.verbalize(a.getFgColor())
+ + "," + verbalizer.verbalize(a.getSelected()) + ")";
+ } else if (action instanceof CreateAction) {
+ CreateAction a = (CreateAction) action;
+ StringBuilder features = new StringBuilder();
+ if (a.getFeatures() != null) {
+ features.append(",");
+ for (Entry<StringExpression, TextMarkerExpression> each : a.getFeatures().entrySet()) {
+ features.append(verbalizer.verbalize(each.getKey()));
+ features.append("=");
+ features.append(verbalizer.verbalize(each.getValue()));
+ features.append(",");
+ }
+ }
+ String feats = features.toString();
+ if (feats.endsWith(",")) {
+ feats = feats.substring(0, features.length() - 1);
+ }
+ String indexes = "";
+ if (a.getIndexes() != null) {
+ indexes = verbalizer.verbalizeExpressionList(a.getIndexes());
+ indexes += ", ";
+ }
+ return "CREATE(" + verbalizer.verbalize(a.getStructureType()) + indexes + feats + ")";
+ } else if (action instanceof GatherAction) {
+ GatherAction a = (GatherAction) action;
+ String features = "";
+ if (a.getFeatures() != null) {
+ features += ", ";
+ for (StringExpression each : a.getFeatures().keySet()) {
+ features += verbalizer.verbalize(each);
+ features += "=";
+ features += verbalizer.verbalize(a.getFeatures().get(each));
+ features += ", ";
+ }
+ }
+ if (features.endsWith(", ")) {
+ features = features.substring(0, features.length() - 2);
+ }
+ String indexes = "";
+ if (a.getIndexes() != null) {
+ indexes += ", ";
+ indexes = verbalizer.verbalizeExpressionList(a.getIndexes());
+ }
+ if (indexes.endsWith(", ") && !a.getFeatures().isEmpty()) {
+ indexes = indexes.substring(0, features.length() - 1);
+ }
+ return "GATHER(" + verbalizer.verbalize(a.getStructureType()) + ", " + indexes + features
+ + ")";
+ } else if (action instanceof DelAction) {
+ return "DEL";
+ } else if (action instanceof FillAction) {
+ FillAction a = (FillAction) action;
+ String features = "";
+ if (a.getFeatures() != null) {
+ features += ",";
+ for (StringExpression each : a.getFeatures().keySet()) {
+ features += verbalizer.verbalize(each);
+ features += "=";
+ features += verbalizer.verbalize(a.getFeatures().get(each));
+ features += ",";
+ }
+ }
+ if (features.endsWith(",")) {
+ features = features.substring(0, features.length() - 1);
+ }
+ return "FILL(" + verbalizer.verbalize(a.getStructureType()) + features + ")";
+ } else if (action instanceof FilterMarkupAction) {
+ FilterMarkupAction a = (FilterMarkupAction) action;
+ return a.getMarkup().isEmpty() ? "FILTERMARKUP" : "FILTERMARKUP("
+ + verbalizer.verbalizeExpressionList(a.getMarkup()) + ")";
+ } else if (action instanceof FilterTypeAction) {
+ FilterTypeAction a = (FilterTypeAction) action;
+ return a.getList().isEmpty() ? "FILTERTYPE" : "FILTERTYPE("
+ + verbalizer.verbalizeExpressionList(a.getList()) + ")";
+ } else if (action instanceof LogAction) {
+ LogAction a = (LogAction) action;
+ return "LOG(" + verbalizer.verbalize(a.getText()) + "," + a.getLevel() + ")";
+ } else if (action instanceof MarkOnceAction) {
+ MarkOnceAction a = (MarkOnceAction) action;
+ // String score = verbalizer.verbalize(a.getScore());
+ // if (!"".equals(score)) {
+ // score += ",";
+ // }
+ String string = "";
+ if (a.getList() != null && !a.getList().isEmpty()) {
+ string = "," + verbalizer.verbalizeExpressionList(a.getList());
+ }
+ return "MARKONCE(" + verbalizer.verbalize(a.getType()) + string + ")";
+ } else if (action instanceof ExpandAction) {
+ ExpandAction a = (ExpandAction) action;
+ String string = "";
+ if (a.getList() != null && !a.getList().isEmpty()) {
+ string = "," + verbalizer.verbalizeExpressionList(a.getList());
+ }
+ return "EXPAND(" + verbalizer.verbalize(a.getType()) + string + ")";
+ } else if (action instanceof MarkAction) {
+ MarkAction a = (MarkAction) action;
+ if (a.getScore() != null) {
+ String score = verbalizer.verbalize(a.getScore());
+ if (!"".equals(score)) {
+ score += ",";
+ }
+ String string = "";
+ if (a.getList() != null && !a.getList().isEmpty()) {
+ string = "," + verbalizer.verbalizeExpressionList(a.getList());
+ }
+ return "MARKSCORE(" + score + verbalizer.verbalize(a.getType()) + string + ")";
+ } else {
+ String string = "";
+ if (a.getList() != null && !a.getList().isEmpty()) {
+ string = "," + verbalizer.verbalizeExpressionList(a.getList());
+ }
+ return "MARK(" + verbalizer.verbalize(a.getType()) + string + ")";
+ }
+ } else if (action instanceof MarkFastAction) {
+ MarkFastAction a = (MarkFastAction) action;
+ return "MARKFAST(" + verbalizer.verbalize(a.getType()) + ","
+ + verbalizer.verbalize(a.getList()) + ")";
+ } else if (action instanceof MarkLastAction) {
+ MarkLastAction a = (MarkLastAction) action;
+ return "MARKLAST(" + verbalizer.verbalize(a.getType()) + ")";
+ } else if (action instanceof ReplaceAction) {
+ ReplaceAction a = (ReplaceAction) action;
+ return "REPLACE(" + verbalizer.verbalize(a.getReplacement()) + ")";
+ } else if (action instanceof RetainMarkupAction) {
+ RetainMarkupAction a = (RetainMarkupAction) action;
+ return a.getMarkup().isEmpty() ? "RETAINMARKUP" : "RETAINMARKUP("
+ + verbalizer.verbalizeExpressionList(a.getMarkup()) + ")";
+ } else if (action instanceof RetainTypeAction) {
+ RetainTypeAction a = (RetainTypeAction) action;
+ return a.getList().isEmpty() ? "RETAINTYPE" : "RETAINTYPE("
+ + verbalizer.verbalizeExpressionList(a.getList()) + ")";
+ } else if (action instanceof SetFeatureAction) {
+ SetFeatureAction a = (SetFeatureAction) action;
+ String e1 = verbalizer.verbalize(a.getFeatureStringExpression());
+ String name = "";
+ String e2 = "";
+ if (a.getBooleanExpr() != null) {
+ name = "SETFEATURE(";
+ e2 = verbalizer.verbalize(a.getBooleanExpr());
+ } else if (a.getNumberExpr() != null) {
+ name = "SETFEATURE(";
+ e2 = verbalizer.verbalize(a.getNumberExpr());
+ } else if (a.getStringExpr() != null) {
+ name = "SETFEATURE(";
+ e2 = verbalizer.verbalize(a.getStringExpr());
+ }
+ return name + e1 + "," + e2 + ")";
+ } else if (action instanceof GetFeatureAction) {
+ GetFeatureAction a = (GetFeatureAction) action;
+ String name = "GETFEATURE(";
+ return name + verbalizer.verbalize(a.getFeatureStringExpression()) + "," + a.getVariable()
+ + ")";
+ } else if (action instanceof UnmarkAction) {
+ UnmarkAction a = (UnmarkAction) action;
+ return "UNMARK(" + verbalizer.verbalize(a.getType()) + ")";
+ } else if (action instanceof TransferAction) {
+ TransferAction a = (TransferAction) action;
+ return "TRANSFER(" + verbalizer.verbalize(a.getType()) + ")";
+
+ } else if (action instanceof TrieAction) {
+ TrieAction a = (TrieAction) action;
+ String map = "";
+ if (a.getMap() != null) {
+ map += ",";
+ for (StringExpression each : a.getMap().keySet()) {
+ map += verbalizer.verbalize(each);
+ map += "=";
+ map += verbalizer.verbalize(a.getMap().get(each));
+ map += ",";
+ }
+ }
+ return "TRIE(" + map + verbalizer.verbalize(a.getList()) + ","
+ + verbalizer.verbalize(a.getIgnoreCase()) + ","
+ + verbalizer.verbalize(a.getIgnoreLength()) + "," + verbalizer.verbalize(a.getEdit())
+ + "," + verbalizer.verbalize(a.getDistance()) + ","
+ + verbalizer.verbalize(a.getIgnoreChar()) + ")";
+ } else if (action instanceof AddAction) {
+ AddAction a = (AddAction) action;
+ return "ADD(" + a.getListExpr() + "," + verbalizer.verbalizeExpressionList(a.getElements())
+ + ")";
+ } else if (action instanceof RemoveAction) {
+ RemoveAction a = (RemoveAction) action;
+ return "REMOVE(" + a.getListExpr() + ","
+ + verbalizer.verbalizeExpressionList(a.getElements()) + ")";
+ } else if (action instanceof RemoveAction) {
+ RemoveAction a = (RemoveAction) action;
+ return "REMOVEDUPLICATE(" + a.getListExpr() + ")";
+ } else if (action instanceof MergeAction) {
+ MergeAction a = (MergeAction) action;
+ return "MERGE(" + verbalizer.verbalize(a.getUnion()) + "," + a.getTarget() + ","
+ + verbalizer.verbalizeExpressionList(a.getLists()) + ")";
+ } else if (action instanceof GetAction) {
+ GetAction a = (GetAction) action;
+ return "GET(" + verbalizer.verbalize(a.getListExpr()) + "," + a.getVar() + ","
+ + verbalizer.verbalize(a.getOpExpr()) + ")";
+ }
+
+ return action.getClass().getSimpleName();
+ }
+}
Propchange: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/ActionVerbalizer.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/ActionVerbalizer.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/ConditionVerbalizer.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/ConditionVerbalizer.java?rev=1157040&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/ConditionVerbalizer.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/ConditionVerbalizer.java Fri Aug 12 10:44:04 2011
@@ -0,0 +1,439 @@
+/*
+ * 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.verbalize;
+
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.uima.textmarker.condition.AbstractTextMarkerCondition;
+import org.apache.uima.textmarker.condition.AfterCondition;
+import org.apache.uima.textmarker.condition.AndCondition;
+import org.apache.uima.textmarker.condition.BeforeCondition;
+import org.apache.uima.textmarker.condition.ContainsCondition;
+import org.apache.uima.textmarker.condition.ContextCountCondition;
+import org.apache.uima.textmarker.condition.CountCondition;
+import org.apache.uima.textmarker.condition.CurrentCountCondition;
+import org.apache.uima.textmarker.condition.EndsWithCondition;
+import org.apache.uima.textmarker.condition.FeatureCondition;
+import org.apache.uima.textmarker.condition.IfCondition;
+import org.apache.uima.textmarker.condition.InListCondition;
+import org.apache.uima.textmarker.condition.IsCondition;
+import org.apache.uima.textmarker.condition.IsInTagCondition;
+import org.apache.uima.textmarker.condition.LastCondition;
+import org.apache.uima.textmarker.condition.MOfNCondition;
+import org.apache.uima.textmarker.condition.NearCondition;
+import org.apache.uima.textmarker.condition.NotCondition;
+import org.apache.uima.textmarker.condition.OrCondition;
+import org.apache.uima.textmarker.condition.ParseCondition;
+import org.apache.uima.textmarker.condition.PartOfCondition;
+import org.apache.uima.textmarker.condition.PartOfNeqCondition;
+import org.apache.uima.textmarker.condition.PositionCondition;
+import org.apache.uima.textmarker.condition.RegExpCondition;
+import org.apache.uima.textmarker.condition.ScoreCondition;
+import org.apache.uima.textmarker.condition.SizeCondition;
+import org.apache.uima.textmarker.condition.StartsWithCondition;
+import org.apache.uima.textmarker.condition.TotalCountCondition;
+import org.apache.uima.textmarker.condition.VoteCondition;
+import org.apache.uima.textmarker.expression.number.NumberExpression;
+import org.apache.uima.textmarker.expression.number.SimpleNumberExpression;
+
+
+public class ConditionVerbalizer {
+
+ private TextMarkerVerbalizer verbalizer;
+
+ public ConditionVerbalizer(TextMarkerVerbalizer verbalizer) {
+ super();
+ this.verbalizer = verbalizer;
+ }
+
+ public String verbalizeName(AbstractTextMarkerCondition condition) {
+ if (condition instanceof AndCondition) {
+ return "AND";
+ } else if (condition instanceof ContainsCondition) {
+ String name = "CONTAINS";
+ return name;
+ } else if (condition instanceof ContextCountCondition) {
+ String name = "CONTEXTCOUNT";
+ return name;
+ } else if (condition instanceof CountCondition) {
+ String name = "COUNT";
+ return name;
+ } else if (condition instanceof CurrentCountCondition) {
+ String name = "CURRENTCOUNT";
+ return name;
+ } else if (condition instanceof IfCondition) {
+ String name = "IF";
+ return name;
+ } else if (condition instanceof InListCondition) {
+ return "INLIST";
+ } else if (condition instanceof IsInTagCondition) {
+ return "ISINTAG";
+ } else if (condition instanceof LastCondition) {
+ return "LAST";
+ } else if (condition instanceof MOfNCondition) {
+ return "MOFN";
+ } else if (condition instanceof NearCondition) {
+ String name = "NEAR";
+ return name;
+ } else if (condition instanceof NotCondition) {
+ return "NOT";
+ } else if (condition instanceof OrCondition) {
+ return "OR";
+ } else if (condition instanceof PartOfCondition) {
+ return "PARTOF";
+ } else if (condition instanceof PartOfNeqCondition) {
+ return "PARTOFNEQ";
+ } else if (condition instanceof PositionCondition) {
+ return "POSITION";
+ } else if (condition instanceof RegExpCondition) {
+ return "REGEXP";
+ } else if (condition instanceof ScoreCondition) {
+ String name = "SCORE";
+ return name;
+ } else if (condition instanceof TotalCountCondition) {
+ String name = "TOTALCOUNT";
+ return name;
+ } else if (condition instanceof VoteCondition) {
+ String name = "VOTE";
+ return name;
+ } else if (condition instanceof FeatureCondition) {
+ String name = "FEATURE";
+ return name;
+ } else if (condition instanceof ParseCondition) {
+ String name = "PARSE";
+ return name;
+ } else if (condition instanceof IsCondition) {
+ String name = "IS";
+ return name;
+ } else if (condition instanceof BeforeCondition) {
+ String name = "BEFORE";
+ return name;
+ } else if (condition instanceof AfterCondition) {
+ String name = "AFTER";
+ return name;
+ } else if (condition instanceof StartsWithCondition) {
+ String name = "STARTSWITH";
+ return name;
+ } else if (condition instanceof EndsWithCondition) {
+ String name = "ENDSWITH";
+ return name;
+ } else if (condition instanceof SizeCondition) {
+ String name = "SIZE";
+ return name;
+ }
+
+ return condition.getClass().getSimpleName();
+ }
+
+ public String verbalize(AbstractTextMarkerCondition condition) {
+ if (condition instanceof AndCondition) {
+ AndCondition c = (AndCondition) condition;
+ List<AbstractTextMarkerCondition> conditions = c.getConditions();
+ StringBuilder sb = new StringBuilder();
+ sb.append("AND(");
+ Iterator<AbstractTextMarkerCondition> it = conditions.iterator();
+ while (it.hasNext()) {
+ AbstractTextMarkerCondition each = (AbstractTextMarkerCondition) it.next();
+ sb.append(verbalize(each));
+ if (it.hasNext()) {
+ sb.append(",");
+ }
+ }
+ sb.append(")");
+ return sb.toString();
+ } else if (condition instanceof ContainsCondition) {
+ ContainsCondition c = (ContainsCondition) condition;
+ String name = "CONTAINS(";
+ NumberExpression minE = c.getMin();
+ String min = verbalizeMin(minE, 1);
+ NumberExpression maxE = c.getMax();
+ String max = verbalizeMax(maxE, Integer.MAX_VALUE);
+ String percent = verbalizer.verbalize(c.getPercent());
+ percent = !percent.equals("false") ? "," + percent : "";
+ if (c.getType() != null) {
+ String type = verbalizer.verbalize(c.getType());
+ return name + type + min + max + percent + ")";
+ } else {
+ return name + verbalizer.verbalize(c.getArgList()) + "," + verbalizer.verbalize(c.getArg())
+ + min + max + percent + ")";
+ }
+ } else if (condition instanceof ContextCountCondition) {
+ ContextCountCondition c = (ContextCountCondition) condition;
+ String name = "CONTEXTCOUNT(";
+ String type = verbalizer.verbalize(c.getType());
+ NumberExpression minE = c.getMin();
+ String min = verbalizeMin(minE, Integer.MIN_VALUE);
+ NumberExpression maxE = c.getMax();
+ String max = verbalizeMax(maxE, Integer.MAX_VALUE);
+ String var = c.getVar() == null ? "" : "," + c.getVar();
+ return name + type + min + max + var + ")";
+ } else if (condition instanceof CountCondition) {
+ CountCondition c = (CountCondition) condition;
+ if (c.getArg() == null) {
+ String name = "COUNT(";
+ String type = verbalizer.verbalize(c.getType());
+ NumberExpression minE = c.getMin();
+ String min = verbalizeMin(minE, Integer.MIN_VALUE);
+ NumberExpression maxE = c.getMax();
+ String max = verbalizeMax(maxE, Integer.MAX_VALUE);
+ String var = c.getVar() == null ? "" : "," + c.getVar();
+ return name + type + min + max + var + ")";
+ } else {
+ String name = "COUNT(";
+ String list = verbalizer.verbalize(c.getList());
+ String arg = verbalizer.verbalize(c.getArg());
+ NumberExpression minE = c.getMin();
+ String min = verbalizeMin(minE, Integer.MIN_VALUE);
+ NumberExpression maxE = c.getMax();
+ String max = verbalizeMax(maxE, Integer.MAX_VALUE);
+ String var = c.getVar() == null ? "" : "," + c.getVar();
+ return name + list + "," + arg + min + max + var + ")";
+ }
+ } else if (condition instanceof CurrentCountCondition) {
+ CurrentCountCondition c = (CurrentCountCondition) condition;
+ String name = "CURRENTCOUNT(";
+ String type = verbalizer.verbalize(c.getType());
+ NumberExpression minE = c.getMin();
+ String min = verbalizeMin(minE, Integer.MIN_VALUE);
+ NumberExpression maxE = c.getMax();
+ String max = verbalizeMax(maxE, Integer.MAX_VALUE);
+ String var = c.getVar() == null ? "" : "," + c.getVar();
+ return name + type + min + max + var + ")";
+ } else if (condition instanceof IfCondition) {
+ IfCondition c = (IfCondition) condition;
+ return "IF(" + verbalizer.verbalize(c.getExpression()) + ")";
+ } else if (condition instanceof InListCondition) {
+ InListCondition c = (InListCondition) condition;
+ String name = "INLIST";
+ String list = "";
+ if (c.getListExpression() != null) {
+ list = verbalizer.verbalize(c.getListExpression());
+ } else {
+ list = verbalizer.verbalize(c.getStringList());
+ list = "[" + list + "]";
+ }
+ NumberExpression distE = c.getDistance();
+ String dist = "";
+ String rel = "";
+ if (distE != null) {
+ dist = "," + verbalizer.verbalize(distE);
+ }
+ if (c.getRelative() != null) {
+ rel = "," + verbalizer.verbalize(c.getRelative());
+ }
+ return name + "(" + list + dist + rel + ")";
+ } else if (condition instanceof IsInTagCondition) {
+ IsInTagCondition c = (IsInTagCondition) condition;
+ return "ISINTAG(" + verbalizer.verbalize(c.getTag()) + ")";
+ } else if (condition instanceof LastCondition) {
+ LastCondition c = (LastCondition) condition;
+ return "LAST(" + verbalizer.verbalize(c.getType()) + ")";
+ } else if (condition instanceof MOfNCondition) {
+ MOfNCondition c = (MOfNCondition) condition;
+ StringBuilder sb = new StringBuilder();
+ sb.append("MOFN(");
+
+ NumberExpression minE = c.getMin();
+ String min = verbalizeMin(minE, Integer.MIN_VALUE);
+ NumberExpression maxE = c.getMax();
+ String max = verbalizeMax(maxE, Integer.MAX_VALUE);
+ sb.append(min.substring(1, min.length()));
+ sb.append(max);
+ List<AbstractTextMarkerCondition> conditions = c.getConditions();
+ if (!conditions.isEmpty()) {
+ sb.append(",");
+ }
+ Iterator<AbstractTextMarkerCondition> it = conditions.iterator();
+ while (it.hasNext()) {
+ AbstractTextMarkerCondition each = (AbstractTextMarkerCondition) it.next();
+ sb.append(verbalize(each));
+ if (it.hasNext()) {
+ sb.append(",");
+ }
+ }
+ sb.append(")");
+ return sb.toString();
+ } else if (condition instanceof NearCondition) {
+ NearCondition c = (NearCondition) condition;
+ String name = "NEAR(";
+ String type = verbalizer.verbalize(c.getType());
+ String var = verbalizer.verbalize(c.getForward());
+ NumberExpression minE = c.getMin();
+ String min = verbalizeMin(minE, Integer.MIN_VALUE);
+ NumberExpression maxE = c.getMax();
+ String max = verbalizeMax(maxE, Integer.MAX_VALUE);
+ return name + type + min + max + "," + var + ")";
+ } else if (condition instanceof NotCondition) {
+ NotCondition c = (NotCondition) condition;
+ List<AbstractTextMarkerCondition> conditions = c.getConditions();
+ if (conditions.size() != 1) {
+ StringBuilder sb = new StringBuilder();
+ sb.append("NOT(");
+ Iterator<AbstractTextMarkerCondition> it = conditions.iterator();
+ while (it.hasNext()) {
+ AbstractTextMarkerCondition each = (AbstractTextMarkerCondition) it.next();
+ sb.append(verbalize(each));
+ if (it.hasNext()) {
+ sb.append(",");
+ }
+ }
+ sb.append(")");
+ return sb.toString();
+ } else {
+ return "-" + verbalize(conditions.get(0));
+ }
+ } else if (condition instanceof OrCondition) {
+ OrCondition c = (OrCondition) condition;
+ List<AbstractTextMarkerCondition> conditions = c.getConditions();
+ StringBuilder sb = new StringBuilder();
+ sb.append("OR(");
+ Iterator<AbstractTextMarkerCondition> it = conditions.iterator();
+ while (it.hasNext()) {
+ AbstractTextMarkerCondition each = (AbstractTextMarkerCondition) it.next();
+ sb.append(verbalize(each));
+ if (it.hasNext()) {
+ sb.append(",");
+ }
+ }
+ sb.append(")");
+ return sb.toString();
+ } else if (condition instanceof PartOfCondition) {
+ PartOfCondition c = (PartOfCondition) condition;
+ if (c.getType() == null) {
+ return "PARTOF(" + verbalizer.verbalize(c.getList()) + ")";
+ } else {
+ return "PARTOF(" + verbalizer.verbalize(c.getType()) + ")";
+ }
+ } else if (condition instanceof PartOfNeqCondition) {
+ PartOfNeqCondition c = (PartOfNeqCondition) condition;
+ return "PARTOFNEQ(" + verbalizer.verbalize(c.getType()) + ")";
+ } else if (condition instanceof PositionCondition) {
+ PositionCondition c = (PositionCondition) condition;
+ return "POSITION(" + verbalizer.verbalize(c.getType()) + ","
+ + verbalizer.verbalize(c.getPosition()) + ")";
+ } else if (condition instanceof RegExpCondition) {
+ RegExpCondition c = (RegExpCondition) condition;
+ return "REGEXP(" + verbalizer.verbalize(c.getPattern()) + ")";
+ } else if (condition instanceof ScoreCondition) {
+ ScoreCondition c = (ScoreCondition) condition;
+ String name = "SCORE(";
+ // String type = verbalizer.verbalize(c.getType());
+ NumberExpression minE = c.getMin();
+ String min = verbalizeMin(minE, Integer.MIN_VALUE);
+ NumberExpression maxE = c.getMax();
+ String max = verbalizeMax(maxE, Integer.MAX_VALUE);
+ String var = c.getVar() == null ? "" : "," + c.getVar();
+ return name + min.replaceFirst("[,]", "") + max + var + ")";
+ } else if (condition instanceof TotalCountCondition) {
+ TotalCountCondition c = (TotalCountCondition) condition;
+ String name = "TOTALCOUNT(";
+ String type = verbalizer.verbalize(c.getType());
+ NumberExpression minE = c.getMin();
+ String min = verbalizeMin(minE, Integer.MIN_VALUE);
+ NumberExpression maxE = c.getMax();
+ String max = verbalizeMax(maxE, Integer.MAX_VALUE);
+ String var = c.getVar() == null ? "" : "," + c.getVar();
+ return name + type + min + max + var + ")";
+ } else if (condition instanceof VoteCondition) {
+ VoteCondition c = (VoteCondition) condition;
+ String name = "VOTE(";
+ String type1 = verbalizer.verbalize(c.getType1());
+ String type2 = verbalizer.verbalize(c.getType2());
+ return name + type1 + "," + type2 + ")";
+ } else if (condition instanceof FeatureCondition) {
+ FeatureCondition c = (FeatureCondition) condition;
+ String e1 = verbalizer.verbalize(c.getFeatureStringExpression());
+ String name = "";
+ String e2 = "";
+ if (c.getBooleanExpr() != null) {
+ name = "FEATURE(";
+ e2 = verbalizer.verbalize(c.getBooleanExpr());
+ } else if (c.getNumberExpr() != null) {
+ name = "FEATURE(";
+ e2 = verbalizer.verbalize(c.getNumberExpr());
+ } else if (c.getStringExpr() != null) {
+ name = "FEATURE(";
+ e2 = verbalizer.verbalize(c.getStringExpr());
+ }
+ return name + e1 + "," + e2 + ")";
+ } else if (condition instanceof ParseCondition) {
+ ParseCondition c = (ParseCondition) condition;
+ String name = "PARSE(";
+ String var = c.getVar();
+ return name + var + ")";
+ } else if (condition instanceof IsCondition) {
+ IsCondition c = (IsCondition) condition;
+ String name = "IS(";
+ String type = verbalizer.verbalize(c.getType());
+ return name + type + ")";
+ } else if (condition instanceof BeforeCondition) {
+ BeforeCondition c = (BeforeCondition) condition;
+ String name = "BEFORE(";
+ String type = verbalizer.verbalize(c.getType());
+ return name + type + ")";
+ } else if (condition instanceof AfterCondition) {
+ AfterCondition c = (AfterCondition) condition;
+ String name = "AFTER(";
+ String type = verbalizer.verbalize(c.getType());
+ return name + type + ")";
+ } else if (condition instanceof StartsWithCondition) {
+ StartsWithCondition c = (StartsWithCondition) condition;
+ String name = "STARTSWITH(";
+ String type = verbalizer.verbalize(c.getType());
+ return name + type + ")";
+ } else if (condition instanceof EndsWithCondition) {
+ EndsWithCondition c = (EndsWithCondition) condition;
+ String name = "ENDSWITH(";
+ String type = verbalizer.verbalize(c.getType());
+ return name + type + ")";
+ } else if (condition instanceof SizeCondition) {
+ SizeCondition c = (SizeCondition) condition;
+ String name = "SIZE(";
+ NumberExpression minE = c.getMinExpr();
+ String min = verbalizeMin(minE, Integer.MIN_VALUE);
+ NumberExpression maxE = c.getMaxExpr();
+ String max = verbalizeMax(maxE, Integer.MAX_VALUE);
+ String var = c.getVarExpr() == null ? "" : "," + c.getVarExpr();
+ return name + min + max + var + ")";
+ }
+
+ return condition.getClass().getSimpleName();
+ }
+
+ private String verbalizeMax(NumberExpression maxE, int def) {
+ String max = "";
+ if (!(maxE instanceof SimpleNumberExpression && ((SimpleNumberExpression) maxE).getNumber()
+ .equals(def))) {
+ max = "," + verbalizer.verbalize(maxE);
+ }
+ return max;
+ }
+
+ private String verbalizeMin(NumberExpression minE, int def) {
+ String min = "";
+ if (!(minE instanceof SimpleNumberExpression && ((SimpleNumberExpression) minE).getNumber()
+ .equals(def))) {
+ min = "," + verbalizer.verbalize(minE);
+ }
+ return min;
+ }
+
+}
Propchange: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/ConditionVerbalizer.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/ConditionVerbalizer.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/ExpressionVerbalizer.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/ExpressionVerbalizer.java?rev=1157040&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/ExpressionVerbalizer.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/ExpressionVerbalizer.java Fri Aug 12 10:44:04 2011
@@ -0,0 +1,256 @@
+/*
+ * 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.verbalize;
+
+import java.util.Iterator;
+
+import org.apache.uima.cas.Type;
+import org.apache.uima.textmarker.expression.TextMarkerExpression;
+import org.apache.uima.textmarker.expression.bool.BooleanExpression;
+import org.apache.uima.textmarker.expression.bool.BooleanNumberExpression;
+import org.apache.uima.textmarker.expression.bool.BooleanTypeExpression;
+import org.apache.uima.textmarker.expression.bool.ReferenceBooleanExpression;
+import org.apache.uima.textmarker.expression.bool.SimpleBooleanExpression;
+import org.apache.uima.textmarker.expression.list.ListExpression;
+import org.apache.uima.textmarker.expression.list.ReferenceBooleanListExpression;
+import org.apache.uima.textmarker.expression.list.ReferenceNumberListExpression;
+import org.apache.uima.textmarker.expression.list.ReferenceStringListExpression;
+import org.apache.uima.textmarker.expression.list.ReferenceTypeListExpression;
+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.ComposedDoubleExpression;
+import org.apache.uima.textmarker.expression.number.ComposedIntegerExpression;
+import org.apache.uima.textmarker.expression.number.NegativeNumberExpression;
+import org.apache.uima.textmarker.expression.number.NumberExpression;
+import org.apache.uima.textmarker.expression.number.ReferenceDoubleExpression;
+import org.apache.uima.textmarker.expression.number.ReferenceIntegerExpression;
+import org.apache.uima.textmarker.expression.number.SimpleNumberExpression;
+import org.apache.uima.textmarker.expression.resource.ReferenceWordListExpression;
+import org.apache.uima.textmarker.expression.resource.ReferenceWordTableExpression;
+import org.apache.uima.textmarker.expression.resource.WordListExpression;
+import org.apache.uima.textmarker.expression.resource.WordTableExpression;
+import org.apache.uima.textmarker.expression.string.ComposedStringExpression;
+import org.apache.uima.textmarker.expression.string.LiteralStringExpression;
+import org.apache.uima.textmarker.expression.string.ReferenceStringExpression;
+import org.apache.uima.textmarker.expression.string.SimpleStringExpression;
+import org.apache.uima.textmarker.expression.string.StringExpression;
+import org.apache.uima.textmarker.expression.type.ReferenceTypeExpression;
+import org.apache.uima.textmarker.expression.type.SimpleTypeExpression;
+import org.apache.uima.textmarker.expression.type.TypeExpression;
+
+public class ExpressionVerbalizer {
+
+ private TextMarkerVerbalizer verbalizer;
+
+ public ExpressionVerbalizer(TextMarkerVerbalizer verbalizer) {
+ super();
+ this.verbalizer = verbalizer;
+ }
+
+ public String verbalize(TextMarkerExpression expression) {
+ if (expression instanceof TypeExpression) {
+ return verbalize((TypeExpression) expression);
+ } else if (expression instanceof BooleanExpression) {
+ return verbalize((BooleanExpression) expression);
+ } else if (expression instanceof NumberExpression) {
+ return verbalize((NumberExpression) expression);
+ } else if (expression instanceof WordListExpression) {
+ return verbalize((WordListExpression) expression);
+ } else if (expression instanceof WordTableExpression) {
+ return verbalize((WordTableExpression) expression);
+ } else if (expression instanceof ListExpression<?>) {
+ return verbalize((ListExpression<?>) expression);
+ } else if (expression instanceof StringExpression) {
+ return verbalize((StringExpression) expression);
+ }
+ return expression.getClass().getSimpleName();
+ }
+
+ public String verbalize(WordTableExpression expression) {
+ if (expression instanceof ReferenceWordTableExpression) {
+ ReferenceWordTableExpression e = (ReferenceWordTableExpression) expression;
+ return e.getRef();
+ }
+ return expression.getClass().getSimpleName();
+ }
+
+ public String verbalize(WordListExpression expression) {
+ if (expression instanceof ReferenceWordListExpression) {
+ ReferenceWordListExpression e = (ReferenceWordListExpression) expression;
+ return e.getRef();
+ }
+ return expression.getClass().getSimpleName();
+ }
+
+ public String verbalize(ListExpression<?> expression) {
+ if (expression instanceof SimpleBooleanListExpression) {
+ SimpleBooleanListExpression e = (SimpleBooleanListExpression) expression;
+ return "{" + verbalizer.verbalizeExpressionList(e.getList()) + "}";
+ } else if (expression instanceof ReferenceBooleanListExpression) {
+ ReferenceBooleanListExpression e = (ReferenceBooleanListExpression) expression;
+ return e.getVar();
+ } else if (expression instanceof SimpleNumberListExpression) {
+ SimpleNumberListExpression e = (SimpleNumberListExpression) expression;
+ return "{" + verbalizer.verbalizeExpressionList(e.getList()) + "}";
+ } else if (expression instanceof ReferenceNumberListExpression) {
+ ReferenceNumberListExpression e = (ReferenceNumberListExpression) expression;
+ return e.getVar();
+ } else if (expression instanceof SimpleStringListExpression) {
+ SimpleStringListExpression e = (SimpleStringListExpression) expression;
+ return "{" + verbalizer.verbalizeExpressionList(e.getList()) + "}";
+ } else if (expression instanceof ReferenceStringListExpression) {
+ ReferenceStringListExpression e = (ReferenceStringListExpression) expression;
+ return e.getVar();
+ } else if (expression instanceof SimpleTypeListExpression) {
+ SimpleTypeListExpression e = (SimpleTypeListExpression) expression;
+ return "{" + verbalizer.verbalizeExpressionList(e.getList()) + "}";
+ } else if (expression instanceof ReferenceTypeListExpression) {
+ ReferenceTypeListExpression e = (ReferenceTypeListExpression) expression;
+ return e.getVar();
+ }
+ return expression.getClass().getSimpleName();
+ }
+
+ public String verbalize(NumberExpression expression) {
+ if (expression == null) {
+ return "";
+ } else if (expression instanceof NegativeNumberExpression) {
+ NegativeNumberExpression e = (NegativeNumberExpression) expression;
+ return "-(" + e.getExpression() + ")";
+ } else if (expression instanceof ReferenceDoubleExpression) {
+ ReferenceDoubleExpression e = (ReferenceDoubleExpression) expression;
+ return e.getVar();
+ } else if (expression instanceof ReferenceIntegerExpression) {
+ ReferenceIntegerExpression e = (ReferenceIntegerExpression) expression;
+ return e.getVar();
+ } else if (expression instanceof SimpleNumberExpression) {
+ SimpleNumberExpression e = (SimpleNumberExpression) expression;
+ return e.getNumber().toString();
+ } else if (expression instanceof ComposedDoubleExpression) {
+ ComposedDoubleExpression e = (ComposedDoubleExpression) expression;
+ NumberExpression ne = e.getExpressions().get(0);
+ if (ne == null) {
+ return "";
+ }
+ StringBuilder result = new StringBuilder(verbalize(ne));
+ for (int i = 0; i < e.getOperators().size(); i++) {
+ result.append(e.getOperators().get(i));
+ if (e.getExpressions().size() > i + 1) {
+ result.append(verbalize(e.getExpressions().get(i + 1)));
+ }
+ }
+ return result.toString();
+ } else if (expression instanceof ComposedIntegerExpression) {
+ ComposedIntegerExpression e = (ComposedIntegerExpression) expression;
+ NumberExpression ne = e.getExpressions().get(0);
+ if (ne == null) {
+ return "";
+ }
+ String result = verbalize(ne);
+ for (int i = 0; i < e.getOperators().size(); i++) {
+ result += e.getOperators().get(i) + verbalize(e.getExpressions().get(i + 1));
+ }
+ return result;
+ }
+ return expression.getClass().getSimpleName();
+ }
+
+ public String verbalize(BooleanExpression expression) {
+ if (expression == null) {
+ return "";
+ } else if (expression instanceof BooleanNumberExpression) {
+ BooleanNumberExpression e = (BooleanNumberExpression) expression;
+ return verbalize(e.getFristExpression()) + e.getOperator()
+ + verbalize(e.getSecondExpression());
+ } else if (expression instanceof BooleanTypeExpression) {
+ BooleanTypeExpression e = (BooleanTypeExpression) expression;
+ return verbalize(e.getFristExpression()) + e.getOperator()
+ + verbalize(e.getSecondExpression());
+ } else if (expression instanceof ReferenceBooleanExpression) {
+ ReferenceBooleanExpression e = (ReferenceBooleanExpression) expression;
+ return e.getVar();
+ } else if (expression instanceof SimpleBooleanExpression) {
+ SimpleBooleanExpression e = (SimpleBooleanExpression) expression;
+ return e.getPrimitiveValue() ? "true" : "false";
+ }
+ return expression.getClass().getSimpleName();
+ }
+
+ public String verbalize(StringExpression expression) {
+ if (expression == null) {
+ return "";
+ } else if (expression instanceof NumberExpression) {
+ return verbalize((NumberExpression) expression);
+ } else if (expression instanceof TypeExpression) {
+ return verbalize((TypeExpression) expression);
+ } else if (expression instanceof BooleanExpression) {
+ return verbalize((BooleanExpression) expression);
+ } else if (expression instanceof ListExpression) {
+ return verbalize((ListExpression) expression);
+ } else if (expression instanceof LiteralStringExpression) {
+ return verbalize((LiteralStringExpression) expression);
+ }
+ return expression.getClass().getSimpleName();
+ }
+
+ public String verbalize(LiteralStringExpression expression) {
+ if (expression == null) {
+ return "";
+ } else if (expression instanceof ComposedStringExpression) {
+ ComposedStringExpression e = (ComposedStringExpression) expression;
+ StringBuilder sb = new StringBuilder();
+ Iterator<StringExpression> it = e.getExpressions().iterator();
+ while (it.hasNext()) {
+ StringExpression each = it.next();
+ sb.append(verbalize(each));
+ if (it.hasNext()) {
+ sb.append("+");
+ }
+ }
+ return sb.toString();
+ } else if (expression instanceof ReferenceStringExpression) {
+ ReferenceStringExpression e = (ReferenceStringExpression) expression;
+ return e.getVar();
+ } else if (expression instanceof SimpleStringExpression) {
+ SimpleStringExpression e = (SimpleStringExpression) expression;
+ return "\"" + e.getValue() + "\"";
+ }
+ return expression.getClass().getSimpleName();
+ }
+
+ public String verbalize(TypeExpression expression) {
+ if (expression instanceof SimpleTypeExpression) {
+ SimpleTypeExpression e = (SimpleTypeExpression) expression;
+ Type type = e.getType(null);
+ String shortName = type.getShortName();
+ if (shortName.equals("DocumentAnnotation")) {
+ shortName = "Document";
+ }
+ return shortName;
+ } else if (expression instanceof ReferenceTypeExpression) {
+ ReferenceTypeExpression e = (ReferenceTypeExpression) expression;
+ return e.getVar();
+ }
+ return expression.getClass().getSimpleName();
+ }
+
+}
Propchange: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/ExpressionVerbalizer.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/ExpressionVerbalizer.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/ScriptVerbalizer.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/ScriptVerbalizer.java?rev=1157040&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/ScriptVerbalizer.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/ScriptVerbalizer.java Fri Aug 12 10:44:04 2011
@@ -0,0 +1,184 @@
+/*
+ * 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.verbalize;
+
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.uima.textmarker.TextMarkerBlock;
+import org.apache.uima.textmarker.TextMarkerElement;
+import org.apache.uima.textmarker.TextMarkerStatement;
+import org.apache.uima.textmarker.action.AbstractTextMarkerAction;
+import org.apache.uima.textmarker.condition.AbstractTextMarkerCondition;
+import org.apache.uima.textmarker.rule.ComposedRuleElement;
+import org.apache.uima.textmarker.rule.RuleElement;
+import org.apache.uima.textmarker.rule.TextMarkerMatcher;
+import org.apache.uima.textmarker.rule.TextMarkerRule;
+import org.apache.uima.textmarker.rule.TextMarkerRuleElement;
+import org.apache.uima.textmarker.rule.quantifier.MinMaxGreedy;
+import org.apache.uima.textmarker.rule.quantifier.MinMaxReluctant;
+import org.apache.uima.textmarker.rule.quantifier.NormalQuantifier;
+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 ScriptVerbalizer {
+
+ private TextMarkerVerbalizer verbalizer;
+
+ public ScriptVerbalizer(TextMarkerVerbalizer verbalizer) {
+ super();
+ this.verbalizer = verbalizer;
+ }
+
+ public String verbalizeBlock(TextMarkerBlock block, boolean withElements) {
+ StringBuilder result = new StringBuilder();
+ TextMarkerRule rule = block.getRule();
+ List<TextMarkerStatement> elements = block.getElements();
+ result.append("BLOCK(");
+ result.append(block.getId());
+ result.append(")");
+ result.append(" ");
+ if (rule != null) {
+ result.append(verbalizeRule(rule));
+ }
+ if (withElements) {
+ result.append(" {\n");
+ for (TextMarkerStatement each : elements) {
+ if (each instanceof TextMarkerBlock) {
+ result.append(verbalizeBlock((TextMarkerBlock) each, withElements));
+ } else if (each instanceof TextMarkerRule) {
+ result.append(verbalizeRule((TextMarkerRule) each));
+ }
+ result.append(";");
+ result.append("\n");
+ }
+ result.append(" }\n");
+ }
+ return result.toString();
+ }
+
+ public String verbalizeRule(TextMarkerRule rule) {
+ List<RuleElement> elements = rule.getElements();
+ StringBuilder result = new StringBuilder();
+ for (RuleElement each : elements) {
+ result.append(verbalizeRuleElement(each));
+ result.append(" ");
+ }
+ return result.toString();
+ }
+
+ public String verbalizeRuleElement(RuleElement re) {
+ if (re instanceof TextMarkerRuleElement) {
+ TextMarkerRuleElement element = (TextMarkerRuleElement) re;
+ TextMarkerMatcher matcher = element.getMatcher();
+ List<AbstractTextMarkerCondition> conditions = element.getConditions();
+ List<AbstractTextMarkerAction> actions = element.getActions();
+ RuleElementQuantifier quantifier = element.getQuantifier();
+ StringBuilder result = new StringBuilder();
+ result.append(verbalizer.verbalize(matcher.getExpression()));
+ result.append(verbalizeQuantifier(quantifier));
+
+ if (!conditions.isEmpty() || !actions.isEmpty()) {
+ result.append("{");
+ Iterator<AbstractTextMarkerCondition> cit = conditions.iterator();
+ while (cit.hasNext()) {
+ AbstractTextMarkerCondition each = cit.next();
+ result.append(verbalizer.verbalize(each));
+ if (cit.hasNext()) {
+ result.append(",");
+ }
+ }
+ if (!actions.isEmpty()) {
+ result.append(" -> ");
+ Iterator<AbstractTextMarkerAction> ait = actions.iterator();
+ while (ait.hasNext()) {
+ AbstractTextMarkerAction each = ait.next();
+ result.append(verbalizer.verbalize(each));
+ if (ait.hasNext()) {
+ result.append(",");
+ }
+ }
+ }
+ result.append("}");
+ }
+ return result.toString();
+ } else if (re instanceof ComposedRuleElement) {
+ ComposedRuleElement cre = (ComposedRuleElement) re;
+ StringBuilder result = new StringBuilder("(");
+ for (RuleElement each : cre.getElements()) {
+ if (cre.getElements().indexOf(each) != 0) {
+ result.append(" ");
+ }
+ result.append(verbalizeRuleElement(each));
+ }
+ result.append(")");
+ result.append(verbalizeQuantifier(cre.getQuantifier()));
+ return result.toString();
+ }
+ return "";
+ }
+
+ public String verbalizeQuantifier(RuleElementQuantifier quantifier) {
+ if (quantifier instanceof NormalQuantifier) {
+ return "";
+ } else if (quantifier instanceof MinMaxGreedy) {
+ MinMaxGreedy mmg = (MinMaxGreedy) quantifier;
+ return "[" + verbalizer.verbalize(mmg.getMin()) + "," + verbalizer.verbalize(mmg.getMax())
+ + "]";
+ } else if (quantifier instanceof MinMaxReluctant) {
+ MinMaxReluctant mmr = (MinMaxReluctant) quantifier;
+ return "[" + verbalizer.verbalize(mmr.getMin()) + "," + verbalizer.verbalize(mmr.getMax())
+ + "]?";
+ } else if (quantifier instanceof PlusGreedy) {
+ return "+";
+ } else if (quantifier instanceof PlusReluctant) {
+ return "+?";
+ } else if (quantifier instanceof QuestionGreedy) {
+ return "?";
+ } else if (quantifier instanceof QuestionReluctant) {
+ return "??";
+ } else if (quantifier instanceof StarGreedy) {
+ return "*";
+ } else if (quantifier instanceof StarReluctant) {
+ return "*?";
+ }
+ return null;
+ }
+
+ public String verbalize(TextMarkerElement element) {
+ if (element instanceof TextMarkerBlock) {
+ return verbalizeBlock((TextMarkerBlock) element, false);
+ } else if (element instanceof RuleElementQuantifier) {
+ return verbalizeQuantifier((RuleElementQuantifier) element);
+ } else if (element instanceof TextMarkerRule) {
+ return verbalizeRule((TextMarkerRule) element);
+ } else if (element instanceof TextMarkerRuleElement) {
+ return verbalizeRuleElement((TextMarkerRuleElement) element);
+ }
+ return null;
+ }
+
+}
Propchange: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/ScriptVerbalizer.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/ScriptVerbalizer.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/TextMarkerVerbalizer.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/TextMarkerVerbalizer.java?rev=1157040&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/TextMarkerVerbalizer.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/TextMarkerVerbalizer.java Fri Aug 12 10:44:04 2011
@@ -0,0 +1,118 @@
+/*
+ * 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.verbalize;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.uima.cas.Type;
+import org.apache.uima.textmarker.TextMarkerBlock;
+import org.apache.uima.textmarker.TextMarkerElement;
+import org.apache.uima.textmarker.action.AbstractTextMarkerAction;
+import org.apache.uima.textmarker.condition.AbstractTextMarkerCondition;
+import org.apache.uima.textmarker.expression.TextMarkerExpression;
+import org.apache.uima.textmarker.extensions.ITextMarkerExtension;
+import org.apache.uima.textmarker.rule.RuleElement;
+
+public class TextMarkerVerbalizer {
+
+ private Map<Class<?>, ITextMarkerExtension> externalVerbalizers = new HashMap<Class<?>, ITextMarkerExtension>();
+
+ private ActionVerbalizer actionVerbalizer;
+
+ private ConditionVerbalizer conditionVerbalizer;
+
+ private ExpressionVerbalizer expressionVerbalizer;
+
+ private ScriptVerbalizer scriptVerbalizer;
+
+ private VerbalizerUtils verbalizerUtils;
+
+ public TextMarkerVerbalizer() {
+ super();
+ actionVerbalizer = new ActionVerbalizer(this);
+ conditionVerbalizer = new ConditionVerbalizer(this);
+ expressionVerbalizer = new ExpressionVerbalizer(this);
+ scriptVerbalizer = new ScriptVerbalizer(this);
+ verbalizerUtils = new VerbalizerUtils(this);
+ }
+
+ public void addExternalVerbalizers(ITextMarkerExtension verbalizer) {
+ Class<?>[] extensions = verbalizer.extensions();
+ for (Class<?> eachClass : extensions) {
+ externalVerbalizers.put(eachClass, verbalizer);
+ }
+ }
+
+ public String verbalize(TextMarkerElement element) {
+ if (externalVerbalizers.keySet().contains(element.getClass())) {
+ return externalVerbalizers.get(element.getClass()).verbalize(element, this);
+ } else if (element instanceof AbstractTextMarkerAction) {
+ return actionVerbalizer.verbalize((AbstractTextMarkerAction) element);
+ } else if (element instanceof AbstractTextMarkerCondition) {
+ return conditionVerbalizer.verbalize((AbstractTextMarkerCondition) element);
+ } else if (element instanceof TextMarkerExpression) {
+ return expressionVerbalizer.verbalize((TextMarkerExpression) element);
+ } else {
+ return scriptVerbalizer.verbalize(element);
+ }
+ }
+
+ public String verbalizeName(TextMarkerElement element) {
+ if (externalVerbalizers.keySet().contains(element.getClass())) {
+ return externalVerbalizers.get(element.getClass()).verbalizeName(element);
+ } else if (element instanceof AbstractTextMarkerAction) {
+ return actionVerbalizer.verbalizeName((AbstractTextMarkerAction) element);
+ } else if (element instanceof AbstractTextMarkerCondition) {
+ return conditionVerbalizer.verbalizeName((AbstractTextMarkerCondition) element);
+ }
+ return element.getClass().getSimpleName();
+ }
+
+ public String verbalize(TextMarkerBlock block, boolean withElements) {
+ return scriptVerbalizer.verbalizeBlock(block, withElements);
+ }
+
+ public String verbalize(RuleElement element) {
+ return scriptVerbalizer.verbalizeRuleElement(element);
+ }
+
+ public String verbalizeType(Type type) {
+ if (type.getName().equals("uima.tcas.DocumentAnnotation")) {
+ return "Document";
+ } else {
+ return type.getShortName();
+ }
+ }
+
+ public String verbalizeList(List<?> list) {
+ return verbalizerUtils.verbalizeList(list);
+ }
+
+ public String verbalizeTypeList(List<Type> list) {
+ return verbalizerUtils.verbalizeTypeList(list);
+ }
+
+ public String verbalizeExpressionList(List<? extends TextMarkerExpression> list) {
+ return verbalizerUtils.verbalizeExpressionList(list);
+ }
+
+}
Propchange: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/TextMarkerVerbalizer.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/TextMarkerVerbalizer.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/VerbalizerUtils.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/VerbalizerUtils.java?rev=1157040&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/VerbalizerUtils.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/VerbalizerUtils.java Fri Aug 12 10:44:04 2011
@@ -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.verbalize;
+
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.uima.cas.Type;
+import org.apache.uima.textmarker.expression.TextMarkerExpression;
+
+public class VerbalizerUtils {
+
+ private TextMarkerVerbalizer verbalizer;
+
+ public VerbalizerUtils(TextMarkerVerbalizer verbalizer) {
+ super();
+ this.verbalizer = verbalizer;
+ }
+
+ public String verbalizeList(List<?> list) {
+ StringBuilder result = new StringBuilder();
+ Iterator<?> it = list.iterator();
+ while (it.hasNext()) {
+ Object obj = it.next();
+ result.append(obj.toString());
+ if (it.hasNext()) {
+ result.append(", ");
+ }
+ }
+ return result.toString();
+ }
+
+ public String verbalizeTypeList(List<Type> list) {
+ StringBuilder result = new StringBuilder();
+ Iterator<Type> it = list.iterator();
+ while (it.hasNext()) {
+ Type type = it.next();
+ result.append(verbalizer.verbalizeType(type));
+ if (it.hasNext()) {
+ result.append(", ");
+ }
+ }
+ return result.toString();
+ }
+
+ public String verbalizeExpressionList(List<? extends TextMarkerExpression> list) {
+ StringBuilder result = new StringBuilder();
+ Iterator<? extends TextMarkerExpression> it = list.iterator();
+ while (it.hasNext()) {
+ TextMarkerExpression e = it.next();
+ result.append(verbalizer.verbalize(e));
+ if (it.hasNext()) {
+ result.append(", ");
+ }
+ }
+ return result.toString();
+ }
+
+}
Propchange: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/VerbalizerUtils.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/verbalize/VerbalizerUtils.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/visitor/DebugInfoCollectorVisitor.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/visitor/DebugInfoCollectorVisitor.java?rev=1157040&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/visitor/DebugInfoCollectorVisitor.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/visitor/DebugInfoCollectorVisitor.java Fri Aug 12 10:44:04 2011
@@ -0,0 +1,189 @@
+/*
+ * 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.visitor;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Stack;
+
+import org.apache.uima.textmarker.BlockApply;
+import org.apache.uima.textmarker.ScriptApply;
+import org.apache.uima.textmarker.TextMarkerBlock;
+import org.apache.uima.textmarker.TextMarkerElement;
+import org.apache.uima.textmarker.TextMarkerModule;
+import org.apache.uima.textmarker.TextMarkerStatement;
+import org.apache.uima.textmarker.TextMarkerStream;
+import org.apache.uima.textmarker.rule.RuleApply;
+import org.apache.uima.textmarker.rule.TextMarkerRule;
+import org.apache.uima.textmarker.type.DebugScriptApply;
+import org.apache.uima.textmarker.verbalize.TextMarkerVerbalizer;
+
+
+public class DebugInfoCollectorVisitor implements TextMarkerInferenceVisitor {
+
+ private boolean createDebugInfo;
+
+ private List<String> ids;
+
+ private DebugInfoFactory debugFactory;
+
+ private boolean withMatches;
+
+ private ScriptApply rootApply;
+
+ private Map<TextMarkerStatement, Stack<ScriptApply>> applies;
+
+ private Stack<TextMarkerElement> callStack;
+
+ public DebugInfoCollectorVisitor(boolean createDebugInfo, boolean withMatches, List<String> ids,
+ TextMarkerVerbalizer verbalizer) {
+ super();
+ this.createDebugInfo = createDebugInfo;
+ this.withMatches = withMatches;
+ this.ids = ids;
+
+ debugFactory = new DebugInfoFactory(verbalizer);
+ applies = new HashMap<TextMarkerStatement, Stack<ScriptApply>>();
+ callStack = new Stack<TextMarkerElement>();
+ }
+
+ public DebugInfoCollectorVisitor(boolean createDebugInfo) {
+ super();
+ this.createDebugInfo = createDebugInfo;
+ this.ids = new ArrayList<String>();
+ }
+
+ public boolean isCreateDebugInfo() {
+ return createDebugInfo;
+ }
+
+ public boolean createDebugInfo(TextMarkerRule rule) {
+ return createDebugInfo || ids.contains("" + rule.getId());
+ }
+
+ public void beginVisit(TextMarkerElement element, ScriptApply result) {
+ if (element instanceof TextMarkerStatement) {
+ callStack.push(element);
+ TextMarkerStatement stmt = (TextMarkerStatement) element;
+ Stack<ScriptApply> stack = applies.get(stmt);
+ if (stack == null) {
+ stack = new Stack<ScriptApply>();
+ applies.put(stmt, stack);
+ }
+ stack.push(result);
+ applies.put(stmt, stack);
+ if (result instanceof RuleApply) {
+ RuleApply ra = (RuleApply) result;
+ ra.setAcceptMatches(ra.isAcceptMatches() || withMatches);
+ }
+ }
+ }
+
+ public void endVisit(TextMarkerElement element, ScriptApply result) {
+ // TODO create UIMA stuff here not later -> save memory!
+ if (element instanceof TextMarkerStatement) {
+ TextMarkerStatement stmt = (TextMarkerStatement) element;
+ TextMarkerBlock parent = stmt.getParent();
+ Stack<ScriptApply> stack = applies.get(stmt);
+ if (stack == null) {
+ stack = new Stack<ScriptApply>();
+ applies.put(stmt, stack);
+ }
+ // replace
+ if (!stack.isEmpty()) {
+ stack.pop();
+ }
+ stack.push(result);
+ applies.put(stmt, stack);
+ if (parent != null) {
+ Stack<ScriptApply> parentStack = applies.get(parent);
+ if (parentStack != null && !parentStack.isEmpty()) {
+ ScriptApply parentApply = parentStack.peek();
+ if (parentApply instanceof BlockApply) {
+ BlockApply blockApply = (BlockApply) parentApply;
+ if (element instanceof TextMarkerRule && parent.getRule().equals(element)
+ && result instanceof RuleApply) {
+ blockApply.setRuleApply((RuleApply) result);
+
+ // } else {
+ // blockApply.add(result);
+ // }
+
+ } else if (stack.size() == 1) {
+ if (callStack.size() > 1) {
+ // TODO hotfixed
+ TextMarkerElement tme = callStack.get(callStack.size() - 2);
+ if (tme.equals(parent)
+ // || tme.equals(element)
+ ) {
+ blockApply.add(result);
+ } else {
+ // System.out.println();
+ // TODO too many blocks added
+ }
+ } else {
+ blockApply.add(result);
+
+ }
+ } else {
+ // TODO refactor !!! ... really!!!!
+ TextMarkerElement tme = callStack.get(callStack.size() - 2);
+ if (tme.equals(parent)
+ // || tme.equals(element)
+ ) {
+ blockApply.add(result);
+ } else {
+ // System.out.println();
+ // TODO too many blocks added
+ }
+ }
+ }
+ }
+ }
+ stack.pop();
+ callStack.pop();
+ }
+ if (element instanceof TextMarkerModule) {
+ rootApply = result;
+ }
+ }
+
+ public void finished(TextMarkerStream stream, List<TextMarkerInferenceVisitor> visitors) {
+ if (createDebugInfo) {
+ Map<TextMarkerElement, Long> timeInfo = getTimeInfo(visitors);
+
+ DebugScriptApply debugScriptApply = debugFactory.createDebugScriptApply(rootApply, stream,
+ false, withMatches, timeInfo);
+ debugScriptApply.addToIndexes();
+ }
+ }
+
+ private Map<TextMarkerElement, Long> getTimeInfo(List<TextMarkerInferenceVisitor> visitors) {
+ for (TextMarkerInferenceVisitor each : visitors) {
+ if (each instanceof TimeProfilerVisitor) {
+ return ((TimeProfilerVisitor) each).getTimeInfo();
+ }
+ }
+ return null;
+ }
+
+}
Propchange: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/visitor/DebugInfoCollectorVisitor.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/visitor/DebugInfoCollectorVisitor.java
------------------------------------------------------------------------------
svn:mime-type = text/plain