package mbp;

import java.util.ArrayList;
public class MonkeyBanana
{
	Variable floor = new Variable("floor");
	Variable chair = new Variable("chair");
	Variable bananas = new Variable("bananas");
	Variable monkey = new Variable("monkey");
	
	ArrayList variables = new ArrayList(4);
	static StringBuffer output;
	static final String bar="------------------------------\n";
	static final String therefore="=> ";
	
	UnaryPredicate dextrous = new UnaryPredicate("dextrous");
	UnaryPredicate tall = new UnaryPredicate("tall");
	UnaryPredicate inRoom = new UnaryPredicate("in-Room");
	Predicate canMove = new Predicate("can-move");
	BinaryPredicate canClimb = new BinaryPredicate("can-climb");

	BinaryAtom canReach = new BinaryAtom("can-reach"){
		public boolean getTruth(Term x, Term y){
			boolean truth = dextrous.getTruth(x) && close.getTruth(x, y);
			setEcho(toString()+'('+x+", "+y+')', truth);
			echo(
					bar+
					therefore+
					getEcho()+
					'\n'+bar);
			return truth;
		}
	};
	BinaryAtom close = new BinaryAtom("close"){
		public boolean getTruth(Term x, Term z){
			boolean truth;
			for(Variable y: variables)
			{
				truth=getOn.getTruth(x, y) && under.getTruth(y, z) && tall.getTruth(y);
				setEcho(toString()+'('+x+", "+z+')', truth);
				echo(
						bar+
						therefore+
						getEcho()+
						'\n'+bar);
				if(truth)
					return true;
			}
			return false;
		}
	};
	BinaryAtom getOn = new BinaryAtom("get-on"){
		public boolean getTruth(Term x, Term y){
			boolean truth = canClimb.getTruth(x, y);
			setEcho(toString()+'('+x+", "+y+')', truth);
			echo(
					bar+
					therefore+
					getEcho()+
					'\n'+bar);
			return truth;
		}
	};
	BinaryAtom under = new BinaryAtom("under"){
		public boolean getTruth(Term y, Term z){
			boolean truth;
			for(Variable x: variables)
			{ 
				truth = inRoom.getTruth(x) ; inRoom.echo(); 
				truth = truth && inRoom.getTruth(y); inRoom.echo();
				truth = truth && inRoom.getTruth(z) && canMove.getTruth(new Term[]{x, y, z});
				setEcho(toString()+'('+x+", "+y+')', truth);
				echo(
						bar+
						therefore+
						getEcho()+
						'\n'+bar);
				if(truth)
					return true;
			}
			return false;
		}		
	};
	public MonkeyBanana()
	{
		init();
		output = new StringBuffer(200);
	}
	public void init()
	{
		variables.add(floor);
		variables.add(chair);
		variables.add(bananas);
		variables.add(monkey);
		inRoom.setTruth(bananas, true);
		inRoom.setTruth(chair, true);
		inRoom.setTruth(monkey, true);
		dextrous.setTruth(monkey, true);
		tall.setTruth(chair, true);
		canMove.setTruth(new Term[]{monkey, chair, bananas}, true);
		canClimb.setTruth(monkey, chair, true);
	}
	public boolean canMonkeyReachTheBananas()
	{
		boolean retval = canReach.getTruth(monkey, bananas);
		return retval;
	}
	public static void append(String echo)
	{
		output.append(echo);
		output.append('\n');
	}
	public static String getOutput()
	{
		return output.toString();
	}
	public static void main(String args[])
	{
		new MonkeyBanana().canMonkeyReachTheBananas();
	}
}
1
Hosted by www.Geocities.ws