Example #1
0
static void
print_result(PgfExprProb* ep, PgfConcr* to_concr, 
             GuWriter* wtr, GuExn* err, GuPool* ppool)
{
	// Write out the abstract syntax tree
	gu_printf(wtr, err, " [%f] ", ep->prob);
	pgf_print_expr(ep->expr, 0, wtr, err);
	gu_putc('\n', wtr, err);

	// Enumerate the concrete syntax trees corresponding
	// to the abstract tree.
	GuEnum* cts = pgf_lzr_concretize(to_concr, ep->expr, ppool);
	while (true) {
		PgfCncTree ctree =
			gu_next(cts, PgfCncTree, ppool);
		if (gu_variant_is_null(ctree)) {
			break;
		}
		gu_putc(' ', wtr, err);
		// Linearize the concrete tree as a simple
		// sequence of strings.
		pgf_lzr_linearize_simple(to_concr	, ctree, 0, wtr, err);
		gu_putc('\n', wtr, err);
		gu_writer_flush(wtr, err);
	}
}
Example #2
0
static void
print_result(PgfExprProb* ep, PgfConcr* to_concr,
             GuOut* out, GuExn* err, GuPool* ppool)
{
    // Write out the abstract syntax tree
    gu_printf(out, err, " [%f] ", ep->prob);
    pgf_print_expr(ep->expr, NULL, 0, out, err);
    gu_putc('\n', out, err);

    // Enumerate the concrete syntax trees corresponding
    // to the abstract tree.
    GuEnum* cts = pgf_lzr_concretize(to_concr, ep->expr, err, ppool);
    while (true) {
        PgfCncTree ctree =
            gu_next(cts, PgfCncTree, ppool);
        if (gu_variant_is_null(ctree)) {
            break;
        }
        gu_putc(' ', out, err);
        // Linearize the concrete tree as a simple
        // sequence of strings.
        pgf_lzr_linearize_simple(to_concr, ctree, 0, out, err, ppool);

        if (gu_exn_caught(err, PgfLinNonExist)) {
            // encountered nonExist. Unfortunately there
            // might be some output printed already. The
            // right solution should be to use GuStringBuf.
            gu_exn_clear(err);
        }
        gu_putc('\n', out, err);
        gu_out_flush(out, err);
    }
}
Example #3
0
File: jpgf.c Project: Deseaus/GF
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;
}
Example #4
0
File: jpgf.c Project: Deseaus/GF
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;
}