|
|
|
@ -24,6 +24,7 @@ import com.hp.hpl.jena.rdf.model.Model;
|
|
|
|
|
import com.hp.hpl.jena.rdf.model.ModelFactory;
|
|
|
|
|
import com.hp.hpl.jena.rdf.model.Property;
|
|
|
|
|
import com.hp.hpl.jena.rdf.model.RDFNode;
|
|
|
|
|
import com.hp.hpl.jena.rdf.model.ResIterator;
|
|
|
|
|
import com.hp.hpl.jena.rdf.model.Resource;
|
|
|
|
|
import com.hp.hpl.jena.rdf.model.StmtIterator;
|
|
|
|
|
import com.hp.hpl.jena.util.FileManager;
|
|
|
|
@ -91,21 +92,15 @@ public class JenaTripleStore {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void addFile(String rdffile) {
|
|
|
|
|
|
|
|
|
|
Model tmp = ModelFactory.createDefaultModel();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
InputStream in = new ByteArrayInputStream(UTF8.getBytes(rdffile));
|
|
|
|
|
|
|
|
|
|
// read the RDF/XML file
|
|
|
|
|
tmp.read(in, null);
|
|
|
|
|
} finally {
|
|
|
|
|
model = model.union(tmp);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
model = model.union(tmp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void saveFile(String filename) {
|
|
|
|
@ -133,65 +128,25 @@ public class JenaTripleStore {
|
|
|
|
|
init(model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static Resource getResource(String uri, String username) {
|
|
|
|
|
|
|
|
|
|
if (privatestorage != null && privatestorage.containsKey(username)) {
|
|
|
|
|
|
|
|
|
|
return getResource (uri, privatestorage.get(username));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return a Resource instance with the given URI in this model.
|
|
|
|
|
* @param uri
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static Resource getResource(String uri) {
|
|
|
|
|
return getResource(uri, model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Resource getResource(String uri, Model model) {
|
|
|
|
|
return model.getResource(uri);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Resource getProperty(String uri, String username) {
|
|
|
|
|
|
|
|
|
|
if (privatestorage != null && privatestorage.containsKey(username)) {
|
|
|
|
|
|
|
|
|
|
return getProperty (uri, privatestorage.get(username));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return a Property instance in this model.
|
|
|
|
|
* @param uri
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static Property getProperty(String uri) {
|
|
|
|
|
return getProperty(uri, model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Property getProperty(String uri, Model model) {
|
|
|
|
|
return model.getProperty(uri);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void deleteObjects(String subject, String predicate) {
|
|
|
|
|
Resource r = subject == null ? null : getResource(subject);
|
|
|
|
|
Property pr = getProperty(predicate);
|
|
|
|
|
Property pr = model.getProperty(predicate);
|
|
|
|
|
JenaTripleStore.model.removeAll(r, pr, (Resource) null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void addTriple(String subject, String predicate, String object, String username) {
|
|
|
|
|
if (privatestorage != null && privatestorage.containsKey(username)) {
|
|
|
|
|
|
|
|
|
|
addTriple (subject, predicate, object, privatestorage.get(username));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void addTriple(String subject, String predicate, String object) {
|
|
|
|
@ -199,55 +154,46 @@ public class JenaTripleStore {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void addTriple(String subject, String predicate, String object, Model model) {
|
|
|
|
|
Resource r = getResource(subject, model);
|
|
|
|
|
Property pr = getProperty(predicate, model);
|
|
|
|
|
Resource r = model.getResource(subject);
|
|
|
|
|
Property pr = model.getProperty(predicate);
|
|
|
|
|
r.addProperty(pr, object);
|
|
|
|
|
Log.logInfo("TRIPLESTORE", "ADD " + subject + " - " + predicate + " - " + object);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String getObject (final String subject, final String predicate) {
|
|
|
|
|
|
|
|
|
|
public static String getObject(final String subject, final String predicate) {
|
|
|
|
|
Log.logInfo("TRIPLESTORE", "GET " + subject + " - " + predicate + " ... ");
|
|
|
|
|
|
|
|
|
|
Iterator<RDFNode> ni = JenaTripleStore.getObjects (subject, predicate);
|
|
|
|
|
Iterator<RDFNode> ni = JenaTripleStore.getObjects(subject, predicate);
|
|
|
|
|
if (!ni.hasNext()) return "";
|
|
|
|
|
return ni.next().toString();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Iterator<RDFNode> getObjects(final String subject, final String predicate) {
|
|
|
|
|
final Resource r = subject == null ? null : JenaTripleStore.getResource(subject);
|
|
|
|
|
return getObjects(r, predicate);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String getObject (final String subject, final String predicate, final String username) {
|
|
|
|
|
|
|
|
|
|
public static String getPrivateObject(final String subject, final String predicate, final String username) {
|
|
|
|
|
Log.logInfo("TRIPLESTORE", "GET " + subject + " - " + predicate + " ... ("+username+")");
|
|
|
|
|
|
|
|
|
|
Iterator<RDFNode> ni = JenaTripleStore.getObjects (subject, predicate, username);
|
|
|
|
|
Iterator<RDFNode> ni = JenaTripleStore.getPrivateObjects(subject, predicate, username);
|
|
|
|
|
if (!ni.hasNext()) return "";
|
|
|
|
|
return ni.next().toString();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Iterator<RDFNode> getObjects(final String subject, final String predicate, final String username) {
|
|
|
|
|
|
|
|
|
|
final Resource r = JenaTripleStore.getResource(subject, username);
|
|
|
|
|
|
|
|
|
|
private static Iterator<RDFNode> getPrivateObjects(final String subject, final String predicate, final String username) {
|
|
|
|
|
if (privatestorage != null && privatestorage.containsKey(username)) {
|
|
|
|
|
|
|
|
|
|
return getObjects(r, predicate, privatestorage.get(username));
|
|
|
|
|
return getObjects(privatestorage.get(username).getResource(subject), predicate, privatestorage.get(username));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Iterator<RDFNode> getObjects(final String subject, final String predicate) {
|
|
|
|
|
final Resource r = JenaTripleStore.getResource(subject);
|
|
|
|
|
return getObjects(r, predicate);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Iterator<RDFNode> getObjects(final Resource r, final String predicate) {
|
|
|
|
|
return getObjects (r, predicate, model);
|
|
|
|
|
return getObjects(r, predicate, model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Iterator<RDFNode> getObjects(final Resource r, final String predicate, final Model model) {
|
|
|
|
|
final Property pr = JenaTripleStore.getProperty(predicate, model);
|
|
|
|
|
private static Iterator<RDFNode> getObjects(final Resource r, final String predicate, final Model model) {
|
|
|
|
|
final Property pr = model.getProperty(predicate);
|
|
|
|
|
final StmtIterator iter = model.listStatements(r, pr, (Resource) null);
|
|
|
|
|
|
|
|
|
|
return new Iterator<RDFNode>() {
|
|
|
|
@ -265,6 +211,30 @@ public class JenaTripleStore {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Iterator<Resource> getSubjects(final String predicate) {
|
|
|
|
|
return getSubjects(predicate, model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Iterator<Resource> getSubjects(final String predicate, final Model model) {
|
|
|
|
|
final Property pr = model.getProperty(predicate);
|
|
|
|
|
final ResIterator iter = model.listSubjectsWithProperty(pr);
|
|
|
|
|
|
|
|
|
|
return new Iterator<Resource>() {
|
|
|
|
|
@Override
|
|
|
|
|
public boolean hasNext() {
|
|
|
|
|
return iter.hasNext();
|
|
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
public Resource next() {
|
|
|
|
|
return iter.nextResource();
|
|
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
public void remove() {
|
|
|
|
|
iter.remove();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void initPrivateStores() {
|
|
|
|
|
|
|
|
|
|