JNIEXPORT jobject JNICALL Java_org_grammaticalframework_pgf_PGF_readPGF__Ljava_io_InputStream_2(JNIEnv *env, jclass cls, jobject java_stream) { GuPool* pool = gu_new_pool(); GuPool* tmp_pool = gu_local_pool(); GuInStream* jstream = jpgf_new_java_stream(env, java_stream, tmp_pool); if (!jstream) { gu_pool_free(pool); gu_pool_free(tmp_pool); return NULL; } GuIn* in = gu_new_in(jstream, tmp_pool); // Create an exception frame that catches all errors. GuExn* err = gu_exn(tmp_pool); // Read the PGF grammar. PgfPGF* pgf = pgf_read_in(in, pool, tmp_pool, err); if (!gu_ok(err)) { throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", "The grammar cannot be loaded"); gu_pool_free(pool); gu_pool_free(tmp_pool); return NULL; } gu_pool_free(tmp_pool); jmethodID constrId = (*env)->GetMethodID(env, cls, "<init>", "(JJ)V"); return (*env)->NewObject(env, cls, constrId, p2l(pool), p2l(pgf)); }
JNIEXPORT jobject JNICALL Java_org_grammaticalframework_pgf_PGF_getLanguages(JNIEnv* env, jobject self) { jclass map_class = (*env)->FindClass(env, "java/util/HashMap"); if (!map_class) return NULL; jmethodID constrId = (*env)->GetMethodID(env, map_class, "<init>", "()V"); if (!constrId) return NULL; jobject languages = (*env)->NewObject(env, map_class, constrId); if (!languages) 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_langs }, env, self, languages }; pgf_iter_languages(pgf, &clo.fn, err); if (!gu_ok(err)) { gu_pool_free(tmp_pool); return NULL; } gu_pool_free(tmp_pool); return languages; }
JNIEXPORT jobject JNICALL Java_org_grammaticalframework_pgf_Expr_readExpr(JNIEnv* env, jclass clazz, jstring s) { GuPool* pool = gu_new_pool(); GuPool* tmp_pool = gu_local_pool(); GuString buf = j2gu_string(env, s, tmp_pool); GuIn* in = gu_data_in((uint8_t*) buf, strlen(buf), tmp_pool); GuExn* err = gu_exn(tmp_pool); PgfExpr e = pgf_read_expr(in, pool, err); if (!gu_ok(err) || gu_variant_is_null(e)) { throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", "The expression cannot be parsed"); gu_pool_free(tmp_pool); gu_pool_free(pool); return NULL; } gu_pool_free(tmp_pool); jclass pool_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/Pool"); jmethodID pool_constrId = (*env)->GetMethodID(env, pool_class, "<init>", "(J)V"); jobject jpool = (*env)->NewObject(env, pool_class, pool_constrId, p2l(pool)); jmethodID constrId = (*env)->GetMethodID(env, clazz, "<init>", "(Lorg/grammaticalframework/pgf/Pool;Ljava/lang/Object;J)V"); return (*env)->NewObject(env, clazz, constrId, jpool, NULL, p2l(gu_variant_to_ptr(e))); }
JNIEXPORT jstring JNICALL Java_org_grammaticalframework_pgf_Concr_linearize(JNIEnv* env, jobject self, jobject jexpr) { GuPool* tmp_pool = gu_local_pool(); GuExn* err = gu_exn(tmp_pool); GuStringBuf* sbuf = gu_string_buf(tmp_pool); GuOut* out = gu_string_buf_out(sbuf); pgf_linearize(get_ref(env, self), gu_variant_from_ptr((void*) get_ref(env, jexpr)), out, err); if (!gu_ok(err)) { if (gu_exn_caught(err, PgfLinNonExist)) { gu_pool_free(tmp_pool); return NULL; } 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, tmp_pool); jstring jstr = gu2j_string(env, str); gu_pool_free(tmp_pool); return jstr; }
JNIEXPORT jobject JNICALL Java_org_grammaticalframework_sg_SG_openSG(JNIEnv *env, jclass cls, jstring path) { GuPool* tmp_pool = gu_local_pool(); // Create an exception frame that catches all errors. GuExn* err = gu_exn(tmp_pool); const char *fpath = (*env)->GetStringUTFChars(env, path, 0); // Read the PGF grammar. SgSG* sg = sg_open(fpath, err); (*env)->ReleaseStringUTFChars(env, path, fpath); if (!gu_ok(err)) { GuString msg; if (gu_exn_caught(err, SgError)) { msg = (GuString) gu_exn_caught_data(err); } else { msg = "The database cannot be opened"; } throw_string_exception(env, "org/grammaticalframework/sg/SGError", msg); gu_pool_free(tmp_pool); return NULL; } gu_pool_free(tmp_pool); jmethodID constrId = (*env)->GetMethodID(env, cls, "<init>", "(J)V"); return (*env)->NewObject(env, cls, constrId, p2l(sg)); }
JNIEXPORT jobject JNICALL Java_org_grammaticalframework_pgf_PGF_readPGF__Ljava_lang_String_2(JNIEnv *env, jclass cls, jstring s) { GuPool* pool = gu_new_pool(); GuPool* tmp_pool = gu_local_pool(); // Create an exception frame that catches all errors. GuExn* err = gu_exn(tmp_pool); const char *fpath = (*env)->GetStringUTFChars(env, s, 0); // Read the PGF grammar. PgfPGF* pgf = pgf_read(fpath, pool, err); (*env)->ReleaseStringUTFChars(env, s, fpath); if (!gu_ok(err)) { if (gu_exn_caught(err, GuErrno)) { throw_jstring_exception(env, "java/io/FileNotFoundException", s); } else { throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", "The grammar cannot be loaded"); } gu_pool_free(pool); gu_pool_free(tmp_pool); return NULL; } gu_pool_free(tmp_pool); jmethodID constrId = (*env)->GetMethodID(env, cls, "<init>", "(JJ)V"); return (*env)->NewObject(env, cls, constrId, p2l(pool), p2l(pgf)); }
JNIEXPORT jobject JNICALL Java_org_grammaticalframework_pgf_Concr_lookupMorpho(JNIEnv* env, jobject self, jstring sentence) { jclass list_class = (*env)->FindClass(env, "java/util/ArrayList"); jmethodID list_constrId = (*env)->GetMethodID(env, list_class, "<init>", "()V"); jobject analyses = (*env)->NewObject(env, list_class, list_constrId); jmethodID addId = (*env)->GetMethodID(env, list_class, "add", "(Ljava/lang/Object;)Z"); jclass an_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/MorphoAnalysis"); jmethodID an_constrId = (*env)->GetMethodID(env, an_class, "<init>", "(Ljava/lang/String;Ljava/lang/String;D)V"); GuPool* tmp_pool = gu_new_pool(); GuExn* err = gu_exn(tmp_pool); JMorphoCallback callback = { { jpgf_collect_morpho }, analyses, 0, env, addId, an_class, an_constrId }; pgf_lookup_morpho(get_ref(env, self), j2gu_string(env, sentence, tmp_pool), &callback.fn, err); if (!gu_ok(err)) { 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 lookup failed"); } analyses = NULL; } gu_pool_free(tmp_pool); return analyses; }
JNIEXPORT jlong JNICALL Java_org_grammaticalframework_sg_SG_insertTriple(JNIEnv *env, jobject self, jobject jsubj, jobject jpred, jobject jobj) { SgSG *sg = get_ref(env, self); GuPool* tmp_pool = gu_local_pool(); GuExn* err = gu_exn(tmp_pool); SgTriple triple; triple[0] = gu_variant_from_ptr((void*) get_ref(env, jsubj)); triple[1] = gu_variant_from_ptr((void*) get_ref(env, jpred)); triple[2] = gu_variant_from_ptr((void*) get_ref(env, jobj)); SgId id = sg_insert_triple(sg, triple, err); if (!gu_ok(err)) { GuString msg; if (gu_exn_caught(err, SgError)) { msg = (GuString) gu_exn_caught_data(err); } else { msg = "The insertion failed"; } throw_string_exception(env, "org/grammaticalframework/sg/SGError", msg); gu_pool_free(tmp_pool); return 0; } gu_pool_free(tmp_pool); return id; }
JNIEXPORT void JNICALL Java_org_grammaticalframework_pgf_Concr_load__Ljava_io_InputStream_2(JNIEnv* env, jobject self, jobject java_stream) { GuPool* tmp_pool = gu_local_pool(); GuInStream* jstream = jpgf_new_java_stream(env, java_stream, tmp_pool); if (!jstream) { gu_pool_free(tmp_pool); return; } GuIn* in = gu_new_in(jstream, tmp_pool); // Create an exception frame that catches all errors. GuExn* err = gu_exn(tmp_pool); pgf_concrete_load(get_ref(env, self), in, err); if (!gu_ok(err)) { 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 language cannot be loaded"); } } gu_pool_free(tmp_pool); }
int main(int argc, char* argv[]) { // Set the character locale, so we can produce proper output. setlocale(LC_CTYPE, ""); if (argc != 2) { fprintf(stderr, "usage: %s pgf\n", argv[0]); return EXIT_FAILURE; } char* filename = argv[1]; GuPool* pool = gu_new_pool(); GuExn* err = gu_exn(NULL, type, pool); PgfPGF* pgf = pgf_read(filename, pool, err); int status = 0; if (!gu_ok(err)) { fprintf(stderr, "Reading PGF failed\n"); status = 1; goto fail_read; } GuOut* out = gu_file_out(stdout, pool); pgf_print(pgf, out, err); gu_out_flush(out, err); fail_read: gu_pool_free(pool); return status; }
JNIEXPORT jstring JNICALL Java_org_grammaticalframework_pgf_Expr_showExpr(JNIEnv* env, jclass clazz, jlong ref) { GuPool* tmp_pool = gu_local_pool(); GuExn* err = gu_exn(tmp_pool); GuStringBuf* sbuf = gu_string_buf(tmp_pool); GuOut* out = gu_string_buf_out(sbuf); pgf_print_expr(gu_variant_from_ptr(l2p(ref)), NULL, 0, out, err); GuString str = gu_string_buf_freeze(sbuf, tmp_pool); jstring jstr = gu2j_string(env, str); gu_pool_free(tmp_pool); return jstr; }
JNIEXPORT jobject JNICALL Java_org_grammaticalframework_pgf_FullFormIterator_fetchFullFormEntry (JNIEnv* env, jobject clazz, jlong enumRef, jobject jpool, jobject jconcr) { GuEnum* res = (GuEnum*) l2p(enumRef); PgfFullFormEntry* entry = gu_next(res, PgfFullFormEntry*, get_ref(env, jpool)); if (entry == NULL) return NULL; GuString form = pgf_fullform_get_string(entry); jclass list_class = (*env)->FindClass(env, "java/util/ArrayList"); jmethodID list_constrId = (*env)->GetMethodID(env, list_class, "<init>", "()V"); jobject analyses = (*env)->NewObject(env, list_class, list_constrId); jmethodID addId = (*env)->GetMethodID(env, list_class, "add", "(Ljava/lang/Object;)Z"); jclass an_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/MorphoAnalysis"); jmethodID an_constrId = (*env)->GetMethodID(env, an_class, "<init>", "(Ljava/lang/String;Ljava/lang/String;D)V"); GuPool* tmp_pool = gu_local_pool(); GuExn* err = gu_exn(tmp_pool); JMorphoCallback callback = { { jpgf_collect_morpho }, analyses, 0, env, addId, an_class, an_constrId }; pgf_fullform_get_analyses(entry, &callback.fn, err); if (!gu_ok(err)) { 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 lookup failed"); } analyses = NULL; } gu_pool_free(tmp_pool); jclass entry_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/FullFormEntry"); jmethodID entry_constrId = (*env)->GetMethodID(env, entry_class, "<init>", "(Ljava/lang/String;DLjava/util/List;)V"); jobject jentry = (*env)->NewObject(env, entry_class, entry_constrId, gu2j_string(env,form), - log(callback.prob), analyses); return jentry; }
JNIEXPORT void JNICALL Java_org_grammaticalframework_sg_TripleResult_close(JNIEnv *env, jobject self) { SgTripleResult *res = get_ref(env, self); GuPool* tmp_pool = gu_local_pool(); GuExn* err = gu_exn(tmp_pool); sg_triple_result_close(res, err); if (!gu_ok(err)) { GuString msg; if (gu_exn_caught(err, SgError)) { msg = (GuString) gu_exn_caught_data(err); } else { msg = "Closing the result failed"; } throw_string_exception(env, "org/grammaticalframework/sg/SGError", msg); } gu_pool_free(tmp_pool); }
JNIEXPORT jobject JNICALL Java_org_grammaticalframework_sg_SG_queryTriple(JNIEnv *env, jobject self, jobject jsubj, jobject jpred, jobject jobj) { SgSG *sg = get_ref(env, self); GuPool* tmp_pool = gu_local_pool(); GuExn* err = gu_exn(tmp_pool); SgTriple triple; triple[0] = (jsubj == NULL) ? gu_null_variant : gu_variant_from_ptr((void*) get_ref(env, jsubj)); triple[1] = (jpred == NULL) ? gu_null_variant : gu_variant_from_ptr((void*) get_ref(env, jpred)); triple[2] = (jobj == NULL) ? gu_null_variant : gu_variant_from_ptr((void*) get_ref(env, jobj)); SgTripleResult* res = sg_query_triple(sg, triple, err); if (!gu_ok(err)) { GuString msg; if (gu_exn_caught(err, SgError)) { msg = (GuString) gu_exn_caught_data(err); } else { msg = "The query failed"; } throw_string_exception(env, "org/grammaticalframework/sg/SGError", msg); gu_pool_free(tmp_pool); return NULL; } gu_pool_free(tmp_pool); jclass res_class = (*env)->FindClass(env, "org/grammaticalframework/sg/TripleResult"); jmethodID constrId = (*env)->GetMethodID(env, res_class, "<init>", "(JLorg/grammaticalframework/pgf/Expr;Lorg/grammaticalframework/pgf/Expr;Lorg/grammaticalframework/pgf/Expr;)V"); jobject jres = (*env)->NewObject(env, res_class, constrId, p2l(res), jsubj, jpred, jobj); return jres; }
JNIEXPORT jobjectArray JNICALL Java_org_grammaticalframework_sg_SG_readTriple(JNIEnv *env, jclass cls, jstring s) { GuPool* pool = gu_new_pool(); GuPool* tmp_pool = gu_local_pool(); GuString buf = j2gu_string(env, s, tmp_pool); GuIn* in = gu_data_in((uint8_t*) buf, strlen(buf), tmp_pool); GuExn* err = gu_exn(tmp_pool); const int len = 3; PgfExpr exprs[len]; int res = pgf_read_expr_tuple(in, 3, exprs, pool, err); if (!gu_ok(err) || res == 0) { throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", "The expression cannot be parsed"); gu_pool_free(tmp_pool); gu_pool_free(pool); return NULL; } gu_pool_free(tmp_pool); jclass pool_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/Pool"); jmethodID pool_constrId = (*env)->GetMethodID(env, pool_class, "<init>", "(J)V"); jobject jpool = (*env)->NewObject(env, pool_class, pool_constrId, p2l(pool)); jclass expr_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/Expr"); jmethodID expr_constrId = (*env)->GetMethodID(env, expr_class, "<init>", "(Lorg/grammaticalframework/pgf/Pool;Ljava/lang/Object;J)V"); jobjectArray array = (*env)->NewObjectArray(env, len, expr_class, NULL); for (int i = 0; i < len; i++) { jobject obj = (*env)->NewObject(env, expr_class, expr_constrId, jpool, NULL, p2l(gu_variant_to_ptr(exprs[i]))); (*env)->SetObjectArrayElement(env, array, i, obj); (*env)->DeleteLocalRef(env, obj); } return array; }
JNIEXPORT jobject JNICALL Java_org_grammaticalframework_pgf_Generator_generateAll(JNIEnv* env, jclass clazz, jobject jpgf, jstring jstartCat) { GuPool* pool = gu_new_pool(); GuPool* out_pool = gu_new_pool(); GuString startCat = j2gu_string(env, jstartCat, pool); GuExn* err = gu_exn(pool); GuEnum* res = pgf_generate_all(get_ref(env, jpgf), startCat, err, pool, out_pool); if (res == NULL) { throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", "The generation failed"); gu_pool_free(pool); return NULL; } jclass expiter_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/ExprIterator"); jmethodID constrId = (*env)->GetMethodID(env, expiter_class, "<init>", "(Lorg/grammaticalframework/pgf/PGF;JJJ)V"); jobject jexpiter = (*env)->NewObject(env, expiter_class, constrId, jpgf, p2l(pool), p2l(out_pool), p2l(res)); return jexpiter; }
JNIEXPORT void JNICALL Java_org_grammaticalframework_sg_SG_close(JNIEnv *env, jobject self) { GuPool* tmp_pool = gu_local_pool(); // Create an exception frame that catches all errors. GuExn* err = gu_exn(tmp_pool); sg_close(get_ref(env, self), err); if (!gu_ok(err)) { GuString msg; if (gu_exn_caught(err, SgError)) { msg = (GuString) gu_exn_caught_data(err); } else { msg = "The database cannot be closed"; } throw_string_exception(env, "org/grammaticalframework/sg/SGError", msg); gu_pool_free(tmp_pool); return; } gu_pool_free(tmp_pool); }
JNIEXPORT jobjectArray JNICALL Java_org_grammaticalframework_pgf_Concr_bracketedLinearize(JNIEnv* env, jobject self, jobject jexpr) { jclass object_class = (*env)->FindClass(env, "java/lang/Object"); if (!object_class) return NULL; jclass bracket_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/Bracket"); if (!bracket_class) return NULL; jmethodID bracket_constrId = (*env)->GetMethodID(env, bracket_class, "<init>", "(Ljava/lang/String;Ljava/lang/String;II[Ljava/lang/Object;)V"); if (!bracket_constrId) return NULL; GuPool* tmp_pool = gu_local_pool(); GuExn* err = gu_exn(tmp_pool); PgfConcr* concr = get_ref(env, self); GuEnum* cts = pgf_lzr_concretize(concr, gu_variant_from_ptr((void*) get_ref(env, jexpr)), err, tmp_pool); if (!gu_ok(err)) { 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 concretized"); } gu_pool_free(tmp_pool); return NULL; } PgfCncTree ctree = gu_next(cts, PgfCncTree, tmp_pool); if (gu_variant_is_null(ctree)) { throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", "The expression cannot be concretized"); gu_pool_free(tmp_pool); return NULL; } ctree = pgf_lzr_wrap_linref(ctree, tmp_pool); PgfBracketLznState state; state.funcs = &pgf_bracket_lin_funcs; state.env = env; state.tmp_pool = tmp_pool; state.stack = gu_new_buf(GuBuf*, tmp_pool); state.list = gu_new_buf(jobject, tmp_pool); state.object_class = object_class; state.bracket_class = bracket_class; state.bracket_constrId = bracket_constrId; pgf_lzr_linearize(concr, ctree, 0, &state.funcs, tmp_pool); size_t len = gu_buf_length(state.list); jobjectArray array = (*env)->NewObjectArray(env, len, object_class, NULL); for (int i = 0; i < len; i++) { jobject obj = gu_buf_get(state.list, jobject, i); (*env)->SetObjectArrayElement(env, array, i, obj); (*env)->DeleteLocalRef(env, obj); } gu_pool_free(tmp_pool); return array; }
JNIEXPORT jobject JNICALL Java_org_grammaticalframework_pgf_Concr_tabularLinearize(JNIEnv* env, jobject self, jobject jexpr) { jclass map_class = (*env)->FindClass(env, "java/util/HashMap"); if (!map_class) return NULL; jmethodID constrId = (*env)->GetMethodID(env, map_class, "<init>", "()V"); if (!constrId) return NULL; jobject table = (*env)->NewObject(env, map_class, constrId); if (!table) return NULL; jmethodID put_method = (*env)->GetMethodID(env, map_class, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); if (!put_method) return NULL; PgfConcr* concr = get_ref(env, self); 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)) { 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 concretized"); } gu_pool_free(tmp_pool); return NULL; } PgfCncTree ctree = gu_next(cts, PgfCncTree, tmp_pool); if (gu_variant_is_null(ctree)) { gu_pool_free(tmp_pool); return NULL; } size_t n_lins; GuString* labels; pgf_lzr_get_table(concr, ctree, &n_lins, &labels); for (size_t lin_idx = 0; lin_idx < n_lins; lin_idx++) { GuStringBuf* sbuf = gu_string_buf(tmp_pool); GuOut* out = gu_string_buf_out(sbuf); pgf_lzr_linearize_simple(concr, ctree, lin_idx, out, err, tmp_pool); jstring jstr = NULL; if (gu_ok(err)) { GuString str = gu_string_buf_freeze(sbuf, tmp_pool); jstr = gu2j_string(env, str); } else { gu_exn_clear(err); } jstring jname = gu2j_string(env, labels[lin_idx]); (*env)->CallObjectMethod(env, table, put_method, jname, jstr); (*env)->DeleteLocalRef(env, jname); if (jstr != NULL) (*env)->DeleteLocalRef(env, jstr); } gu_pool_free(tmp_pool); return table; }
JNIEXPORT jboolean JNICALL Java_org_grammaticalframework_sg_TripleResult_hasNext(JNIEnv *env, jobject self) { SgTripleResult *res = get_ref(env, self); GuPool* tmp_pool = gu_local_pool(); GuPool* out_pool = gu_new_pool(); GuExn* err = gu_exn(tmp_pool); SgId key; SgTriple triple; int r = sg_triple_result_fetch(res, &key, triple, out_pool, err); if (!gu_ok(err)) { GuString msg; if (gu_exn_caught(err, SgError)) { msg = (GuString) gu_exn_caught_data(err); } else { msg = "The fetch failed"; } throw_string_exception(env, "org/grammaticalframework/sg/SGError", msg); gu_pool_free(out_pool); gu_pool_free(tmp_pool); return JNI_FALSE; } gu_pool_free(tmp_pool); if (r) { SgTriple orig_triple; sg_triple_result_get_query(res, orig_triple); jclass pool_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/Pool"); jmethodID pool_constrId = (*env)->GetMethodID(env, pool_class, "<init>", "(J)V"); jobject jpool = (*env)->NewObject(env, pool_class, pool_constrId, p2l(out_pool)); jclass expr_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/Expr"); jmethodID constrId = (*env)->GetMethodID(env, expr_class, "<init>", "(Lorg/grammaticalframework/pgf/Pool;Ljava/lang/Object;J)V"); jclass result_class = (*env)->GetObjectClass(env, self); jfieldID keyId = (*env)->GetFieldID(env, result_class, "key", "J"); (*env)->SetLongField(env, self, keyId, key); if (triple[0] != orig_triple[0]) { jfieldID subjId = (*env)->GetFieldID(env, result_class, "subj", "Lorg/grammaticalframework/pgf/Expr;"); jobject jsubj = (*env)->NewObject(env, expr_class, constrId, jpool, jpool, p2l(gu_variant_to_ptr(triple[0]))); (*env)->SetObjectField(env, self, subjId, jsubj); } if (triple[1] != orig_triple[1]) { jfieldID predId = (*env)->GetFieldID(env, result_class, "pred", "Lorg/grammaticalframework/pgf/Expr;"); jobject jpred = (*env)->NewObject(env, expr_class, constrId, jpool, jpool, p2l(gu_variant_to_ptr(triple[1]))); (*env)->SetObjectField(env, self, predId, jpred); } if (triple[2] != orig_triple[2]) { jfieldID objId = (*env)->GetFieldID(env, result_class, "obj", "Lorg/grammaticalframework/pgf/Expr;"); jobject jobj = (*env)->NewObject(env, expr_class, constrId, jpool, jpool, p2l(gu_variant_to_ptr(triple[2]))); (*env)->SetObjectField(env, self, objId, jobj); } return JNI_TRUE; } else { gu_pool_free(out_pool); return JNI_FALSE; } }
static PgfExprProb* jpgf_literal_callback_match(PgfLiteralCallback* self, PgfConcr* concr, size_t lin_idx, GuString sentence, size_t* poffset, GuPool *out_pool) { JPgfLiteralCallback* callback = gu_container(self, JPgfLiteralCallback, callback); JNIEnv *env; (*cachedJVM)->AttachCurrentThread(cachedJVM, &env, NULL); jstring jsentence = gu2j_string(env, sentence); size_t joffset = gu2j_string_offset(sentence, *poffset); jobject result = (*env)->CallObjectMethod(env, callback->jcallback, callback->match_methodId, lin_idx, jsentence, joffset); if (result == NULL) return NULL; jclass result_class = (*env)->GetObjectClass(env, result); jfieldID epId = (*env)->GetFieldID(env, result_class, "ep", "Lorg/grammaticalframework/pgf/ExprProb;"); jobject jep = (*env)->GetObjectField(env, result, epId); jclass ep_class = (*env)->GetObjectClass(env, jep); jfieldID exprId = (*env)->GetFieldID(env, ep_class, "expr", "Lorg/grammaticalframework/pgf/Expr;"); jobject jexpr = (*env)->GetObjectField(env, jep, exprId); jfieldID probId = (*env)->GetFieldID(env, ep_class, "prob", "D"); double prob = (*env)->GetDoubleField(env, jep, probId); jfieldID offsetId = (*env)->GetFieldID(env, result_class, "offset", "I"); *poffset = j2gu_string_offset(sentence, (*env)->GetIntField(env, result, offsetId)); PgfExprProb* ep = gu_new(PgfExprProb, out_pool); ep->expr = gu_variant_from_ptr(get_ref(env, jexpr)); ep->prob = prob; { // This is an uggly hack. We first show the expression ep->expr // and then we read it back but in out_pool. The whole purpose // of this is to copy the expression from the temporary pool // that was created in the Java binding to the parser pool. // There should be a real copying function or even better // there must be a way to avoid copying at all. GuPool* tmp_pool = gu_local_pool(); GuExn* err = gu_exn(tmp_pool); GuStringBuf* sbuf = gu_string_buf(tmp_pool); GuOut* out = gu_string_buf_out(sbuf); pgf_print_expr(ep->expr, NULL, 0, out, err); GuString str = gu_string_buf_freeze(sbuf, tmp_pool); GuIn* in = gu_data_in((uint8_t*) str, strlen(str), tmp_pool); ep->expr = pgf_read_expr(in, out_pool, err); if (!gu_ok(err) || gu_variant_is_null(ep->expr)) { throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", "The expression cannot be parsed"); gu_pool_free(tmp_pool); return NULL; } gu_pool_free(tmp_pool); } return ep; }