Search in sources :

Example 6 with BNode

use of org.eclipse.rdf4j.model.BNode in project rdf4j by eclipse.

the class TriXWriter method writeValue.

/**
 * Writes out the XML-representation for the supplied value.
 */
private void writeValue(Value value) throws IOException, RDFHandlerException {
    if (value instanceof IRI) {
        IRI uri = (IRI) value;
        xmlWriter.textElement(URI_TAG, uri.toString());
    } else if (value instanceof BNode) {
        BNode bNode = (BNode) value;
        xmlWriter.textElement(BNODE_TAG, bNode.getID());
    } else if (value instanceof Literal) {
        Literal literal = (Literal) value;
        IRI datatype = literal.getDatatype();
        if (Literals.isLanguageLiteral(literal)) {
            xmlWriter.setAttribute(LANGUAGE_ATT, literal.getLanguage().get());
            xmlWriter.textElement(PLAIN_LITERAL_TAG, literal.getLabel());
        } else {
            xmlWriter.setAttribute(DATATYPE_ATT, datatype.toString());
            xmlWriter.textElement(TYPED_LITERAL_TAG, literal.getLabel());
        }
    } else {
        throw new RDFHandlerException("Unknown value type: " + value.getClass());
    }
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) BNode(org.eclipse.rdf4j.model.BNode) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) Literal(org.eclipse.rdf4j.model.Literal)

Example 7 with BNode

use of org.eclipse.rdf4j.model.BNode in project rdf4j by eclipse.

the class ModelsTest method testIsSubset.

@Test
public void testIsSubset() {
    // two empty sets
    assertTrue(Models.isSubset(model1, model2));
    assertTrue(Models.isSubset(model2, model1));
    // two identical statements, no bnodes
    model1.add(foo, RDF.TYPE, bar);
    assertFalse(Models.isSubset(model1, model2));
    assertTrue(Models.isSubset(model2, model1));
    model2.add(foo, RDF.TYPE, bar);
    assertTrue(Models.isSubset(model1, model2));
    assertTrue(Models.isSubset(model2, model1));
    // two identical statements with bnodes added.
    model1.add(foo, RDF.TYPE, VF.createBNode());
    assertFalse(Models.isSubset(model1, model2));
    assertTrue(Models.isSubset(model2, model1));
    model2.add(foo, RDF.TYPE, VF.createBNode());
    assertTrue(Models.isSubset(model1, model2));
    assertTrue(Models.isSubset(model2, model1));
    // chained bnodes
    BNode chainedNode1 = VF.createBNode();
    model1.add(bar, RDFS.SUBCLASSOF, chainedNode1);
    model1.add(chainedNode1, RDFS.SUBCLASSOF, foo);
    assertFalse(Models.isSubset(model1, model2));
    assertTrue(Models.isSubset(model2, model1));
    BNode chainedNode2 = VF.createBNode();
    model2.add(bar, RDFS.SUBCLASSOF, chainedNode2);
    model2.add(chainedNode2, RDFS.SUBCLASSOF, foo);
    assertTrue(Models.isSubset(model1, model2));
    assertTrue(Models.isSubset(model2, model1));
    // two bnode statements with non-identical predicates
    model1.add(foo, foo, VF.createBNode());
    model2.add(foo, bar, VF.createBNode());
    assertFalse(Models.isSubset(model1, model2));
    assertFalse(Models.isSubset(model2, model1));
}
Also used : BNode(org.eclipse.rdf4j.model.BNode) Test(org.junit.Test)

Example 8 with BNode

use of org.eclipse.rdf4j.model.BNode in project rdf4j by eclipse.

the class QueryResults method bindingSetsMatch.

private static boolean bindingSetsMatch(BindingSet bs1, BindingSet bs2, Map<BNode, BNode> bNodeMapping) {
    if (bs1.size() != bs2.size()) {
        return false;
    }
    for (Binding binding1 : bs1) {
        Value value1 = binding1.getValue();
        Value value2 = bs2.getValue(binding1.getName());
        if (value1 instanceof BNode && value2 instanceof BNode) {
            BNode mappedBNode = bNodeMapping.get(value1);
            if (mappedBNode != null) {
                // bNode 'value1' was already mapped to some other bNode
                if (!value2.equals(mappedBNode)) {
                    // 'value1' and 'value2' do not match
                    return false;
                }
            } else {
                // possible mapping candidate
                if (bNodeMapping.containsValue(value2)) {
                    // 'value2' is already mapped to some other value.
                    return false;
                }
            }
        } else {
            // values are not (both) bNodes
            if (value1 instanceof Literal && value2 instanceof Literal) {
                // do literal value-based comparison for supported datatypes
                Literal leftLit = (Literal) value1;
                Literal rightLit = (Literal) value2;
                IRI dt1 = leftLit.getDatatype();
                IRI dt2 = rightLit.getDatatype();
                if (dt1 != null && dt2 != null && dt1.equals(dt2) && XMLDatatypeUtil.isValidValue(leftLit.getLabel(), dt1) && XMLDatatypeUtil.isValidValue(rightLit.getLabel(), dt2)) {
                    Integer compareResult = null;
                    if (dt1.equals(XMLSchema.DOUBLE)) {
                        compareResult = Double.compare(leftLit.doubleValue(), rightLit.doubleValue());
                    } else if (dt1.equals(XMLSchema.FLOAT)) {
                        compareResult = Float.compare(leftLit.floatValue(), rightLit.floatValue());
                    } else if (dt1.equals(XMLSchema.DECIMAL)) {
                        compareResult = leftLit.decimalValue().compareTo(rightLit.decimalValue());
                    } else if (XMLDatatypeUtil.isIntegerDatatype(dt1)) {
                        compareResult = leftLit.integerValue().compareTo(rightLit.integerValue());
                    } else if (dt1.equals(XMLSchema.BOOLEAN)) {
                        Boolean leftBool = Boolean.valueOf(leftLit.booleanValue());
                        Boolean rightBool = Boolean.valueOf(rightLit.booleanValue());
                        compareResult = leftBool.compareTo(rightBool);
                    } else if (XMLDatatypeUtil.isCalendarDatatype(dt1)) {
                        XMLGregorianCalendar left = leftLit.calendarValue();
                        XMLGregorianCalendar right = rightLit.calendarValue();
                        compareResult = left.compare(right);
                    }
                    if (compareResult != null) {
                        if (compareResult.intValue() != 0) {
                            return false;
                        }
                    } else if (!value1.equals(value2)) {
                        return false;
                    }
                } else if (!value1.equals(value2)) {
                    return false;
                }
            } else if (!value1.equals(value2)) {
                return false;
            }
        }
    }
    return true;
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) BNode(org.eclipse.rdf4j.model.BNode) Literal(org.eclipse.rdf4j.model.Literal) Value(org.eclipse.rdf4j.model.Value)

