1:- module(dictoo, [
    2  oo/1,
    3  oo_bind/2,
    4  is_oo/1,
    5  is_oo_invokable/2,
    6  oo_call/3,
    7  oo_jpl_call/3,
    8  oo_deref/2,
    9  oo_inner_class_begin/1,
   10  oo_inner_class_end/1,
   11  oo_class_field/1,
   12  oo_class_begin/1,
   13  oo_class_end/1]).

dictoo - Dict-like OO Syntax Pack

Author: Douglas R. Miles E-mail: logicmoo@gmail.com WWW: http://www.logicmoo.org Copyright (C): 2017

This program is free software; you can redistribute it and/or modify it.

   27:- use_module(library(gvar_syntax)).   28:- use_module(library(dicts)).   29
   30
   31:- '$set_source_module'('$dicts').   32:- findall(('.'(Dict, Func, Value) :- BODY),clause('.'(Dict, Func, Value),BODY),List),
   33  nb_setval(dictoo, List).   34:- redefine_system_predicate('system':'.'(_Dict, _Func, _Value)).   35:- 'system':abolish('$dicts':'.'/3).   36:- if(\+ current_prolog_flag(dictoo,non_extendable)).   37:- dynamic('$dicts':'.'/3).   38:- multifile('$dicts':'.'/3).   39:- module_transparent('$dicts':'.'/3).   40:- endif.   41:- nb_getval(dictoo,WAS),
   42 compile_aux_clauses([(('$dicts':'.'(Self,Func,Value):- 
   43   dictoo:is_oo_invokable(Self,DeRef),!,dictoo:oo_call(DeRef,Func,Value)))|WAS]).   44:- if(current_prolog_flag(dictoo,non_extendable)).   45:- compile_predicates(['$dicts':'.'(_Dict, _Func, _Value)]).   46:- endif.   47:- system:import('$dicts':'.'/3).   48:- system:lock_predicate('$dicts':'.'/3).   49:- '$set_source_module'(dictoo).   50
   51
   52% :- use_module(library(jpl),[jpl_set/3,jpl_get/3,jpl_call/4]).
   53% :- use_module(atts).
   54% :- use_module(multivar).
   55
   56:- meta_predicate(fail_on_missing(*)).   57% fail_on_missing(G):-current_predicate(G),!,call(G).
   58fail_on_missing(G):- notrace(catch(G,error(existence_error(_,_),_),fail)).
 is_oo_invokable(+Self, -DeRef) is det
DeRef''s an OO Whatnot and ckecks if invokable
   64is_oo_invokable(Was,Ref):- oo_deref(Was,Ref),!,(Was\==Ref;is_oo(Ref)).
 is_oo(+Self) is det
