%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% exe.pl         6/25/2002 
%%
%% execute one action
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    

exec(_, create(X)) :-
    assert(dyn(X)). 

% x is the new value 
exec(_, update(X)) :-

    %% checking the correctness of the update action    
    \+ X = instance_of(Instance, Entity),
    X = instance_att((Instance, Entity), Att, _),
    dyn(instance_att((Instance, Entity), Att, _)), 

    retract(dyn(instance_att((Instance, Entity), Att, _))), 
    assert(dyn(X)). 

exec(_, delete(X) ) :-
     retract(dyn(X)). 

exec(Instance/Process, cond_action(X, Y)) :- 
     all_preconditions_satisfied(Process, Instance),  
     exec_process(Y). 

%% otherwise, the update operations has been failed

exec(_, update(X)) :-

    %%% checking the correctness of the update action    
    ( X = instance_of(Instance, Entity) 
      ; 
      X = instance_att((Instance, Entity), Att, _),
      \+ instance_att((Instance, Entity), Att, _)
    ), 

    write('Error: the below update operation has been failed: '), 
    write(X),nl, 
    write('Analysis: the process has either tried to update an instance 
or an attribute that does not exist. To update an instance, one needs
to delete it and create it from scratch. To insert a new attribute,
use create/1 command.'), nl, fail. 

%%%%%%%%%%%% post issue %%%%%%%%%%

exec(_, post_issue(X, Y)) :- 
    call_external_agent(post_issue(X, Y)). 

%%%%%%%%%%%% receive issue %%%%%%%%%%

exec(_, receive_issue(X, Y)) :- 
    assert(receive_issue(X, Y)). 

%%----------------------- end core actions ----------------------
%%%%%%%% get customer requirements %%%%%%%%

exec(_, get_customer_requirements(Event_content, Requirements)) :-

     Event_content = event_occ(_, _, _, _, _, _, List), 
     member(instance_of(Instance, customer_requirements, Requirements), 
            List). 

%%%%%%%%%%%% construct issue content %%%%%%%%%%
%%% note that same ID, but process is changed to issue before 
%%% it is bening transmitted. 

exec(Instance/Process, get_issue_content(Issue, New_issue_content)) :-
   
    dyn(agenda(process(Instance, Process/PID, Status, Priority, 
               Begin_time/End_time, Time_cost, 
               (Provider/Provider_type, Receiver/Receiver_type), 
               P_Trigger, Precond, Action, Postcond))), 
    
     %%% get requirements and old solution from old issue 
   ( issue_occ(Instance, Issue, Status, Priority, Begin_time/End_time, 
               [solution(Provider, Requirements, 
                         _, Old_solution, _diff)])
    ; 
     %%% get basic solution from issue template
     issue(Instance, Issue, Status, Priority, Begin_time/End_time, 
           [solution( Provider, Requirements, 
            Old_solution, _, _diff)] ), 
 
     %% get customer requirements 
     dyn(instance_of(Instance, customer_requirements, Requirements)), 
     
   ), % end ; 

    New_issue_content = 
    issue_occ(Instance, Issue, Status, Priority, Begin_time/End_time, 
           [solution(Provider, Requirements, 
                     Old_solution, _new_sol, _diff) ] ). 


%%%%%%%%%%%%%%%%%%%%%%%   end of file %%%%%%%%%%%%%%%%%%%%    
    

                       


