use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.
the class TurtleWriter method handleList.
private void handleList(Statement st) throws IOException {
Resource subj = st.getSubject();
boolean first = RDF.FIRST.equals(st.getPredicate());
boolean rest = RDF.REST.equals(st.getPredicate()) && !RDF.NIL.equals(st.getObject());
boolean nil = RDF.REST.equals(st.getPredicate()) && RDF.NIL.equals(st.getObject());
if (first && REST != lastWrittenPredicate && isHanging()) {
// new collection
writer.write("(");
writer.increaseIndentation();
wrapLine(false);
lastWrittenSubject = subj;
path.addLast(FIRST);
lastWrittenPredicate = FIRST;
writeValue(st.getObject(), inlineBNodes);
} else if (first && REST == lastWrittenPredicate) {
// item in existing collection
lastWrittenSubject = subj;
path.addLast(FIRST);
lastWrittenPredicate = FIRST;
writeValue(st.getObject(), inlineBNodes);
} else {
closeNestedResources(subj);
if (rest && FIRST == lastWrittenPredicate) {
// next item
wrapLine(true);
// RDF.FIRST
path.removeLast();
path.addLast(REST);
lastWrittenPredicate = REST;
writeValue(st.getObject(), inlineBNodes);
} else if (nil && FIRST == lastWrittenPredicate) {
writer.decreaseIndentation();
writer.write(")");
// RDF.FIRST
path.removeLast();
path.addLast(REST);
while (REST == path.peekLast()) {
stack.pollLast();
path.pollLast();
lastWrittenSubject = stack.peekLast();
lastWrittenPredicate = path.peekLast();
}
} else {
handleStatementInternal(st, false, inlineBNodes, inlineBNodes);
}
}
}
use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.
the class TurtleWriter method handleInlineNode.
private void handleInlineNode(Statement st) throws IOException {
Resource subj = st.getSubject();
IRI pred = st.getPredicate();
if (isHanging() && subj.equals(stack.peekLast())) {
// blank subject
lastWrittenSubject = subj;
writer.write("[");
if (prettyPrint && !RDF.TYPE.equals(pred)) {
writer.writeEOL();
} else {
wrapLine(prettyPrint);
}
writer.increaseIndentation();
// Write new predicate
writePredicate(pred);
writer.increaseIndentation();
wrapLine(true);
path.addLast(pred);
lastWrittenPredicate = pred;
writeValue(st.getObject(), inlineBNodes);
} else if (!subj.equals(lastWrittenSubject) && stack.contains(subj)) {
closeNestedResources(subj);
handleStatementInternal(st, false, inlineBNodes, inlineBNodes);
} else {
assert false;
}
}
use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.
the class TurtleWriter method handleStatement.
@Override
public void handleStatement(Statement st) throws RDFHandlerException {
if (!writingStarted) {
throw new RuntimeException("Document writing has not yet been started");
}
try {
Resource subj = st.getSubject();
IRI pred = st.getPredicate();
if (inlineBNodes && (pred.equals(RDF.FIRST) || pred.equals(RDF.REST))) {
handleList(st);
} else if (inlineBNodes && !subj.equals(lastWrittenSubject) && stack.contains(subj)) {
handleInlineNode(st);
} else {
closeHangingResource();
handleStatementInternal(st, false, inlineBNodes, inlineBNodes);
}
} catch (IOException e) {
throw new RDFHandlerException(e);
}
}
use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.
the class TurtleWriter method handleStatementInternal.
/**
* Internal method that differentiates between the pretty-print and streaming writer cases.
*
* @param st
* The next statement to write
* @param endRDFCalled
* True if endRDF has been called before this method is called. This is used to buffer statements
* for pretty-printing before dumping them when all statements have been delivered to us.
* @param canShortenSubjectBNode
* True if, in the current context, we may be able to shorten the subject of this statement iff it
* is an instance of {@link BNode}.
* @param canShortenObjectBNode
* True if, in the current context, we may be able to shorten the object of this statement iff it
* is an instance of {@link BNode}.
*/
protected void handleStatementInternal(Statement st, boolean endRDFCalled, boolean canShortenSubjectBNode, boolean canShortenObjectBNode) {
Resource subj = st.getSubject();
IRI pred = st.getPredicate();
Value obj = st.getObject();
try {
if (subj.equals(lastWrittenSubject)) {
if (pred.equals(lastWrittenPredicate)) {
// Identical subject and predicate
writer.write(",");
wrapLine(prettyPrint);
} else {
// Identical subject, new predicate
writer.write(";");
writer.writeEOL();
// Write new predicate
writer.decreaseIndentation();
writePredicate(pred);
writer.increaseIndentation();
wrapLine(true);
path.removeLast();
path.addLast(pred);
lastWrittenPredicate = pred;
}
} else {
// New subject
closePreviousStatement();
stack.addLast(subj);
// Write new subject:
if (prettyPrint) {
writer.writeEOL();
}
writeResource(subj, canShortenSubjectBNode);
wrapLine(true);
writer.increaseIndentation();
lastWrittenSubject = subj;
// Write new predicate
writePredicate(pred);
wrapLine(true);
path.addLast(pred);
lastWrittenPredicate = pred;
statementClosed = false;
writer.increaseIndentation();
}
writeValue(obj, canShortenObjectBNode);
// Don't close the line just yet. Maybe the next
// statement has the same subject and/or predicate.
} catch (IOException e) {
throw new RDFHandlerException(e);
}
}
use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.
the class QueryBuilderFactory method describe.
/**
* Create a QueryBuilder for creating a describe query
*
* @param theVars
* the variables to be described
* @param theValues
* the specific bound URI values to be described
* @return a describe query builder
*/
public static QueryBuilder<ParsedGraphQuery> describe(String[] theVars, Resource... theValues) {
QueryBuilder<ParsedGraphQuery> aBuilder = new AbstractQueryBuilder<ParsedGraphQuery>(new ParsedDescribeQuery());
aBuilder.reduced();
aBuilder.addProjectionVar("descr_subj", "descr_pred", "descr_obj");
GroupBuilder<?, ?> aGroup = aBuilder.group();
if (theVars != null) {
for (String aVar : theVars) {
Var aVarObj = new Var(aVar);
aVarObj.setAnonymous(true);
aGroup.filter().or(new SameTerm(aVarObj, new Var("descr_subj")), new SameTerm(aVarObj, new Var("descr_obj")));
}
}
if (theValues != null) {
for (Resource aVar : theValues) {
Var aSubjVar = new Var("descr_subj");
aSubjVar.setAnonymous(true);
Var aObjVar = new Var("descr_obj");
aObjVar.setAnonymous(true);
aGroup.filter().or(new SameTerm(new ValueConstant(aVar), aSubjVar), new SameTerm(new ValueConstant(aVar), aObjVar));
}
}
aGroup.atom("descr_subj", "descr_pred", "descr_obj");
return aBuilder;
}
Aggregations