Tests to see if Self Has Member Functions
   72is_oo(O):-  
   73 notrace((((var(O),!,attvar(O));
   74  (((O=was_dictoo(_);
   75     O=jclass(_);
   76     O=jpl(_);
   77     O= class(_);
   78     O='&'(_) ;
   79     O='&'(_,_) ;
   80  fail_on_missing(jpl_is_ref(O));
   81  fail_on_missing(cli_is_object(O));
   82  fail_on_missing(cli_is_struct(O)))))))),!.
   83
   84
   85oo(O):- call(ignore,multivar(O)).
   86oo_bind(O,Value):- oo(O),put_attr(O,oo,binding(O,Value)).
   87oo:attr_unify_hook(B,Value):- B = binding(_Var,Prev),Prev.equals(Value).
   88
   89
   90oo_set(UDT,Key, Value):- attvar(UDT),!,put_attr(UDT,Key, Value).
   91oo_set(UDT,Key, Value):- jpl_set(UDT,Key,Value).
   92
   93
   94put_oo(Key, UDT, Value, NewUDT):- is_dict(UDT),!,put_dict(Key, UDT, Value, NewUDT).
   95put_oo(Key, UDT, Value, NewUDT):- oo_copy_term(UDT,NewUDT),put_oo(NewUDT,Key, Value).
   96
   97oo_copy_term(UDT,NewUDT):- copy_term(UDT,NewUDT).
   98
   99put_oo(Key, UDT, Value):- is_dict(UDT),!,put_dict(Key, UDT, Value).
  100put_oo(Key, UDT, Value):- oo_set(UDT,Key, Value).
  101
  102
  103get_oo(Key, UDT, Value):- oo_call(UDT,Key, Value).
  104
  105oo_jpl_call(A,B,C):- (integer(B);B==length; B= (_-_)),!,jpl_get(A,B,C).
  106oo_jpl_call(A,B,C):- B=..[H|L], fail_on_missing(jpl_call(A,H,L,C)),!.
  107oo_jpl_call(A,B,C):- jpl_get(A,B,C).
  108
  109oo_call(Self,Memb,Value):- notrace((oo_deref(Self,NewSelf)-> NewSelf\=Self)),!,oo_call(NewSelf,Memb,Value).
  110%oo_call(Self,Memb,Value):- is_dict(Self),!, '$dictoo_dot3'(Self, Memb, Value).
  111oo_call('&'(Self),Memb,Value):- !,oo_call(Self,Memb,Value).
  112%oo_call('$'(Self),Memb,Value):- !,gvar_syntax:gvar_interp(Self,Memb,Value).
  113oo_call(jpl(Self),Memb,Value):- !, oo_jpl_call(Self, Memb, Value).
  114oo_call(jclass(Self),Memb,Value):- !, oo_jpl_call(Self, Memb, Value).
  115oo_call(class(Self),Memb,Value):- fail_on_missing(cli_call(Self, Memb, Value)),!.
  116
  117oo_call(Self,Memb,Value):- fail_on_missing(jpl_is_ref(Self)),!,oo_jpl_call(Self, Memb, Value).
  118oo_call(Self,Memb,Value):- notrace((atom(Memb),(get_attr(Self, Memb, Value);var(Self)))),!,freeze(Value,put_oo(Self, Memb, Value)).
  119
  120oo_call(Self,Memb,Value):-  fail_on_missing(cli_is_object(Self)),!,fail_on_missing(cli_call(Self, Memb, Value)).
  121oo_call(Self,Memb,Value):-  fail_on_missing(cli_is_struct(Self)),!,fail_on_missing(cli_call(Self, Memb, Value)).
  122oo_call(Class,Inner,Value):- notrace(is_oo_class(inner(Class,Inner))),!,oo_call(inner(Class,Inner),Value,_).
  123
  124
  125%oo_call(Self,deref,Value):- var(Self),nonvar(Value),!,oo_call(Value,deref,Self).
  126%oo_call(Self,deref,Self):-!.
  127oo_call(Self,Memb,Value):- var(Value),!,freeze(Value,put_oo(Self, Memb, Value)).
  128oo_call(Self,Memb,Var):- var(Var),!,Var='&'(Self,Memb).
  129oo_call(Self,Memb,Value):- throw(oo_call(Self,Memb,Value)).
  130
  131/*
  132oo_call(Self,Memb,Value):- nb_linkval(Self,construct(Self,Memb,Value)),!,oo_call(Self,Memb,Value).
  133oo_call(Self,Memb,Value):- to_member_path(Memb,[F|Path]),append(Path,[Value],PathWValue),
  134   Call =.. [F,Self|PathWValue],
  135   oo_call(Call).
  136
  137to_member_path(C,[F|ARGS]):-compound(C),!,compound_name_args(C,F,ARGS).
  138to_member_path(C,[C]).
  139
  140*/
  141
  142oo_deref(Obj,RObj):- var(Obj),!,once(get_attr(Obj,oo,binding(_,RObj));Obj=RObj),!.
  143%oo_deref('$'(GVar),Value):- atom(GVar),nb_current(GVar,ValueM),!,oo_deref(ValueM,Value).
  144oo_deref('&'(GVar),Value):- atom(GVar),nb_current(GVar,ValueM),!,oo_deref(ValueM,Value).
  145oo_deref(Value,Value):- \+ compound(Value),!.
  146oo_deref(cl_eval(Call),Result):-is_list(Call),!,fail_on_missing(cl_eval(Call,Result)).
  147oo_deref(cl_eval(Call),Result):-!,nonvar(Call),oo_deref(Call,CallE),!,call(CallE,Result).
  148oo_deref(Value,Value):- jpl_is_ref(Value),!.
  149%%oo_deref([A|B],Result):-!, maplist(oo_deref,[A|B],Result).
  150%%oo_deref(Call,Result):- call(Call,Result),!.
  151oo_deref(Head,HeadE):- Head=..B,maplist(oo_deref,B,A),HeadE=..A,!.
  152oo_deref(Value,Value).
  153
  154
  155
  156get_oo(Key, Dict, Value, NewDict, NewDict) :- is_dict(Dict),!,
  157   get_dict(Key, Dict, Value, NewDict, NewDict).
  158get_oo(Key, Dict, Value, NewDict, NewDict) :-
  159        get_oo(Key, Dict, Value),
  160        put_oo(Key, Dict, NewDict, NewDict).
 eval_oo_function(+Func, +Tag, +UDT, -Value)
