Search in sources :

Example 21 with Statement

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

the class Models method findMatchingStatements.

private static List<Statement> findMatchingStatements(Statement st, Model model, Map<Resource, Resource> bNodeMapping) {
    Resource s = isBlank(st.getSubject()) ? null : st.getSubject();
    IRI p = st.getPredicate();
    Value o = isBlank(st.getObject()) ? null : st.getObject();
    Resource[] g = isBlank(st.getContext()) ? new Resource[0] : new Resource[] { st.getContext() };
    List<Statement> result = new ArrayList<Statement>();
    for (Statement modelSt : model.filter(s, p, o, g)) {
        if (statementsMatch(st, modelSt, bNodeMapping)) {
            // All components possibly match
            result.add(modelSt);
        }
    }
    return result;
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) Statement(org.eclipse.rdf4j.model.Statement) Resource(org.eclipse.rdf4j.model.Resource) Value(org.eclipse.rdf4j.model.Value) ArrayList(java.util.ArrayList)

Example 22 with Statement

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

the class Models method matchModels.

/**
 * A recursive method for finding a complete mapping between blank nodes in model1 and blank nodes in
 * model2. The algorithm does a depth-first search trying to establish a mapping for each blank node
 * occurring in model1.
 *
 * @param model1
 * @param model2
 * @param bNodeMapping
 * @param idx
 * @return true if a complete mapping has been found, false otherwise.
 */
private static boolean matchModels(List<? extends Statement> model1, Model model2, Map<Resource, Resource> bNodeMapping, int idx) {
    boolean result = false;
    if (idx < model1.size()) {
        Statement st1 = model1.get(idx);
        List<Statement> matchingStats = findMatchingStatements(st1, model2, bNodeMapping);
        for (Statement st2 : matchingStats) {
            // Map bNodes in st1 to bNodes in st2
            Map<Resource, Resource> newBNodeMapping = new HashMap<Resource, Resource>(bNodeMapping);
            if (isBlank(st1.getSubject()) && isBlank(st2.getSubject())) {
                newBNodeMapping.put(st1.getSubject(), st2.getSubject());
            }
            if (isBlank(st1.getObject()) && isBlank(st2.getObject())) {
                newBNodeMapping.put((Resource) st1.getObject(), (Resource) st2.getObject());
            }
            if (isBlank(st1.getContext()) && isBlank(st2.getContext())) {
                newBNodeMapping.put(st1.getContext(), st2.getContext());
            }
            // FIXME: this recursive implementation has a high risk of
            // triggering a stack overflow
            // Enter recursion
            result = matchModels(model1, model2, newBNodeMapping, idx + 1);
            if (result == true) {
                // models match, look no further
                break;
            }
        }
    } else {
        // All statements have been mapped successfully
        result = true;
    }
    return result;
}
Also used : HashMap(java.util.HashMap) Statement(org.eclipse.rdf4j.model.Statement) Resource(org.eclipse.rdf4j.model.Resource)

Example 23 with Statement

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

the class RDFCollections method extract.

/**
 * Extracts an RDF Collection starting with the supplied list head from the statement supplier and sends
 * all statements that make up the collection to the supplied {@link Consumer} function. This method
 * expects the RDF Collection to be well-formed. If the collection is not well-formed the method may
 * report only part of the collection, or may throw an exception.
 *
 * @param statementSupplier
 *        the source of the statements from which the RDF collection is to be read, specified as a
 *        functional interface.
 * @param head
 *        the {@link Resource} that represents the list head, that is the start resource of the RDF
 *        Collection to be read. May not be {@code null}.
 * @param collectionConsumer
 *        the Java {@link Consumer} function to which the collection statements are reported.
 * @param exceptionSupplier
 *        a functional interface that produces the exception type this method will throw when an error
 *        occurs.
 * @param contexts
 *        the context(s) from which to read the RDF Collection. This argument is an optional vararg and
 *        can be left out.
 * @throws E
 *         if a problem occurs reading the RDF Collection, for example if it is not well-formed.
 */
