package prolog.implementation;

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

/**
 * <p>Title: MatchingStatement</p>
 * <p>Descrpition: a statement able to query a factlist or another statement.
 * it is the most bottom class in the expression tree</p>
 */
public class MatchingStatement
	extends
		Fact
	implements
		IStatement
{
	/**
	 * constructor
	 */
	public MatchingStatement( int name )
	{
		super( name );
	}

	/**
	 * constructor 2
	 * @param f - the fact from which to take the parameters
	 */
	public MatchingStatement( int name, IRelation relation )
	{
		super( name, relation );
	}

	/**
	 * querries the fact database for a entity that can handle the querry.
	 * this can be a factlist or another statement.
	 * @param f
	 * @param l
	 * @param constraints
	 * @return
	 * @throws ParameterMatchingError
	 */
	public IFactList query( IFact f, IFactDb l, IStatementMap constraints  )
		throws ParameterMatchingError
	{
		IFactList rl = l.getFactList( f.getName() );
		if( rl == null )
		{
			IStatement s = constraints.getStatement( f.getName() );
			if( s == null )
				throw new ParameterMatchingError( "FATAL ERROR: MatchingNode.query failed!", f );
			else
				return s.query( f, l, constraints );
		}
		else
			return rl.getMatchings( f );
	}
}
