图可以描绘大部分任何逻辑,给个我用图的实例
public void walk(IGraph graph, AbstractNodeVisitor<IPigBuilder> visitor, PigWriter writer) throws NodeErrorException{
this.graph = graph;
setAllNodesUnvisited();
Stack<AbstractNode> visitedNodes = new Stack<AbstractNode>();
for (AbstractNode node: graph.getNodeList()){
if (getVisitState(node) == VisitState.UNVISITED){
dfs(node, visitedNodes);
}
}
while (!visitedNodes.empty()){
visitedNodes.pop().accept(visitor).write(writer);
}
}
private void dfs(AbstractNode node, Stack<AbstractNode> visitedNodes){
setVisitState(node, VisitState.PASSED);
Set<AbstractNode> succNodes = GraphHelper.getSuccNodes(node);
for (AbstractNode succNode: succNodes){
VisitState state = getVisitState(succNode);
if (state == VisitState.UNVISITED){
dfs(succNode, visitedNodes);
}
else if(state == VisitState.PASSED){
throw new IllegalStateException("Loop");
}
}
setVisitState(node, VisitState.VISITED);
visitedNodes.push(node);
}