List of usage examples for org.apache.commons.rdf.api Triple getPredicate
IRI getPredicate();
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 www .jav a2 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
public static String tripleAsString(Triple t) { return t.getSubject().ntriplesString() + " " + t.getPredicate().ntriplesString() + " " + t.getObject().ntriplesString() + " ."; }
From source file:example.UserGuideTest.java
@Test public void triple() throws Exception { BlankNodeOrIRI subject = factory.createBlankNode(); IRI predicate = factory.createIRI("http://example.com/says"); RDFTerm object = factory.createLiteral("Hello"); Triple triple = factory.createTriple(subject, predicate, object); BlankNodeOrIRI subj = triple.getSubject(); System.out.println(subj.ntriplesString()); IRI pred = triple.getPredicate(); System.out.println(pred.getIRIString()); RDFTerm obj = triple.getObject();// ww w .j ava2 s. c om System.out.println(obj.ntriplesString()); if (subj instanceof IRI) { String s = ((IRI) subj).getIRIString(); System.out.println(s); } // .. if (obj instanceof Literal) { IRI type = ((Literal) obj).getDatatype(); System.out.println(type); } System.out.println(triple.equals(factory.createTriple(subj, pred, obj))); }
From source file:example.UserGuideTest.java
@Test public void graph() throws Exception { IRI nameIri = factory.createIRI("http://example.com/name"); BlankNode aliceBlankNode = factory.createBlankNode(); Literal aliceLiteral = factory.createLiteral("Alice"); Triple triple = factory.createTriple(aliceBlankNode, nameIri, aliceLiteral); Graph graph = factory.createGraph(); graph.add(triple);/* w ww.j a v a2 s . com*/ IRI bob = factory.createIRI("http://example.com/bob"); Literal bobName = factory.createLiteral("Bob"); graph.add(bob, nameIri, bobName); System.out.println(graph.contains(triple)); System.out.println(graph.contains(null, nameIri, bobName)); System.out.println(graph.size()); for (Triple t : graph.iterate()) { System.out.println(t.getObject()); } for (Triple t : graph.iterate(null, null, bobName)) { System.out.println(t.getPredicate()); } Stream<RDFTerm> subjects = graph.getTriples().map(t -> t.getObject()); String s = subjects.map(RDFTerm::ntriplesString).collect(Collectors.joining(" ")); System.out.println(s); Stream<? extends Triple> namedB = graph.getTriples(null, nameIri, null) .filter(t -> t.getObject().ntriplesString().contains("B")); System.out.println(namedB.map(t -> t.getSubject()).findAny().get()); graph.remove(triple); System.out.println(graph.contains(triple)); graph.remove(null, nameIri, null); graph.clear(); System.out.println(graph.contains(null, null, null)); }
From source file:org.apache.jena.commonsrdf.impl.JCR_Triple.java
JCR_Triple(org.apache.jena.graph.Triple triple) { this.subject = (BlankNodeOrIRI) JCR_Factory.fromJena(triple.getSubject()); this.predicate = (IRI) JCR_Factory.fromJena(triple.getPredicate()); this.object = JCR_Factory.fromJena(triple.getObject()); this.triple = triple; }
From source file:org.apache.jena.commonsrdf.impl.JCR_Triple.java
@Override public boolean equals(Object other) { if (other == this) return true; if (other == null) return false; if (!(other instanceof Triple)) return false; Triple triple = (Triple) other; return getSubject().equals(triple.getSubject()) && getPredicate().equals(triple.getPredicate()) && getObject().equals(triple.getObject()); }
From source file:org.apache.jena.commonsrdf.JenaCommonsRDF.java
/** Convert a CommonsRDF Triple to a Jena Triple. * If the Triple was from Jena originally, return that original object else * create a copy using Jena objects. /*from w ww .j av a 2 s . c o m*/ */ public static org.apache.jena.graph.Triple toJena(Triple triple) { if (triple instanceof JenaTriple) return ((JenaTriple) triple).getTriple(); return new org.apache.jena.graph.Triple(toJena(triple.getSubject()), toJena(triple.getPredicate()), toJena(triple.getObject())); }
From source file:org.apache.jena.commonsrdf.JenaCommonsRDF.java
/** Convert from Jena {@link org.apache.jena.graph.Triple} to any RDFCommons implementation */ public static Triple fromJena(RDFTermFactory factory, org.apache.jena.graph.Triple triple) { BlankNodeOrIRI subject = (BlankNodeOrIRI) (fromJena(factory, triple.getSubject())); IRI predicate = (IRI) (fromJena(factory, triple.getPredicate())); RDFTerm object = fromJena(factory, triple.getObject()); return factory.createTriple(subject, predicate, object); }
From source file:org.semanticweb.owlapi.io.RDFTriple.java
@Override public boolean equals(@Nullable Object obj) { if (obj == this) { return true; }/*from w ww . j a v a 2 s . c om*/ if (obj instanceof RDFTriple) { RDFTriple other = (RDFTriple) obj; return subject.equals(other.subject) && predicate.equals(other.predicate) && object.equals(other.object); } // Commons RDF Triple.equals() contract if (obj instanceof Triple) { // Note: This also works on RDFLiteral // but is slightly more expensive as it must call the // getter methods when accessing obj. // // To ensure future compatibility, the Commons RDF getter // methods are also called on this rather than using the fields. Triple triple = (Triple) obj; return getSubject().equals(triple.getSubject()) && getPredicate().equals(triple.getPredicate()) && getObject().equals(triple.getObject()); } return false; }
From source file:org.trellisldp.rdfa.HtmlData.java
private LabelledTriple labelTriple(final Triple triple) { final String pred = triple.getPredicate().getIRIString(); if (triple.getObject() instanceof IRI) { return new LabelledTriple(triple, getLabel(pred), getLabel(((IRI) triple.getObject()).getIRIString())); } else if (triple.getObject() instanceof Literal) { return new LabelledTriple(triple, getLabel(pred), ((Literal) triple.getObject()).getLexicalForm()); }//from ww w . j a v a 2 s.co m return new LabelledTriple(triple, getLabel(pred), triple.getObject().ntriplesString()); }