package ws.prova.examples;

/*
 * Copyright (C) 2002-2007 <a href="mailto:a.kozlenkov@city.ac.uk">Alex Kozlenkov</a>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.io.Serializable;

/**
 * An example class used in test024.prova
 * <p>Title: Prova</p>
 * <p>Description: An example class used in test024.prova</p>
 * <p>Copyright: Copyright (C) 2002-2007</p>
 * <p>Company: City University, London, United Kingdom</p>
 *
 * @author Alex Kozlenkov
 * @version 2.0 <25 March 2007>
 */
public class Portfolio implements Serializable {
	private String name;

	public Portfolio() {
		name = "default";
	}

	public Portfolio(String _name) {
		name = _name;
	}

	public String toString() {
		return new String("I am Portfolio " + name);
	}

	public Object[] toList() {
		return new Object[]{new String("IBM"), new String("Sun")};
	}

	/**
	 * An example of a method with a long duration calculation
	 *
	 * @param nsec - the number of seconds to sleep as Integer
	 * @return - always Boolean.TRUE
	 */
	public Boolean longcalc(long nsec) {
		// The only parameter: the number of seconds to sleep
//		Integer nsec = (Integer)arg1;
		try {
			Thread.sleep(nsec * 1000);
		} catch (Exception exc) {
		}
//		// Add returned objects to the return list
//		ret.add(nsec);
		return Boolean.TRUE;
	}

}

/*
Useful snippets that work
//		Fact queryfact = fact(holds,rlist(RList.class,"[Msg]"));  //OK
//		Fact queryfact = fact(holds,rlist("[[A,location,flock,britain]]"));  //OK
//		Fact queryfact = fact(state,rlist("[A,location,flock,britain]"));  //OK
//			Fact queryfact = fact(state,rlist("[spec,investment,\"IBM\",Investment.X]"));  //OK
//			Fact queryfact = fact(actions,rhs4);  //OK
//			Fact queryfact = fact(state,rlist("[State]")); //OK
//			Fact queryfact = fact(demo,rlist("[[meta,symmetric,A]]"));  //OK
//			Fact queryfact = fact(demo,rlist("[[meta,inverse,north,A]]"));  //OK
//		Fact queryfact = fact(trusted,variable("A",String.class),variable("B",String.class));
//		Fact queryfact = fact(in_message,variable("A",String.class),"update",cplx(list(),variable("Msg",RList.class),variable("M",String.class)));
//			Fact queryfact = fact(proc,rhs4);  //OK

//		rule = rule(
//				fact(state,rlist("[spec,investment,\"IBM\",%0]",new Object[] {new Investment("IBM",100)}))
//				);
//		kb.add(rule);

 static SimplePredicate in_message = in_message();
 static SimplePredicate proc = proc();
static SimplePredicate trusted = trusted();

 public static SimplePredicate in_message() {
	 Class[] struct = { String.class, String.class, RList.class, RList.class, RList.class };
	 SimplePredicate p = new SimplePredicate ("in_message", struct);
	 return p;
 }

 public static SimplePredicate proc() {
	 Class[] struct = { String.class, String.class, RList.class, RList.class, RList.class };
	 SimplePredicate p = new SimplePredicate ("proc", struct);
	 return p;
 }

 public static SimplePredicate trusted() {
	 Class[] struct = { String.class, String.class };
	 SimplePredicate p = new SimplePredicate ("trusted", struct);
	 return p;
 }

  rule = rule(fact(trusted,"2","update"));
  kb.add(rule);

 public Boolean retractall( Object arg1, Object arg2 ) {
	 TermWrapper tw = (TermWrapper)arg2;
	 ComplexTerm ct = (ComplexTerm)tw.term();
	 for( TermIterator ti = ct.allSubterms(); ti.hasNext(); ) {
		 Term t = ti.nextTerm();
		 if( t.isVariable() ) {
			 ((VariableTerm)t).`
			 t.resolve();
		 }
	 }
	 return Boolean.TRUE;
 }

*/
/*
// default assimilation
fact = fact(transition,
						rlist("[Agent,inform,[meta,unchecked_assimilation]]"),
						rlist("[[meta,unchecked_assimilation]]"),
						rlist("[]"),
						rlist("[]"));
kb.add(fact);
*/
/*
// transition([spec,season,autumn],[[spec,location,flock,spain]],[[spec,location,flock,britain]],[]).
rule = rule(fact(transition,
						rlist("[spec,season,autumn]"),
						rlist("[[spec,location,flock,spain]]"),
						rlist("[[spec,location,flock,britain]]"),
						rlist("[]")));
kb.add(rule);
// transition([spec,season,winter],[[spec,location,flock,egypt]],[[spec,location,flock,spain]],[]).
rule = rule(fact(transition,
						rlist("[spec,season,winter]"),
						rlist("[[spec,location,flock,egypt]]"),
						rlist("[[spec,location,flock,spain]]"),
						rlist("[]")));
kb.add(rule);
// transition([spec,fly,Obj,Direction],[[spec,location,Obj,To]],[[spec,location,Obj,From]],[[spec,Direction,To,From]]).
rule = rule(fact(transition,
						rlist("[spec,fly,Obj,Direction]"),
						rlist("[[spec,location,Obj,To]]"),
						rlist("[[spec,location,Obj,From]]"),
						rlist("[[spec,Direction,To,From]]")));
kb.add(rule);
// ask if Y=X+1
fact = fact(transition,
				rlist("[Agent,askif,[spec,inc,Integer.X,Integer.Y]]"),
				rlist("[]"),
				rlist("[]"),
				rlist("[[exec,%0,inc,Integer.X,Integer.Y],[exec,%1,retractall,Integer.X,%2]]",
						new Object[]{calc,this,new TermWrapper(rlist("[Integer.X,Integer.Y]"))}));
*/
/*
//		queue.add(new RMessage("2","inform","[calc,inc,1,2]"));
		queue.add(new RMessage("2","inform","[meta,trusted,2,update]"));
		queue.add(new RMessage("2","inform","[meta,symmetric,inverse]"));
		queue.add(new RMessage("2","inform","[meta,inverse,south,north]"));
		queue.add(new RMessage("2","inform","[meta,unique_property,location]"));
		queue.add(new RMessage("2","inform","[spec,south,spain,britain]"));
		queue.add(new RMessage("2","inform","[spec,north,spain,egypt]"));
		//queue.add(new RMessage("2","inform","[spec,location,flock,_]"));
		queue.add(new RMessage("2","inform","[spec,location,flock,spain]"));
		queue.add(new RMessage("2","inform","[spec,location,flock,britain]"));
		queue.add(new RMessage("2","inform","[spec,season,autumn]"));
		queue.add(new RMessage("2","inform","[spec,season,winter]"));
		queue.add(new RMessage("2","inform","[spec,fly,_,north]"));
		queue.add(new RMessage("2","inform","[spec,fly,_,north]"));
		queue.add(new RMessage("2","inform","[spec,fly,_,south]"));
		queue.add(new RMessage("2","inform","[neg,spec,location,flock,X]"));
		queue.add(new RMessage("2","inform","[spec,fly,X,south]"));
*/

