Example usage for org.apache.commons.rdf.api IRI equals

List of usage examples for org.apache.commons.rdf.api IRI equals

Introduction

In this page you can find the example usage for org.apache.commons.rdf.api IRI equals.

Prototype

@Override
public boolean equals(Object other);

Source Link

Document

Check it this IRI is equal to another IRI.

Usage

From source file:example.IntroToRDF.java

public static void main(String[] args) {
    RDF rdf = new SimpleRDF();

    IRI alice = rdf.createIRI("Alice");
    System.out.println(alice.ntriplesString());

    IRI knows = rdf.createIRI("knows");
    IRI bob = rdf.createIRI("Bob");

    Triple aliceKnowsBob = rdf.createTriple(alice, knows, bob);
    System.out.println(aliceKnowsBob.getSubject().ntriplesString());

    System.out.println(aliceKnowsBob);

    Graph graph = rdf.createGraph();/*from   w  ww . j  a va 2 s.c  o m*/
    graph.add(aliceKnowsBob);

    IRI charlie = rdf.createIRI("Charlie");

    IRI plays = rdf.createIRI("plays");

    IRI football = rdf.createIRI("Football");
    IRI tennis = rdf.createIRI("Tennis");

    graph.add(alice, knows, charlie);
    graph.add(alice, plays, tennis);
    graph.add(bob, knows, charlie);
    graph.add(bob, plays, football);
    graph.add(charlie, plays, tennis);

    System.out.println("Who plays Tennis?");
    for (Triple triple : graph.iterate(null, plays, tennis)) {
        System.out.println(triple.getSubject());
        System.out.println(plays.equals(triple.getPredicate()));
        System.out.println(tennis.equals(triple.getObject()));
    }

    System.out.println("Who does Alice know?");
    for (Triple triple : graph.iterate(alice, knows, null)) {
        System.out.println(triple.getObject());
    }

    System.out.println("Does Alice anyone that plays Football?");
    for (Triple triple : graph.iterate(alice, knows, null)) {
        RDFTerm aliceFriend = triple.getObject();
        if (!(aliceFriend instanceof BlankNodeOrIRI)) {
            continue;
        }
        if (graph.contains((BlankNodeOrIRI) aliceFriend, plays, football)) {
            System.out.println("Yes, it is " + aliceFriend);
        }
    }

    Literal aliceName = rdf.createLiteral("Alice W. Land");
    IRI name = rdf.createIRI("name");
    graph.add(alice, name, aliceName);

    Optional<? extends Triple> nameTriple = graph.stream(alice, name, null).findAny();
    if (nameTriple.isPresent()) {
        System.out.println(nameTriple.get());
    }

    graph.stream(alice, name, null).findAny().map(Triple::getObject).filter(obj -> obj instanceof Literal)
            .map(literalName -> ((Literal) literalName).getLexicalForm()).ifPresent(System.out::println);

    IRI playerRating = rdf.createIRI("playerRating");
    Literal aliceRating = rdf.createLiteral("13.37", Types.XSD_FLOAT);
    graph.add(alice, playerRating, aliceRating);

    Literal footballInEnglish = rdf.createLiteral("football", "en");
    Literal footballInNorwegian = rdf.createLiteral("fotball", "no");
    graph.add(football, name, footballInEnglish);
    graph.add(football, name, footballInNorwegian);

    Literal footballInAmericanEnglish = rdf.createLiteral("soccer", "en-US");
    graph.add(football, name, footballInAmericanEnglish);

    BlankNode someone = rdf.createBlankNode();
    graph.add(charlie, knows, someone);
    graph.add(someone, plays, football);

    BlankNode someoneElse = rdf.createBlankNode();
    graph.add(charlie, knows, someoneElse);

    for (Triple heKnows : graph.iterate(charlie, knows, null)) {
        if (!(heKnows.getObject() instanceof BlankNodeOrIRI)) {
            continue;
        }
        BlankNodeOrIRI who = (BlankNodeOrIRI) heKnows.getObject();
        System.out.println("Charlie knows " + who);
        for (Triple whoPlays : graph.iterate(who, plays, null)) {
            System.out.println("  who plays " + whoPlays.getObject());
        }
    }

    // Delete previous BlankNode statements
    graph.remove(null, null, someone);
    graph.remove(someone, null, null);

    // no Java variable for the new BlankNode instance
    graph.add(charlie, knows, rdf.createBlankNode("someone"));
    // at any point later (with the same RDF instance)
    graph.add(rdf.createBlankNode("someone"), plays, football);

    for (Triple heKnows : graph.iterate(charlie, knows, null)) {
        if (!(heKnows.getObject() instanceof BlankNodeOrIRI)) {
            continue;
        }
        BlankNodeOrIRI who = (BlankNodeOrIRI) heKnows.getObject();
        System.out.println("Charlie knows " + who);
        for (Triple whoPlays : graph.iterate(who, plays, null)) {
            System.out.println("  who plays " + whoPlays.getObject());
        }
    }

}

From source file:example.UserGuideTest.java

