package prolog.demonstration;

import java.util.*;
import prolog.model.*;
import prolog.util.*;
import prolog.implementation.*;
import prolog.treeview.*;

public class DemoConstraintStatement
	extends
		ConstraintStatement
	implements
		IDemoStatement
{
	/**
	 * the animation tree
	 */
	protected Tree tree = null;

	/**
	 * the parent of this node in animation tree
	 */
	protected Node thisNode = null;

	/**
	 * constructor
	 */
	public DemoConstraintStatement( Tree tree, int name )
	{
		super( name );
		this.tree = tree;
	}

	/**
	 * constructor 2
	 */
	public DemoConstraintStatement( Tree tree, int name, IRelation r )
	{
		super( name, r );
		this.tree = tree;
	}

	/**
	 * querries the most top expression statement
	 * @param f - the fact to search for
	 * @param l - the fact database
	 * @param constraints - the statements to resolve unfound facts
	 * @return all relations matching the given fact
	 * @throws ParameterMatchingError
	 */
	public IFactList query( IFact f, IFactDb l, IStatementMap constraints )
		throws ParameterMatchingError
	{
		IFactList back =
			expression.query(
				generateFact(
					f,
					getRelation().buildMap(),
					expression ),
				l,
				constraints );

		return back;
	}

	/**
	 * @return the node in the demo tree
	 */
	public Node getNode()
	{
		if( thisNode == null )
		{
			thisNode = new Node();
			thisNode.setContent( this );
			thisNode.addChild( ((IDemoStatement)expression).getNode() );
		}
		return thisNode;
	}

	/**
	 * clears the cached node
	 */
	public void resetNode()
	{
		this.thisNode = null;
		((IDemoStatement)this.expression).resetNode();
	}

	public String toString()
	{
		return toString( SingletonFactory.getBuilder().getSymbolTable() );
	}
}
