ss in the agenda, 
        % report these processes 

        % write('********************************************'),nl,
        % write('All processes in the Agenda are given below:'),nl,nl,
        % write_list(Process_list),nl,
        % write('********************************************'),nl,

        % checking that no conflict processes are to be executed 
        % at the same time !
        % calling process conflict detector in 'conflict.pl'

        % write('* Searching for contradicatory processes...'),nl, 
        % \+ detect_conflicted_processes(Process_list),
        % write('  No contradicatory processes found.'),nl, 

        % choose and execute these processes
        choose_process(Process_list). 

meta_exec_process. 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% choose process for execution from process agenda 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
choose_process( []) :- !. 

choose_process( [(Process, PID)|Tail] ) :-

       dyn(agenda(process(Instance, Process/PID, PStatus, Priority, 
              Begin_time/End_time, Time_cost, 
              (Provider/Provider_type, Receiver/Receiver_type), 
              P_Trigger, Precond, Action, Postcond))), 

       P = process(Instance, Process/PID, PStatus, Priority, 
              Begin_time/End_time, Time_cost, 
              (Provider/Provider_type, Receiver/Receiver_type), 
              P_Trigger, Precond, Action, Postcond),

        % decide whether to execute the process or not, 
        % this is determined by the user
        nl, 
        write('*****************************************'),nl,
        write('Would you like to execute this process:'),nl,nl,
        write('Process: '), write(Process_type),nl,
        write('Process/Event Instance: '),write(Tri_id),nl,
        write('The full process:'),nl, 
        pretty_print(P), nl, 
        write('*****************************************'),nl,
        write('Execution? (y/n) '), read(Ans), nl, 

      (\+ Ans = y, choose_process(Tail); 

          Ans = y, 

        write('**********************************'),nl,
        write('     Process Execution Phase      '),nl,       
        write('**********************************'),nl,

        (A) check all actions are correct, 'top.pl'
        write('* Checking the trigger....'),nl, 
        \+ triggering_conditions_satisfied(Process_type, Tri_id),
        write('  checking trigger succeed.'),nl,

%%%%%%% work stops here %%%%%%%

        % (1) check all preconditions are satisfied, 'cond.pl'
        write('* Checking preconditions....'),nl, 
        all_preconditions_satisfied(Process_type, Tri_id),  
        write('  checking precond succeed.'),nl,

        % (B) execute the process, 'exe.pl'
        write('* Executing the process ...'),nl,
        execute_process(Process_type, Tri_id),
        write('  Process execution succeed.'),nl,

        % update the system state status 
        (\+ dyn(flag(state_changed)), 
         assert(flag(state_changed)) ;
         true), 

        % (C) all postconditions are satisfied, 'cond.pl'
        %     if an error occurs, it is reported, but 
        %     it normally does not happen.

        % write('* Verifying postconditions...'),nl,
        % all_postconditions_satisfied(Process_type, Tri_id), 
        % write('  Verifying postconditions succeed.'),nl,

        % (D) Clear up stuff: remove dyn(ent_fun()) predicates, 
        %     these are only used for this process, in 'exe.pl'
        % remove_ref_scope(Process_type, Tri_id), 

        choose_process( Tail ), ! 
      ). 

choose_process( [_|Tail] ) :-
        write('*****************************************'),nl,
        write('WARNING: Failed to execute this process, execute the next one.'),nl, 
        write('*****************************************'),
        nl,nl, 

        choose_process( Tail ). 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

triggering_conditions_satisfied(Process, PID) :- 

   dyn(agenda(process(Instance, Process/PID, PStatus, Priority, 
                      Begin_time/End_time, Time_cost, 
                      (Provider/Provider_type, Receiver/Receiver_type), 
                      [Trigger], Precond, Action, Postcond))), 

       (Trigger = true; 
        Trigger = dyn(event(Instance, Event, _, Priority, 
                 Begin_time/End_time, 
                (Provider/Provider_type, Receiver/Receiver_type), 
                 Event_content)), 

        dyn(event(Instance, Event, TStatus, Priority, 
                 Begin_time/End_time, 
                (Provider/Provider_type, Receiver/Receiver_type), 
                 Event_content)), 

        TState = Status/_, 
        \+ Status = suspended, \+ Status = cancelled, 
        \+ Status = finished ). 