@Test
public void ntriples() throws Exception {
    IRI iri = factory.createIRI("http://example.com/alice");
    System.out.println(iri.getIRIString());

    IRI iri2 = factory.createIRI("http://example.com/alice");
    System.out.println(iri.equals(iri2));

    IRI iri3 = factory.createIRI("http://example.com/alice/./");
    System.out.println(iri.equals(iri3));

    System.out.println(iri.equals("http://example.com/alice"));
    System.out.println(iri.equals(factory.createLiteral("http://example.com/alice")));

}

From source file:org.trellisldp.http.impl.BaseTestHandler.java

protected Stream<Executable> checkLdpType(final Response res, final IRI type) {
    final Set<String> types = HttpUtils.ldpResourceTypes(type).map(IRI::getIRIString).collect(toSet());
    final Set<String> responseTypes = res.getLinks().stream().filter(link -> TYPE.equals(link.getRel()))
            .map(link -> link.getUri().toString()).collect(toSet());
    return of(LDP.Resource, LDP.RDFSource, LDP.NonRDFSource, LDP.Container, LDP.BasicContainer,
            LDP.DirectContainer, LDP.IndirectContainer).map(t -> checkLdpType(types, responseTypes, t));
}

From source file:org.trellisldp.http.impl.PostHandler.java

/**
 * Create a new resource/*  w  ww.  java  2s. c  o m*/
 * @return the response builder
 */
public ResponseBuilder createResource() {
    final String baseUrl = getBaseUrl();
    final String identifier = baseUrl + req.getPartition() + req.getPath() + id;
    final String contentType = req.getContentType();
    final Session session = ofNullable(req.getSession()).orElseGet(HttpSession::new);

    LOGGER.info("Creating resource as {}", identifier);

    final Optional<RDFSyntax> rdfSyntax = ofNullable(contentType).flatMap(RDFSyntax::byMediaType)
            .filter(SUPPORTED_RDF_TYPES::contains);

    final IRI defaultType = nonNull(contentType) && !rdfSyntax.isPresent() ? LDP.NonRDFSource : LDP.RDFSource;
    final IRI internalId = rdf.createIRI(TRELLIS_PREFIX + req.getPartition() + req.getPath() + id);

    // Add LDP type (ldp:Resource results in the defaultType)
    final IRI ldpType = ofNullable(req.getLink()).filter(l -> "type".equals(l.getRel())).map(Link::getUri)
            .map(URI::toString).filter(l -> l.startsWith(LDP.URI)).map(rdf::createIRI)
            .filter(l -> !LDP.Resource.equals(l)).orElse(defaultType);

    if (ldpType.equals(LDP.NonRDFSource) && rdfSyntax.isPresent()) {
        return status(BAD_REQUEST).type(TEXT_PLAIN).entity("Cannot save a NonRDFSource with RDF syntax");
    }

    try (final TrellisDataset dataset = TrellisDataset.createDataset()) {

        // Add Audit quads
        audit.ifPresent(svc -> svc.creation(internalId, session).stream()
                .map(skolemizeQuads(resourceService, baseUrl)).forEachOrdered(dataset::add));

        dataset.add(rdf.createQuad(PreferServerManaged, internalId, RDF.type, ldpType));

        // Add user-supplied data
        if (ldpType.equals(LDP.NonRDFSource)) {
            // Check the expected digest value
            final Digest digest = req.getDigest();
            if (nonNull(digest) && !getDigestForEntity(digest).equals(digest.getDigest())) {
                return status(BAD_REQUEST);
            }

            final Map<String, String> metadata = singletonMap(CONTENT_TYPE,
                    ofNullable(contentType).orElse(APPLICATION_OCTET_STREAM));
            final IRI binaryLocation = rdf
                    .createIRI(binaryService.getIdentifierSupplier(req.getPartition()).get());
            dataset.add(rdf.createQuad(PreferServerManaged, internalId, DC.hasPart, binaryLocation));
            dataset.add(rdf.createQuad(PreferServerManaged, binaryLocation, DC.modified,
                    rdf.createLiteral(now().toString(), XSD.dateTime)));
            dataset.add(rdf.createQuad(PreferServerManaged, binaryLocation, DC.format,
                    rdf.createLiteral(ofNullable(contentType).orElse(APPLICATION_OCTET_STREAM))));
            dataset.add(rdf.createQuad(PreferServerManaged, binaryLocation, DC.extent,
                    rdf.createLiteral(Long.toString(entity.length()), XSD.long_)));

            // Persist the content
            persistContent(binaryLocation, metadata);
        } else {
            readEntityIntoDataset(identifier, baseUrl, PreferUserManaged, rdfSyntax.orElse(TURTLE), dataset);

            // Check for any constraints
            checkConstraint(dataset, PreferUserManaged, ldpType, TRELLIS_PREFIX + req.getPartition(),
                    rdfSyntax.orElse(TURTLE));
        }

        if (resourceService.put(internalId, dataset.asDataset())) {
            final ResponseBuilder builder = status(CREATED).location(create(identifier));

            // Add LDP types
            ldpResourceTypes(ldpType).map(IRI::getIRIString).forEach(type -> builder.link(type, "type"));

            return builder;
        }
    }

    LOGGER.error("Unable to persist data to location at {}", internalId.getIRIString());
    return serverError().type(TEXT_PLAIN)
            .entity("Unable to persist data. Please consult the logs for more information");
}