本文整理了Java中org.apache.commons.rdf.api.Graph
类的一些代码示例,展示了Graph
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Graph
类的具体详情如下:
包路径:org.apache.commons.rdf.api.Graph
类名称:Graph
[英]An RDF 1.1 Graph, a set of RDF triples, as defined by RDF-1.1 Concepts and Abstract Syntax, a W3C Recommendation published on 25 February 2014.
[中]一个RDF 1.1 Graph,一组RDF三元组,由RDF-1.1 Concepts and Abstract Syntax定义,W3C建议于2014年2月25日发布。
代码示例来源:origin: org.apache.commons/commons-rdf-api
/**
* This method is deprecated, use the equivalent method {@link #stream()}
* instead.
*
* @return A {@link Stream} over all triples.
*/
@Deprecated
default Stream<? extends Triple> getTriples() {
return stream();
}
代码示例来源:origin: org.trellisldp/trellis-constraint-rules
private static boolean hasMembershipProps(final Graph graph) {
return graph.contains(null, LDP.membershipResource, null)
&& (graph.stream(null, LDP.hasMemberRelation, null).count()
+ graph.stream(null, LDP.isMemberOfRelation, null).count() == 1L);
}
代码示例来源:origin: org.apache.commons/commons-rdf-api
@Test
public void clear() throws Exception {
graph.clear();
assertFalse(graph.contains(alice, knows, bob));
assertEquals(0, graph.size());
graph.clear(); // no-op
assertEquals(0, graph.size());
}
代码示例来源:origin: org.apache.commons/commons-rdf-jena
final JenaIRI s = factory.createIRI("http://example.com/s");
final JenaLiteral literal123 = factory.createLiteral("123", Types.XSD_INTEGER);
assertTrue(graph.contains(s, p, literal123));
final BlankNodeOrIRI bnode1 = graph.stream(null, p1, null).findFirst().map(Triple::getSubject).get();
assertTrue(bnode1 instanceof BlankNode);
final RDFTerm obj = graph.stream(bnode1, p1, null).findFirst().map(Triple::getObject).get();
assertTrue(graph.contains(bnode2, q, literalR));
assertEquals(3, graph.size());
graph.add(bnode1, p1, bnode2);
assertEquals(3, graph.size());
graph.stream(bnode2, null, null).findFirst().ifPresent(graph::add);
assertEquals(3, graph.size());
final JenaIRI p2 = factory.createIRI("http://example/p2");
final JenaLiteral foo = factory.createLiteral("foo");
graph.add(s2, p2, foo);
assertEquals(4, graph.size());
assertTrue(graph.contains(s2, p2, foo));
代码示例来源:origin: org.apache.commons/commons-rdf-api
@Test
public void remove() throws Exception {
final long fullSize = graph.size();
graph.remove(alice, knows, bob);
final long shrunkSize = graph.size();
assertEquals(1, fullSize - shrunkSize);
graph.remove(alice, knows, bob);
assertEquals(shrunkSize, graph.size()); // unchanged
graph.add(alice, knows, bob);
graph.add(alice, knows, bob);
graph.add(alice, knows, bob);
assertTrue(graph.size() > shrunkSize);
graph.remove(alice, knows, bob);
assertEquals(shrunkSize, graph.size());
try (Stream<? extends Triple> stream = graph.stream()) {
final Optional<? extends Triple> anyTriple = stream.findAny();
Assume.assumeTrue(anyTriple.isPresent());
graph.remove(otherTriple);
assertEquals(shrunkSize - 1, graph.size());
graph.remove(otherTriple);
assertEquals(shrunkSize - 1, graph.size()); // no change
graph.add(otherTriple);
代码示例来源:origin: org.apache.commons/commons-rdf-api
@Test
public void getGraph1() throws Exception {
// graph1 should be present
try (final Graph g1 = dataset.getGraph(graph1).get()) {
assertEquals(4, g1.size());
assertTrue(g1.contains(alice, name, aliceName));
assertTrue(g1.contains(alice, knows, bob));
assertTrue(g1.contains(alice, member, null));
assertTrue(g1.contains(null, name, secretClubName));
}
}
代码示例来源:origin: trellis-ldp/trellis
graph.add(rdf.createTriple(subject, DC.title, literal));
graph.add(rdf.createTriple(subject, DC.subject, bnode));
graph.add(rdf.createTriple(bnode, DC.title, literal));
final List<Triple> triples = graph.stream()
.map(HttpUtils.skolemizeTriples(mockResourceService, "http://example.org/"))
.collect(toList());
.map(t -> () -> assertTrue(graph.contains(t), "Graph doesn't include triple: " + t)));
代码示例来源:origin: org.trellisldp/trellis-test
/**
* Check an event graph for required properties.
* @param resource the resource IRI
* @param agent the agent IRI
* @param activity the activity IRI
* @param ldpType the LDP type of the resource
* @return a predicate function
*/
public static Predicate<Graph> checkEventGraph(final String resource, final IRI agent, final IRI activity,
final IRI ldpType) {
final IRI objectIRI = getInstance().createIRI(resource);
return g -> g.contains(null, AS.object, objectIRI)
&& g.contains(null, AS.actor, agent)
&& g.contains(null, type, PROV.Activity)
&& g.contains(null, type, activity)
&& g.contains(objectIRI, type, ldpType);
}
代码示例来源:origin: org.apache.commons/commons-rdf-api
/**
* Add all triples from the source to the target.
* <p>
* The triples may be copied in any order. No special conversion or
* adaptation of {@link BlankNode}s are performed.
*
* @param source
* Source Graph to copy triples from
* @param target
* Target Graph where triples will be added
*/
private void addAllTriples(final Graph source, final Graph target) {
// unordered() as we don't need to preserve triple order
// sequential() as we don't (currently) require target Graph to be
// thread-safe
try (Stream<? extends Triple> stream = source.stream()) {
stream.unordered().sequential().forEach(t -> target.add(t));
}
}
代码示例来源:origin: trellis-ldp/trellis
@Test
public void testUpdate() {
final Graph graph = rdf.createGraph();
getTriples().forEach(graph::add);
assertEquals(3L, graph.size(), "Incorrect graph size!");
service.update(graph, "DELETE WHERE { ?s <http://purl.org/dc/terms/title> ?o }", SPARQL_UPDATE, "test:info");
assertEquals(2L, graph.size(), "Incorrect graph size, post update!");
service.update(graph, "INSERT { " +
"<> <http://purl.org/dc/terms/title> \"Other title\" } WHERE {}", SPARQL_UPDATE,
"trellis:data/resource");
assertEquals(3L, graph.size(), "Incorrect graph size, after adding triple!");
service.update(graph, "DELETE WHERE { ?s ?p ?o };" +
"INSERT { <> <http://purl.org/dc/terms/title> \"Other title\" } WHERE {}", SPARQL_UPDATE,
"trellis:data/");
assertEquals(1L, graph.size(), "Incorrect graph size after removing triples!");
assertEquals("<trellis:data/>", graph.stream().findFirst().map(Triple::getSubject)
.map(RDFTerm::ntriplesString).get(), "Incorrect graph subject from updates!");
}
代码示例来源:origin: org.trellisldp/trellis-test
/**
* Check the absense of audit triples.
*/
@Test
@DisplayName("Check the absense of audit triples.")
default void testNoAuditTriples() {
try (final Response res = target(getResourceLocation()).request().get()) {
final Graph g = readEntityAsGraph(res.getEntity(), getBaseURL(), TURTLE);
assertEquals(2L, g.size(), "Check that the graph has 2 triples");
}
}
代码示例来源:origin: org.apache.commons/commons-rdf-api
@Test
public void testCreateGraph() throws Exception {
try (final Graph graph = factory.createGraph(); final Graph graph2 = factory.createGraph()) {
assertEquals("Graph was not empty", 0, graph.size());
graph.add(factory.createBlankNode(), factory.createIRI("http://example.com/"), factory.createBlankNode());
assertNotSame(graph, graph2);
assertEquals("Graph was empty after adding", 1, graph.size());
assertEquals("New graph was not empty", 0, graph2.size());
}
}
代码示例来源:origin: commons-rdf/commons-rdf
@Test
public void remove() throws Exception {
long fullSize = graph.size();
graph.remove(alice, knows, bob);
long shrunkSize = graph.size();
assertEquals(1, fullSize - shrunkSize);
graph.remove(alice, knows, bob);
assertEquals(shrunkSize, graph.size()); // unchanged
graph.add(alice, knows, bob);
graph.add(alice, knows, bob);
graph.add(alice, knows, bob);
// Undetermined size at this point -- but at least it
// should be bigger
assertTrue(graph.size() > shrunkSize);
// and after a single remove they should all be gone
graph.remove(alice, knows, bob);
assertEquals(shrunkSize, graph.size());
Optional<? extends Triple> anyTriple = graph.getTriples().findAny();
Assume.assumeTrue(anyTriple.isPresent());
Triple otherTriple = anyTriple.get();
graph.remove(otherTriple);
assertEquals(shrunkSize - 1, graph.size());
graph.remove(otherTriple);
assertEquals(shrunkSize - 1, graph.size()); // no change
graph.add(otherTriple);
assertEquals(shrunkSize, graph.size());
}
代码示例来源:origin: org.apache.commons/commons-rdf-api
@Test
public void removeLanguageTagsCaseInsensitive() throws Exception {
// COMMONSRDF-51: Ensure we can remove with any casing
// of literal language tag
final Literal lower = factory.createLiteral("Hello", "en-gb");
final Literal upper = factory.createLiteral("Hello", "EN-GB");
final Literal mixed = factory.createLiteral("Hello", "en-GB");
final IRI example1 = factory.createIRI("http://example.com/s1");
final IRI greeting = factory.createIRI("http://example.com/greeting");
try (final Graph graph = factory.createGraph()) {
graph.add(example1, greeting, upper);
// Remove should also honour any case
graph.remove(example1, null, mixed);
assertFalse(graph.contains(null, greeting, null));
graph.add(example1, greeting, lower);
graph.remove(example1, null, upper);
// Check with Triple
graph.add(factory.createTriple(example1, greeting, mixed));
graph.remove(factory.createTriple(example1, greeting, upper));
assertFalse(graph.contains(null, greeting, null));
}
}
代码示例来源:origin: org.apache.commons/commons-rdf-api
@Test
public void containsLanguageTagsCaseInsensitive() throws Exception {
// COMMONSRDF-51: Ensure we can add/contains/remove with any casing
// of literal language tag
final Literal lower = factory.createLiteral("Hello", "en-gb");
final Literal upper = factory.createLiteral("Hello", "EN-GB");
final Literal mixed = factory.createLiteral("Hello", "en-GB");
final IRI example1 = factory.createIRI("http://example.com/s1");
final IRI greeting = factory.createIRI("http://example.com/greeting");
try (final Graph graph = factory.createGraph()) {
graph.add(example1, greeting, upper);
// any kind of Triple should match
assertTrue(graph.contains(factory.createTriple(example1, greeting, upper)));
assertTrue(graph.contains(factory.createTriple(example1, greeting, lower)));
assertTrue(graph.contains(factory.createTriple(example1, greeting, mixed)));
// or as patterns
assertTrue(graph.contains(null, null, upper));
assertTrue(graph.contains(null, null, lower));
assertTrue(graph.contains(null, null, mixed));
}
}
代码示例来源:origin: trellis-ldp/trellis
/**
* Add a triple to the graph.
*
* @param triple an RDF Triple
*/
public void add(final Triple triple) {
graph.add(triple);
}
代码示例来源:origin: org.apache.commons/commons-rdf-api
@Test
public void iterate() throws Exception {
Assume.assumeTrue(graph.size() > 0);
final List<Triple> triples = new ArrayList<>();
for (final Triple t : graph.iterate()) {
triples.add(t);
}
assertEquals(graph.size(), triples.size());
if (bobNameTriple != null) {
assertTrue(triples.contains(bobNameTriple));
}
// aborted iteration
final Iterable<Triple> iterate = graph.iterate();
final Iterator<Triple> it = iterate.iterator();
assertTrue(it.hasNext());
it.next();
closeIterable(iterate);
// second iteration - should start from fresh and
// get the same count
long count = 0;
final Iterable<Triple> iterable = graph.iterate();
for (@SuppressWarnings("unused") final
Triple t : iterable) {
count++;
}
assertEquals(graph.size(), count);
}
代码示例来源:origin: commons-rdf/commons-rdf
@Test
public void getTriples() throws Exception {
long tripleCount = graph.getTriples().count();
assertTrue(tripleCount > 0);
assertTrue(graph.getTriples().allMatch(t -> graph.contains(t)));
// Check exact count
Assume.assumeNotNull(org1, org2, aliceName, bobName, secretClubName,
companyName, bobNameTriple);
assertEquals(8, tripleCount);
}
代码示例来源:origin: trellis-ldp/trellis
@BeforeEach
public void setUp() {
final IRI other = rdf.createIRI("trellis:data/other");
graph.clear();
graph.add(rdf.createTriple(subject, ACL.agent, rdf.createIRI("info:agent/foo")));
graph.add(rdf.createTriple(subject, ACL.agent, rdf.createIRI("info:agent/bar")));
graph.add(rdf.createTriple(other, ACL.agent, rdf.createIRI("info:agent/baz")));
graph.add(rdf.createTriple(subject, ACL.agentClass, rdf.createIRI("info:agent/SomeClass")));
graph.add(rdf.createTriple(other, ACL.agentClass, rdf.createIRI("info:agent/SomeOtherClass")));
graph.add(rdf.createTriple(subject, ACL.agentGroup, rdf.createIRI("info:group/group1")));
graph.add(rdf.createTriple(subject, ACL.agentGroup, rdf.createIRI("info:group/group2")));
graph.add(rdf.createTriple(subject, ACL.agentGroup, rdf.createIRI("info:group/group3")));
graph.add(rdf.createTriple(subject, ACL.agentGroup, rdf.createIRI("info:group/group4")));
graph.add(rdf.createTriple(subject, ACL.mode, ACL.Read));
graph.add(rdf.createTriple(subject, ACL.accessTo, rdf.createIRI("trellis:data/resource2")));
graph.add(rdf.createTriple(subject, ACL.accessTo, rdf.createIRI("trellis:data/resource3")));
graph.add(rdf.createTriple(subject, ACL.accessTo, rdf.createIRI("trellis:data/resource4")));
graph.add(rdf.createTriple(subject, ACL.accessTo, rdf.createIRI("trellis:data/resource4")));
graph.add(rdf.createTriple(other, ACL.accessTo, rdf.createIRI("trellis:data/resource5")));
graph.add(rdf.createTriple(subject, ACL.accessToClass, PROV.Activity));
graph.add(rdf.createTriple(other, ACL.accessToClass, PROV.Entity));
graph.add(rdf.createTriple(subject, ACL.default_, rdf.createIRI("trellis:data/container")));
}
代码示例来源:origin: trellis-ldp/trellis
@Override
public void close() {
try {
innerGraph.close();
} catch (final Exception ex) {
throw new RuntimeTrellisException("Error closing graph", ex);
}
}
内容来源于网络,如有侵权,请联系作者删除!