📄 reteoovisitor.java
字号:
package org.drools.reteoo;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.drools.ide.editors.rete.model.Connection;
import org.drools.ide.editors.rete.model.ReteGraph;
import org.drools.util.ReflectiveVisitor;
/**
* Produces a graph in GraphViz DOT format.
*
* @see http://www.research.att.com/sw/tools/graphviz/
* @see http://www.pixelglow.com/graphviz/
*
* @author Andy Barnett
*/
public class ReteooVisitor extends ReflectiveVisitor {
private static final String PACKAGE_NAME = "org.drools.reteoo.";
/**
* Keeps track of visited JoinNode DOT IDs. This mapping allows the visitor
* to recognize JoinNodes it has already visited and as a consequence link
* existing nodes back together. This is vital to the Visitor being able to
* link two JoinNodeInputs together through their common JoinNode.
*/
private final Map visitedNodes = new HashMap();
private ReteGraph graph;
private BaseVertex rootVertex;
private BaseVertex parentVertex;
/**
* Constructor.
*/
public ReteooVisitor(final ReteGraph graph) {
this.graph = graph;
}
public ReteGraph getGraph() {
return this.graph;
}
public BaseVertex getRootVertex() {
return this.rootVertex;
}
/**
* RuleBaseImpl visits its Rete.
*/
public void visitReteooRuleBase(final ReteooRuleBase ruleBase) {
visit( (ruleBase).getRete() );
}
/**
* Rete visits each of its ObjectTypeNodes.
*/
public void visitRete(final Rete rete) {
this.rootVertex = (ReteVertex) this.visitedNodes.get( dotId( rete ) );
if ( this.rootVertex == null ) {
this.rootVertex = new ReteVertex( rete );
this.visitedNodes.put( dotId( rete ),
this.rootVertex );
}
this.graph.addChild( this.rootVertex );
this.parentVertex = this.rootVertex;
for ( final Iterator i = rete.objectTypeNodeIterator(); i.hasNext(); ) {
final Object nextNode = i.next();
visitNode( nextNode );
}
}
public void visitBaseNode(final BaseNode node) {
BaseVertex vertex = (BaseVertex) this.visitedNodes.get( dotId( node ) );
if ( vertex == null ) {
try {
String name = node.getClass().getName();
name = name.substring( name.lastIndexOf( '.' ) + 1 ) + "Vertex";
final Class clazz = Class.forName( PACKAGE_NAME + name );
vertex = (BaseVertex) clazz.getConstructor( new Class[]{node.getClass()} ).newInstance( new Object[]{node} );
} catch ( final Exception e ) {
throw new RuntimeException( "problem visiting vertex " + node.getClass().getName(),
e );
}
this.graph.addChild( vertex );
this.visitedNodes.put( dotId( node ),
vertex );
new Connection( this.parentVertex,
vertex );
final BaseVertex oldParentVertex = this.parentVertex;
this.parentVertex = vertex;
List list = null;
if ( node instanceof ObjectSource ) {
list = ((ObjectSource) node).getObjectSinksAsList();
} else if ( node instanceof TupleSource ) {
list = ((TupleSource) node).getTupleSinks();
}
if ( list != null ) {
for ( final Iterator it = list.iterator(); it.hasNext(); ) {
final Object nextNode = it.next();
visitNode( nextNode );
}
}
this.parentVertex = oldParentVertex;
} else {
new Connection( this.parentVertex,
vertex );
}
}
/**
* Helper method to ensure nodes are not visited more than once.
*/
private void visitNode(final Object node) {
visit( node );
}
/**
* The identity hashCode for the given object is used as its unique DOT
* identifier.
*/
private static String dotId(final Object object) {
return Integer.toHexString( System.identityHashCode( object ) ).toUpperCase();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -