use of org.eclipse.rdf4j.model.BNode in project opentheso by miledrousset.
the class ReadRdfFileTest method test3.
public static void test3() {
// To create a blank node for the address, we need a ValueFactory
ValueFactory vf = SimpleValueFactory.getInstance();
BNode address = vf.createBNode();
// Identically to example 03, we create a model with some data
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();
// Instead of simply printing the statements to the screen, we use a Rio writer to
// write the model in RDF/XML syntax:
Rio.write(model, System.out, RDFFormat.RDFXML);
File file = new File("test.rdf");
FileWriter writer = null;
try {
file.createNewFile();
writer = new FileWriter(file);
} catch (IOException ex) {
Logger.getLogger(ReadRdfFileTest.class.getName()).log(Level.SEVERE, null, ex);
}
Rio.write(model, writer, RDFFormat.RDFXML);
}
use of org.eclipse.rdf4j.model.BNode 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.BNode in project rdf4j by eclipse.
the class SPARQLJSONTupleTest method testBindings2.
@Test
public void testBindings2() throws Exception {
SPARQLResultsJSONParser parser = new SPARQLResultsJSONParser(SimpleValueFactory.getInstance());
QueryResultCollector handler = new QueryResultCollector();
parser.setQueryResultHandler(handler);
InputStream stream = this.getClass().getResourceAsStream("/sparqljson/bindings2.srj");
assertNotNull("Could not find test resource", stream);
parser.parseQueryResult(stream);
// there must be 7 variables
assertEquals(7, handler.getBindingNames().size());
// first must be called "x", etc.,
assertEquals("x", handler.getBindingNames().get(0));
assertEquals("hpage", handler.getBindingNames().get(1));
assertEquals("name", handler.getBindingNames().get(2));
assertEquals("mbox", handler.getBindingNames().get(3));
assertEquals("age", handler.getBindingNames().get(4));
assertEquals("blurb", handler.getBindingNames().get(5));
assertEquals("friend", handler.getBindingNames().get(6));
// 2 results
assertEquals(2, handler.getBindingSets().size());
// Results are ordered, so first should be alice
assertEquals("http://work.example.org/alice/", handler.getBindingSets().get(0).getValue("hpage").stringValue());
for (BindingSet b : handler.getBindingSets()) {
assertNotNull(b.getValue("x"));
assertNotNull(b.getValue("hpage"));
assertNotNull(b.getValue("name"));
assertNotNull(b.getValue("mbox"));
assertNotNull(b.getValue("friend"));
assertTrue(b.getValue("x") instanceof BNode);
assertTrue(b.getValue("hpage") instanceof IRI);
assertTrue(b.getValue("name") instanceof Literal);
assertTrue(b.getValue("friend") instanceof BNode);
BNode value = (BNode) b.getValue("x");
if (value.getID().equals("r1")) {
assertNotNull(b.getValue("blurb"));
assertTrue(b.getValue("mbox") instanceof Literal);
assertTrue(b.getValue("blurb") instanceof Literal);
assertEquals("http://work.example.org/alice/", b.getValue("hpage").stringValue());
Literal name = (Literal) b.getValue("name");
assertEquals("Alice", name.stringValue());
assertFalse(name.getLanguage().isPresent());
assertEquals(XMLSchema.STRING, name.getDatatype());
Literal mbox = (Literal) b.getValue("mbox");
assertEquals("", mbox.stringValue());
assertFalse(mbox.getLanguage().isPresent());
assertEquals(XMLSchema.STRING, mbox.getDatatype());
Literal blurb = (Literal) b.getValue("blurb");
assertEquals("<p xmlns=\"http://www.w3.org/1999/xhtml\">My name is <b>alice</b></p>", blurb.stringValue());
assertFalse(blurb.getLanguage().isPresent());
assertEquals(RDF.XMLLITERAL, blurb.getDatatype());
} else if (value.getID().equals("r2")) {
assertNull(b.getValue("blurb"));
assertTrue(b.getValue("mbox") instanceof IRI);
assertEquals("http://work.example.org/bob/", b.getValue("hpage").stringValue());
Literal name = (Literal) b.getValue("name");
assertEquals("Bob", name.stringValue());
assertEquals("en", name.getLanguage().orElse(null));
assertEquals(RDF.LANGSTRING, name.getDatatype());
assertEquals("mailto:bob@work.example.org", b.getValue("mbox").stringValue());
} else {
fail("Found unexpected binding set in result: " + b.toString());
}
}
assertEquals(1, handler.getLinks().size());
assertEquals("http://www.w3.org/TR/2013/REC-sparql11-results-json-20130321/#example", handler.getLinks().get(0));
}
use of org.eclipse.rdf4j.model.BNode in project rdf4j by eclipse.
the class RenderUtils method toSeRQL.
/**
* Return the query string rendering of the {@link Value}
*
* @param theValue
* the value to render
* @return the value rendered in its query string representation
*/
public static String toSeRQL(Value theValue) {
StringBuilder aBuffer = new StringBuilder();
if (theValue instanceof IRI) {
IRI aURI = (IRI) theValue;
aBuffer.append("<").append(aURI.toString()).append(">");
} else if (theValue instanceof BNode) {
aBuffer.append("_:").append(((BNode) theValue).getID());
} else if (theValue instanceof Literal) {
Literal aLit = (Literal) theValue;
aBuffer.append("\"").append(escape(aLit.getLabel())).append("\"");
if (Literals.isLanguageLiteral(aLit)) {
aBuffer.append("@").append(aLit.getLanguage());
} else {
aBuffer.append("^^<").append(aLit.getDatatype().toString()).append(">");
}
}
return aBuffer.toString();
}
use of org.eclipse.rdf4j.model.BNode in project rdf4j by eclipse.
the class ArrangedWriter method queueBlankStatements.
private synchronized Set<Statement> queueBlankStatements(SubjectInContext key) {
Model firstMatch = blanks.filter(key.getSubject(), null, null, key.getContext());
Model matches = firstMatch.isEmpty() ? blankReferences.filter(key.getSubject(), null, null, key.getContext()) : firstMatch;
if (matches.isEmpty()) {
return null;
}
Set<Statement> set = stmtBySubject.get(key);
if (set == null) {
stmtBySubject.put(key, set = new TreeSet<Statement>(comparator));
}
set.addAll(matches);
if (firstMatch.isEmpty()) {
// repeat blank node values
queueSize += matches.size();
} else {
if (repeatBlankNodes && key.getSubject() instanceof BNode && isStillReferenced(key)) {
blankReferences.addAll(matches);
}
blanks.remove(key.getSubject(), null, null, key.getContext());
}
return set;
}
Aggregations