<?php
include("../knowledgeBase.php");
include("../parser.php");
include("../primitives/atom.php");
include("../primitives/fact.php");
include("../primitives/match.php");
include("../primitives/rule.php");
include("../primitives/variable.php");
include("../misc/aux.php");
include("../jtms/jtmsNode.php");
include("../jtms/jtms.php");

unit0b();
unit1b();
unit2b();
unit3b();
unit4b();

function unit0b(){
	echo("<br><b>Unit 0b: Test adding facts</b><br>");
	$kb = new knowledgeBase();
	$jtms = new jtms($kb);
	$kb->register_jtms($jtms);
	$fact = new fact("describesTechnique", Array(new atom("paper")));
	$kb->add_fact($fact);
	$kb->add_fact($fact);
	echo("<pre>");
	print_r($kb);
	echo("</pre>");	
	$kb->retract_fact($fact);
	$kb->retract_fact($fact);
	$kb->retract_fact($fact);
}

function unit1b(){
	echo("<br><b>Unit 1b: testing unification of facts, variables and atoms</b><br>");
	$kb = new knowledgeBase();
	$inputString = "fact1(myAtom, *Variable*, atom2);
					fact1(myAtom, const, atom2);";
	$kb = parse($inputString, $kb);
	$match = $kb->unify($kb->facts[0], $kb->facts[1]);
	echo("<pre>");
	print_r($match);
	echo("</pre>");
}

function unit2b(){
	echo("<br><b>Unit 2b: Testing rule unification</b><br>");
	$kb = new knowledgeBase();
	$inputString = "relevant(*A*) ^ significant(*A*) => accept(*A*);
					relevant(myPaper);
					significant(myPaper);
					relevant(myPaper2);
					significant(myPaper2);
					relevant(myPaper3);
					significant(myPaper5)";
	$kb = parse($inputString, $kb);
	$return = $kb->unify_rule($kb->rules[0], array());
	echo("<pre>");
	print_r($return);
	echo("</pre>");
}

function unit3b(){
	//test unifying across the kb
	echo("<br><br><b>Unit Test 3b</b> Test modus ponens<br><br>");
	$kb = new knowledgeBase();
	$inputstring = "original(*hypothesis*, *paper*) ^ nontrivial(*hypothesis*, *paper*) => significant(*paper*);
			significant(*paper*) ^ relevant(*paper*, strongly) => accept(*paper*);
			review(*paper*) ^ comprehensive(*paper*) => accept(*paper*);
			original(hypothesis1, myPaper);
			nontrivial(hypothesis1, myPaper);
			original(hypothesis2, myPaper);
			nontrivial(hypothesis2, myPaper);
			relevant(myPaper, strongly);
			review(myPaper);
			comprehensive(myPaper);";
	$kb = parse($inputstring, $kb);
	echo('Facts before modus ponens:<br>');
	foreach($kb->facts as $fact){
		echo($fact->toString());
		echo("<br>");
	}
	echo('<br>Rules before modus ponens:<br>');
	foreach($kb->rules as $rule){
		echo($rule->toString());
		echo("<br>");
	}
	$kb->modus_ponens();
	echo('<br><br>Facts after modus ponens:<br>');
	foreach($kb->facts as $fact){
		echo($fact->toString());
		echo("<br>");
	}
	
	
}

function unit4b(){
	echo("<br><br><b>Unit Test 4b</b> Test jtms<br><br>");
	//create a new kb and jtms and register the jtms to the kb
	$kb = new knowledgeBase();
	$jtms = new jtms($kb);
	$kb->register_jtms($jtms);
	
	$rules = "original(*hypothesis*, *paper*) ^ nontrivial(*hypothesis*, *paper*) => significant(*paper*);
			significant(*paper*) ^ relevant(*paper*, strongly) => accept(*paper*);
			review(*paper*) ^ comprehensive(*paper*) => accept(*paper*);";
	$facts = "original(hypothesis1, myPaper);
			nontrivial(hypothesis1, myPaper);
			original(hypothesis2, myPaper);
			nontrivial(hypothesis2, myPaper);
			relevant(myPaper, strongly);
			review(myPaper);
			comprehensive(myPaper)";
	
	$kb = parse($rules, $kb);
	$kb = parse($facts, $kb);
	
	echo('<br><b>Facts before modus ponens:</b><br>');
	foreach($kb->facts as $fact){
		echo($fact->toString());
		echo("<br>");
	}
	echo('<br><b>Rules before modus ponens:</b><br>');
	foreach($kb->rules as $rule){
		echo($rule->toString());
		echo("<br>");
	}
	$kb->modus_ponens();
	echo('<br><b>Facts after modus ponens:</b><br>');
	foreach($kb->facts as $fact){
		echo($fact->toString());
		echo("<br>");
	}
	//$node = $jtms->jtms_find_node(new fact("accept", array(new atom("myPaper"))));
	//echo("<pre>)");
	//print_r($node);
	
	echo("<br><b>Retracting original(hypothesis1, myPaper)</b></br>");
	$kb->retract_fact(new fact("original", Array(new atom("hypothesis1"), new atom("myPaper"))));
	echo('<br><b>Facts after retracting original(hypothesis1, myPaper):</b><br>');
	foreach($kb->facts as $fact){
		echo($fact->toString());
		echo("<br>");
	}
	
	//$node = $jtms->jtms_find_node(new fact("accept", array(new atom("myPaper"))));
	//echo("<pre>)");
	//print_r($node);
	
	echo("<br><b>Retracting original(hypothesis2, myPaper)</b></br>");
	$kb->retract_fact(new fact("original", Array(new atom("hypothesis2"), new atom("myPaper"))));
	echo('<br><b>Facts after retracting original(hypothesis2, myPaper):</b><br>');
	foreach($kb->facts as $fact){
		echo($fact->toString());
		echo("<br>");
	}
	
	//$node = $jtms->jtms_find_node(new fact("accept", array(new atom("myPaper"))));
	//echo("<pre>)");
	//print_r($node);
	
	echo("<br><b>Retracting review(myPaper)</b></br>");
	$kb->retract_fact(new fact("review", Array(new atom("myPaper"))));
	echo('<br><b>Facts after retracting review(myPaper):</b><br>');
	foreach($kb->facts as $fact){
		echo($fact->toString());
		echo("<br>");
	}
	
	//$node = $jtms->jtms_find_node(new fact("accept", array(new atom("myPaper"))));
	//echo("<pre>)");
	//print_r($node);
}

?>