Search in sources :

Example 51 with Resource

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

the class RDFXMLPrettyWriterTest method inSequenceItemsMixedWithOtherElementsAreAbbreviated.

@Test
public void inSequenceItemsMixedWithOtherElementsAreAbbreviated() throws RDFHandlerException, IOException {
    StringWriter writer = new StringWriter();
    RDFWriter rdfWriter = rdfWriterFactory.getWriter(writer);
    rdfWriter.startRDF();
    Resource res = vf.createIRI("http://example.com/#");
    rdfWriter.handleStatement(vf.createStatement(res, RDF.TYPE, RDF.BAG));
    rdfWriter.handleStatement(vf.createStatement(res, vf.createIRI(RDF.NAMESPACE + "_2"), vf.createIRI("http://example.com/#2")));
    rdfWriter.handleStatement(vf.createStatement(res, vf.createIRI(RDF.NAMESPACE + "_1"), vf.createIRI("http://example.com/#1")));
    rdfWriter.handleStatement(vf.createStatement(res, vf.createIRI(RDF.NAMESPACE + "_3"), vf.createIRI("http://example.com/#3")));
    rdfWriter.handleStatement(vf.createStatement(res, vf.createIRI(RDF.NAMESPACE + "_2"), vf.createIRI("http://example.com/#2")));
    rdfWriter.endRDF();
    List<String> rdfLines = rdfOpenTags(writer.toString());
    assertEquals(Arrays.asList("<rdf:RDF", "<rdf:Bag", "<rdf:_2", "<rdf:li", "<rdf:_3", "<rdf:li"), rdfLines);
}
Also used : StringWriter(java.io.StringWriter) Resource(org.eclipse.rdf4j.model.Resource) RDFWriter(org.eclipse.rdf4j.rio.RDFWriter) Test(org.junit.Test)

Example 52 with Resource

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

the class TransactionWriter method serialize.

protected void serialize(Resource[] contexts, XMLWriter xmlWriter) throws IOException {
    if (contexts.length > 0) {
        xmlWriter.startTag(TransactionXMLConstants.CONTEXTS_TAG);
        for (Resource context : contexts) {
            serialize(context, xmlWriter);
        }
        xmlWriter.endTag(TransactionXMLConstants.CONTEXTS_TAG);
    } else {
        xmlWriter.emptyElement(TransactionXMLConstants.CONTEXTS_TAG);
    }
}
Also used : Resource(org.eclipse.rdf4j.model.Resource)

Example 53 with Resource

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

the class TransactionSAXParser method createRemoveStatementsOperation.

private TransactionOperation createRemoveStatementsOperation() throws SAXException {
    if (parsedValues.size() < 3) {
        throw new SAXException("At least three values required for RemoveStatementsOperation, found: " + parsedValues.size());
    }
    try {
        Resource subject = (Resource) parsedValues.get(0);
        IRI predicate = (IRI) parsedValues.get(1);
        Value object = parsedValues.get(2);
        Resource[] contexts = createContexts(3);
        parsedValues.clear();
        return new RemoveStatementsOperation(subject, predicate, object, contexts);
    } catch (ClassCastException e) {
        throw new SAXException("Invalid argument(s) for RemoveStatementsOperation", e);
    }
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) RemoveStatementsOperation(org.eclipse.rdf4j.http.protocol.transaction.operations.RemoveStatementsOperation) Resource(org.eclipse.rdf4j.model.Resource) Value(org.eclipse.rdf4j.model.Value) SAXException(org.xml.sax.SAXException)

Example 54 with Resource

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

the class Models method statementsMatch.