Example 9 with BNode

use of org.eclipse.rdf4j.model.BNode in project rdf4j by eclipse.

the class DAWGTestResultSetWriter method handleSolution.

@Override
public void handleSolution(BindingSet bindingSet) throws TupleQueryResultHandlerException {
    try {
        BNode solutionNode = vf.createBNode();
        reportStatement(resultSetNode, SOLUTION, solutionNode);
        for (Binding binding : bindingSet) {
            BNode bindingNode = vf.createBNode();
            reportStatement(solutionNode, BINDING, bindingNode);
            reportStatement(bindingNode, VARIABLE, vf.createLiteral(binding.getName()));
            Value value = binding.getValue();
            // generated for the result format
            if (value instanceof BNode) {
                BNode mappedBNode = bnodeMap.get(value);
                if (mappedBNode == null) {
                    mappedBNode = vf.createBNode();
                    bnodeMap.put((BNode) value, mappedBNode);
                }
                value = mappedBNode;
            }
            reportStatement(bindingNode, VALUE, value);
        }
    } catch (RDFHandlerException e) {
        throw new TupleQueryResultHandlerException(e);
    }
}
Also used : Binding(org.eclipse.rdf4j.query.Binding) BNode(org.eclipse.rdf4j.model.BNode) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) Value(org.eclipse.rdf4j.model.Value)

Example 10 with BNode

use of org.eclipse.rdf4j.model.BNode in project rdf4j by eclipse.

the class RepositoryUtil method difference.

/**
 * Compares two models, defined by two statement collections, and returns the difference between the first
 * and the second model (that is, all statements that are present in model1 but not in model2). Blank node
 * IDs are not relevant for model equality, they are mapped from one model to the other by using the
 * attached properties. *
 * <p>
 * <b>NOTE: this algorithm is currently broken; it doesn't actually map blank nodes between the two
 * models.</b>
 *
 * @return The collection of statements that is the difference between model1 and model2.
 */
public static Collection<? extends Statement> difference(Collection<? extends Statement> model1, Collection<? extends Statement> model2) {
    // Create working copies
    LinkedList<Statement> copy1 = new LinkedList<Statement>(model1);
    LinkedList<Statement> copy2 = new LinkedList<Statement>(model2);
    Collection<Statement> result = new ArrayList<Statement>();
    // Compare statements that don't contain bNodes
    Iterator<Statement> iter1 = copy1.iterator();
    while (iter1.hasNext()) {
        Statement st = iter1.next();
        if (st.getSubject() instanceof BNode || st.getObject() instanceof BNode) {
            // these statements are handled later
            continue;
        }
        // Try to remove the statement from model2
        boolean removed = copy2.remove(st);
        if (!removed) {
            // statement was not present in model2 and is part of the difference
            result.add(st);
        }
        iter1.remove();
    }
    // FIXME: this algorithm is broken: bNodeMapping is assumed to contain a
    // bnode mapping while in reallity it is an empty map
    HashMap<BNode, BNode> bNodeMapping = new HashMap<BNode, BNode>();
    for (Statement st1 : copy1) {
        boolean foundMatch = false;
        for (Statement st2 : copy2) {
            if (statementsMatch(st1, st2, bNodeMapping)) {
                // Found a matching statement
                foundMatch = true;
                break;
            }
        }
        if (!foundMatch) {
            // No statement matching st1 was found in model2, st1 is part of
            // the difference.
            result.add(st1);
        }
    }
    return result;
}
Also used : BNode(org.eclipse.rdf4j.model.BNode) HashMap(java.util.HashMap) Statement(org.eclipse.rdf4j.model.Statement) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList)

Aggregations

BNode (org.eclipse.rdf4j.model.BNode)40 IRI (org.eclipse.rdf4j.model.IRI)16 Literal (org.eclipse.rdf4j.model.Literal)14 Test (org.junit.Test)12 Resource (org.eclipse.rdf4j.model.Resource)10 Model (org.eclipse.rdf4j.model.Model)9 Value (org.eclipse.rdf4j.model.Value)7 LinkedHashModel (org.eclipse.rdf4j.model.impl.LinkedHashModel)7 StringReader (java.io.StringReader)6 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)6 Statement (org.eclipse.rdf4j.model.Statement)5 Binding (org.eclipse.rdf4j.query.Binding)4 BindingSet (org.eclipse.rdf4j.query.BindingSet)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 ParsedIRI (org.eclipse.rdf4j.common.net.ParsedIRI)3 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)3 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)3 InputStream (java.io.InputStream)2 ModelBuilder (org.eclipse.rdf4j.model.util.ModelBuilder)2