more carefull tracking of references in the Java binding

This commit is contained in:
kr.angelov
2013-08-29 05:35:45 +00:00
parent 93db90e99c
commit 48b6af710d
5 changed files with 24 additions and 18 deletions

View File

@@ -24,7 +24,7 @@ public class Test {
Concr ger = gr.getLanguages().get("PhrasebookGer");
try {
for (ExprProb ep : eng.parse(gr.getStartCat(), "where is the conference")) {
for (ExprProb ep : eng.parse(gr.getStartCat(), "where is the hotel")) {
System.out.println("["+ep.getProb()+"] "+ep.getExpr());
System.out.println(ger.linearize(ep.getExpr()));
}

View File

@@ -201,7 +201,7 @@ Java_org_grammaticalframework_pgf_Concr_getName(JNIEnv* env, jobject self)
JNIEXPORT jobject JNICALL
Java_org_grammaticalframework_pgf_Parser_parse
(JNIEnv* env, jclass self, jobject concr, jstring jstartCat, jstring js)
(JNIEnv* env, jclass clazz, jobject concr, jstring jstartCat, jstring js)
{
GuPool* pool = gu_new_pool();
GuPool* out_pool = gu_new_pool();
@@ -229,16 +229,19 @@ Java_org_grammaticalframework_pgf_Parser_parse
return NULL;
}
jfieldID refId = (*env)->GetFieldID(env, (*env)->GetObjectClass(env, concr), "gr", "Lorg/grammaticalframework/pgf/PGF;");
jobject jpgf = (*env)->GetObjectField(env, concr, refId);
jclass expiter_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/ExprIterator");
jmethodID constrId = (*env)->GetMethodID(env, expiter_class, "<init>", "(Lorg/grammaticalframework/pgf/Concr;JJJ)V");
jobject jexpiter = (*env)->NewObject(env, expiter_class, constrId, concr, (jlong) pool, (jlong) out_pool, (jlong) res);
jmethodID constrId = (*env)->GetMethodID(env, expiter_class, "<init>", "(Lorg/grammaticalframework/pgf/PGF;JJJ)V");
jobject jexpiter = (*env)->NewObject(env, expiter_class, constrId, jpgf, (jlong) pool, (jlong) out_pool, (jlong) res);
return jexpiter;
}
JNIEXPORT jobject JNICALL
Java_org_grammaticalframework_pgf_ExprIterator_fetchExprProb
(JNIEnv* env, jobject self, jlong enumRef, jobject out_pool)
(JNIEnv* env, jobject self, jlong enumRef, jobject pool, jobject gr)
{
GuEnum* res = (GuEnum*) enumRef;
@@ -247,9 +250,10 @@ Java_org_grammaticalframework_pgf_ExprIterator_fetchExprProb
return NULL;
jclass expprob_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/ExprProb");
jmethodID methodId = (*env)->GetStaticMethodID(env, expprob_class, "mkExprProb", "(Lorg/grammaticalframework/pgf/Pool;JD)Lorg/grammaticalframework/pgf/ExprProb;");
jmethodID methodId = (*env)->GetStaticMethodID(env, expprob_class, "mkExprProb",
"(Lorg/grammaticalframework/pgf/Pool;Lorg/grammaticalframework/pgf/PGF;JD)Lorg/grammaticalframework/pgf/ExprProb;");
jobject jexpprob = (*env)->CallStaticObjectMethod(env, expprob_class, methodId,
out_pool, (jlong) gu_variant_to_ptr(ep->expr), (double) ep->prob);
pool, gr, (jlong) gu_variant_to_ptr(ep->expr), (double) ep->prob);
return jexpprob;
}

View File

@@ -2,13 +2,15 @@ package org.grammaticalframework.pgf;
public class Expr {
private Pool pool;
private PGF gr;
private long ref;
Expr(Pool pool, long ref) {
Expr(Pool pool, PGF gr, long ref) {
this.pool = pool;
this.gr = gr;
this.ref = ref;
}
public String toString() {
return showExpr(ref);
}

View File

@@ -3,14 +3,14 @@ package org.grammaticalframework.pgf;
import java.util.*;
class ExprIterator implements Iterator<ExprProb> {
private Concr concr;
private PGF gr;
private Pool pool, out_pool;
private long ref;
private ExprProb ep;
private boolean fetched;
public ExprIterator(Concr concr, long pool, long out_pool, long ref) {
this.concr = concr;
public ExprIterator(PGF gr, long pool, long out_pool, long ref) {
this.gr = gr;
this.pool = new Pool(pool);
this.out_pool = new Pool(out_pool);
this.ref = ref;
@@ -18,20 +18,20 @@ class ExprIterator implements Iterator<ExprProb> {
this.fetched = false;
}
private native ExprProb fetchExprProb(long ref, Pool out_pool);
private native ExprProb fetchExprProb(long ref, Pool pool, PGF gr);
private void fetch() {
if (!fetched) {
ep = fetchExprProb(ref, out_pool);
ep = fetchExprProb(ref, out_pool, gr);
fetched = true;
}
}
public boolean hasNext() {
fetch();
return (ep != null);
}
public ExprProb next() {
fetch();
fetched = false;

View File

@@ -9,8 +9,8 @@ public class ExprProb {
this.prob = prob;
}
private static ExprProb mkExprProb(Pool pool, long expr, double prob) {
return new ExprProb(new Expr(pool, expr), prob);
private static ExprProb mkExprProb(Pool pool, PGF gr, long expr, double prob) {
return new ExprProb(new Expr(pool, gr, expr), prob);
}
public Expr getExpr() {