private static boolean statementsMatch(Statement st1, Statement st2, Map<Resource, Resource> bNodeMapping) {
    IRI pred1 = st1.getPredicate();
    IRI pred2 = st2.getPredicate();
    if (!pred1.equals(pred2)) {
        // predicates don't match
        return false;
    }
    Resource subj1 = st1.getSubject();
    Resource subj2 = st2.getSubject();
    if (isBlank(subj1) && isBlank(subj2)) {
        Resource mappedBNode = bNodeMapping.get(subj1);
        if (mappedBNode != null) {
            // bNode 'subj1' was already mapped to some other bNode
            if (!subj2.equals(mappedBNode)) {
                // 'subj1' and 'subj2' do not match
                return false;
            }
        } else {
            // possible mapping candidate
            if (bNodeMapping.containsValue(subj2)) {
                // 'subj2' is already mapped to some other value.
                return false;
            }
        }
    } else {
        // subjects are not (both) bNodes
        if (!subj1.equals(subj2)) {
            return false;
        }
    }
    Value obj1 = st1.getObject();
    Value obj2 = st2.getObject();
    if (isBlank(obj1) && isBlank(obj2)) {
        Resource mappedBNode = bNodeMapping.get(obj1);
        if (mappedBNode != null) {
            // bNode 'obj1' was already mapped to some other bNode
            if (!obj2.equals(mappedBNode)) {
                // 'obj1' and 'obj2' do not match
                return false;
            }
        } else {
            // possible mapping candidate
            if (bNodeMapping.containsValue(obj2)) {
                // 'obj2' is already mapped to some other value.
                return false;
            }
        }
    } else {
        // objects are not (both) bNodes
        if (!obj1.equals(obj2)) {
            return false;
        }
    }
    Resource context1 = st1.getContext();
    Resource context2 = st2.getContext();
    // no match if in different contexts
    if (context1 == null) {
        return context2 == null;
    } else if (context2 == null) {
        return false;
    }
    if (isBlank(context1) && isBlank(context2)) {
        Resource mappedBNode = bNodeMapping.get(context1);
        if (mappedBNode != null) {
            // bNode 'context1' was already mapped to some other bNode
            if (!context2.equals(mappedBNode)) {
                // 'context1' and 'context2' do not match
                return false;
            }
        } else {
            // possible mapping candidate
            if (bNodeMapping.containsValue(context2)) {
                // 'context2' is already mapped to some other value.
                return false;
            }
        }
    } else {
        // contexts are not (both) bNodes
        if (!context1.equals(context1)) {
            return false;
        }
    }
    return true;
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) Resource(org.eclipse.rdf4j.model.Resource) Value(org.eclipse.rdf4j.model.Value)

Example 55 with Resource

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

the class Statements method consume.

/**
 * Creates one or more {@link Statement} objects with the given subject, predicate and object, one for
 * each given context, and sends each created statement to the supplied {@link Consumer}. If no context is
 * supplied, only a single statement (without any assigned context) is created.
 *
 * @param vf
 *        the {@link ValueFactory} to use for creating statements.
 * @param subject
 *        the subject of each statement. May not be null.
 * @param predicate
 *        the predicate of each statement. May not be null.
 * @param object
 *        the object of each statement. May not be null.
 * @param consumer
 *        the {@link Consumer} function for the produced statements.
 * @param contexts
 *        the context(s) for which to produce statements. This argument is an optional vararg: leave it
 *        out completely to produce a single statement without context.
 */
public static void consume(ValueFactory vf, Resource subject, IRI predicate, Value object, Consumer<Statement> consumer, Resource... contexts) {
    OpenRDFUtil.verifyContextNotNull(contexts);
    Objects.requireNonNull(consumer);
    if (contexts.length > 0) {
        for (Resource context : contexts) {
            consumer.accept(vf.createStatement(subject, predicate, object, context));
        }
    } else {
        consumer.accept(vf.createStatement(subject, predicate, object));
    }
}
Also used : Resource(org.eclipse.rdf4j.model.Resource)

Aggregations

Resource (org.eclipse.rdf4j.model.Resource)90 IRI (org.eclipse.rdf4j.model.IRI)37 Value (org.eclipse.rdf4j.model.Value)30 Test (org.junit.Test)16 Statement (org.eclipse.rdf4j.model.Statement)15 Model (org.eclipse.rdf4j.model.Model)12 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)12 BNode (org.eclipse.rdf4j.model.BNode)11 IOException (java.io.IOException)9 Literal (org.eclipse.rdf4j.model.Literal)9 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)7 StringWriter (java.io.StringWriter)6 ParsedIRI (org.eclipse.rdf4j.common.net.ParsedIRI)6 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)6 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)6 TreeModel (org.eclipse.rdf4j.model.impl.TreeModel)6 RepositoryConnection (org.eclipse.rdf4j.repository.RepositoryConnection)6 RDFWriter (org.eclipse.rdf4j.rio.RDFWriter)6 LinkedHashModel (org.eclipse.rdf4j.model.impl.LinkedHashModel)5 ArrayList (java.util.ArrayList)4