Revision: 15239
http://gate.svn.sourceforge.net/gate/?rev=15239&view=rev
Author: valyt
Date: 2012-01-27 15:14:21 +0000 (Fri, 27 Jan 2012)
Log Message:
-----------
Tested on a very simple grammar and it actually produced output annotations,
with no exceptions!
We have a JAPE compiler once more!
More comprehensive testing coming next...
Modified Paths:
--------------
futures/JAPE_PlusC/src/gate/jape/plus/SPTBase.java
futures/JAPE_PlusC/src/gate/jape/plus/SPTBuilder.java
futures/JAPE_PlusC/src/gate/jape/plus/SPTPhaseEG.java
Property Changed:
----------------
futures/JAPE_PlusC/test/
Modified: futures/JAPE_PlusC/src/gate/jape/plus/SPTBase.java
===================================================================
--- futures/JAPE_PlusC/src/gate/jape/plus/SPTBase.java 2012-01-27 14:56:46 UTC
(rev 15238)
+++ futures/JAPE_PlusC/src/gate/jape/plus/SPTBase.java 2012-01-27 15:14:21 UTC
(rev 15239)
@@ -151,14 +151,15 @@
// longest is better, hence the reversed test
int res = other.annotationIndex - annotationIndex;
if(res == 0) {
- // highest is better, hence the reversed test
- res = rules[states[other.state].rule].getPriority() -
- rules[states[state].rule].getPriority();
- if(res == 0) {
- // lower is better
- res = rules[states[state].rule].getPosition() -
- rules[states[other.state].rule].getPosition();
+ // compare rule priority: highest is better, hence the reversed test
+ if(rule >= 0 && other.rule >= 0) {
+ res = rules[other.rule].getPriority() - rules[rule].getPriority();
+ // compare rule position: lower is better
+ if(res == 0) {
+ res = rules[rule].getPosition() - rules[other.rule].getPosition();
+ }
}
+
if(res == 0) {
// same rule matching same document segment (probably some
zero-length
// annotations are involved) -> prefer the version with more
annotations
@@ -202,6 +203,13 @@
* The current state for this instance.
*/
public int state;
+
+ /**
+ * If the instance is in a final state (only applicable for FSMInstances
+ * stored inside the {@link SPTBase#acceptingInstances} list) this value
+ * stores the rule to be applied.
+ */
+ public int rule = -1;
/**
* Store the currently bound annotations. Keys are binding labels, values
@@ -393,7 +401,7 @@
* The states of the state machine. State at index 0 is always the initial
* state.
*/
- protected State[] states;
+// protected State[] states;
/**
* The binding names to be used when a transition of type
closing-round-bracket
@@ -1219,7 +1227,7 @@
}
newBindings.put(entry.getKey(), boundAnnots);
}
- rules[states[instance.state].rule].getRHS().transduce(document,
+ rules[instance.rule].getRHS().transduce(document,
newBindings, document.getAnnotations(inputASName),
document.getAnnotations(outputASName), ontology, actionContext);
}
Modified: futures/JAPE_PlusC/src/gate/jape/plus/SPTBuilder.java
===================================================================
--- futures/JAPE_PlusC/src/gate/jape/plus/SPTBuilder.java 2012-01-27
14:56:46 UTC (rev 15238)
+++ futures/JAPE_PlusC/src/gate/jape/plus/SPTBuilder.java 2012-01-27
15:14:21 UTC (rev 15239)
@@ -27,6 +27,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
import com.ontotext.jape.pda.FSMPDA;
import com.ontotext.jape.pda.SinglePhaseTransducerPDA;
@@ -36,6 +37,7 @@
import cern.colt.list.IntArrayList;
import cern.colt.map.OpenIntIntHashMap;
+import gate.Gate;
import gate.creole.ResourceInstantiationException;
import gate.jape.plus.SPTBase.Transition;
import gate.jape.Constraint;
@@ -49,6 +51,8 @@
import gate.jape.plus.SPTBase.FSMInstance;
import gate.jape.plus.SPTBase.MatchMode;
import gate.jape.plus.SPTBase.State;
+import gate.util.GateException;
+import gate.util.Javac;
/**
* An utility class for converting a default JAPE transducer into a JAPE-Plus
transducer.
@@ -60,6 +64,9 @@
"\t\t\t\t", "\t\t\t\t\t", "\t\t\t\t\t\t", "\t\t\t\t\t\t\t",
"\t\t\t\t\t\t\t\t"};
+ private static final String GENERATED_CLASS_PACKAGE = "japephases";
+ private static AtomicInteger generatedClassUID = new AtomicInteger();
+
/**
* Stores the states for the optimised transducer.
*/
@@ -103,7 +110,7 @@
optimisePredicates();
StringBuilder sptCode = new StringBuilder();
- String className = "Phase" + oldSpt.getName();
+ String className = "Phase" + oldSpt.getName() +
generatedClassUID.getAndIncrement();
writeClassHeader(className, sptCode);
writeConstructor(className, oldSpt, 1, sptCode);
@@ -115,7 +122,7 @@
try {
FileWriter outWriter = new FileWriter(
-
"/home/valyt.plain/vcs/gate-top/externals/JAPE_PlusC/src/gate/jape/plus/phases/"
+
+ "/home/valyt.plain/vcs/gate-top/externals/JAPE_PlusC/test/src/japephases/"
+
className + ".java");
outWriter.write(sptCode.toString());
outWriter.close();
@@ -124,9 +131,6 @@
e1.printStackTrace();
}
- // compile the class
- Class<? extends SPTBase> sptClass = null; //TODO
-
//prepare the parameters for calling the constructor
//predicates
Predicate[][] predicatesByTypeArray = new Predicate[
@@ -144,12 +148,21 @@
}
SPTBase optimisedTransducer = null;
try {
+
+ Map<String, String> classes = new HashMap<String, String>(1);
+ String fqcn = GENERATED_CLASS_PACKAGE + "." + className;
+ classes.put(fqcn, sptCode.toString());
+ Javac.loadClasses(classes);
+ // compile the class
+ @SuppressWarnings("unchecked")
+ Class<? extends SPTBase> sptClass = (Class<? extends SPTBase>)
+ Gate.getClassLoader().loadClass(fqcn);
+
Constructor<? extends SPTBase> sptConstructor = sptClass.getConstructor(
Rule[].class, Predicate[][].class);
optimisedTransducer = sptConstructor.newInstance(rules,
predicatesByTypeArray);
} catch(SecurityException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ throw new ResourceInstantiationException(e);
} catch(NoSuchMethodException e) {
throw new ResourceInstantiationException(e);
} catch(IllegalArgumentException e) {
@@ -160,6 +173,10 @@
throw new ResourceInstantiationException(e);
} catch(InvocationTargetException e) {
throw new ResourceInstantiationException(e);
+ } catch(GateException e) {
+ throw new ResourceInstantiationException(e);
+ } catch(ClassNotFoundException e) {
+ throw new ResourceInstantiationException(e);
}
//input types
@@ -187,14 +204,15 @@
protected void writeClassHeader(String className,
StringBuilder out) {
- out.append("package gate.jape.plus.phases;\n");
+ out.append("package ").append(GENERATED_CLASS_PACKAGE).append(";\n");
out.append("import gate.jape.Rule;\n");
out.append("import gate.jape.plus.Predicate;\n");
- out.append("import gate.jape.plus.SPTBase;\n");
- out.append("import java.util.List;\n");
- out.append("import static gate.jape.plus.SPTBase.MatchMode.*;\n");
- out.append("import cern.colt.list.IntArrayList;\n\n");
- out.append("public class ").append(className).append(" extends SPTBase
{\n");
+ out.append("import gate.jape.plus.SPTBase;\n");
+ out.append("import cern.colt.list.IntArrayList;\n");
+ out.append("import static gate.jape.plus.SPTBase.MatchMode.*;\n\n");
+ out.append("public class ").append(className).append(" extends SPTBase
{\n\n");
+ out.append("\t// default serialisation ID\n");
+ out.append("\tprivate static final long serialVersionUID = 1L;\n\n");
}
protected void writeConstructor(String className,
@@ -286,8 +304,12 @@
out.append(TABS[tabs]).append(
"// current instance is in a final state\n");
out.append(TABS[tabs]).append(
- "acceptingInstances.add(instance.clone());\n");
+ "FSMInstance newInstance = instance.clone();\n");
out.append(TABS[tabs]).append(
+ "newInstance.rule = ").append(state.rule).append(";\n");
+ out.append(TABS[tabs]).append(
+ "acceptingInstances.add(newInstance);\n");
+ out.append(TABS[tabs]).append(
"if (matchMode == MatchMode.FIRST || matchMode == MatchMode.ONCE)
{\n");
tabs++;
out.append(TABS[tabs]).append("// we're done!\n");
@@ -298,11 +320,10 @@
// for each transition
for(int transId = 0; transId < state.transitions.length; transId++) {
Transition transition = state.transitions[transId];
- out.append(TABS[tabs]).append("s").append(stateId).append("t").append(
- transId).append(": ").append("do { // transition block: ");
if(transition.type == TransitionPDA.TYPE_OPENING_ROUND_BRACKET){
// opening-round-bracket transition
- out.append("opening-round-bracket transition\n");
+ out.append(TABS[tabs]).append(
+ "{ // transition block: opening-round-bracket transition\n");
tabs++;
out.append(TABS[tabs]).append(
"FSMInstance nextInstance = instance.clone();\n");
@@ -312,9 +333,12 @@
"nextInstance.state = ").append(transition.nextState).append(";\n");
out.append(TABS[tabs]).append(
"activeInstances.addLast(nextInstance);\n");
+ tabs--;
+ out.append(TABS[tabs]).append("} // end transition block\n\n");
} else if(transition.type != TransitionPDA.TYPE_CONSTRAINT){
// closing-round-bracket transition
- out.append("closing-round-bracket transition\n");
+ out.append(TABS[tabs]).append(
+ "{ // transition block: closing-round-bracket transition\n");
tabs++;
out.append(TABS[tabs]).append(
"FSMInstance nextInstance = instance.clone();\n");
@@ -323,16 +347,22 @@
out.append(TABS[tabs]).append(
"nextInstance.state =
").append(transition.nextState).append(";\n");
out.append(TABS[tabs]).append(
- "activeInstances.addLast(nextInstance);\n");
+ "activeInstances.addLast(nextInstance);\n\n");
+ tabs--;
+ out.append(TABS[tabs]).append("} // end transition block\n");
} else {
// constrained transition
- out.append("constrained transition\n");
+ out.append(TABS[tabs]).append("s").append(stateId).append("t").append(
+ transId).append(": ").append(
+ "do { // transition block: constrained transition\n");
tabs++;
writeConstrainedTransitionBlock(stateId, transId, tabs, out);
+ tabs--;
+ out.append(TABS[tabs]).append(
+ "} while (false); // end transition block\n\n");
}
- tabs--;
- out.append(TABS[tabs]).append("} while (false); // end transition
block\n");
+
}
@@ -347,20 +377,24 @@
out.append(TABS[tabs]).append(
"IntArrayList[] annotsForConstraints = new IntArrayList[").append(
transition.constraints.length).append("];\n");
+ // unfurl, the constraints
for(int constrId = 0; constrId < transition.constraints.length;
constrId++) {
- String blockLabel = "s" + stateId + "t" + transId + "c" + constrId;
- out.append(TABS[tabs]).append(blockLabel).append(
- ": do{ // constraint block\n");
+// String blockLabel = "s" + stateId + "t" + transId + "c" + constrId;
+// out.append(TABS[tabs]).append(blockLabel).append(": do{ // constraint
block\n");
+ out.append(TABS[tabs]).append("{ // constraint block\n");
tabs++;
+ int[] constraint = transition.constraints[constrId];
out.append(TABS[tabs]).append("final int[] constraint = new int[] {");
boolean first = true;
- for(int elem : transition.constraints[constrId]) {
+ for(int elem : constraint) {
if(first) first = false; else out.append(", ");
out.append(elem);
}
out.append("};\n");
- out.append(TABS[tabs]).append(
- "annotations: for(int annIdx = instance.annotationIndex;\n");
+ out.append(TABS[tabs]);
+ // if there are any predicates to check, we need a break label
+ if(constraint.length > 2) out.append("annotations: ");
+ out.append("for(int annIdx = instance.annotationIndex;\n");
tabs++;
tabs++;
out.append(TABS[tabs]).append("annIdx < annotation.length &&\n");
@@ -370,21 +404,19 @@
out.append(TABS[tabs]).append(
"if(constraint[0] == annotationType[annIdx]) {\n");
tabs++;
- out.append(TABS[tabs]).append("// type matched, now check
predicates;\n");
+ out.append(TABS[tabs]).append("// type matched, now check
predicates:\n");
+ // unfurl the predicates
+ for(int predIdx = 2; predIdx < constraint.length; predIdx++) {
+ out.append(TABS[tabs]).append(
+ "if(!checkPredicate(annIdx,
").append(constraint[predIdx]).append(")) {\n");
+ tabs++;
+ out.append(TABS[tabs]).append(
+ "// one predicate failed -> move to next annotation\n");
+ out.append(TABS[tabs]).append("continue annotations;\n");
+ tabs--;
+ out.append(TABS[tabs]).append("}\n");
+ }
out.append(TABS[tabs]).append(
- "for(int predIdx = 2; predIdx < constraint.length; predIdx++) {\n");
- tabs++;
- out.append(TABS[tabs]).append(
- "if(!checkPredicate(annIdx, constraint[predIdx])) {\n");
- tabs++;
- out.append(TABS[tabs]).append(
- "// one predicate failed -> move to next annotation\n");
- out.append(TABS[tabs]).append("continue annotations;\n");
- tabs--;
- out.append(TABS[tabs]).append("}\n");
- tabs--;
- out.append(TABS[tabs]).append("}\n");
- out.append(TABS[tabs]).append(
"// if we got this far, all predicates succeeded, so this\n");
out.append(TABS[tabs]).append(
"// annotation matches -> add it to the list for the current\n");
@@ -403,44 +435,40 @@
tabs--;
out.append(TABS[tabs]).append("}\n");
out.append(TABS[tabs]).append("// we just finished checking one
constraint\n");
- out.append(TABS[tabs]).append("if(constraint[1] < 0){\n");
- tabs++;
- out.append(TABS[tabs]).append("// constraint is negated\n");
- out.append(TABS[tabs]).append("if(annotsForConstraints[").append(
- constrId).append("] == null){\n");
- tabs++;
- out.append(TABS[tabs]).append(
- "//no annotations matched -> constraint succeeds!\n");
- out.append(TABS[tabs]).append("annotsForConstraints[").append(
- constrId).append("] = new IntArrayList();\n");
- out.append(TABS[tabs]).append(
- "// no annotation is bound though!\n");
- out.append(TABS[tabs]).append("annotsForConstraints[").append(
- constrId).append("].add(-1);\n");
+ if(constraint[1] < 0){
+ out.append(TABS[tabs]).append("// constraint is negated\n");
+ out.append(TABS[tabs]).append("if(annotsForConstraints[").append(
+ constrId).append("] == null){\n");
+ tabs++;
+ out.append(TABS[tabs]).append(
+ "// no annotations matched -> constraint succeeds!\n");
+ out.append(TABS[tabs]).append("annotsForConstraints[").append(
+ constrId).append("] = new IntArrayList();\n");
+ out.append(TABS[tabs]).append(
+ "// no annotation is bound though!\n");
+ out.append(TABS[tabs]).append("annotsForConstraints[").append(
+ constrId).append("].add(-1);\n");
+ tabs--;
+ out.append(TABS[tabs]).append("}else{\n");
+ tabs++;
+ out.append(TABS[tabs]).append(
+ "// annotation were matched -> so the negated constraint fails!\n");
+ out.append(TABS[tabs]).append("break ").append("s").append(
+ stateId).append("t").append(transId).append(";\n");
+ tabs--;
+ out.append(TABS[tabs]).append("}\n");
+ }else{
+ out.append(TABS[tabs]).append("if(annotsForConstraints[").append(
+ constrId).append("] == null) {\n");
+ tabs++;
+ out.append(TABS[tabs]).append("// current constraint matched nothing
-> transition failed.\n");
+ out.append(TABS[tabs]).append("break ").append("s").append(
+ stateId).append("t").append(transId).append(";\n");
+ tabs--;
+ out.append(TABS[tabs]).append("}\n");
+ }
tabs--;
- out.append(TABS[tabs]).append("}else{\n");
- tabs++;
- out.append(TABS[tabs]).append(
- "// annotation were matched -> so the negated constraint fails!\n");
- out.append(TABS[tabs]).append("break ").append("s").append(
- stateId).append("t").append(transId).append(";\n");
- tabs--;
- out.append(TABS[tabs]).append("}\n");
- tabs--;
- out.append(TABS[tabs]).append("}else{\n");
- tabs++;
- out.append(TABS[tabs]).append("if(annotsForConstraints[").append(
- constrId).append("] == null) {\n");
- tabs++;
- out.append(TABS[tabs]).append("// current constraint matched nothing ->
transition failed.\n");
- out.append(TABS[tabs]).append("break ").append("s").append(
- stateId).append("t").append(transId).append(";\n");
- tabs--;
- out.append(TABS[tabs]).append("}\n");
- tabs--;
- out.append(TABS[tabs]).append("}\n");
- tabs--;
- out.append(TABS[tabs]).append("} while (false); // end constraint
block\n");
+ out.append(TABS[tabs]).append("} // end constraint block\n");
out.append(TABS[tabs]).append(
"// we finished checking all constraints, and they all succeeded\n");
Modified: futures/JAPE_PlusC/src/gate/jape/plus/SPTPhaseEG.java
===================================================================
--- futures/JAPE_PlusC/src/gate/jape/plus/SPTPhaseEG.java 2012-01-27
14:56:46 UTC (rev 15238)
+++ futures/JAPE_PlusC/src/gate/jape/plus/SPTPhaseEG.java 2012-01-27
15:14:21 UTC (rev 15239)
@@ -42,7 +42,7 @@
private final void state0(FSMInstance instance) {
- if(states[instance.state].rule >= 0) {
+ if(instance.rule >= 0) {
// current instance is in a final state
acceptingInstances.add(instance.clone());
if (matchMode == MatchMode.FIRST || matchMode == MatchMode.ONCE) {
Property changes on: futures/JAPE_PlusC/test
___________________________________________________________________
Added: svn:ignore
+ src
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
------------------------------------------------------------------------------
Try before you buy = See our experts in action!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-dev2
_______________________________________________
GATE-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gate-cvs