[关闭]
@contribute 2016-09-19T03:20:30.000000Z 字数 3222 阅读 3396

Traversal、TraversalSource、Step的理解

tinkerpop3



1. TraversalSource

a generator of traversals for a particular graph, domain specific language (DSL), and execution engine.

Traversals in Gremlin are spawned from a TraversalSource. The GraphTraversalSource is the typical "graph-oriented" DSL used throughout the documentation and will most likely be the most used DSL in a TinkerPop application.

A GraphTraversal<S,E> is spawned from a GraphTraversalSource. It can also be spawned anonymously (i.e. empty) via __. A graph traversal is composed of an ordered list of steps. All the steps provided by GraphTraversal inherit from the more general forms diagrammed above. A list of all the steps (and their descriptions) are provided in the TinkerPop3 GraphTraversal JavaDoc. The following subsections will demonstrate the GraphTraversal steps using the Gremlin Console.

TraversalSource是一个接口,GraphTraversalSource是其中一个实现,也是“图导向”的DSL。

2. Traversal

GraphTraversal<S,E>GraphTraversalSource大量产生,也可以由__匿名产生。一个图遍历是由一个由step组成的有序列表构成。
在最一般的情况下,遍历就是Traversal<S,E>,它实现了Iterator<E>接口,其中S代表开始,E代表结束。一个遍历由以下四种组件组成:
1. Step<S,E>:将S产生E的一个独立功能。一个遍历有多个step级联组成。(Steps are chained within a traversal.)
2. TraversalStrategy:改变遍历执行的拦截方法。(例如:查询重写)。
3. TraversalSideEffects:用于存储关于遍历的全局信息的键值对。
4. Traverser<T>:在当前的Traversal中传播的对象,T代表处理的对象。

GraphTraversal<S,E>提供了一个图遍历的经典概念,它继承的Traversal<S,E>。提供了通过点、边等图术语对图数据的解释。

The underlying Step implementations provided by TinkerPop should encompass most of the functionality required by a DSL author. It is important that DSL authors leverage the provided steps as then the common optimization and decoration strategies can reason on the underlying traversal sequence. If new steps are introduced, then common traversal strategies may not function properly.

Traverser<T>实现了Iterator<E>SerializableCloneable。说明能迭代,是对Step迭代。能序列化,能复制。
Traverser<T>接口中包含有Admin<S, E>接口,此接口提供了一下一些接口。

3. Traverser

Traverser<T>:在当前的Traversal中传播的对象,T代表处理的对象。

4. Step

以下就是一个Traversal:

  1. g.V(1).as('a').out('created').in('created').where(neq('a')).addE('co-developer').from('a').property('year',2009)

其中V(1)as('a')out('created')in('created')
where(neq('a'))addE('co-developer')from('a')
property('year',2009)、都是step。所以Traversal是一个由多个step组成的链。

step 分为以下5中类型:

image_1as6d93aslj9gh01jm91fnsjb9.png-119kB

5. DefaultGraphTraversal

DefaultGraphTraversal继承DefaultTraversal,并实现了GraphTraversal.Admin<S, E>接口。DefaultTraversal实现Traversal接口。
一个Traversal中包含的所有step都存放在DefaultTraversal中的protected List<Step> steps = new ArrayList<>();中,并提供了addStep(final int index, final Step<?, ?> step)removeStep(final int index)方法。这两个方法都被Traversal中的addStep(final Step<?, E2> step)封装。每一个step中都维护一个id,此id由DefaultTraversal中的stepPosition来维护,它提供了nextXId()方法。
TraverserGenerator generator是产生各种Traverser的生产器,系统还提供了DefaultTraverserGeneratorFactory,能根据Set<TraverserRequirement> requirements来产生相应的TraverserGenerator,采用工厂模式。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注