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 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 jlong JNICALL Java_org_grammaticalframework_pgf_Expr_initApp(JNIEnv* env, jclass clazz, jstring jfun, jobjectArray args, jlong jpool) { GuPool* pool = l2p(jpool); PgfExpr expr; GuString fun = (*env)->GetStringUTFChars(env, jfun, 0); PgfExprFun* e = gu_new_flex_variant(PGF_EXPR_FUN, PgfExprFun, fun, strlen(fun)+1, &expr, pool); strcpy(e->fun, fun); (*env)->ReleaseStringUTFChars(env, jfun, fun); size_t n_args = (*env)->GetArrayLength(env, args); for (size_t i = 0; i < n_args; i++) { PgfExpr fun = expr; PgfExpr arg = gu_variant_from_ptr(get_ref(env, (*env)->GetObjectArrayElement(env, args, i))); PgfExprApp* e = gu_new_variant(PGF_EXPR_APP, PgfExprApp, &expr, pool); e->fun = fun; e->arg = arg; } return expr; }
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 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 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; }
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; }