use of org.eclipse.rdf4j.model.BNode in project rdf4j by eclipse.
the class ModelsTest method testModelsIsomorphic.
@Test
public void testModelsIsomorphic() {
// two identical statements, no bnodes
model1.add(foo, RDF.TYPE, bar);
assertFalse(Models.isomorphic(model1, model2));
model2.add(foo, RDF.TYPE, bar);
assertTrue(Models.isomorphic(model1, model2));
// add same statement again
model2.add(foo, RDF.TYPE, bar);
assertTrue("Duplicate statement should not be considered", Models.isomorphic(model1, model2));
// two identical statements with bnodes added.
model1.add(foo, RDF.TYPE, VF.createBNode());
model2.add(foo, RDF.TYPE, VF.createBNode());
assertTrue(Models.isomorphic(model1, model2));
// chained bnodes
BNode chainedNode1 = VF.createBNode();
model1.add(bar, RDFS.SUBCLASSOF, chainedNode1);
model1.add(chainedNode1, RDFS.SUBCLASSOF, foo);
BNode chainedNode2 = VF.createBNode();
model2.add(bar, RDFS.SUBCLASSOF, chainedNode2);
model2.add(chainedNode2, RDFS.SUBCLASSOF, foo);
assertTrue(Models.isomorphic(model1, model2));
// two bnode statements with non-identical predicates
model1.add(foo, foo, VF.createBNode());
model2.add(foo, bar, VF.createBNode());
assertFalse(Models.isomorphic(model1, model2));
}
use of org.eclipse.rdf4j.model.BNode in project rdf4j by eclipse.
the class SPARQLCSVTupleBackgroundTest method matchBindingSets.
private boolean matchBindingSets(List<? extends BindingSet> queryResult1, Iterable<? extends BindingSet> queryResult2, Map<BNode, BNode> bNodeMapping, int idx) {
boolean result = false;
if (idx < queryResult1.size()) {
BindingSet bs1 = queryResult1.get(idx);
List<BindingSet> matchingBindingSets = findMatchingBindingSets(bs1, queryResult2, bNodeMapping);
for (BindingSet bs2 : matchingBindingSets) {
// Map bNodes in bs1 to bNodes in bs2
Map<BNode, BNode> newBNodeMapping = new HashMap<BNode, BNode>(bNodeMapping);
for (Binding binding : bs1) {
if (binding.getValue() instanceof BNode) {
newBNodeMapping.put((BNode) binding.getValue(), (BNode) bs2.getValue(binding.getName()));
}
}
// FIXME: this recursive implementation has a high risk of
// triggering a stack overflow
// Enter recursion
result = matchBindingSets(queryResult1, queryResult2, newBNodeMapping, idx + 1);
if (result == true) {
// models match, look no further
break;
}
}
} else {
// All statements have been mapped successfully
result = true;
}
return result;
}
use of org.eclipse.rdf4j.model.BNode in project rdf4j by eclipse.
the class SPARQLCSVTupleTest method matchBindingSets.
private boolean matchBindingSets(List<? extends BindingSet> queryResult1, Iterable<? extends BindingSet> queryResult2, Map<BNode, BNode> bNodeMapping, int idx) {
boolean result = false;
if (idx < queryResult1.size()) {
BindingSet bs1 = queryResult1.get(idx);
List<BindingSet> matchingBindingSets = findMatchingBindingSets(bs1, queryResult2, bNodeMapping);
for (BindingSet bs2 : matchingBindingSets) {
// Map bNodes in bs1 to bNodes in bs2
Map<BNode, BNode> newBNodeMapping = new HashMap<BNode, BNode>(bNodeMapping);
for (Binding binding : bs1) {
if (binding.getValue() instanceof BNode) {
newBNodeMapping.put((BNode) binding.getValue(), (BNode) bs2.getValue(binding.getName()));
}
}
// FIXME: this recursive implementation has a high risk of
// triggering a stack overflow
// Enter recursion
result = matchBindingSets(queryResult1, queryResult2, newBNodeMapping, idx + 1);
if (result == true) {
// models match, look no further
break;
}
}
} else {
// All statements have been mapped successfully
result = true;
}
return result;
}
use of org.eclipse.rdf4j.model.BNode in project rdf4j by eclipse.
the class SPARQLJSONTupleBackgroundTest 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 RDFXMLWriter method handleStatement.
public void handleStatement(Statement st) throws RDFHandlerException {
if (!writingStarted) {
throw new RDFHandlerException("Document writing has not yet been started");
}
Resource subj = st.getSubject();
IRI pred = st.getPredicate();
Value obj = st.getObject();
// Verify that an XML namespace-qualified name can be created for the
// predicate
String predString = pred.toString();
int predSplitIdx = XMLUtil.findURISplitIndex(predString);
if (predSplitIdx == -1) {
throw new RDFHandlerException("Unable to create XML namespace-qualified name for predicate: " + predString);
}
String predNamespace = predString.substring(0, predSplitIdx);
String predLocalName = predString.substring(predSplitIdx);
try {
if (!headerWritten) {
writeHeader();
}
// SUBJECT
if (!subj.equals(lastWrittenSubject)) {
flushPendingStatements();
// Write new subject:
writeNewLine();
writeStartOfStartTag(RDF.NAMESPACE, "Description");
if (subj instanceof BNode) {
BNode bNode = (BNode) subj;
writeAttribute(RDF.NAMESPACE, "nodeID", getValidNodeId(bNode));
} else if (baseIRI != null) {
writeAttribute(RDF.NAMESPACE, "about", baseIRI.relativize(subj.stringValue()));
} else {
IRI uri = (IRI) subj;
writeAttribute(RDF.NAMESPACE, "about", uri.toString());
}
writeEndOfStartTag();
writeNewLine();
lastWrittenSubject = subj;
}
// PREDICATE
writeIndent();
writeStartOfStartTag(predNamespace, predLocalName);
// OBJECT
if (obj instanceof Resource) {
Resource objRes = (Resource) obj;
if (objRes instanceof BNode) {
BNode bNode = (BNode) objRes;
writeAttribute(RDF.NAMESPACE, "nodeID", getValidNodeId(bNode));
} else if (baseIRI != null) {
writeAttribute(RDF.NAMESPACE, "resource", baseIRI.relativize(objRes.stringValue()));
} else {
IRI uri = (IRI) objRes;
writeAttribute(RDF.NAMESPACE, "resource", uri.toString());
}
writeEndOfEmptyTag();
} else if (obj instanceof Literal) {
Literal objLit = (Literal) obj;
// datatype attribute
boolean isXMLLiteral = false;
// language attribute
if (Literals.isLanguageLiteral(objLit)) {
writeAttribute("xml:lang", objLit.getLanguage().get());
} else {
IRI datatype = objLit.getDatatype();
// Check if datatype is rdf:XMLLiteral
isXMLLiteral = datatype.equals(RDF.XMLLITERAL);
if (isXMLLiteral) {
writeAttribute(RDF.NAMESPACE, "parseType", "Literal");
} else if (!datatype.equals(XMLSchema.STRING)) {
writeAttribute(RDF.NAMESPACE, "datatype", datatype.toString());
}
}
writeEndOfStartTag();
// label
if (isXMLLiteral) {
// Write XML literal as plain XML
writer.write(objLit.getLabel());
} else {
writeCharacterData(objLit.getLabel());
}
writeEndTag(predNamespace, predLocalName);
}
writeNewLine();
// Don't write </rdf:Description> yet, maybe the next statement
// has the same subject.
} catch (IOException e) {
throw new RDFHandlerException(e);
}
}
Aggregations