/*
  fact = fact(transition,
				  rlist("[]"),
				  rlist("[[spec,change,pyrophosphate,Diff]]"),
				  rlist("Ab"),
				  rlist("[[spec,change,Diff]]"));
//		rlist("[[spec,change,Diff],[[spec,change,membrane_glycoprotein_1,Diff]|Ab]]"));
  kb.add(fact);
// a general query-ref with abducibles
fact = fact(transition,
				rlist("[[tranid,TID],Agent,queryref,[spec|Xs]|Abducibles]"),
				rlist("[[sendMsg,Agent,[tranid,TID],Me,reply,[spec|Xs]]]"),
				rlist("[]"),
				rlist("[[[spec|Xs]|Abducibles],[spec,iam,Me]]"));
kb.add(fact);

  add(new RMessage("[0,queryref,[spec,change,pyrophosphate,X],[spec,change,membrane_glycoprotein_1,X]]"));
//		add(new RMessage("[0,queryref,[spec,change,pyrophosphate,X],[spec,change,pyrophosphate,X]]"));
*/
// query investments
/*
fact = fact(transition,
				rlist("[[tranid,TID],Agent,queryref,[spec,investment,Symbol,Investment.I]]"),
				rlist("[[sendMsg,Agent,[tranid,TID],Me,reply,[spec,investment,Symbol,Investment.I]],[exec,Investment.I,send,Agent,Me]]"),
				rlist("[]"),
				rlist("[[spec,investment,Symbol,Investment.I],[spec,iam,Me]]"));
kb.add(fact);

// . Abduction rules
rule = rule(
		fact(demo,rlist(RList.class,"[[Ab,Ab]]"))
		);
kb.add(rule);
rule = rule(
		fact(demo,rlist("Xs")),
		fact(demo,rlist(RList.class,"[[Ab,Ab]|Xs]"))
		);
kb.add(rule);
rule = rule(
		fact(actions,rlist("[]"),rlist("[[spec,change,pyrophosphate,Diff]]"),rlist(RList.class,"Ab"),rlist("Cond")),
		fact(demo,rlist("Xs")),
		fact(demo,rlist(RList.class,"[[A|Ab]|Xs]"))
		);
kb.add(rule);



//		add(new RMessage("[0,inform,[meta,inverse,increase,decrease]]"));
  add(new RMessage("[0,inform,[spec,change,increase]]"));
//		add(new RMessage("[0,inform,[spec,change,decrease]]"));
  add(new RMessage("[0,inform,[spec,isa,membrane_glycoprotein_1,enzyme]]"));
//		add(new RMessage("[2,inform,[spec,change,enzyme,decrease]]"));
//		add(new RMessage("[2,inform,[spec,change,pyrophosphate,decrease]]"));
  add(new RMessage("[2,inform,[spec,change,assoc,hydroxyapatite_crystals,formation_and_growth,increase]]"));
  add(new RMessage("[0,queryref,[spec,change,bone_mineralization,X]]"));

*/
/*
rule = rule(
		fact(demo,rlist("Xs")),
		fact(actions2,rlist("[]"),rlist("[[P|Ps]]"),rlist("State"),rlist("Cond")),
		fact(demo,rlist("[[P|Ps]|Xs]"))
		);
kb.add(rule);
*/
//  add(new RMessage("[2,inform,[spec,change,assoc,hydroxyapatite_crystals,formation_and_growth,increase]]"));
//		add(new RMessage("[2,inform,[spec,change,pyrophosphate,decrease]]"));
/*
queryfact = fact(state,rlist("[spec,change,assoc,hydroxyapatite_crystals,formation_and_growth,increase]"));
kb.add(queryfact);
queryfact = fact(state,rlist("[spec,iam,0]"));
kb.add(queryfact);
//		queryfact = fact(holds,rlist("[[spec,change,assoc,hydroxyapatite_crystals|X],[spec,iam,Y]]"));
//		queryfact = fact(actions2,rlist("[]"),
//									 rlist("[[spec,change,assoc,hydroxyapatite_crystals|X]]"),rlist("State"),rlist("Cond"));

//		queryfact = fact(actions2,rlist("[]"),
//								rlist("[[spec,change,bone_mineralization|X]]"),rlist("State"),rlist("Cond"));
queryfact = fact(demo,
								rlist("[[spec,iam,Y],[spec,change,bone_mineralization|X],[spec,iam,Y],[spec,iam,Y]]"));
//		queryfact = fact(demo,
//									 rlist("[[spec,iam,Y],[spec,iam,Y],[spec,iam,Y]]"));
//		queryfact = fact(demo,
//									 rlist("[[spec,change,bone_mineralization|X]]"));
//		queryfact = fact(demo,
//									 rlist("[[spec,iam|X]]"));

query = query(queryfact,"enumerate temporal rules");

try {
	ResultSet rs = ie.query(query,kb,ie.ALL,ie.BUBBLE_EXCEPTIONS);
	while( rs.next() ) {
		System.out.println("["+rs.getResult(String.class,"RuleID")+"]="+term_as_string(rs.getResult(RList.class,"X")));
	}
	rs.close();
}
catch (InferenceException exc) {
	exc.printStackTrace(System.out);
}
*/
/*

class TermWrapper {
	private Term term;

	public TermWrapper( Term _term ) {
		term = _term;
	}

	public Term term() {
		return term;
	}
}
*/

