From 3c363182461c2b745276da9d4bd9ffbd4cd742c5 Mon Sep 17 00:00:00 2001 From: krasimir Date: Tue, 24 Jan 2017 10:14:12 +0000 Subject: [PATCH] added linearizeAll in the Java API --- src/runtime/java/jpgf.c | 62 +++++++++++++++++++ .../org/grammaticalframework/pgf/Concr.java | 3 + 2 files changed, 65 insertions(+) diff --git a/src/runtime/java/jpgf.c b/src/runtime/java/jpgf.c index 9383ccc20..a732e571e 100644 --- a/src/runtime/java/jpgf.c +++ b/src/runtime/java/jpgf.c @@ -695,6 +695,68 @@ Java_org_grammaticalframework_pgf_Concr_linearize(JNIEnv* env, jobject self, job return jstr; } +JNIEXPORT jobject JNICALL +Java_org_grammaticalframework_pgf_Concr_linearizeAll(JNIEnv* env, jobject self, jobject jexpr) +{ + PgfConcr* concr = get_ref(env, self); + + jclass list_class = (*env)->FindClass(env, "java/util/ArrayList"); + jmethodID list_constrId = (*env)->GetMethodID(env, list_class, "", "()V"); + jobject strings = (*env)->NewObject(env, list_class, list_constrId); + + jmethodID addId = (*env)->GetMethodID(env, list_class, "add", "(Ljava/lang/Object;)Z"); + + GuPool* tmp_pool = gu_local_pool(); + GuExn* err = gu_exn(tmp_pool); + + GuEnum* cts = + pgf_lzr_concretize(concr, + gu_variant_from_ptr((void*) get_ref(env, jexpr)), + err, tmp_pool); + if (!gu_ok(err)) { + gu_pool_free(tmp_pool); + return NULL; + } + + for (;;) { + PgfCncTree ctree = gu_next(cts, PgfCncTree, tmp_pool); + if (gu_variant_is_null(ctree)) + break; + + GuPool* step_pool = gu_local_pool(); + GuStringBuf* sbuf = gu_string_buf(step_pool); + GuOut* out = gu_string_buf_out(sbuf); + + ctree = pgf_lzr_wrap_linref(ctree, step_pool); + pgf_lzr_linearize_simple(concr, ctree, 0, out, err, step_pool); + if (!gu_ok(err)) { + gu_pool_free(step_pool); + + if (gu_exn_caught(err, PgfLinNonExist)) { + continue; + } else if (gu_exn_caught(err, PgfExn)) { + GuString msg = (GuString) gu_exn_caught_data(err); + throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", msg); + } else { + throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", "The expression cannot be linearized"); + } + gu_pool_free(tmp_pool); + return NULL; + } + + GuString str = gu_string_buf_freeze(sbuf, step_pool); + jstring jstr = gu2j_string(env, str); + + (*env)->CallBooleanMethod(env, strings, addId, jstr); + + gu_pool_free(step_pool); + } + + gu_pool_free(tmp_pool); + + return strings; +} + JNIEXPORT jobject JNICALL Java_org_grammaticalframework_pgf_Concr_tabularLinearize(JNIEnv* env, jobject self, jobject jexpr) { diff --git a/src/runtime/java/org/grammaticalframework/pgf/Concr.java b/src/runtime/java/org/grammaticalframework/pgf/Concr.java index 891257b5a..e6cd958bb 100644 --- a/src/runtime/java/org/grammaticalframework/pgf/Concr.java +++ b/src/runtime/java/org/grammaticalframework/pgf/Concr.java @@ -42,6 +42,9 @@ public class Concr { /** Computes the linearization of the abstract expression. */ public native String linearize(Expr expr); + /** Computes all linearizations of the abstract expression and returns an iterator over the alternatives. */ + public native Iterable linearizeAll(Expr expr); + /** Linearizes the expression as an inflection table. * @return a map from the name of the inflection form to its value. */