public static <E extends RDF4JException> void extract(GetStatementOptional statementSupplier, Resource head, Consumer<Statement> collectionConsumer, Function<String, Supplier<E>> exceptionSupplier, Resource... contexts) throws E {
    OpenRDFUtil.verifyContextNotNull(contexts);
    Objects.requireNonNull(head, "list head may not be null");
    Objects.requireNonNull(collectionConsumer, "collection consumer may not be null");
    Resource current = head;
    final Set<Resource> visited = new HashSet<>();
    while (!RDF.NIL.equals(current)) {
        if (visited.contains(current)) {
            throw exceptionSupplier.apply("list not well-formed: cycle detected").get();
        }
        statementSupplier.get(current, RDF.TYPE, RDF.LIST, contexts).ifPresent(collectionConsumer);
        collectionConsumer.accept(statementSupplier.get(current, RDF.FIRST, null, contexts).orElseThrow(exceptionSupplier.apply("list not wellformed: rdf:first statement missing.")));
        Statement next = statementSupplier.get(current, RDF.REST, null, contexts).orElseThrow(exceptionSupplier.apply("list not well-formed: rdf:rest statement missing."));
        collectionConsumer.accept(next);
        if (!(next.getObject() instanceof Resource)) {
            throw exceptionSupplier.apply("list not well-formed: value of rdf:rest should be one of (IRI, BNode).").get();
        }
        visited.add(current);
        current = (Resource) next.getObject();
    }
}
Also used : Statement(org.eclipse.rdf4j.model.Statement) Resource(org.eclipse.rdf4j.model.Resource) HashSet(java.util.HashSet)

Example 24 with Statement

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

the class RDFXMLHandlingTest method writeRDFXML.

/**
 * Helper method to write the given model to RDFXML and return an InputStream containing the results.
 *
 * @param statements
 * @return An {@link InputStream} containing the results.
 * @throws RDFHandlerException
 */
private InputStream writeRDFXML(Model statements) throws RDFHandlerException {
    StringWriter writer = new StringWriter();
    RDFWriter rdfxmlWriter = new RDFXMLWriter(writer);
    rdfxmlWriter.startRDF();
    for (Statement nextStatement : statements) {
        rdfxmlWriter.handleStatement(nextStatement);
    }
    rdfxmlWriter.endRDF();
    return new ByteArrayInputStream(writer.toString().getBytes(Charset.forName("UTF-8")));
}
Also used : StringWriter(java.io.StringWriter) RDFXMLWriter(org.eclipse.rdf4j.rio.rdfxml.RDFXMLWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) Statement(org.eclipse.rdf4j.model.Statement) RDFWriter(org.eclipse.rdf4j.rio.RDFWriter)

Example 25 with Statement

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

the class TreeModel method pollFirst.

public Statement pollFirst() {
    try {
        Statement first = trees.get(0).tree.first();
        remove(first);
        return first;
    } catch (NoSuchElementException e) {
        return null;
    }
}
Also used : Statement(org.eclipse.rdf4j.model.Statement) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

Statement (org.eclipse.rdf4j.model.Statement)74 IRI (org.eclipse.rdf4j.model.IRI)17 Test (org.junit.Test)17 Model (org.eclipse.rdf4j.model.Model)16 Literal (org.eclipse.rdf4j.model.Literal)15 Resource (org.eclipse.rdf4j.model.Resource)15 Value (org.eclipse.rdf4j.model.Value)13 RDFWriter (org.eclipse.rdf4j.rio.RDFWriter)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ArrayList (java.util.ArrayList)10 LinkedHashModel (org.eclipse.rdf4j.model.impl.LinkedHashModel)8 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)8 RDFParseException (org.eclipse.rdf4j.rio.RDFParseException)8 StringWriter (java.io.StringWriter)7 BNode (org.eclipse.rdf4j.model.BNode)6 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)6 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)6 RepositoryConnection (org.eclipse.rdf4j.repository.RepositoryConnection)6 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)6 IOException (java.io.IOException)5