use of org.eclipse.rdf4j.model.Statement in project rdf4j by eclipse.
the class NQuadsParser method parseQuad.
private int parseQuad(int c) throws IOException, RDFParseException, RDFHandlerException {
boolean ignoredAnError = false;
try {
c = parseSubject(c);
c = skipWhitespace(c);
c = parsePredicate(c);
c = skipWhitespace(c);
c = parseObject(c);
c = skipWhitespace(c);
// Context is not required
if (c != '.') {
c = parseContext(c);
c = skipWhitespace(c);
}
if (c == -1) {
throwEOFException();
} else if (c != '.') {
reportFatalError("Expected '.', found: " + new String(Character.toChars(c)));
}
c = assertLineTerminates(c);
} catch (RDFParseException rdfpe) {
if (getParserConfig().isNonFatalError(NTriplesParserSettings.FAIL_ON_NTRIPLES_INVALID_LINES)) {
reportError(rdfpe, NTriplesParserSettings.FAIL_ON_NTRIPLES_INVALID_LINES);
ignoredAnError = true;
} else {
throw rdfpe;
}
}
c = skipLine(c);
if (!ignoredAnError) {
Statement st = createStatement(subject, predicate, object, context);
if (rdfHandler != null) {
rdfHandler.handleStatement(st);
}
}
subject = null;
predicate = null;
object = null;
context = null;
return c;
}
use of org.eclipse.rdf4j.model.Statement in project rdf4j by eclipse.
the class NTriplesParser method parseTriple.
private int parseTriple(int c) throws IOException, RDFParseException, RDFHandlerException {
boolean ignoredAnError = false;
try {
c = parseSubject(c);
c = skipWhitespace(c);
c = parsePredicate(c);
c = skipWhitespace(c);
c = parseObject(c);
c = skipWhitespace(c);
if (c == -1) {
throwEOFException();
} else if (c != '.') {
reportError("Expected '.', found: " + new String(Character.toChars(c)), NTriplesParserSettings.FAIL_ON_NTRIPLES_INVALID_LINES);
}
c = assertLineTerminates(c);
} catch (RDFParseException rdfpe) {
if (getParserConfig().isNonFatalError(NTriplesParserSettings.FAIL_ON_NTRIPLES_INVALID_LINES)) {
reportError(rdfpe, NTriplesParserSettings.FAIL_ON_NTRIPLES_INVALID_LINES);
ignoredAnError = true;
} else {
throw rdfpe;
}
}
c = skipLine(c);
if (!ignoredAnError) {
Statement st = createStatement(subject, predicate, object);
if (rdfHandler != null) {
rdfHandler.handleStatement(st);
}
}
subject = null;
predicate = null;
object = null;
return c;
}
use of org.eclipse.rdf4j.model.Statement in project rdf4j by eclipse.
the class BufferedGroupingRDFHandler method processBuffer.
/*
* not synchronized, assumes calling method has obtained a lock on bufferLock
*/
private void processBuffer() throws RDFHandlerException {
// primary grouping per context.
for (Resource context : contexts) {
Set<Resource> subjects = GraphUtil.getSubjects(bufferedStatements, null, null, context);
for (Resource subject : subjects) {
Set<IRI> processedPredicates = new HashSet<IRI>();
// give rdf:type preference over other predicates.
Iterator<Statement> typeStatements = bufferedStatements.match(subject, RDF.TYPE, null, context);
while (typeStatements.hasNext()) {
Statement typeStatement = typeStatements.next();
super.handleStatement(typeStatement);
}
processedPredicates.add(RDF.TYPE);
// retrieve other statement from this context with the same
// subject, and output them grouped by predicate
Iterator<Statement> subjectStatements = bufferedStatements.match(subject, null, null, context);
while (subjectStatements.hasNext()) {
Statement subjectStatement = subjectStatements.next();
IRI predicate = subjectStatement.getPredicate();
if (!processedPredicates.contains(predicate)) {
Iterator<Statement> toWrite = bufferedStatements.match(subject, predicate, null, context);
while (toWrite.hasNext()) {
Statement toWriteSt = toWrite.next();
super.handleStatement(toWriteSt);
}
processedPredicates.add(predicate);
}
}
}
}
bufferedStatements.clear();
contexts.clear();
}
use of org.eclipse.rdf4j.model.Statement in project opentheso by miledrousset.
the class WriteRdfFileTest method write3.
@Test
public void write3() {
ValueFactory vf = SimpleValueFactory.getInstance();
BNode address = vf.createBNode();
// First we do the same thing we did in example 02: create a new ModelBuilder, and add
// two statements about Picasso.
ModelBuilder builder = new ModelBuilder();
builder.setNamespace("ex", "http://example.org/").subject("ex:Picasso").add(RDF.TYPE, "ex:Artist").add(FOAF.FIRST_NAME, "Pablo").add("ex:homeAddress", // link the blank node
address).subject(// switch the subject
address).add("ex:street", "31 Art Gallery").add("ex:city", "Madrid").add("ex:country", "Spain");
Model model = builder.build();
// To see what's in our model, let's just print it to the screen
for (Statement st : model) {
System.out.println(st);
}
Rio.write(model, System.out, RDFFormat.RDFXML);
}
use of org.eclipse.rdf4j.model.Statement in project opentheso by miledrousset.
the class WriteRdfFileTest method write2.
@Test
public void write2() {
ValueFactory vf = SimpleValueFactory.getInstance();
// Create a new RDF model containing information about the painting "The Potato Eaters"
ModelBuilder builder = new ModelBuilder();
Model model = builder.setNamespace("ex", "http://example.org/").subject("ex:PotatoEaters").add("ex:creationDate", vf.createLiteral("1885-04-01T00:00:00Z", XMLSchema.DATETIME)).add("ex:peopleDepicted", 5).build();
// To see what's in our model, let's just print stuff to the screen
for (Statement st : model) {
// we want to see the object values of each property
IRI property = st.getPredicate();
Value value = st.getObject();
if (value instanceof Literal) {
Literal literal = (Literal) value;
System.out.println("datatype: " + literal.getDatatype());
// get the value of the literal directly as a Java primitive.
if (property.getLocalName().equals("peopleDepicted")) {
int peopleDepicted = literal.intValue();
System.out.println(peopleDepicted + " people are depicted in this painting");
} else if (property.getLocalName().equals("creationDate")) {
XMLGregorianCalendar calendar = literal.calendarValue();
Date date = calendar.toGregorianCalendar().getTime();
System.out.println("The painting was created on " + date);
}
// you can also just get the lexical value (a string) without worrying about the datatype
System.out.println("Lexical value: '" + literal.getLabel() + "'");
}
}
Rio.write(model, System.out, RDFFormat.RDFXML);
}
Aggregations