Test for predefined functions on Objects or evaluate a user-defined function.
  169eval_oo_function(Func, Tag, UDT, Value) :- is_dict(Tag),!,
  170   '$dicts':eval_dict_function(Func, Tag, UDT, Value).
  171
  172eval_oo_function(get(Key), _, UDT, Value) :-
  173    !,
  174    get_oo(Key, UDT, Value).
  175eval_oo_function(put(Key, Value), _, UDT, NewUDT) :-
  176    !,
  177    (   atomic(Key)
  178    ->  put_oo(Key, UDT, Value, NewUDT)
  179    ;   put_oo_path(Key, UDT, Value, NewUDT)
  180    ).
  181eval_oo_function(put(New), _, UDT, NewUDT) :-
  182    !,
  183    put_oo(New, UDT, NewUDT).
  184eval_oo_function(Func, Tag, UDT, Value) :-
  185    call(Tag:Func, UDT, Value).
 put_oo_path(+KeyPath, +UDT, +Value, -NewUDT)
Add/replace a value according to a path definition. Path segments are separated using '/'.
  193put_oo_path(Key, UDT, Value, NewUDT) :-
  194    atom(Key),
  195    !,
  196    put_oo(Key, UDT, Value, NewUDT).
  197put_oo_path(Path, UDT, Value, NewUDT) :-
  198    get_oo_path(Path, UDT, _Old, NewUDT, Value).
  199
  200get_oo_path(Path, _, _, _, _) :-
  201    var(Path),
  202    !,
  203    '$instantiation_error'(Path).
  204get_oo_path(Path/Key, UDT, Old, NewUDT, New) :-
  205    !,
  206    get_oo_path(Path, UDT, OldD, NewUDT, NewD),
  207    (   get_oo(Key, OldD, Old, NewD, New),
  208        is_oo(Old)
  209    ->  true
  210    ;   Old = _{},
  211        put_oo(Key, OldD, New, NewD)
  212    ).
  213get_oo_path(Key, UDT, Old, NewUDT, New) :-
  214    get_oo(Key, UDT, Old, NewUDT, New),
  215    is_oo(Old),
  216    !.
  217get_oo_path(Key, UDT, _{}, NewUDT, New) :-
  218    put_oo(Key, UDT, New, NewUDT).
  219
  220
  221
  222:- dynamic(is_oo_class/1).  223:- dynamic(is_oo_class_field/2).  224
  225oo_class_begin(Name):-asserta(is_oo_class(Name)).
  226oo_class_end(Name):- is_oo_class(Name),!,retract(is_oo_class(Name)),assertz(is_oo_class(Name)).
  227
  228oo_inner_class(Name,Inner):-asserta(is_oo_class(inner(Name,Inner))).
  229
  230oo_inner_class_begin(Inner):- is_oo_class(Name),!,oo_class_begin(inner(Name,Inner)).
  231oo_inner_class_end(Inner):- is_oo_class(inner(Name,Inner)),!,oo_class_end(inner(Name,Inner)).
  232
  233oo_class_field(Inner):- is_oo_class(Name),!,asserta(is_oo_class_field(Name,Inner))