% Control means controlling which rule to use; control by whether the 

:- dynamic enzyme_state/2.
:-['topCla'].
:-['b'].

%set(depthLimit,s(s(s(s(s(s(s(s(0))))))))). %PianoNumDepthLimit):- numMap(6,PianoNumDepthLimit).
set(depthLimit,s(s(0))).
%set(depthLimit,s(s(s(0)))).
%set(depthLimit,s(s(s(s(s(0)))))).
set(etoB,yes).





/*********** using other examples ***********/
concentration(DepthLimit,Tr,Tr,VisitedNodes,X,Change):-	
	set(depthLimit,DepthLimit0),
	DepthLimit \== DepthLimit0,
	ex(EI,concentration(X,Change),1),!,
	write('***using this example***'), write(X),nl.


concentration(DepthLimit,Tr,Tr,VisitedNodes,X,Change):- 
	neg(Change,OppositeChange),
	ex(EI,concentration(X,OppositeChange),1),%write('!!! you are looking for sth inconsistent'),
	!,
	fail.

neg(up,down).
neg(down,up).



/***************************** Regulation Rules ******************************/
/****************** Direct Effect ******************/

/*---------- Blocked/Slower down ----------*/
% 12,15,18 enzyme inhibited
concentration(DepthLimit,TrSoFar,Tr,VisitedNodes,PID,down):-
	produced_by(PID,ReactionID,'IMPD'),
	catalyzed_by_ECclass(ReactionID,EC_Number),
	(concentration_enzClass(EC_Number,Change)->
		write('*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-'),write(EC_Number),write('inhibited'),
		(Change=='up'->
			write('No, disagreed');
			write('Yes, agreed')
		);
		Foo=1
	),
	enzyme_state(TrSoFar,Tr,EC_Number,inhibited),
	nl,write(VisitedNodes),nl.

% enzyme inhibited
concentration(DepthLimit,TrSoFar,Tr,VisitedNodes,SID,up):-				%concept_name(S,SID,true),	
	consumed_by(SID,ReactionID,'IMPD'),	%reaction(ReactionID,'','LycoCyc','IMPD'),
	catalyzed_by_ECclass(ReactionID,EC_Number),
	(concentration_enzClass(EC_Number,Change)->
		write('*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-'),write(EC_Number),write('inhibited'),
		(Change=='up'->
			write('No, disagreed');
			write('Yes, agreed')
		);
		Foo=1
	),
	enzyme_state(TrSoFar,Tr,EC_Number,inhibited),
	nl,write(VisitedNodes),nl.

/*---------- Speeded Up ----------*/
% 10,13,16 enzyme activated
concentration(DepthLimit,TrSoFar,Tr,VisitedNodes,PID,up):-
	produced_by(PID,ReactionID,'IMPD'),
	catalyzed_by_ECclass(ReactionID,EC_Number),
	(concentration_enzClass(EC_Number,Change)->
		write('*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-'),write(EC_Number),write('activated'),
		(Change=='down'->
			write('No, disagreed');
			write('Yes, agreed')
		);
		Foo=1
	),
	enzyme_state(TrSoFar,Tr,EC_Number,activated),
	nl,write(VisitedNodes),nl.


% enzyme activated
concentration(DepthLimit,TrSoFar,Tr,VisitedNodes,SID,down):-				%concept_name(S,SID,true),	
	consumed_by(SID,ReactionID,'IMPD'),	%reaction(ReactionID,'','LycoCyc','IMPD'),
	catalyzed_by_ECclass(ReactionID,EC_Number),
	(concentration_enzClass(EC_Number,Change)->
		write('*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-'),write(EC_Number),write('activated'),
		(Change=='down'->
			write('No, disagreed');
			write('Yes, agreed')
		);
		Foo=1
	),
	enzyme_state(TrSoFar,Tr,EC_Number,activated),
	nl,write(VisitedNodes),nl.



/****************** Indirect Effect ******************/
% enzyme no_change -- S up
concentration(s(DepthLimit),TrSoFar,Tr,VisitedNodes,PID,up):-				%concept_name(P,PID,true),	
	produced_by(PID,ReactionID,'IMPD'),	%reaction(ReactionID,'','LycoCyc','IMPD'),
	catalyzed_by_ECclass(ReactionID,EC_Number), 
	\+concentration_enzClass(EC_Number,Change),
	enzyme_state(TrSoFar,Tr1,EC_Number,no_change),
	consumed_by(SID,ReactionID,'IMPD'), \+member(SID,VisitedNodes),
	concentration(DepthLimit,Tr1,Tr,[SID|VisitedNodes],SID,up).

% enzyme no_change -- S down
concentration(s(DepthLimit),TrSoFar,Tr,VisitedNodes,PID,down):-				%concept_name(P,PID,true),	
	produced_by(PID,ReactionID,'IMPD'),	%reaction(ReactionID,'','LycoCyc','IMPD'),
	catalyzed_by_ECclass(ReactionID,EC_Number), nl,write(VisitedNodes),
	\+concentration_enzClass(EC_Number,Change),
	enzyme_state(TrSoFar,Tr1,EC_Number,no_change),
	consumed_by(SID,ReactionID,'IMPD'), \+member(SID,VisitedNodes),
	concentration(DepthLimit,Tr1,Tr,[SID|VisitedNodes],SID,down).




/*************** Abduce enzyme_state	****************/
/* Simple version -- no incorporation of integrity constraint
enzyme_state(TrSoFar,Tr,EC_Number,State):-
	Tr=[[enzyme_state-{EC_Number,State}]|TrSoFar].
*/


% incorporate integrity constraint
% 1. one enzyme not to be hypothesized to several states: (1) the existing B; (2) the same 
% 2. the gene expression data. 

enzyme_state(TrSoFar,Tr,EC_Number,State):-
	%write({EC_Number,State}),
	(appeared(EC_Number,TrSoFar,State2)->
		State2==State, write('not contradiction'),nl,
		Tr=TrSoFar;
		Tr=[[enzyme_state-{EC_Number,State}]|TrSoFar]
	).


appeared(EC_Number,TrSoFar,State):-
	member([enzyme_state-{EC_Number,State}],TrSoFar), write('--Aha, appear before!--'),write({EC_Number,TrSoFar}),nl.
% appeared(EC_Number,TrSoFar,State):-	% won't happen since no inhibited is known, nor added to the background knowledge
%	enzyme_state(EC_Number,State).




