@contribute
2016-09-19T03:20:30.000000Z
字数 3222
阅读 3556
tinkerpop3
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。
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>
、Serializable
、Cloneable
。说明能迭代,是对Step
迭代。能序列化,能复制。
Traverser<T>
接口中包含有Admin<S, E>
接口,此接口提供了一下一些接口。
addStarts(final Iterator<Traverser.Admin<S>> starts)
addStart(final Traverser.Admin<S> start)
List<Step> getSteps()
<E2> Traversal.Admin<S, E2> addStep(final Step<?, E2> step)
Traversal.Admin<S2, E2> addStep(final int index, final Step<?, ?> step)
<S2, E2> Traversal.Admin<S2, E2> removeStep(final Step<?, ?> step)
<S2, E2> Traversal.Admin<S2, E2> removeStep(final int index)
Step<S, ?> getStartStep()
Step<?, E> getEndStep()
TraverserGenerator
和Set<TraverserRequirement>
Traverser<T>
:在当前的Traversal
中传播的对象,T
代表处理的对象。
以下就是一个Traversal
:
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中类型:
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
,采用工厂模式。