All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jadex.rules.tools.reteviewer.ReteLayout Maven / Gradle / Ivy

There is a newer version: 4.0.267
Show newest version
package jadex.rules.tools.reteviewer;

import java.awt.Dimension;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import edu.uci.ics.jung.algorithms.layout.AbstractLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.graph.Graph;
import jadex.rules.rulesystem.rete.nodes.BetaNode;
import jadex.rules.rulesystem.rete.nodes.INode;
import jadex.rules.rulesystem.rete.nodes.ITupleConsumerNode;
import jadex.rules.rulesystem.rete.nodes.TerminalNode;

/**
 *  Jung layout for a Rete network.
 */
public class ReteLayout extends AbstractLayout implements Layout
{
	/** The size. */
	// Managed directly, because jung's abstractlayout introduces
	// it's own (incompatible) offsets. 
	protected Dimension	rsize;
	
	/** The nodes, sorted in layers. */
	protected List	layers;
	
	/** Flag to indicate when positions are up-to-date. */
	protected boolean	positions;
	
	/** Flag to enable layout optimization.
	 *  With layout optimization, not only edge lengths are optimized,
	 *  but also left/right directions of connected alpha and beta nodes. */
	protected boolean layout;
	
	/** Flag to enable simulated annealing (SA).
	 *  With SA, the layout algorithm will take longer,
	 *  but can escape sub optimal local minima. */
	protected boolean annealing;
	
	//-------- constructors --------
	
	/**
	 *  Create a new Rete layout.
	 */
	public ReteLayout(Graph g)
	{
		super(g);
		this.layout	= true;
		this.annealing	= true;
	}
	
	//-------- methods --------
	
	/**
	 *  Called when a (re-)layout is needed.
	 */
	public void initialize()
	{
		// Method required by Layout interface.
		// Nothing to do here: use lazy evaluation triggered by getGraph().
	}

	/**
	 *  Called when ?
	 */
	public void reset()
	{
		// Method required by Layout interface.
		// Nothing to do here?
	}

	/**
	 *  Get the graph to be layouted.
	 */
	public Graph getGraph()
	{
		if(layers==null)
			layoutLayers();
		if(!positions)
			setPositions();
		
		return super.getGraph();
	}
	
	/**
	 *  Called, when the component is resized.
	 *  New positions will be calculated on next redrawn.
	 */
	public void setSize(Dimension size)
	{
		positions	= false;
		this.rsize	= size;
	}
	
	/**
	 *  Get the size.
	 */
	public Dimension	getSize()
	{
		return this.rsize;
	}
	
	/**
	 *  Called, when the graph structure has changed.
	 *  New layer structure will be calculated on next redraw.
	 */
	public void graphChanged()
	{
		layers	= null;
		positions	= false;
	}
	
	//-------- helper methods --------
	
	/**
	 *  Called when a (re-)layout is needed.
	 *  Arrange node in layers structure (lists of lists of nodes),
	 *  which is independent of component size.
	 */
	protected void layoutLayers()
	{
		// Find root/leaf node(s).
		Graph	graph	= super.getGraph();
		List	rootnodes	= new ArrayList();
		List	leafnodes	= new ArrayList();
		for(Iterator it=graph.getVertices().iterator(); it.hasNext(); )
		{
			Object	next	= it.next();
			if(graph.getInEdges(next).isEmpty())
			{
				rootnodes.add(next);
			}
			if(graph.getOutEdges(next).isEmpty())
			{
				leafnodes.add(next);
			}
		}

		// Add nodes to layers according to depth in rete network.
		this.layers	= new ArrayList();
		layers.add(rootnodes);
		for(int l=0; l




© 2015 - 2024 Weber Informatics LLC | Privacy Policy