triggering_conditions_satisfied(Process, PID) :- 
  write('Error: triggering conditions for the process is no longer true.'), nl, 
  fail. 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% execute the process, in 'exe.pl'
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

execute_process(Process_type, Tri_id) :-

        dyn(current_state(S)),
        read_next_state(S1),
        dyn(agenda(process(Instance, Process/PID, PStatus, Priority, 
                      Begin_time/End_time, Time_cost, 
                      (Provider/Provider_type, Receiver/Receiver_type), 
                      [Trigger], Precond, Action, Postcond))), 

        exec_process(process(Instance, Process/PID,  PStatus, Priority, 
                      Begin_time/End_time, Time_cost, 
                      (Provider/Provider_type, Receiver/Receiver_type), 
                      [Trigger], Precond, Action, Postcond )), 

        % record the change of state

        P = dyn(agenda(process(Instance, Process/PID, PStatus, Priority, 
                      Begin_time/End_time, Time_cost, 
                      (Provider/Provider_type, Receiver/Receiver_type), 
                      [Trigger], Precond, Action, Postcond))), 

        New = dyn(agenda(process(Instance, Process/PID, PStatus, Priority, 
                      Begin_time/End_time, Time_cost, 
                      (Provider/Provider_type, Receiver/Receiver_type), 
                      [Trigger], Precond, Action, Postcond))), 

        retract(P),

        assert(dyn(pro_occ(Process_type, Tri_id))),
        assert(dyn(occ_begin( Process_type, Tri_id, Begin_time))),
        assert(dyn(occ_end(   Process_type, Tri_id, End_time))),

        assert(dyn(change((S, S1), (Process_type, Tri_id),
	       [Del],
	       [dyn(pro_occ(Process_type, Tri_id)),
	        dyn(occ_begin(Process_type, Tri_id, Begin_time)),
		dyn(occ_end(  Process_type, Tri_id, End_time))] ))). 

%----------------------------------------------------------------------
% adding new trigger occurrences from the dynamic business model !!

query_for_adding_triggers :-
   
    write('Would you like to add new triggers to the system? (y./n.)'),nl,
    read(Ans),
    (Ans = y, ['/hame/jessicac/kbst-bm/simulator/data/new.db'],
     write('* Finish importing a new trigger.'), nl; 
     \+ Ans = y ). 

%-------------------------- Common Predicates-------------------------
% move current state to the next state.

update_state :-
    dyn(current_state(S)),
    read_next_state(ID),

    retract(dyn(current_state(S))),
    assert(dyn(current_state(ID))),

    dyn(state_id(X)),
    retract(dyn(state_id(X))), 
    assert(dyn(state_id(ID))),

    dyn(current_time(Time)),
    assert(dyn(state(ID, time(Time)))).

%-----------------------------------------------------------------------
%% Initial Value. state_id/1 is separated from current_state ID so that
%% state_id always generated with an unique ID (since state ID can be a
%% revisited state number). 

read_next_state(ID1) :-
   dyn(state_id(ID)),
   ID1 is ID + 1.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
look :-
    report_state.
%-------------------------------------/***************************************************************** 
File: top.pl                           15/6/99 
****************************************************************** 

Hierarchical view of predicates in system:

top (top predicate of the system)
   -> meta_match/exec_rule (meta-rules manage all process)
     -> exec_originate/change_rule (occ-rules manage process occs)
       -> exec_pred/postconditions (occ-rules manage entity  occs)

-----------------------------------------------------------
Top Rule to call system: top(+Step_to_execute)

System Predicates:
Note that Begin_time usually starts from 0.

dyn(state(ID, Time)).
dyn(current_state(ID)).
dyn(current_time(Time)).

All entity/process instances are dynamic occs.
Time and states is managed by top level system predicates.
------------------------------------------------------------
Definition of predicates:

dyn(
  agenda(process(Process_type, Process_id, Actions, 
                (Begin_time, End_time)
        ) ).

dyn(state_modified(true)).
-----------------------------------------------------------