use of org.openrdf.model.Resource in project blueprints by tinkerpop.
the class SailTest method testRemoveStatements.
@Test
public void testRemoveStatements() throws Exception {
SailConnection sc = sail.getConnection();
try {
sc.begin();
URI uriA = sail.getValueFactory().createURI("http://example.org/uriA");
URI uriB = sail.getValueFactory().createURI("http://example.org/uriB");
URI uriC = sail.getValueFactory().createURI("http://example.org/uriC");
Resource[] contexts = { uriA, null };
int count;
// Remove from all contexts.
sc.removeStatements(uriA, null, null);
sc.commit();
sc.begin();
count = countStatements(sc, uriA, null, null, false);
assertEquals(0, count);
sc.addStatement(uriA, uriB, uriC, contexts);
sc.commit();
sc.begin();
count = countStatements(sc, uriA, null, null, false);
assertEquals(2, count);
sc.removeStatements(uriA, null, null);
sc.commit();
sc.begin();
count = countStatements(sc, uriA, null, null, false);
assertEquals(0, count);
// Remove from one partition context.
sc.removeStatements(uriA, null, null);
sc.commit();
sc.begin();
count = countStatements(sc, uriA, null, null, false);
assertEquals(0, count);
sc.addStatement(uriA, uriB, uriC, contexts);
sc.commit();
sc.begin();
count = countStatements(sc, uriA, null, null, false);
assertEquals(2, count);
Resource[] oneContext = { uriA };
sc.removeStatements(uriA, null, null, oneContext);
sc.commit();
sc.begin();
count = countStatements(sc, uriA, null, null, false);
assertEquals(1, count);
// Remove from the null context.
sc.removeStatements(uriA, null, null);
sc.commit();
sc.begin();
count = countStatements(sc, uriA, null, null, false);
assertEquals(0, count);
sc.addStatement(uriA, uriB, uriC, contexts);
sc.commit();
sc.begin();
count = countStatements(sc, uriA, null, null, false);
assertEquals(2, count);
Resource[] nullContext = { null };
sc.removeStatements(uriA, null, null, nullContext);
sc.commit();
sc.begin();
count = countStatements(sc, uriA, null, null, false);
assertEquals(1, count);
// Remove from more than one context.
sc.removeStatements(uriA, null, null);
sc.commit();
sc.begin();
count = countStatements(sc, uriA, null, null, false);
assertEquals(0, count);
sc.addStatement(uriA, uriB, uriC, contexts);
sc.commit();
sc.begin();
count = countStatements(sc, uriA, null, null, false);
assertEquals(2, count);
sc.removeStatements(uriA, null, null);
sc.commit();
sc.begin();
count = countStatements(sc, uriA, null, null, false, contexts);
assertEquals(0, count);
} finally {
sc.rollback();
sc.close();
}
}
use of org.openrdf.model.Resource in project blueprints by tinkerpop.
the class SailVertex method setProperty.
public void setProperty(final String key, final Object value) {
if (this.rawVertex instanceof Resource) {
throw new RuntimeException(URI_BLANK_NODE_PROPERTIES);
} else {
boolean update = false;
final Literal oldLiteral = (Literal) this.rawVertex;
if (key.equals(SailTokens.DATATYPE)) {
this.rawVertex = new LiteralImpl(oldLiteral.getLabel(), new URIImpl(this.graph.expandPrefix(value.toString())));
update = true;
} else if (key.equals(SailTokens.LANGUAGE)) {
this.rawVertex = new LiteralImpl(oldLiteral.getLabel(), value.toString());
update = true;
}
if (update) {
this.updateLiteral(oldLiteral, (Literal) this.rawVertex);
}
}
}
use of org.openrdf.model.Resource in project blueprints by tinkerpop.
the class SailVertex method removeProperty.
public <T> T removeProperty(final String key) {
if (this.rawVertex instanceof Resource) {
throw new RuntimeException(URI_BLANK_NODE_PROPERTIES);
} else {
final Literal oldLiteral = (Literal) this.rawVertex;
if (key.equals(SailTokens.DATATYPE) || key.equals(SailTokens.LANGUAGE)) {
this.rawVertex = new LiteralImpl(oldLiteral.getLabel());
this.updateLiteral(oldLiteral, (Literal) this.rawVertex);
}
if (key.equals(SailTokens.DATATYPE)) {
return (T) oldLiteral.getDatatype().toString();
} else if (key.equals(SailTokens.LANGUAGE)) {
return (T) oldLiteral.getLanguage();
}
}
return null;
}
use of org.openrdf.model.Resource in project backstage by zepheira.
the class Database method exportRDFa.
public String exportRDFa(int limit, String title) {
try {
RepositoryConnection con = getRepository().getConnection();
RepositoryResult<Statement> result = con.getStatements(null, null, null, false);
String out = "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><style>span {padding:4px}</style><title>" + title + "</title></head><body>";
int itemCount = 0;
Resource prevS = null;
URI prevP = null;
Value prevO = null;
while (result.hasNext() && itemCount <= limit) {
Statement st = result.next();
String currLabel = null;
Resource s = st.getSubject();
String sStr = s.toString();
// strip "http://127.0.0.1/" prefix from subj/pred
if (sStr.startsWith("http://127.0.0.1/")) {
sStr = sStr.substring(17);
}
URI p = st.getPredicate();
String pStr = p.toString();
if (pStr.startsWith("http://127.0.0.1/")) {
pStr = pStr.substring(17);
}
Value o = st.getObject();
String oStr = o.stringValue();
// Determine if our object and subjects are URI
boolean objectIsURI = false;
try {
oStr = (new java.net.URL(oStr).toString());
objectIsURI = true;
} catch (Exception e) {
objectIsURI = false;
}
boolean subjectIsURI = false;
try {
sStr = (new java.net.URL(sStr).toString());
subjectIsURI = true;
} catch (Exception e) {
subjectIsURI = false;
}
// FIXME. Use heuristic to determine if property is an image type. See above re fix.
boolean objectIsImage = false;
if (oStr.endsWith("png") || oStr.endsWith("jpg") || oStr.endsWith("gif") || oStr.endsWith("svg") || oStr.endsWith("jpeg")) {
objectIsImage = true;
}
// arrives before we need it.
if (pStr.equals("http://www.w3.org/2000/01/rdf-schema#label")) {
currLabel = oStr;
}
// group by subject, count as an item
if (s != prevS) {
itemCount++;
if (itemCount > limit)
break;
if (subjectIsURI) {
out += "<p about=\"" + sStr + "\"" + ">\n";
} else {
out += "<p about=\"#" + sStr + "\"" + ">\n";
}
}
// skip externally irrelevant triples or those we handle elsewhere
if (!pStr.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#type") && !pStr.equals("http://simile.mit.edu/2006/11/exhibit#id") && !pStr.equals("http://www.w3.org/2000/01/rdf-schema#label")) {
if (objectIsURI) {
// if we don't have a label, use subject
if (currLabel == null) {
currLabel = sStr;
}
if (objectIsImage) {
out += "<img property=\"" + pStr + "\" src=\"" + oStr + "\" alt=\"" + currLabel + "\" />";
} else {
out += "<a property=\"" + pStr + "\" href=\"" + oStr + "\">" + currLabel + "</a>";
}
} else {
out += "<span property=\"" + pStr + "\">" + oStr + "</span>\n";
}
}
prevS = s;
prevP = p;
prevO = o;
}
return out + "</body></html>";
} catch (OpenRDFException e) {
return "<html><head><title=\"Error\"></head><body>" + e.toString() + "</body></html>";
}
}
use of org.openrdf.model.Resource in project stanbol by apache.
the class SesameYard method findReferences.
@Override
public QueryResultList<String> findReferences(FieldQuery parsedQuery) throws YardException, IllegalArgumentException {
if (parsedQuery == null) {
throw new IllegalArgumentException("The parsed query MUST NOT be NULL!");
}
final SparqlFieldQuery query = SparqlFieldQueryFactory.getSparqlFieldQuery(parsedQuery);
RepositoryConnection con = null;
TupleQueryResult results = null;
try {
con = repository.getConnection();
con.begin();
//execute the query
int limit = QueryUtils.getLimit(query, getConfig().getDefaultQueryResultNumber(), getConfig().getMaxQueryResultNumber());
results = executeSparqlFieldQuery(con, query, limit, false);
//parse the results
List<String> ids = limit > 0 ? new ArrayList<String>(limit) : new ArrayList<String>();
while (results.hasNext()) {
BindingSet result = results.next();
Value value = result.getValue(query.getRootVariableName());
if (value instanceof Resource) {
ids.add(value.stringValue());
}
}
con.commit();
return new QueryResultListImpl<String>(query, ids, String.class);
} catch (RepositoryException e) {
throw new YardException("Unable to execute findReferences query", e);
} catch (QueryEvaluationException e) {
throw new YardException("Unable to execute findReferences query", e);
} finally {
if (results != null) {
//close the result if present
try {
results.close();
} catch (QueryEvaluationException ignore) {
/* ignore */
}
}
if (con != null) {
try {
con.close();
} catch (RepositoryException ignore) {
/* ignore */
}
}
}
}
Aggregations