package com.franz.ag.examples;
import com.franz.ag.*;
import org.openrdf.model.Literal;
import org.openrdf.model.URI;
/**
* This is a general utility class.
*
* Demonstrates some basics of using Cursors and extracting results.
*
*/
public class AGUtils {
public static void showTriple(Triple tr) throws AllegroGraphException {
System.out.println("<Triple " + tr.queryAGId() +
": " + tr.getSubjectLabel() +
" " + tr.getPredicateLabel() +
" " + tr.getObjectLabel() +
" " + tr.getContextLabel() + ">");
}
public static void showTriples(String msg, Cursor cc) throws AllegroGraphException {
System.out.println(msg);
while (cc.step()) {
System.out.println(toStringWithFullURIs(cc.getTriple()));
}
}
public static void showTriples(Cursor cc) throws AllegroGraphException {
while (cc.step()) {
System.out.println(toStringWithFullURIs(cc.getTriple()));
}
}
public static void showTriplesWithLimit(Cursor cc, int limit) throws AllegroGraphException {
int i=0;
while (cc.step() && i<limit) {
System.out.println(i + ": " + toStringWithFullURIs(cc.getTriple()));
i++;
}
}
public static void showTriplesWithUPIs(Cursor cc) throws AllegroGraphException {
while (cc.step()) {
System.out.println(toStringWithUPIs(cc.getTriple()));
}
}
public static void showTriples(Cursor cc, AllegroGraph ts) throws AllegroGraphException {
while (cc.step()) {
String[]strings = ts.getParts(cc.getS());
System.out.println(strings[1]);
strings = ts.getParts(cc.getP());
System.out.println(strings[1]);
strings = ts.getParts(cc.getO());
System.out.println(strings[1]);
System.out.println("---");
}
}
public static void showTriplesWithParts(Cursor cc, AllegroGraph ts) throws AllegroGraphException {
while (cc.step()) {
Triple tr = cc.getTriple();
System.out.println("Triple " + tr.queryAGId());
String[]strings = ts.getParts(cc.getS());
printStringArray(" Subject: ", strings);
strings = ts.getParts(cc.getP());
printStringArray(" Predicate: ", strings);
strings = ts.getParts(cc.getO());
printStringArray(" Object: ", strings);
}
}
public static String toStringWithFullURIs(Triple tr) {
return tr.toString();
}
public static String toStringWithUPIs(Triple tr) {
return "<Triple: " + tr.queryAGId() + ": " + tr.getS() + " " + tr.getP() + " " + tr.getO()+ ">";
}
public static void showTriples2(Cursor cc) throws AllegroGraphException {
while (cc.step()) {
System.out.println(" <\"" + cc.getSubjectLabel() + "\" \""
+ cc.getPredicateLabel() + "\" \"" + cc.getObjectLabel()
+ "\">");
}
}
public static void showTriplesWithGraph(Cursor cc) throws AllegroGraphException {
String tr;
while (cc.step()) {
tr = "<Triple " + cc.getTriple().queryAGId() + ": ";
tr += cc.getSubjectLabel() + "," + cc.getPredicateLabel() + "," + cc.getObjectLabel();
tr += "," + cc.getContextLabel() + ">";
System.out.println(tr);
}
}
public static void showURI (URI r) {
System.out.println(r.getURI() + ":");
System.out.println(" Namespace: " + r.getNamespace());
System.out.println(" LocalName: " + r.getLocalName());
}
public static void showLiteral (Literal lit) {
System.out.println("Literal: " + lit.toString());
System.out.println(" Label: " + lit.getLabel());
System.out.println(" Datatype: " + lit.getDatatype());
System.out.println(" Language: " + lit.getLanguage());
// Note that only Literals added to the store have a UPI
System.out.println(" UPI: " + ((ValueNode)lit).queryAGId());
}
public static void showEncodedLiteral (EncodedLiteral lit) {
System.out.println("EncodedLiteral: " + lit.toString());
System.out.println(" Label: " + lit.getLabel());
System.out.println(" Datatype: " + lit.getDatatype());
System.out.println(" Language: " + lit.getLanguage());
// Note that only Literals added to the store have a UPI
System.out.println(" UPI: " + ((ValueNode)lit).queryAGId());
}
public static void printObjectArray(String msg, Object[] objArr) {
System.out.println(msg);
if (objArr != null) {
for (int i=0; i<objArr.length;i++) {
System.out.println(" "+i+": "+objArr[i]);
}
}
}
public static void printUPIArray(String msg, AllegroGraph ts, UPI[] objArr) throws AllegroGraphException {
System.out.println(msg);
if (objArr != null) {
for (int i=0; i<objArr.length;i++) {
String[] parts = ts.getParts(objArr[i]);
System.out.println(" "+i+": "+parts[1]);
}
}
}
public static String upiArrayToString(AllegroGraph ts, UPI[] objArr) throws AllegroGraphException {
StringBuffer buf = new StringBuffer();
if (objArr != null) {
buf.append("{");
for (int i=0; i<objArr.length;i++) {
String[] parts = ts.getParts(objArr[i]);
buf.append(parts[1]).append(" ");
}
buf.append("}");
}
return buf.toString();
}
public static void printStringArray(String msg, String[] objArr) {
System.out.println(msg);
if (objArr != null) {
for (int i=0; i<objArr.length;i++) {
System.out.println(" "+i+": "+objArr[i]);
}
}
}
public static void loadNTriplesWithTiming(AllegroGraph ts, String ntriplesFile) throws AllegroGraphException {
loadNTriplesWithTiming(ts, ntriplesFile, "");
}
public static void loadNTriplesWithTiming(AllegroGraph ts, String ntriplesFile, String graph) throws AllegroGraphException {
System.out.println("Loading N-Triples from " + ntriplesFile);
long start = System.currentTimeMillis();
long n = ts.loadNTriples(ntriplesFile,graph,false,null,null);
System.out.println("Loaded " + n + " triples in " + elapsedTime(start));
}
public static void loadRDFWithTiming(AllegroGraph ts, String rdfFile) throws AllegroGraphException {
System.out.println("Loading RDF from " + rdfFile);
long start = System.currentTimeMillis();
long n = ts.loadRDF(rdfFile);
System.out.println("Done loading " + n + " triples in " + elapsedTime(start));
}
public static void indexAllTriplesWithTiming(AllegroGraph ts) throws AllegroGraphException {
System.out.print("Indexing...");
long start = System.currentTimeMillis();
ts.indexAllTriples(true);
System.out.println("done in " + elapsedTime(start));
}
public static void doSparqlSelect(AllegroGraph ts,String twinqlQuery)
throws AllegroGraphException {
doSparqlSelect(false,ts,twinqlQuery);
}
public static void doSparqlSelect(boolean infer, AllegroGraph ts,
String query)
throws AllegroGraphException {
if (infer) {
System.out.println("\nQuery (with RDFS++ inference):");
} else {
System.out.println("\nQuery:");
}
System.out.println(" " + query);
SPARQLQuery sq = new SPARQLQuery();
sq.setIncludeInferred(infer);
ValueObject[][] r = sq.select(ts, query);
String[] var = sq.getResultNames();
System.out.println("Number of solutions: " + r.length);
for (int i = 0; i < r.length; i++) {
ValueObject[] objects = r[i];
System.out.println("Solution " + (i+1) + ":");
for (int j = 0; j < objects.length; j++) {
ValueObject term = objects[j];
System.out.println(" " + var[j] + " = " + printValueObject(term));
}
}
}
public static String printValueObject(ValueObject o) {
String result;
if (o == null) {
result = "Null";
} else if (o instanceof Literal) {
Literal l = (Literal)o;
result = l.getLabel();
} else if (o instanceof BlankNode) {
BlankNode b = (BlankNode)o;
result = b.getID();
} else if (o instanceof Node) {
Node n = (Node)o;
result = n.toString();
} else {
result = o.toString();
}
return result;
}
public static void doPrologSelect(AllegroGraph ts, String pquery) throws AllegroGraphException {
doPrologQuery(ts,pquery,true,false);
}
public static void doPrologSelectDistinct(AllegroGraph ts, String pquery) throws AllegroGraphException {
doPrologQuery(ts,pquery,true,true);
}
public static void doPrologQuery(AllegroGraph ts, String pquery) throws AllegroGraphException {
doPrologQuery(ts,pquery,true,false);
}
public static void doPrologQuery(AllegroGraph ts, String pquery, Boolean inferred, Boolean distinct) throws AllegroGraphException {
System.out.println("Results for " + pquery);
ts.evalInServer("(enable-!-reader)");
ValueObject[][] v = ts.selectValues(inferred,distinct,pquery, new Object[0], "");
displayValues(v);
while (0 != ts.moreValues(v)) {
v = ts.selectMore(v);
displayValues(v);
}
}
public static void displayValues(ValueObject[][] v) {
for (int i = 0; i < v.length; i++) {
ValueObject[] objects = v[i];
for (int j = 0; j < objects.length; j++) {
System.out.println("[" + i + "][" + j + "]: " + objects[j]);
}
}
}
public static void addPrologRule(String string, AllegroGraph ts) throws AllegroGraphException {
// Add rule by calling evalInServer.
// Note that adding rules affects the entire server. I.e.,
// you may overwrite a rule or redefine a functor used by another
// AllegroGraph client.
ts.evalInServer("(enable-!-reader)");
ts.evalInServer(string);
}
public static String elapsedTime(long start) {
long total = System.currentTimeMillis() - start;
long min = total/60000;
long msec = total%60000;
double sec = msec/1000.0;
String report;
if (min > 0) {
report = min + ":" + sec + " minutes:seconds";
} else {
report = sec + " seconds";
}
return report;
}
}
Up |