JNIEXPORT jobject JNICALL Java_org_grammaticalframework_pgf_Completer_complete(JNIEnv* env, jclass clazz, jobject jconcr, jstring jstartCat, jstring js, jstring jprefix) { GuPool* pool = gu_new_pool(); GuString startCat = j2gu_string(env, jstartCat, pool); GuString s = j2gu_string(env, js, pool); GuString prefix = j2gu_string(env, jprefix, pool); GuExn* parse_err = gu_new_exn(pool); GuEnum* res = pgf_complete(get_ref(env, jconcr), startCat, s, prefix, parse_err, pool); if (!gu_ok(parse_err)) { if (gu_exn_caught(parse_err, PgfExn)) { GuString msg = (GuString) gu_exn_caught_data(parse_err); throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", msg); } else if (gu_exn_caught(parse_err, PgfParseError)) { GuString tok = (GuString) gu_exn_caught_data(parse_err); throw_string_exception(env, "org/grammaticalframework/pgf/ParseError", tok); } gu_pool_free(pool); return NULL; } jclass tokiter_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/TokenIterator"); jmethodID constrId = (*env)->GetMethodID(env, tokiter_class, "<init>", "(JJ)V"); jobject jtokiter = (*env)->NewObject(env, tokiter_class, constrId, p2l(pool), p2l(res)); return jtokiter; }
JNIEXPORT jobject JNICALL Java_org_grammaticalframework_pgf_Lexicon_lookupWordPrefix (JNIEnv* env, jclass clazz, jobject jconcr, jstring prefix) { GuPool* pool = gu_new_pool(); GuExn* err = gu_new_exn(pool); GuEnum* en = pgf_lookup_word_prefix(get_ref(env, jconcr), j2gu_string(env, prefix, pool), pool, 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"); } return NULL; } jclass iter_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/FullFormIterator"); jmethodID iter_constrId = (*env)->GetMethodID(env, iter_class, "<init>", "(Lorg/grammaticalframework/pgf/Concr;JJ)V"); jobject iter = (*env)->NewObject(env, iter_class, iter_constrId, jconcr, p2l(pool), p2l(en)); return iter; }
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 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 jboolean JNICALL Java_org_grammaticalframework_pgf_Concr_hasLinearization(JNIEnv* env, jobject self, jstring jid) { PgfConcr* concr = get_ref(env, self); GuPool* tmp_pool = gu_local_pool(); PgfCId id = j2gu_string(env, jid, tmp_pool); bool res = pgf_has_linearization(concr, id); gu_pool_free(tmp_pool); return res; }
JNIEXPORT jobject JNICALL Java_org_grammaticalframework_pgf_Parser_parseWithHeuristics (JNIEnv* env, jclass clazz, jobject jconcr, jstring jstartCat, jstring js, jdouble heuristics, jlong callbacksRef, jobject jpool) { GuPool* pool = get_ref(env, jpool); GuPool* out_pool = gu_new_pool(); GuString startCat = j2gu_string(env, jstartCat, pool); GuString s = j2gu_string(env, js, pool); GuExn* parse_err = gu_new_exn(pool); GuEnum* res = pgf_parse_with_heuristics(get_ref(env, jconcr), startCat, s, heuristics, l2p(callbacksRef), parse_err, pool, out_pool); if (!gu_ok(parse_err)) { if (gu_exn_caught(parse_err, PgfExn)) { GuString msg = (GuString) gu_exn_caught_data(parse_err); throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", msg); } else if (gu_exn_caught(parse_err, PgfParseError)) { GuString tok = (GuString) gu_exn_caught_data(parse_err); throw_string_exception(env, "org/grammaticalframework/pgf/ParseError", tok); } gu_pool_free(out_pool); return NULL; } jfieldID refId = (*env)->GetFieldID(env, (*env)->GetObjectClass(env, jconcr), "gr", "Lorg/grammaticalframework/pgf/PGF;"); jobject jpgf = (*env)->GetObjectField(env, jconcr, refId); jclass expiter_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/ExprIterator"); jmethodID constrId = (*env)->GetMethodID(env, expiter_class, "<init>", "(Lorg/grammaticalframework/pgf/PGF;Lorg/grammaticalframework/pgf/Pool;JJ)V"); jobject jexpiter = (*env)->NewObject(env, expiter_class, constrId, jpgf, jpool, p2l(out_pool), p2l(res)); return jexpiter; }
JNIEXPORT jobject JNICALL Java_org_grammaticalframework_pgf_PGF_getFunctionType(JNIEnv* env, jobject self, jstring jid) { PgfPGF* pgf = get_ref(env, self); GuPool* tmp_pool = gu_new_pool(); PgfCId id = j2gu_string(env, jid, tmp_pool); PgfType* tp = pgf_function_type(pgf, id); gu_pool_free(tmp_pool); if (tp == NULL) return NULL; jclass type_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/Type"); jmethodID constrId = (*env)->GetMethodID(env, type_class, "<init>", "(Lorg/grammaticalframework/pgf/PGF;J)V"); jobject jtype = (*env)->NewObject(env, type_class, constrId, self, p2l(tp)); return jtype; }
JNIEXPORT void JNICALL Java_org_grammaticalframework_pgf_Parser_addLiteralCallback (JNIEnv* env, jclass clazz, jobject jconcr, jlong callbacksRef, jstring jcat, jobject jcallback, jobject jpool) { PgfConcr* concr = get_ref(env, jconcr); GuPool* pool = get_ref(env, jpool); JPgfLiteralCallback* callback = gu_new(JPgfLiteralCallback, pool); callback->callback.match = jpgf_literal_callback_match; callback->callback.predict = jpgf_literal_callback_predict; callback->jcallback = (*env)->NewGlobalRef(env, jcallback); callback->fin.fn = jpgf_literal_callback_fin; jclass callback_class = (*env)->GetObjectClass(env, jcallback); callback->match_methodId = (*env)->GetMethodID(env, callback_class, "match", "(ILjava/lang/String;I)Lorg/grammaticalframework/pgf/LiteralCallback$CallbackResult;"); callback->predict_methodId = (*env)->GetMethodID(env, callback_class, "predict", "(ILjava/lang/String;)Ljava/util/Iterator;"); gu_pool_finally(pool, &callback->fin); pgf_callbacks_map_add_literal(concr, l2p(callbacksRef), j2gu_string(env, jcat, pool), &callback->callback); }
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 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; }