drop the SG library completely.

This commit is contained in:
krangelov
2020-07-08 21:12:01 +02:00
parent 47d1da0845
commit 33818076ff
17 changed files with 11 additions and 52880 deletions

View File

@@ -1,8 +1,7 @@
INSTALL_PATH = /usr/local
C_SOURCES = jpgf.c jsg.c jni_utils.c
JAVA_SOURCES = $(wildcard org/grammaticalframework/pgf/*.java) \
$(wildcard org/grammaticalframework/sg/*.java)
C_SOURCES = jpgf.c jni_utils.c
JAVA_SOURCES = $(wildcard org/grammaticalframework/pgf/*.java)
JNI_INCLUDES = $(if $(wildcard /usr/lib/jvm/default-java/include/.*), -I/usr/lib/jvm/default-java/include -I/usr/lib/jvm/default-java/include/linux, \
$(if $(wildcard /usr/lib/jvm/java-1.11.0-openjdk-amd64/include/.*), -I/usr/lib/jvm/java-1.11.0-openjdk-amd64/include/ -I/usr/lib/jvm/java-1.11.0-openjdk-amd64/include/linux, \
@@ -28,13 +27,13 @@ LIBTOOL = $(if $(shell command -v glibtool 2>/dev/null), glibtool, libtool) --t
all: libjpgf.la jpgf.jar
libjpgf.la: $(patsubst %.c, %.lo, $(C_SOURCES))
$(LIBTOOL) --mode=link $(GCC) $(CFLAGS) -g -O -o libjpgf.la -shared $^ -rpath $(INSTALL_PATH)/lib -lgu -lpgf -lsg $(WINDOWS_LDFLAGS)
$(LIBTOOL) --mode=link $(GCC) $(CFLAGS) -g -O -o libjpgf.la -shared $^ -rpath $(INSTALL_PATH)/lib -lgu -lpgf $(WINDOWS_LDFLAGS)
%.lo : %.c
$(LIBTOOL) --mode=compile $(GCC) $(CFLAGS) -g -O -c $(JNI_INCLUDES) $(WINDOWS_CCFLAGS) -std=c99 -shared $< -o $@
jpgf.jar: $(patsubst %.java, %.class, $(JAVA_SOURCES))
jar -cf $@ org/grammaticalframework/pgf/*.class org/grammaticalframework/sg/*.class
jar -cf $@ org/grammaticalframework/pgf/*.class
%.class : %.java
javac $<
@@ -45,7 +44,7 @@ install: libjpgf.la jpgf.jar
doc:
javadoc org.grammaticalframework.pgf org.grammaticalframework.sg -d java-api
javadoc org.grammaticalframework.pgf -d java-api
clean:
rm -f *.lo

View File

@@ -1,339 +0,0 @@
#include <jni.h>
#include <sg/sg.h>
#include <pgf/expr.h>
#include <pgf/linearizer.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 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_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, 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;
}
}
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 void JNICALL
Java_org_grammaticalframework_sg_SG_beginTrans(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_begin_trans(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 transaction cannot be started";
}
throw_string_exception(env, "org/grammaticalframework/sg/SGError", msg);
gu_pool_free(tmp_pool);
return;
}
gu_pool_free(tmp_pool);
}
JNIEXPORT void JNICALL
Java_org_grammaticalframework_sg_SG_commit(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_commit(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 transaction cannot be commited";
}
throw_string_exception(env, "org/grammaticalframework/sg/SGError", msg);
gu_pool_free(tmp_pool);
return;
}
gu_pool_free(tmp_pool);
}
JNIEXPORT void JNICALL
Java_org_grammaticalframework_sg_SG_rollback(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_rollback(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 transaction cannot be rolled back";
}
throw_string_exception(env, "org/grammaticalframework/sg/SGError", msg);
gu_pool_free(tmp_pool);
return;
}
gu_pool_free(tmp_pool);
}
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;
}

View File

@@ -1,56 +0,0 @@
package org.grammaticalframework.sg;
import java.io.Closeable;
import org.grammaticalframework.pgf.*;
/** This class represents a connection to a semantic graph database.
* The semantic graph is a graph represented as a set of tripples
* of abstract expressions. The graph can be used for instance to store
* semantic information for entities in a GF grammar.
*/
public class SG implements Closeable {
/** Opens a new database file. */
public static native SG openSG(String path) throws SGError;
/** Closes an already opened database. */
public native void close() throws SGError;
/** Reads a triple in the format &lt;expr,expr,expr&gt; and returns it as an array. */
public static native Expr[] readTriple(String s) throws PGFError;
/** Simple triple queries.
* Each of the arguments subj, pred and obj could be null.
* A null argument is interpreted as a wild card.
* If one of the arguments is not null then only triples with matching values
* will be retrieved.
*
* @return An iterator over the matching triples.
*/
public native TripleResult queryTriple(Expr subj, Expr pred, Expr obj) throws SGError;
/** Starts a new transaction. */
public native void beginTrans() throws SGError;
/** Commits the transaction. */
public native void commit() throws SGError;
/** Rollbacks all changes made in the current transaction. */
public native void rollback() throws SGError;
/** Inserts a new triple.
* @return an unique id that identifies this triple in the database
*/
public native long insertTriple(Expr subj, Expr pred, Expr obj) throws SGError;
//////////////////////////////////////////////////////////////////
// private stuff
private long ref;
private SG(long ref) {
this.ref = ref;
}
static {
System.loadLibrary("jpgf");
}
}

View File

@@ -1,11 +0,0 @@
package org.grammaticalframework.sg;
/** This exception is thrown if an error occurs in the semantic graph.
*/
public class SGError extends RuntimeException {
private static final long serialVersionUID = -6098784400143861939L;
public SGError(String message) {
super(message);
}
}

View File

@@ -1,53 +0,0 @@
package org.grammaticalframework.sg;
import java.io.Closeable;
import org.grammaticalframework.pgf.Expr;
/** This class is used to iterate over a list of triples.
* To move to the next triple, call {@link TripleResult#hasNext}.
* When you do not need the iterator anymore then call {@link TripleResult#close}
* to release the allocated resources.
*/
public class TripleResult implements Closeable {
public native boolean hasNext();
/** Closes the iterator and releases the allocated resources. */
public native void close();
/** Each triple has an unique integer key. You can get the key for
* the current triple by calling {@link TripleResult#getKey}.
*/
public long getKey() {
return key;
}
/** This is the first element of the current triple. */
public Expr getSubject() {
return subj;
}
/** This is the second element of the current triple. */
public Expr getPredicate() {
return pred;
}
/** This is the third element of the current triple. */
public Expr getObject() {
return obj;
}
//////////////////////////////////////////////////////////////////
// private stuff
private long ref;
private long key;
private Expr subj;
private Expr pred;
private Expr obj;
private TripleResult(long ref, Expr subj, Expr pred, Expr obj) {
this.ref = ref;
this.subj = subj;
this.pred = pred;
this.obj = obj;
}
}