org.apache.commons.rdf.api.Graph.iterate()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(3.3k)|赞(0)|评价(0)|浏览(94)

本文整理了Java中org.apache.commons.rdf.api.Graph.iterate()方法的一些代码示例,展示了Graph.iterate()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Graph.iterate()方法的具体详情如下:
包路径:org.apache.commons.rdf.api.Graph
类名称:Graph
方法名:iterate

Graph.iterate介绍

[英]Gets an Iterable for iterating over all triples in the graph.

This method is meant to be used with a Java for-each loop, e.g.:

for (Triple t : graph.iterate()) { 
System.out.println(t); 
}

The behaviour of the iterator is not specified if #add(Triple), #remove(Triple) or #clear(), are called on the Graph before it terminates. It is undefined if the returned Iterator supports the Iterator#remove() method.

Implementations may throw ConcurrentModificationException from Iterator methods if they detect a concurrency conflict while the Iterator is active.

The Iterable#iterator() must only be called once, that is the Iterable must only be iterated over once. A IllegalStateExceptionmay be thrown on attempt to reuse the Iterable.

The default implementation of this method will call #stream() to return its Stream#iterator().
[中]获取迭代图中所有三元组的Iterable。
此方法用于每个循环的Java,例如:

for (Triple t : graph.iterate()) { 
System.out.println(t); 
}

如果在图终止之前对图调用#add(Triple)、#remove(Triple)或#clear(),则不会指定迭代器的行为。如果返回的迭代器支持迭代器#remove()方法,则它是未定义的。
如果实现在迭代器处于活动状态时检测到并发冲突,则可能会从迭代器方法抛出ConcurrentModificationException。
Iterable#迭代器()只能调用一次,即Iterable只能迭代一次。试图重用Iterable时可能会抛出非法状态Exception。
此方法的默认实现将调用#stream()以返回其流#迭代器()。

代码示例

代码示例来源:origin: trellis-ldp/trellis

/**
 * Collect a stream of Triples into a Graph.
 *
 * @return a graph
 */
public static Collector<Triple, ?, Graph> toGraph() {
  return Collector.of(rdf::createGraph, Graph::add, (left, right) -> {
    right.iterate().forEach(left::add);
    return left;
  }, UNORDERED);
}

代码示例来源:origin: org.trellisldp/trellis-api

/**
 * Collect a stream of Triples into a Graph.
 *
 * @return a graph
 */
public static Collector<Triple, ?, Graph> toGraph() {
  return Collector.of(rdf::createGraph, Graph::add, (left, right) -> {
    right.iterate().forEach(left::add);
    return left;
  }, UNORDERED);
}

代码示例来源:origin: org.apache.commons/commons-rdf-api

@Test
public void iterateFilter() throws Exception {
  final List<RDFTerm> friends = new ArrayList<>();
  final IRI alice = factory.createIRI("http://example.com/alice");
  final IRI knows = factory.createIRI("http://xmlns.com/foaf/0.1/knows");
  for (final Triple t : graph.iterate(alice, knows, null)) {
    friends.add(t.getObject());
  }
  assertEquals(1, friends.size());
  assertEquals(bob, friends.get(0));
  // .. can we iterate over zero hits?
  final Iterable<Triple> iterate = graph.iterate(bob, knows, alice);
  for (final Triple unexpected : iterate) {
    fail("Unexpected triple " + unexpected);
  }
  // closeIterable(iterate);
}

代码示例来源: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);
}

相关文章

微信公众号

最新文章

更多