1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
#include <jni.h>
#include <sg/sg.h>
#include <pgf/expr.h>
#include "jni_utils.h"
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 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 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 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, 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, 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, gu_variant_to_ptr(triple[2]));
(*env)->SetObjectField(env, self, objId, jobj);
}
return JNI_TRUE;
} else {
gu_pool_free(out_pool);
return JNI_FALSE;
}
}
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);
}
|