/*
- biogrid
- web page
- networks
- lesha search queries, scenarios
- stratify.com
*/

// /**
//  * Rename internal Mandarax variable names so that they start from 0
//  * We need this only because there is no pattern-based retractall in Mandarax (soon to come)
//  * @param ct - a term on which to operate
//  */
// private void flatten_variable_names( ComplexTerm ct ) {
//	 int i = 0;
//	 HashMap renaming = new HashMap();
//	 for( TermIterator ti = ct.allSubterms(); ti.hasNext(); ) {
//		 Term t = (Term)ti.next();
//		 if( t instanceof VariableTerm )  {
//			 VariableTerm vt = (VariableTerm)t;
//			 if( renaming.containsKey(vt.getName()) ) {
//				 continue;
//			 }
//			 ++i;
//			 renaming.put(vt.getName(),new String("#"+i));
//		 }
//	 }
//	 for( TermIterator ti = ct.allSubterms(); ti.hasNext(); ) {
//		 Term t = (Term)ti.next();
//		 if( t instanceof VariableTerm )  {
//			 VariableTerm vt = (VariableTerm)t;
//			 if( vt.getName().charAt(0)=='#' ) {
//				 continue;
//			 }
//			 vt.setName( (String)renaming.get(vt.getName()) );
//				System.out.println(t);
//		 }
//	 }
//	 for( TermIterator ti = ct.allSubterms(); ti.hasNext(); ) {
//		 Term t = (Term)ti.next();
//		 if( t instanceof VariableTerm )  {
//			 VariableTerm vt = (VariableTerm)t;
//			 String was = vt.getName();
//			 if( was.charAt(0)=='#' ) {
//				 vt.setName("@@"+was.ring(1));
//			 }
//		 }
//	 }
// }


// from main event loop--not needed now as cut is in place:
//					if( msg.performative().equals("inform") ) {
//						// Only one action list based on inform performative to be executed
//						break;
//					}
