mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-22 19:22:50 -06:00
added a test class for the Java API plus a small refinement in the implementation for the binding
This commit is contained in:
8
src/runtime/java/Test.java
Normal file
8
src/runtime/java/Test.java
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
import org.grammaticalframework.*;
|
||||||
|
|
||||||
|
public class Test {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
PGF gr = PGF.readPGF("/home/krasimir/www.grammaticalframework.org/treebanks/PennTreebank/ParseEngAbs.pgf");
|
||||||
|
gr.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,17 +4,20 @@
|
|||||||
|
|
||||||
JNIEXPORT jobject JNICALL
|
JNIEXPORT jobject JNICALL
|
||||||
Java_org_grammaticalframework_PGF_readPGF(JNIEnv *env, jclass cls, jstring s)
|
Java_org_grammaticalframework_PGF_readPGF(JNIEnv *env, jclass cls, jstring s)
|
||||||
{
|
{
|
||||||
const char *fpath = (*env)->GetStringUTFChars(env, s, 0);
|
|
||||||
|
|
||||||
GuPool* pool = gu_new_pool();
|
GuPool* pool = gu_new_pool();
|
||||||
GuPool* tmp_pool = gu_local_pool();
|
GuPool* tmp_pool = gu_local_pool();
|
||||||
|
|
||||||
// Create an exception frame that catches all errors.
|
// Create an exception frame that catches all errors.
|
||||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||||
|
|
||||||
|
const char *fpath = (*env)->GetStringUTFChars(env, s, 0);
|
||||||
|
|
||||||
// Read the PGF grammar.
|
// Read the PGF grammar.
|
||||||
PgfPGF* pgf = pgf_read(fpath, pool, err);
|
PgfPGF* pgf = pgf_read(fpath, pool, err);
|
||||||
|
|
||||||
|
(*env)->ReleaseStringUTFChars(env, s, fpath);
|
||||||
|
|
||||||
if (!gu_ok(err)) {
|
if (!gu_ok(err)) {
|
||||||
gu_pool_free(pool);
|
gu_pool_free(pool);
|
||||||
gu_pool_free(tmp_pool);
|
gu_pool_free(tmp_pool);
|
||||||
@@ -23,9 +26,13 @@ Java_org_grammaticalframework_PGF_readPGF(JNIEnv *env, jclass cls, jstring s)
|
|||||||
|
|
||||||
gu_pool_free(tmp_pool);
|
gu_pool_free(tmp_pool);
|
||||||
|
|
||||||
(*env)->ReleaseStringUTFChars(env, s, fpath);
|
jmethodID constrId = (*env)->GetMethodID(env, cls, "<init>", "(JJ)V");
|
||||||
|
|
||||||
jmethodID constrId = (*env)->GetMethodID(env, cls, "<init>", "(II)V");
|
return (*env)->NewObject(env, cls, constrId, (long) pool, (long) pgf);
|
||||||
|
}
|
||||||
return (*env)->NewObject(env, cls, constrId, (int) pool, (int) pgf);
|
|
||||||
|
JNIEXPORT void JNICALL
|
||||||
|
Java_org_grammaticalframework_PGF_free(JNIEnv* env, jclass cls, jlong pool)
|
||||||
|
{
|
||||||
|
gu_pool_free((GuPool*) pool);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,25 +1,31 @@
|
|||||||
package org.grammaticalframework;
|
package org.grammaticalframework;
|
||||||
|
|
||||||
public class PGF {
|
public class PGF {
|
||||||
static native PGF readPGF(String path);
|
public static native PGF readPGF(String path);
|
||||||
|
|
||||||
private int pool;
|
|
||||||
private int gr;
|
|
||||||
|
|
||||||
private PGF(int pool, int gr) {
|
public void close() {
|
||||||
|
if (pool != 0) {
|
||||||
|
free(pool);
|
||||||
|
pool = 0;
|
||||||
|
gr = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static native void free(long pool);
|
||||||
|
|
||||||
|
private long pool;
|
||||||
|
private long gr;
|
||||||
|
|
||||||
|
private PGF(long pool, long gr) {
|
||||||
this.pool = pool;
|
this.pool = pool;
|
||||||
this.gr = gr;
|
this.gr = gr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void finalize () throws Throwable {
|
||||||
|
close();
|
||||||
|
}
|
||||||
|
|
||||||
static {
|
static {
|
||||||
System.loadLibrary("jpgf");
|
System.loadLibrary("jpgf");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void test() {
|
|
||||||
System.out.println("pool="+pool+", gr="+gr);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
readPGF("/home/krasimir/www.grammaticalframework.org/treebanks/PennTreebank/ParseEngAbs.pgf").test();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user