From 18a5548b2b96dd84196b84028dc2d42e5e2e38fd Mon Sep 17 00:00:00 2001 From: krasimir Date: Wed, 13 Apr 2016 12:02:42 +0000 Subject: [PATCH] added getCategories() and getFunctions() in the Java binding --- src/runtime/java/jpgf.c | 78 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/src/runtime/java/jpgf.c b/src/runtime/java/jpgf.c index 4d00b3c4b..60027ea99 100644 --- a/src/runtime/java/jpgf.c +++ b/src/runtime/java/jpgf.c @@ -236,6 +236,84 @@ Java_org_grammaticalframework_pgf_PGF_getLanguages(JNIEnv* env, jobject self) return languages; } +static void +pgf_collect_names(GuMapItor* fn, const void* key, void* value, GuExn* err) +{ + PgfCId name = (PgfCId) key; + JPGFClosure* clo = (JPGFClosure*) fn; + + jstring jname = gu2j_string(clo->env, name); + + jclass list_class = (*clo->env)->GetObjectClass(clo->env, clo->object); + jmethodID add_method = (*clo->env)->GetMethodID(clo->env, list_class, "add", "(Ljava/lang/Object;)Z"); + + (*clo->env)->CallObjectMethod(clo->env, clo->object, add_method, jname); +} + +JNIEXPORT jobject JNICALL +Java_org_grammaticalframework_pgf_PGF_getCategories(JNIEnv* env, jobject self) +{ + jclass list_class = (*env)->FindClass(env, "java/util/ArrayList"); + if (!list_class) + return NULL; + jmethodID constrId = (*env)->GetMethodID(env, list_class, "", "()V"); + if (!constrId) + return NULL; + jobject categories = (*env)->NewObject(env, list_class, constrId); + if (!categories) + return NULL; + + PgfPGF* pgf = get_ref(env, self); + + GuPool* tmp_pool = gu_local_pool(); + + // Create an exception frame that catches all errors. + GuExn* err = gu_exn(tmp_pool); + + JPGFClosure clo = { { pgf_collect_names }, env, self, categories }; + pgf_iter_categories(pgf, &clo.fn, err); + if (!gu_ok(err)) { + gu_pool_free(tmp_pool); + return NULL; + } + + gu_pool_free(tmp_pool); + + return categories; +} + +JNIEXPORT jobject JNICALL +Java_org_grammaticalframework_pgf_PGF_getFunctions(JNIEnv* env, jobject self) +{ + jclass list_class = (*env)->FindClass(env, "java/util/ArrayList"); + if (!list_class) + return NULL; + jmethodID constrId = (*env)->GetMethodID(env, list_class, "", "()V"); + if (!constrId) + return NULL; + jobject functions = (*env)->NewObject(env, list_class, constrId); + if (!functions) + return NULL; + + PgfPGF* pgf = get_ref(env, self); + + GuPool* tmp_pool = gu_local_pool(); + + // Create an exception frame that catches all errors. + GuExn* err = gu_exn(tmp_pool); + + JPGFClosure clo = { { pgf_collect_names }, env, self, functions }; + pgf_iter_functions(pgf, &clo.fn, err); + if (!gu_ok(err)) { + gu_pool_free(tmp_pool); + return NULL; + } + + gu_pool_free(tmp_pool); + + return functions; +} + JNIEXPORT jstring JNICALL Java_org_grammaticalframework_pgf_Concr_getName(JNIEnv* env, jobject self) {