<?php

class rule{

	var $head;
	var $horns;
	
	function rule($head, $horns){
		//creates a new horn clause, allows either an individual fact to be used as a horn
		//or supports entering an array of facts
		$this->horns = array();
		$this->head = $head;
		if(is_array($horns)){
			foreach($horns as $horn){
				array_push($this->horns, $horn);	
			}	
		}
		else{
			array_push($this->horns, $horns);
		}	
	}
	
	function toString(){
		//creates a string representatino of this object, mostly for debugging purposes
		foreach($this->horns as $horn){
			$representation .= $horn->toString();
			$representation .= " ^ ";
		}
		$representation = rtrim($representation, " \^");
		$representation .= " => ";
		$representation .= $this->head->toString();
		return $representation;
	}
	
	function apply_subs($key, $value){
		for($i = 0; $i < count($this->horns); $i++){
			for($j = 0; $j < count($this->horns[$i]->arguments); $j++){
				if($this->horns[$i]->arguments[$j]->name == $key){
					$this->horns[$i]->arguments[$j] = $value;
				}	
			}	
		}
		for($j = 0; $j < count($this->head->arguments); $j++){
				if($this->head->arguments[$j]->name == $key){
					$this->head->arguments[$j] = $value;
				}	
			}	
	}
}

?>