📄 oaa.pl
字号:
% 0. File path passed in
% 1. Value found on commandline (-setup_file) or environment (SETUP_FILE)
% 2. setup.pl in Current directory
% 3. setup.pl in
% "Home directory" for user (under Windows 95/98, \ on current
% drive; under NT, we think
% this comes out to %HOMEDRIVE%%HOMEPATH%)
% 4. setup.pl in Root directory (under Windows 95/98/NT, this is C:\)
% If you want 1-4 to be tried, just call with a variable for File.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
oaa_read_setup_file(_File) :-
oaa_already_loaded(_), !.
oaa_read_setup_file(File) :-
% If File unbound, see if a value has been specified:
( var(File), oaa:oaa_ResolveVariables([
[cmd('-setup_file', File)],
[env('SETUP_FILE', File)]
]) ->
true
| otherwise ->
true
),
( ground(File),
absolute_file_name(File, SpecifiedSetupFile),
can_open_file(SpecifiedSetupFile, read, fail) ->
SetupFile = SpecifiedSetupFile
| File = 'setup.pl',
absolute_file_name(File, UserSetupFile),
can_open_file(UserSetupFile, read, fail) ->
SetupFile = UserSetupFile
| File = 'setup.pl',
concat('~/',File, HomeName),
absolute_file_name(HomeName, UserSetupFile),
can_open_file(UserSetupFile, read, fail) ->
SetupFile = UserSetupFile
| File = 'setup.pl',
( prolog_flag(host_type, ix86) ->
name(C_colon, [67,58,92]),
concat(C_colon, File, RootName)
| otherwise ->
concat('/',File, RootName)
),
absolute_file_name(RootName, RootSetupFile),
can_open_file(RootSetupFile, read, fail) ->
SetupFile = RootSetupFile
| otherwise ->
true
),
( ground(SetupFile) ->
format('Loading setup file:~n ~w~n~n', [SetupFile]),
( oaa_consult(SetupFile, [], _) ->
assert(oaa_already_loaded(SetupFile))
| otherwise ->
format('~w (~w): A problem was encountered in loading the setup file~n ~w~n',
['WARNING', oaa_ResolveVariables, SetupFile])
)
| otherwise ->
format('~w (oaa_ResolveVariables): No setup file found~n',
['WARNING'])
).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% name: oaa_command_actions
% purpose: Executes any built-in command line arguments (except oaa_name,
% oaa_connect, oaa_listen, and any others handled elsewhere)
% remarks:
% Currently supports:
% -init_file <FILENAME> : loads init file (for instance db file)
% Init files contain OAA primitive commands (that can be executed by
% oaa_Interpret(), such as oaa_Declare, oaa_AddData, oaa_AddTrigger,
% potentially even oaa_Solve(). A database state file can be represented
% by one or more oaa_Declare's followed by oaa_AddData(Clause,[address(self)])
% for each data predicate.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
oaa_command_actions :-
unix(argv(ListOfArgs)),
append(_, ['-init_file', File | _], ListOfArgs),
format('Executing init_file: ~p~n', [File]),
(oaa_consult(File,[call(oaa_Interpret(_Term,[]))], _) ->
format('Done.~n~n',[])
| otherwise ->
format('~w: Couldn''t load init file: ~p~n',
['WARNING', File])
),
% repeat for as many init_file FILE pairs as possible
fail.
oaa_command_actions :- true.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% name: oaa_Ready
% purpose: Changes the agent's 'open' status to 'ready', indicating that the
% agent is now ready to receive messages.
% remarks:
% - if requested, prints 'Ready' to standard out.
% - TBD: Let's have an error message if this fails
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
oaa_Ready(ShouldPrint) :-
% replaces 'open' status with 'ready'.
( com:com_Connected(parent, _, _, _), oaa_Name(MySymbolicName) ->
oaa_PostEvent(ev_ready(MySymbolicName), [])
| otherwise ->
true
),
( oaa_type(facilitator) ->
% TBD: Move facilitator-specific code out of here.
oaa:oaa_PrimaryAddress(MyAddress),
oaa:oaa_remove_data_local(
agent_data(MyAddress, Type, _OldStatus, Solvables, Name, Info),
Params),
oaa:oaa_add_data_local(
agent_data(MyAddress, Type, ready, Solvables, Name, Info), Params)
| otherwise ->
true
),
% if ShouldPrint, print ready
(on_exception(_,ShouldPrint,fail) ->
format('Ready.~n', [])
| true).
%*****************************************************************************
% Classifying and Manipulating ICL expressions
%*****************************************************************************
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% name: icl_content_params(Content, Params).
% purpose: Extract params, if any, from an event content. (An
% event has the form event(Content, EventParams); here we
% are concerned with params contained WITHIN Content).
% If Content contains no params, return empty list.
% remarks: This is not necessarily a complete list of valid
% content expressions. Thus, at present, only useful for
% extracting params; NOT for testing validity of Content.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
icl_content_params(Content, Params) :-
content_params(Content, Params),
!,
% There may be occasions when Params is a variable, such as when someone
% oaa_RemoveTrigger without specifying Params, so let's check for that:
( var(Params) ->
Params = []
| otherwise ->
true
).
icl_content_params(_, []).
content_params(ev_connect(Params), Params).
content_params(ev_solve(_ID, _Goal, Params), Params).
content_params(ev_update_data(_ID, _Mode, _Clause, Params), Params).
content_params(
ev_data_updated(_ID, _Mode, _Clause, Params, _Requestees, _Updaters),
Params).
content_params(ev_post_declare(_Mode, _Solvables, Params), Params).
content_params(ev_reply_declared(_Mode, _Solvables, Params, _Declared),
Params).
content_params(ev_register_solvables(_Mode, _Solvables, _MyName, Params),
Params).
content_params(ev_update_trigger(_ID, _Mode, _Type,
_Condition, _Action, Params), Params).
content_params(ev_trigger_updated(_ID, _Mode, _Type, _Condition,
_Action, Params, _Requestees, _Updaters), Params).
content_params(ev_solved(_GoalId, _Requestees, _Solvers,
_Goal, Params, _Solutions), Params).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% name: icl_content_goal(Content, Goal).
% purpose: Extract goal, if any, from an event content; otherwise, FAILS.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
icl_content_goal(ev_solve(_ID, Goal, _Params), Goal).
icl_content_goal(ev_solved(_ID, _R, _S, Goal, _Params, _Solutions), Goal).
icl_content_goal(ev_update_data(_ID, _Mode, Goal, _Params), Goal).
icl_content_goal(
ev_data_updated(_ID, _Mode, Goal, _Params, _Requestees, _Updaters), Goal).
icl_content_goal(
ev_update_trigger(_GoalId,_Mode,_Type,Goal,_Action,_Params),
Goal).
icl_content_goal(
ev_trigger_updated(_GoalId, _Mode, _Type, Goal, _Action,
_Params, _Requestees, _Updaters),
Goal).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% name: icl_content_related_solvable(Content, Solvable).
% purpose: Succeeds if the given event Content is related to a solvable,
% and returns the declaration for the solvable.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
icl_content_related_solvable(Content, Solvable) :-
icl_content_goal(Content, Goal),
( oaa_solvables(Solvables) -> true | otherwise -> Solvables = []),
oaa_goal_matches_solvables(Goal, Solvables, [procedure, data],
_, Solvable).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% name: icl_BuiltIn(+Goal).
% purpose: Test whether an expression is an ICL built-in goal.
% remarks:
% - icl_BuiltIn differs significantly from the Quintus Prolog predicate
% built_in, in that here we do not include basic constructors such
% as ',' and ';'.
% - oaa_Interpret/2 must be defined for every goal for which
% icl_BuiltIn succeeds, or else goal must be callable using call(Goal).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
icl_BuiltIn(true).
icl_BuiltIn(false).
icl_BuiltIn(not(_)).
icl_BuiltIn((_A = _B)).
icl_BuiltIn((_A == _B)).
icl_BuiltIn((_A \== _B)).
icl_BuiltIn((_A =< _B)).
icl_BuiltIn((_A >= _B)).
icl_BuiltIn((_A < _B)).
icl_BuiltIn((_A > _B)).
icl_BuiltIn(var(_)).
icl_BuiltIn(nonvar(_)).
icl_BuiltIn(ground(_)).
icl_BuiltIn(write(_)).
icl_BuiltIn(nl).
icl_BuiltIn(member(_,_)).
icl_BuiltIn(memberchk(_,_)).
icl_BuiltIn(findall(_,_,_)).
icl_BuiltIn(icl_ConsistentParams(_,_)).
icl_BuiltIn(oaa_Solve(_Goal, _Params)).
icl_BuiltIn(oaa_AddData(_Clause, _Params)).
icl_BuiltIn(oaa_RemoveData(_Clause, _Params)).
icl_BuiltIn(oaa_ReplaceData(_Clause1, _Clause2, _Params)).
icl_BuiltIn(oaa_LoadData(_Clauses, _Params)).
icl_BuiltIn(oaa_SaveData(_Clauses, _Params)).
icl_BuiltIn(oaa_AddTrigger(_Type, _Condition, _Action, _Params)).
icl_BuiltIn(oaa_RemoveTrigger(_Type,_Condition,_Action,_Params)).
icl_BuiltIn(oaa_PostEvent(_Contents, _Params)).
icl_BuiltIn(oaa_GetParamValue(_Param, _ParamList)).
icl_BuiltIn(oaa_GetPermValue(_Perm, _PermList)).
icl_BuiltIn(oaa_Interpret(_Goal, _Params)).
icl_BuiltIn(oaa_LibraryVersion(_Version)).
icl_BuiltIn(oaa_Version(_Addr, _Language, _Version)).
icl_BuiltIn(oaa_CanSolve(_Goal, _AddrList)).
icl_BuiltIn(oaa_Ping(_AgentAddr, _TimeLimit, _TotalResponseTime)).
icl_BuiltIn(oaa_Declare(_Solvs,_CPerms,_CParams,_Params,_Declared)).
icl_BuiltIn(oaa_Undeclare(_Solvs,_Params,_UnDeclared)).
icl_BuiltIn(oaa_Redeclare(_Solvable,_NewSolvable,_Params)).
icl_BuiltIn(oaa_Inform(_Type, _Msg,_Params)).
icl_BuiltIn(oaa_PrimaryAddress(_Addr)).
icl_BuiltIn(oaa_PrimaryAddress(_Name)).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% name: icl_BasicGoal(+Goal).
% purpose: Test whether an expression is an ICL basic (non-compound) goal;
% that is, just a functor with 0 or more arguments.
% remarks:
% - Basic goals include built-in's as well as solvables.
% - This is a syntactic test; that is, we're not checking whether the
% Goal is a declared solvable.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
icl_BasicGoal(Goal) :-
var(Goal), !, fail.
icl_BasicGoal(Goal) :-
is_list(Goal), !, fail.
icl_BasicGoal(Goal) :-
icl_compound_goal(Goal), !, fail.
icl_BasicGoal(Goal) :-
icl_BuiltIn(Goal),
!.
icl_BasicGoal(Goal) :-
Goal =.. [Functor | _],
atom(Functor).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% name: icl_compound_goal(+Goal).
% purpose: Test whether an expression is an ICL compound goal.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
icl_compound_goal(_X:_Y).
icl_compound_goal(_X::_Y).
icl_compound_goal((\+ _P)).
icl_compound_goal((_P -> _Q ; _R)).
icl_compound_goal((_P -> _Q)).
icl_compound_goal((_X, _Y)).
icl_compound_goal((_X ; _Y)).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% name: icl_GoalComponents(+ICLGoal, -A, -G, -P).
% icl_GoalComponents(-ICLGoal, +A, +G, +P).
% icl_GoalComponents(+ICLGoal, +A, +G, +P).
% purpose: Assemble, disassemble, or match against the top-level components
% of an ICL goal.
% remarks:
% - The top-level structure of an ICL goal is Address:Goal::Params,
% with Address and Params BOTH OPTIONAL. Thus, every ICL goal
% either explicitly or implicitly includes all three components.
% - This may be used with any ICL goal, basic or compound.
% - When P is missing, its value is returned or matched as []. When A is
% missing, its value is returned or matched as 'unknown'.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The first 4 clauses handled all cases where the ICL Goal is bound;
% the remainder handle those where it is a var.
icl_GoalComponents(A:G::P, Address, Goal, Params) :-
\+ var(A), \+ var(G), \+ var(P),
!,
Address = A, Goal = G, Params = P.
icl_GoalComponents(A:G, Address, Goal, Params) :-
\+ var(A), \+ var(G),
!,
Address = A, Goal = G, Params = [].
icl_GoalComponents(G::P, Address, Goal, Params) :-
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -