1% ===================================================================
    2% File 'logicmoo_module_aiml_gmidx.pl'
    3% Purpose: An Implementation in SWI-Prolog of AIML
    4% Maintainer: Douglas Miles
    5% Contact: $Author: dmiles $@users.sourceforge.net ;
    6% Version: 'logicmoo_module_aiml_gmidx.pl' 1.0.0
    7% Revision:  $Revision: 1.7 $
    8% Revised At:   $Date: 2002/07/11 21:57:28 $
    9% ===================================================================
   10
   11
   12%:-module()
   13%:-include('logicmoo_utils_header.pl'). %<?
   14%:- style_check(-singleton).
   15%%:- style_check(-discontiguous).
   16/*
   17:- if((current_prolog_flag(version,MMmmPP),MMmmPP<70000)).
   18:- style_check(-atom).
   19:- style_check(-string).
   20:- endif.
   21*/

   22
   23:-ensure_loaded(library('programk/logicmoo_module_aiml_graphmaster.pl')).
   24:-ensure_loaded(library('programk/logicmoo_module_aiml_convertor.pl')).
   25:-ensure_loaded(library('programk/logicmoo_module_aiml_cxt_path.pl')).
   26
   27
   28% ===============================================================================================
   29% ===============================================================================================
   30%%:-abolish(dict/3).
   31
   32:-retractall(dict(_,_,_)).
   33
   34:-pp_listing(dict(_,_,_)).
   35
   36
   37useNewCateSigSearch_broken_now:-fail.
   38useIndexPatternsForCateSearch:-true.
   39useCateID:-true.
   40dontAssertIndex:-fail.  % true adds 5 test failures! saves 7mb
   41
   42
   43innerTagPriority(cateid,[template,postpattern]):-useCateID.
   44innerTagPriority(graph,[topic,prepattern]).
   45innerTagPriority(precall,[that,prepattern]).
   46innerTagPriority(topic,[topic,prepattern]).
   47innerTagPriority(that,[that,prepattern]).
   48innerTagPriority(request,[that,prepattern]).
   49innerTagPriority(response,[that,prepattern]).
   50innerTagPriority(pattern,[pattern]).
   51innerTagPriority(flags,[pattern,prepattern]).
   52innerTagPriority(call,[that,postpattern]).
   53innerTagPriority(guard,[that,postpattern]).
   54innerTagPriority(userdict,[template,postpattern]).
   55innerTagPriority(template,[template,postpattern]).
   56
   57
   58
   59
   60% ===================================================================
   61%  aimlCate database decl
   62% ===================================================================
   63
   64:-dynamic(aimlCateSigCached/1).
   65aimlCateSig(X):-aimlCateSigCached(X),!.
   66aimlCateSig(X):-aimlCateOrder(List),length(List,L),functor(Pred,aimlCate,L),asserta(aimlCateSigCached(Pred)),!,copy_term(Pred,X).
   67
   68aimlCateOrder([graph,precall,topic,that,request,pattern,flags,call,guard,userdict,template,srcinfo,srcfile,cateid]):-useCateID,!.
   69aimlCateOrder([graph,precall,topic,that,request,pattern,flags,call,guard,userdict,template,srcinfo,srcfile]):-not(useCateID),!.
   70
   71
   72oneOrList([ID],ID):-!.
   73oneOrList([],_ID):-!,fail.
   74oneOrList(IDs,IDs).
   75
   76oneOrListEach([],_IDs):-!,fail.
   77oneOrListEach(ID,ID):-atomic(ID),!.
   78oneOrListEach(IDs,ID):-member(ID,IDs).
   79
   80%%textToMatchPattern([Text],MatchPattern):-textPred(Text,Pred),!,member(MatchPattern,[Pred,*,'_']).
   81textToMatchPattern([Text,_P|_Attern],MatchPattern):-textPred(Text,Pred),functor(MatchPattern,Pred,1).
   82textToMatchPattern([_Text|Pattern],MatchPattern):-member(T,Pattern),textPred(T,Pred),functor(MatchPattern,Pred,2).
   83textToMatchPattern([_|_TextPattern],'*').
   84textToMatchPattern(_TextPattern,'_').
   85textToMatchPattern([_|_TextPattern],'^').
   86textToMatchPattern(_TextPattern,'#').
   87%%textToMatchPattern(_TextPattern,_).
   88
   89aimlPattern2CateID(Name,Text,IDO):-
   90   argNumsIndexedRepr(aimlCate,Name,N,textInput),
   91   aimlCateSig(CateSig), arg(N,CateSig,Pattern),arg(14,CateSig,ID),!,   
   92   findall(ID,(textToMatchPattern(Text,Pattern),CateSig),IDs),oneOrList(IDs,IDO).
   93
   94aimlCate2ID(Name,Pattern,IDO):-argNumsIndexedRepr(aimlCate,Name,N,textInput),!,argNFound(aimlCate,Name,_S,Pattern),aimlCateSig(CateSig),arg(N,CateSig,Pattern),arg(14,CateSig,ID),findall(ID,CateSig,IDs),oneOrList(IDs,IDO).
   95aimlCate4ID(Name,IDs,Result):-aimlCateSig(CateSig),argNumsIndexedRepr(aimlCate,Name,N,_IndexType),arg(N,CateSig,Result),oneOrListEach(IDs,ID),arg(14,CateSig,ID),CateSig.
   96
   97% textPatternToMatchPattern([suggest,a,topic],Pattern),aimlCate2ID(pattern,Pattern,IDO),aimlCate4ID(template,IDO,Result).
   98
   99% [graph,precall,topic,that,pattern,flags,call,guard,template,userdict]
  100cateMemberTags(Result):- aimlCateOrder(List), findall(E,(member(E0,List),once((E0=[E|_];E0=E))), Result).
  101
  102makeAimlCateSig(Ctx,ListOfValues,Pred):-aimlCateSig(Pred),!,makeAimlCate(Ctx,ListOfValues,Pred),!.
  103
  104:- aimlCateOrder(List),length(List,L),dynamic(aimlCate/L),multifile(aimlCate/L).
  105
  106replaceArgsVar(_Ctx,[],_CateSig):-!.
  107replaceArgsVar(Ctx,[E=Replacement|L],CateSig):-
  108    getCategoryArg1(Ctx,E,_Old,StarNumber,CateSig),
  109    nb_setarg(StarNumber,CateSig,Replacement),
  110    replaceArgsVar(Ctx,L,CateSig),!.
  111
  112% ===============================================================================================
  113%  Indexing of Categories
  114% ===============================================================================================
  115
  116:-dynamic(argNumsTracked/3).
  117:-dynamic(argNFound/4).
  118:-multifile(argNFound/4).
  119
  120
  121argTypeIndexable(textInput).
  122%%argTypeIndexable(name).
  123
  124argNumsIndexedRepr(aimlCate,topic,3,textInput).
  125argNumsIndexedRepr(aimlCate,that,4,textInput).
  126argNumsIndexedRepr(aimlCate,pattern,6,textInput).
  127
  128argNumsIndexedRepr(aimlCate,graph,1,name).
  129argNumsIndexedRepr(aimlCate,precall,2,callable).
  130argNumsIndexedRepr(aimlCate,request,5,flags).
  131argNumsIndexedRepr(aimlCate,flags,7,flags).
  132argNumsIndexedRepr(aimlCate,call,8,callable).
  133argNumsIndexedRepr(aimlCate,guard,9,callable).
  134argNumsIndexedRepr(aimlCate,userdict,10,name).
  135argNumsIndexedRepr(aimlCate,template,11,textOutput).
  136argNumsIndexedRepr(aimlCate,srcinfo,12,any).
  137argNumsIndexedRepr(aimlCate,srcfile,13,any).
  138argNumsIndexedRepr(aimlCate,cateid,14,name):-useCateID.
  139
  140aimlCateSigArg(That,Aiml,Arg):-aimlCateSig(Aiml),argNumsIndexedRepr(aimlCate,That,N,_),arg(N,Aiml,Arg).
  141aimlCateArg(That,Aiml,Arg):-aimlCateSigArg(That,Aiml,Arg),call(Aiml).
  142
  143%%graph,precall,topic,that,request,pattern,flags,call,guard,userdict,template,srcinfo,srcfile
  144
  145argNumsTracked(Pred,ArgName,Position):-argNumsIndexedRepr(Pred,ArgName,Position,ArgType),argTypeIndexable(ArgType).
  146
  147argNFound(F,A,'_','_'):-argNumsIndexedRepr(F,A,_,textInput).
  148argNFound(F,A,*,*):-argNumsIndexedRepr(F,A,_,textInput).
  149argNFound(F,A,List,Index):- dontAssertIndex, argNumsIndexedRepr(F,A,Num,textInput),aimlCateSig(Call),arg(Num,Call,Index),!,call(Call),
  150    not(member(Index,[*,'_'])),fromIndexableSArg(Index,List).
  151
  152
  153%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5555
  154%% aimlCateOrder([graph,precall,topic,that,request,pattern,flags,call,guard,userdict,template,srcinfo,srcfile]).
  155%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5555
  156
  157makeBanner(Name):-
  158   'format'(':-dynamic(cid_~w/2).~n',[Name]),
  159   'format'(':-discontiguous(cid_~w/2).~n',[Name]),
  160   'format'(':-multifile(cid_~w/2).~n',[Name]),
  161   'format'(':-indexed(cid_~w(1,1)).~n',[Name]).
  162
  163makeAccesors:-aimlCateOrder(List),member(Name,List),aimlCateSig(CateSig),
  164   once((argNumsIndexedRepr(aimlCate,Name,Num,_ADef),arg(Num,CateSig,V),arg(14,CateSig,ID),cateFallback(Name,Def),ID='ID',V='V')),
  165   makeBanner(Name),
  166   'format'('cateid_~w(ID,V):- cid_~w(ID,V),!.~n',[Name,Name]),
  167   'format'('cateid_~w(ID,V):- cateid_~w0(ID,V),!.~n',[Name,Name]),
  168   ignore( (arg(N,CateSig,Old),var(Old),nb_setarg(N,CateSig,'_'),fail ) ),
  169   'format'('cateid_~w(_,~q).~n',[Name,Def]),
  170   'format'('cateid_~w0(~w,~w):- ~w.~n',[Name,ID,V,CateSig]),
  171   'format'('~n~n',[]).
  172
  173makePreloaders:-aimlCateSig(CateSig),aimlCateOrder(List),member(Name,List),
  174   once((argNumsIndexedRepr(aimlCate,Name,Num,_ADef),arg(Num,CateSig,V),arg(14,CateSig,ID),cateFallback(Name,Def),ID='ID',V='V')),   
  175   'format'('makeBanner(~w),cateid_~w0(ID,V),V \\== (~w),\'format\'(\'~~q.~~n\',[cid_~w(ID,V)]),fail.',[Name,Name,Def,Name]).
  176
  177dumpCates:-aimlCateSig(CateSig),aimlCateOrder(Order),CateSig,
  178    %%aimlCate(Graph,Precall,Topic,That,Request,Pattern,Flags,Call,Guard,Userdict,Template,Srcinfo,Srcfile,ID),
  179    arg(14,CateSig,ID),
  180    CateSig=..[_|List],
  181    dumpSigPairs(ID,Order,List),
  182    'format'('~n',[]).
  183
  184aimlCateFormed(Graph,Precall,Topic,That,Request,Pattern,Flags,Call,Guard,Userdict,Template,Srcinfo,Srcfile,ID):- cid_template(ID,Template),
  185  cateid_graph(ID,Graph),cateid_precall(ID,Precall),cateid_topic(ID,Topic),cateid_that(ID,That),cateid_request(ID,Request),cateid_pattern(ID,Pattern),cateid_flags(ID,Flags),
  186  cateid_call(ID,Call),cateid_guard(ID,Guard),cateid_userdict(ID,Userdict),cateid_srcinfo(ID,Srcinfo),cateid_srcfile(ID,Srcfile).
  187
  188aimlCate(Graph,Precall,Topic,That,Request,Pattern,Flags,Call,Guard,Userdict,Template,Srcinfo,Srcfile,ID):-
  189   aimlCateFormed(Graph,Precall,Topic,That,Request,Pattern,Flags,Call,Guard,Userdict,Template,Srcinfo,Srcfile,ID).
  190
  191dumpSigPairs(_ID,[],_List):-!.
  192dumpSigPairs(ID,[O|Order],[L|List]):-dumpSPairs(ID,O,L),dumpSigPairs(ID,Order,List).
  193
  194dumpSPairs(_ID,Name,Def):-cateFallback(Name,Def),!.
  195dumpSPairs(_ID,cateid,_):-!.
  196dumpSPairs(ID,srcfile,File:Line-POS):- Term=..[File,Line,POS],!,dumpSPairs(ID,srcfile,Term).
  197dumpSPairs(ID,Name,[Value]):-!,dumpSPairs(ID,Name,Value).
  198dumpSPairs(ID,Name,Value):-'format'('cid_~w(~q,~q).~n',[Name,ID,Value]).
  199
  200redumpFormed:-
  201   aimlCateFormed(Graph,Precall,Topic,That,Request,Pattern,Flags,Call,Guard,Userdict,Template,Srcinfo,Srcfile,ID),
  202   'format'('~q.~n',[aimlCate(Graph,Precall,Topic,That,Request,Pattern,Flags,Call,Guard,Userdict,Template,Srcinfo,Srcfile,ID)]),fail.
  203redumpFormed.
  204
  205:-dynamic(cid_graph/2).
  206:-multifile(cid_graph/2).
  207cateid_graph(ID,V):- cid_graph(ID,V),!.
  208%cateid_graph(ID,V):- cateid_graph0(ID,V),!.
  209cateid_graph(_,default).
  210cateid_graph0(ID,V):- aimlCate(V,_G951,_G952,_G953,_G954,_G955,_G956,_G957,_G958,_G959,_G960,_G961,_G962,ID).
  211
  212
  213
  214:-dynamic(cid_precall/2).
  215:-multifile(cid_precall/2).
  216cateid_precall(ID,V):- cid_precall(ID,V),!.
  217%cateid_precall(ID,V):- cateid_precall0(ID,V),!.
  218cateid_precall(_,true).
  219cateid_precall0(ID,V):- aimlCate(_G950,V,_G952,_G953,_G954,_G955,_G956,_G957,_G958,_G959,_G960,_G961,_G962,ID).
  220
  221
  222
  223:-dynamic(cid_topic/2).
  224:-multifile(cid_topic/2).
  225cateid_topic(ID,V):- cid_topic(ID,V),!.
  226%cateid_topic(ID,V):- cateid_topic0(ID,V),!.
  227cateid_topic(_,*).
  228cateid_topic0(ID,V):- aimlCate(_G950,_G951,V,_G953,_G954,_G955,_G956,_G957,_G958,_G959,_G960,_G961,_G962,ID).
  229
  230
  231
  232:-dynamic(cid_that/2).
  233:-multifile(cid_that/2).
  234cateid_that(ID,V):- cid_that(ID,V),!.
  235%cateid_that(ID,V):- cateid_that0(ID,V),!.
  236cateid_that(_,*).
  237cateid_that0(ID,V):- aimlCate(_G950,_G951,_G952,V,_G954,_G955,_G956,_G957,_G958,_G959,_G960,_G961,_G962,ID).
  238
  239
  240
  241:-dynamic(cid_request/2).
  242:-multifile(cid_request/2).
  243cateid_request(ID,V):- cid_request(ID,V),!.
  244%cateid_request(ID,V):- cateid_request0(ID,V),!.
  245cateid_request(_,*).
  246cateid_request0(ID,V):- aimlCate(_G950,_G951,_G952,_G953,V,_G955,_G956,_G957,_G958,_G959,_G960,_G961,_G962,ID).
  247
  248
  249
  250:-dynamic(cid_pattern/2).
  251:-multifile(cid_pattern/2).
  252cateid_pattern(ID,V):- cid_pattern(ID,V),!.
  253%cateid_pattern(ID,V):- cateid_pattern0(ID,V),!.
  254cateid_pattern(_,*).
  255cateid_pattern0(ID,V):- aimlCate(_G950,_G951,_G952,_G953,_G954,V,_G956,_G957,_G958,_G959,_G960,_G961,_G962,ID).
  256
  257
  258
  259:-dynamic(cid_flags/2).
  260:-multifile(cid_flags/2).
  261cateid_flags(ID,V):- cid_flags(ID,V),!.
  262%cateid_flags(ID,V):- cateid_flags0(ID,V),!.
  263cateid_flags(_,*).
  264cateid_flags0(ID,V):- aimlCate(_G950,_G951,_G952,_G953,_G954,_G955,V,_G957,_G958,_G959,_G960,_G961,_G962,ID).
  265
  266
  267
  268:-dynamic(cid_call/2).
  269:-multifile(cid_call/2).
  270cateid_call(ID,V):- cid_call(ID,V),!.
  271%cateid_call(ID,V):- cateid_call0(ID,V),!.
  272cateid_call(_,true).
  273cateid_call0(ID,V):- aimlCate(_G950,_G951,_G952,_G953,_G954,_G955,_G956,V,_G958,_G959,_G960,_G961,_G962,ID).
  274
  275
  276
  277:-dynamic(cid_guard/2).
  278:-multifile(cid_guard/2).
  279cateid_guard(ID,V):- cid_guard(ID,V),!.
  280%cateid_guard(ID,V):- cateid_guard0(ID,V),!.
  281cateid_guard(_,true).
  282cateid_guard0(ID,V):- aimlCate(_G950,_G951,_G952,_G953,_G954,_G955,_G956,_G957,V,_G959,_G960,_G961,_G962,ID).
  283
  284
  285
  286:-dynamic(cid_userdict/2).
  287:-multifile(cid_userdict/2).
  288cateid_userdict(ID,V):- cid_userdict(ID,V),!.
  289%cateid_userdict(ID,V):- cateid_userdict0(ID,V),!.
  290cateid_userdict(_,user).
  291cateid_userdict0(ID,V):- aimlCate(_G950,_G951,_G952,_G953,_G954,_G955,_G956,_G957,_G958,V,_G960,_G961,_G962,ID).
  292
  293
  294
  295:-dynamic(cid_template/2).
  296:-multifile(cid_template/2).
  297cateid_template(ID,V):- cid_template(ID,V),!.
  298%cateid_template(ID,V):- cateid_template0(ID,V),!.
  299cateid_template(_,[]).
  300cateid_template0(ID,V):- aimlCate(_G950,_G951,_G952,_G953,_G954,_G955,_G956,_G957,_G958,_G959,V,_G961,_G962,ID).
  301
  302
  303
  304:-dynamic(cid_srcinfo/2).
  305:-multifile(cid_srcinfo/2).
  306cateid_srcinfo(ID,V):- cid_srcinfo(ID,V),!.
  307%cateid_srcinfo(ID,V):- cateid_srcinfo0(ID,V),!.
  308cateid_srcinfo(_,missinginfo).
  309cateid_srcinfo0(ID,V):- aimlCate(_G950,_G951,_G952,_G953,_G954,_G955,_G956,_G957,_G958,_G959,_G960,V,_G962,ID).
  310
  311
  312
  313:-dynamic(cid_srcfile/2).
  314:-multifile(cid_srcfile/2).
  315cateid_srcfile(ID,V):- cid_srcfile(ID,V),!.
  316%cateid_srcfile(ID,V):- cateid_srcfile0(ID,V),!.
  317cateid_srcfile(_,missingfile).
  318cateid_srcfile0(ID,V):- aimlCate(_G950,_G951,_G952,_G953,_G954,_G955,_G956,_G957,_G958,_G959,_G960,_G961,V,ID).
  319
  320
  321
  322
  323%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5555
  324%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5555
  325
  326
  327assert_cate_in_load(NEW) :- currentContext(assert_cate_in_load,Ctx),prolog_must(assert_cate_in_load(Ctx,NEW)),!.
  328
  329assert_cate_in_load(Ctx,CateSig):-
  330    duplicate_term(CateSig,CateSigTest),
  331    load_category(Ctx,CateSigTest),!,
  332    ignore((
  333    functor(CateSigTest,_,A),
  334    arg(A,CateSigTest,E),arule107=E,%break,
  335    write(E),write('.'))).
  336    
  337
  338%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  339%% load_category(Ctx,CateSig)
  340%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  341assertaFront(argNFound(_,_,_,_)):-dontAssertIndex,!.
  342assertaFront(Indexable):-tryCatchIgnore(retractall(Indexable)),asserta(Indexable),!.
  343
  344load_category(Ctx,CateSig):-
  345      isRetraction(Ctx,CateSig,RemovemeMask),!,
  346      withArgIndexing(RemovemeMask,dirtyArgIndex,Removeme),
  347      immediateCall(Ctx,findall(Removeme,retract_cate_post_index(Removeme),Retracted)),!,
  348      findall(Removeme,retract_cate_post_index(Removeme),Retracted),!.
  349
  350load_category(Ctx,CateSig):-
  351      withArgIndexing(CateSig,addArgIndex,Indexable),
  352      assertaFront(Indexable),!,
  353      traceIf(((not(not(Indexable==CateSig))),not(arg(6,CateSig,*)))),!,
  354      immediateCall(Ctx,assert_cate_post_index(Indexable)),!,
  355      confirm_args_indexed(Indexable).
  356
  357%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  358%% assert_cate_post_index(Indexable)
  359%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  360assert_cate_post_index(Indexable):-assertaFront(Indexable),confirm_args_indexed(Indexable),!,immediateCall(_Ctx,assert_cate_post_index(Indexable)).
  361
  362%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  363%% retract_cate_post_index(Indexable)
  364%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  365retract_cate_post_index(Removeme):-
  366   immediateCall(_Ctx,retract_cate_post_index(Removeme)),!,
  367   %%withArgIndexing(Retract,dirtyArgIndex,Removeme),
  368   %%debugFmt(retract_cate_post_index(Removeme)),!,
  369   prolog_must(ignore(retract(Removeme))),!.
  370
  371
  372%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  373%% confirm_args_indexed(Indexable)
  374%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  375
  376confirm_args_indexed(_Indexable):-dontAssertIndex,!.
  377confirm_args_indexed(Indexable):-functor(Indexable,F,_),
  378      argNumsIndexedRepr(F,ArgName,N,ArgType),argTypeIndexable(ArgType),
  379      arg(N,Indexable,Value),
  380      confirm_1arg_indexed(F,ArgName,Value),
  381      fail.
  382confirm_args_indexed(_Indexable). %%confirmed
  383
  384confirm_1arg_indexed(F,ArgName,Value):-argNFound(F,ArgName,_,Value),!. 
  385confirm_1arg_indexed(F,ArgName,Value):-fromIndexableSArg(Value,SList),debugFmt(not(argNFound(F,ArgName,SList,Value))),!. 
  387%%%
  388ffffffff.
  389noTrickyIndexing:-fail.
  390
  391toNonIndexable(FAKE,FAKE):-noTrickyIndexing,!.
  392toNonIndexable(OF,INDEXABLE):-OF=..[F|ARGS],functor(OF,F,A),toNonIndexable0(A,F,ARGS,NEWARGS),!,INDEXABLE=..[F|NEWARGS].
  393toNonIndexable0(0,_F,_,[]):-!.
  394toNonIndexable0(3,aimlCate,List,List):-!.
  395toNonIndexable0(N,F,[A|ARGS],[NEW|NEWARGS]):-N2 is N-1, toNonIndexableArg(A,NEW),toNonIndexable0(N2,F,ARGS,NEWARGS).
  396
  397toNonIndexableArg(A,A):-var(A),!.
  398
  399toNonIndexableArg(A,A):-(member(A,['*','[]','_',true]);atomic(A)),!.
  400toNonIndexableArg([A|H],[A|H]):-!.
  401toNonIndexableArg(A,A):-not(compound(A)),!.
  402toNonIndexableArg(A,[A]):-not(compound(A)),!.
  403toNonIndexableArg(A,B):-fromIndexableSArg(B,A),!.
  404toNonIndexableArg(A,[A0]):- trace,A=..[A0/*,idx0*/].
  405toNonIndexableArg(A,[A0|AN]):-A=..[A0,idx|AN].
  406toNonIndexableArg(A,[AA|AL]):-A=..[A0,idxl,AN|AL],AA=..[A0|AN].
  407toNonIndexableArg(A,[A]).
  408
  409/*
  410
  411 [*] ==> *
  412 [he] ==> he
  413
  414 [*,likes,*] ==>  likes(idxm,*)
  415 [*,likes,it] ==>  likes(idxm,it)
  416 [he,likes,it] ==>  he(likes,it)
  417 ['DO', 'THE', 'GENDER', 'TEST'] => do(idx, the, gender, test)).
  418
  419
  420 [*,Word|More]  => Word(idxm,REST)
  421
  422 [i,like,birds] ===> i(_), like(_,_), birds(_,_,_), star_star(_,_), '*'
  423
  424 [i,like,many,birds] ===> i(_), like(_,_), many(_,_), birds(_,_,_), star_star(_,_), '*'
  425
  426*/

  427
  428notStarCard(X):- \+ isStarCard(X), prolog_must(atom(X)).
  429
  430is1Star(X):-isStar0(X).
  431is1Star(element(_,_,_)).
  432is1Star(star(_,_,_)).
  433
  434mustCardSpec(X):-prolog_must(is1Star(X)),!.
  435
  436isStarCard(X):-var(X),!,aiml_error(isStarCard(X)).
  437isStarCard(X):-is1Star(X).
  438isStarCard([_|_]):-!,fail.
  439isStarCard(X):- functor(X,F,_A),member(F,[star1,star_star]).
  440
  441
  442fromIndexableSArg0(I,[var(I)]):-var(I),!.
  443%%fromIndexableSArg0(I,S):-argNFound(_,_,S,I),!.
  444fromIndexableSArg0([A|B],ABS):-!,fromIndexableSArg0(A,AS),fromIndexableSArg0(B,BS),append(AS,BS,ABS),!.
  445fromIndexableSArg0([],[]):-!.
  446fromIndexableSArg0(I,[I]):-atomic(I),!.
  447fromIndexableSArg0(element(E,A,B),[element(E,A,B)]):-!.
  448fromIndexableSArg0(idx_startswith(E),[E]):-!.
  449fromIndexableSArg0(star_star(_Len,List),ABS):-fromIndexableSArg0(List,ABS).
  450fromIndexableSArg0(I,ABS):-I=..[A,B],!,fromIndexableSArg0(A,AS),fromIndexableSArg0(B,BS),append(AS,BS,ABS).
  451fromIndexableSArg0(I,ABS):-I=..[B,idx_endswith,A,_],!,fromIndexableSArg0(A,AS),fromIndexableSArg0(B,BS),append(AS,BS,ABS).
  452fromIndexableSArg0(I,ABCS):-I=..[B,A,C],!,fromIndexableSArg0(A,AS),fromIndexableSArg0(B,BS),fromIndexableSArg0(C,CS),append(AS,BS,ABS),append(ABS,CS,ABCS),!.
  453fromIndexableSArg0(OTHER,[OTHER]):-debugFmt(fromIndexableSArg0(OTHER)).
  454
  455fromIndexableSArg(B,A):-dontAssertIndex,!,prolog_must(fromIndexableSArg0(B,A)),!.
  456fromIndexableSArg(B,A):-prolog_must(fromIndexableSArg0(B,A)),!.
  457fromIndexableSArg(B,A):-nonvar(B),isStarCard(B),!,prolog_must(desegmentStars(B,A)).
  458fromIndexableSArg(B,A):-prolog_must(toIndexableSArg(A,B)),!.
  459
  460
  461reSegmentStars(Star,StarStarO):-desegmentStars(Star,Seg),segmentStar(Seg,StarStar),
  462    (StarStar=Star->StarStarO=Star;StarStarO=StarStar),!.
  463
  464segmentStar([Star],Star):-is1Star(Star),!.
  465segmentStar([Star],star1(Star)):-mustCardSpec(Star),!.
  466segmentStar([Star|SegS],star_star(Len,[Star|SegS])):-length([Star|SegS],Len),mustCardSpec(Star).
  467
  468desegmentStars([Star|SegS],[Star|SegS]):-mustCardSpec(Star).
  469desegmentStars(star_star(_,B),A):-!,desegmentStars(B,A).
  470desegmentStars(star1(B),A):-!,desegmentStars(B,A).
  471desegmentStars(Star,[Star]):-mustCardSpec(Star).
  472
  473mergeStars(Star1,Star2,star_star(Len,StarStar)):-desegmentStars(Star1,Seg1),desegmentStars(Star2,Seg2),append(Seg1,Seg2,StarStar),length(StarStar,Len).
  474
  475% star1/1
  476toIndexableSArg([Star],StarStar):-isStarCard(Star),!,toIndexableSArg(Star,StarStar).
  477% star1/1 and(/)  star_star/2
  478toIndexableSArg(Star,StarStar):-isStarCard(Star),!,prolog_must(reSegmentStars(Star,StarStar)).
  479
  480toIndexableSArg(Star,Star):-atomic(Star),!.
  481
  482% word/0
  483toIndexableSArg([Word],Word):-!.
  484toIndexableSArg([Star],StarStar):-toIndexableSArg(Star,StarStar).
  485
  486% word/3
  487toIndexableSArg([Star,Word|[]],INDEXABLE):-isStarCard(Star),notStarCard(Word),INDEXABLE=..[Word,idx_endswith,(Star),Word].
  488% star_star/2 implicit
  489toIndexableSArg([Star1,Star2|[]],StarStar):-isStarCard(Star1),isStarCard(Star2),mergeStars(Star1,Star2,StarStar).
  490% word/1
  491toIndexableSArg([Word,Star|[]],INDEXABLE):-isStarCard(Star),notStarCard(Word),INDEXABLE=..[Word,StarStar],reSegmentStars(Star,StarStar).
  492% word/1 implicit
  493%toIndexableSArg([Word1,Word2|[]],INDEXABLE):-notStarCard(Word1),notStarCard(Word2),INDEXABLE=..[Word1,Word2].
  494
  495% word/2
  496toIndexableSArg([Star,Word|More],INDEXABLE):-isStarCard(Star),notStarCard(Word),INDEXABLE=..[Word,idx_startswith(Star),REST],toIndexableArg(More,REST).
  497% Unk/N
  498toIndexableSArg([W,Star1,Star2|More],INDEXABLE):-isStarCard(Star1),isStarCard(Star2),mergeStars(Star1,Star2,StarStar),toIndexableArg([W,StarStar|More],INDEXABLE).
  499% Unk/N
  500toIndexableSArg([Star1,Star2|More],INDEXABLE):-isStarCard(Star1),isStarCard(Star2),mergeStars(Star1,Star2,StarStar),toIndexableArg([StarStar|More],INDEXABLE).
  501% word/1
  502toIndexableSArg([Word,Star|More],INDEXABLE):-isStarCard(Star),notStarCard(Word),INDEXABLE=..[Word,REST],toIndexableArg([Star|More],REST).
  503% word/1
  504toIndexableSArg([Word1,Word2|More],INDEXABLE):-notStarCard(Word1),notStarCard(Word2),INDEXABLE=..[Word1,REST],!,toIndexableArg([Word2|More],REST).
  505
  506
  507
  508%%toIndexable(FAKE,FAKE):-!.
  509
  510toIndexable(OF,INDEXABLE):-OF=..[F|ARGS],functor(OF,F,A),toIndexable0(A,F,ARGS,NEWARGS),!,INDEXABLE=..[F|NEWARGS].
  511toIndexable0(0,_F,_,[]):-!.
  512toIndexable0(3,aimlCate,List,List):-!.
  513toIndexable0(N,F,[A|ARGS],[NEW|NEWARGS]):-N2 is N-1, makeIndexableArg(F,N,A,NEW),!,toIndexable0(N2,F,ARGS,NEWARGS).
  514
  515
  516makeIndexableArg(_,_,A,A):-noTrickyIndexing,!.  %%TODO: REMOVE THIS DISABLER
  517makeIndexableArg(F,ArgNumber,A,AHL):-argNumsIndexedRepr(F,_Pattern,ArgNumber,ArgType),makeIndexableArg(F,ArgType,A,AHL).
  518makeIndexableArg(F,ArgType,A,AHL):-argNumsIndexedRepr(F,Pattern,_,ArgType),makeIndexableArg(Pattern,ArgType,A,AHL).
  519makeIndexableArg(Pattern,ArgType,A,AH):-argNumsIndexedRepr(_F,Pattern,_ArgNumber,ArgType),argTypeIndexable(ArgType),toIndexableArg(Pattern,ArgType,A,AH).
  520makeIndexableArg(_,_,A,A).
  521
  522toIndexableArg(_,_,B,AHL):-toIndexableArg(B,A),!,!,prolog_must(toLowercase(A,AHL)).
  523
  524toIndexableArg(A,A):- noTrickyIndexing,!.  %%TODO: REMOVE THIS DISABLER
  525toIndexableArg(A,A):-var(A),!.
  526toIndexableArg([S|H],AH):- string(S),maplist(any_to_atom,[S|H],[C|CT]),!,toIndexableArg([C|CT],AH).
  527toIndexableArg(S,AH):- string(S),atom_string(A,S),!,toIndexableArg(A,AH).
  528toIndexableArg(A,AH):-is_list(A),removeSkippables(A,AL),A\==AL,!,toIndexableArg(AL,AH).
  529toIndexableArg(A,A):-member(A,['*','[]','_']),!.
  530toIndexableArg([A],AA):-not(compound(A)),!,toIndexableArg(A,AA).
  531toIndexableArg(A,A):-not(compound(A)),!.
  532toIndexableArg([A],AH):- atom(A),!,AH=..[A/*,idx0*/],!.
  533toIndexableArg([A],N):-toIndexableArg(A,N).
  534toIndexableArg(B,A):-prolog_must(toIndexableSArg(B,A)),!.
  535toIndexableArg([S|H],AH):- string(S),atom_string(A,S),AH=..[A,idx|H],!.
  536toIndexableArg([A|H],AH):- trace,atom(A),AH=..[A,idx|H],!.
  537toIndexableArg([A|H],AH):- A=..[A0|AN],predify(A,AH,A0,AN,H),!.
  538toIndexableArg(A,A).
  539
  540predify(_A,AH,A0,AN,H):-predify(AH,A0,AN,H).
  541predify(A,[A|H],_A0,_AN,H).
  542
  543predify(AH,A0,[],H):-AH=..[A0,idx|H].
  544predify(AH,A0,H,[]):-AH=..[A0,idx|H].
  545predify(AH,A0,AN,H):-AH=..[A0,idxl,AN|H].
  546
  547%%%%%%%%%%%%%%%%%%%
  548%%withArgIndexing(+CateSig,+DoWhat,-Indexable):-!.
  549%%%%%%%%%%%%%%%%%%5
  550%%withArgIndexing(CateSig,DoWhat):-prolog_must(withArgIndexing(CateSig,DoWhat,_Indexable)).
  551
  552withArgIndexing(CateSig,_DoWhat,Indexable):- \+ useIndexPatternsForCateSearch,!,duplicate_term(CateSig,Indexable).
  553withArgIndexing(CateSig,DoWhat,Indexable):-
  554  functor(CateSig,F,A),
  555  prolog_must(var(Indexable)),
  556  functor(Indexable,F,A),
  557  duplicate_term(CateSig,Indexable),
  558  prolog_must(withArgIndexing4(CateSig,F,DoWhat,Indexable)),!.
  559
  560withArgIndexing4(CateSig,Functor,DoWhat,Indexable):- argNumsTracked(Functor,ArgName,ArgNumber),
  561  argNumsIndexedRepr(Functor,ArgName,ArgNumber,ArgType),
  562  once((arg(ArgNumber,CateSig,Arg),
  563         once((
  564          % call/9 is missing?  call(DoWhat,CateSig,Indexable,Functor,ArgName,ArgNumber,Arg,IndexableArg,ArgType),
  565          apply(DoWhat,[CateSig,Indexable,Functor,ArgName,ArgNumber,Arg,IndexableArg,ArgType]),
  566              nb_setarg(ArgNumber,Indexable,IndexableArg))))),fail.
  567
  568withArgIndexing4(_CateSig,_F,_DoWhat,_Indexable).
  569
  570staredArgIndex(_CateSig,_Indexable,_Functor,_ArgName,_ArgNumber,[IndexableArg],IndexableArg,ArgType):-argTypeIndexable(ArgType),is1Star(IndexableArg),!.
  571staredArgIndex(_CateSig,_Indexable,_Functor,_ArgName,_ArgNumber,IndexableArg,IndexableArg,ArgType):-argTypeIndexable(ArgType),is1Star(IndexableArg),!.
  572
  573addArgIndex(CateSig,Indexable,Functor,ArgName,ArgNumber,Arg,IndexableArg,ArgType):-staredArgIndex(CateSig,Indexable,Functor,ArgName,ArgNumber,Arg,IndexableArg,ArgType),!.
  574
  575addArgIndex(_CateSig,_Indexable,Functor,ArgName,ArgNumber,Arg,IndexableArg,ArgType):-argTypeIndexable(ArgType),
  576  makeIndexableArg(Functor,ArgNumber,Arg,IndexableArg),
  577  assertaFront(argNFound(Functor,ArgName,Arg,IndexableArg)),!,
  578  immediateCall(_Ctx,assert_argNFound(Functor,ArgName,Arg,IndexableArg)).
  579
  580addArgIndex(CateSig,Indexable,Functor,ArgName,ArgNumber,Arg,IndexableArg,ArgType):- atrace,
  581  debugFmt(addArgIndex(CateSig,Indexable,Functor,ArgName,ArgNumber,Arg,IndexableArg,ArgType)),
  582  prolog_must(Arg=IndexableArg),!.
  583
  584
  585assert_argNFound(Functor,ArgName,Arg,IndexableArg):-assertaFront(argNFound(Functor,ArgName,Arg,IndexableArg)).
  586
  587dirtyArgIndex(CateSig,Indexable,Functor,ArgName,ArgNumber,Arg,IndexableArg,ArgType):-staredArgIndex(CateSig,Indexable,Functor,ArgName,ArgNumber,Arg,IndexableArg,ArgType),!.
  588dirtyArgIndex(CateSig,Indexable,Functor,ArgName,ArgNumber,Arg,IndexableArg,ArgType):-
  589  debugFmt(dirtyArgIndex(CateSig,Indexable,Functor,ArgName,ArgNumber,Arg,IndexableArg,ArgType)),!.
  590
  591%%%% todo maybe this.. once((retract(NEW),asserta(NEW)) ; (asserta(NEW),(debugFmt('~q.~n',[asserta(N)])))),!.
  592/*
  593asserta_if_new(NEW):-!,
  594  once(
  595   (retract(NEW),asserta(NEW)) ;
  596   (asserta(NEW),debugFmt('~q.~n',[asserta(NEW)])) ),!.
  597*/

  598asserta_if_new(N):-catch(N,E,debugFmt(error_in(E,N))),!.
  599asserta_if_new(N):-asserta(N),debugFmt(asserta_if_new(N)),!.
  600
  601% ===============================================================================================
  602%  Save Categories
  603% ===============================================================================================
  604assertCate(Ctx,Cate,DoWhat):-
  605      prolog_must(makeAimlCate(Ctx,Cate,Value)),!,
  606      prolog_must(ground(Value)),
  607      prolog_must(assertCate3(Ctx,Value,DoWhat)),!.
assertCate3(Ctx,NEW,DoWhat):-NEW,!.
  611assertCate3(Ctx,NEW,DoWhat):-
  612  flag(cateSigCount,X,X+1),
  613  forall(member(Pred,DoWhat),prolog_must(call(Pred,Ctx,NEW))).
  614% ===============================================================================================
  615%  Make AIML Categories
  616% ===============================================================================================
  617makeAimlCate(Ctx,Cate,Value):-
  618 prolog_mustEach((
  619   convert_template(Ctx,Cate,Assert),
  620   aimlCateOrder(Order),
  621   makeAllParams(Ctx,Order,Assert,Result),
  622   arg2OfList(Result,LISTO), Value =.. [aimlCate|LISTO])).
  623
  624arg2OfList(LIST,LISTO):-maplist_safe(arg2,LIST,LISTO),!.
  625arg2(_=Value,Value):-!.
  626arg2(Value,Value):-!,atrace.
  627
  628
  629translate_cate(Ctx,CateSig):-replaceArgsVar(Ctx,[srcinfo=_],CateSig),assert_cate_in_load(Ctx,CateSig).
  630
  631is_xml_missing(Var):-prolog_must(nonvar(Var)),!,member(Var,['[]','*','_']),!.
  632
  633isRetraction(Ctx,CateSig,OF):-getCategoryArg1(Ctx,'template',NULL,_StarNumber,CateSig),!,is_xml_missing(NULL),
  634   duplicate_term(CateSig,OF),replaceArgsVar(Ctx,['template'=_,srcinfo=_,srcfile=_,cateid=_],OF),!.
  635
  636% ===============================================================================================
  637%  Popping when Building categories
  638% ===============================================================================================
  639
  640clearCateStack(_Ctx):- retractall(dict(category,_,_)).
  641
  642peekCateElements(Ctx,Cate):- cateMemberTags(CATETAGS), peekAttributes(Ctx,CATETAGS,category,Cate),!.
  643
  644popCateElements(Ctx,Cate):- cateMemberTags(CATETAGS), peekAttributes(Ctx,CATETAGS,category,Cate),!.
  645popCateElements(Ctx,CateO):- popCateElements1(Ctx,Cate1),popCateElements2(Ctx,Cate2),append(Cate1,Cate2,Cate),!,CateO=Cate.
  646popCateElements1(Ctx,CateO):- findall(Tag=DCG,cateNodes1(Ctx,category,Tag,DCG),Cate),!,CateO=Cate.
  647popCateElements2(Ctx,CateO):- findall(Tag=DCG,cateNodes2(Ctx,Tag,DCG),Cate),!,CateO=Cate.
  648
  649
  650cateNodes1(Ctx,Scope,Tag,DCGO):-member(Tag,[pattern,template]),once(cateNodes1a(Ctx,Scope,Tag,TEMPLATE)),once(convert_template(Ctx,TEMPLATE,DCG)),!,DCG=DCGO.
  651
  652cateNodes1a(Ctx,Scope,Tag,DCGO):-peekNameValue(Ctx,Scope,Tag,DCG,'$failure'),popNameValue(Ctx,Scope,Tag,DCG),!,DCG=DCGO.
  653cateNodes1a(Ctx,Scope,Tag,DCGO):-listing(dict),aiml_error(peekNameValue(Ctx,Scope,Tag,DCG)),!,DCG=DCGO.
  654cateNodes1a(Ctx,Scope,Tag,DCGO):-peekNameValue(Ctx,Other,Tag,DCG,'$error'),Other\==Scope,!,DCG=DCGO.
  655
  656
  657cateNodes2(Scope,Tag,DCGO):-
  658 member(Tag,
  659    [that,guard,topic]),once(cateNodes2a(Scope,Tag,TEMPLATE)),once(convert_template(_Ctx,TEMPLATE,DCG)),!,DCG=DCGO.
  660
  661cateNodes2a(Scope,Tag,DCGO):-peekNameValue(_Ctx,Other,Tag,DCG,'$failure'),Other\==Scope,!,DCG=DCGO.
  662cateNodes2a(Scope,Tag,DCGO):-aiml_error(peekNameValue(_Ctx,Scope,Tag,DCG)),!,DCG=DCGO.
  663
  664defaultPredicates(N,V):-member(N,[username,botname]),V='*'.
  665
  666%defaultPredicates(N,V):-member(N,[input,pattern]),V='*'.
  667defaultPredicates(N,V):-defaultPredicatesS(S),member(N=V,S).
  668defaultPredicatesS([
  669             cateid=gensym(cateid),
  670             graph='default',
  671             precall='true',
  672             topic='*',
  673             that='*',
  674             request='*',
  675             flags='*',
  676             pattern='*',
  677             call='true',
  678             % hide for testing
  679             dictionary='default',
  680             userdict='user',
  681             substitutions='input',
  682             guard='*',
  683             template=['is ERROR IN CATE (no template is set)'],
  684             lang='bot',
  685             srcinfo=missinginfo,
  686             srcfile=missingfile,
  687             withCategory=[writeqnl,assert_cate_in_load]]).
  688
  689cateMember(Tag):-cateMemberTags(List),member(Tag,List).
  690
  691defaultCatePredicatesS(Defaults):-cateFallback(Defaults).
  692
  693/*
  694And your chair is kept this time For some confidence you can ask them when the next trip to Value Villiage is..
  695You should be permited to keep your chair since you are willing to not leave the grounds except on nursing facility sanctioned trips.
  696I Think they have trips to the Dollar Tree and other places
  697They also can give us special permission on Mondays and Thursdays
  698
  699The main thing is that you are willing to give them the peace of mind that they dont need to "watch you".
  700
  701*/

  702cateFallback(N,V):-cateFallback(List),!,member(N=V,List).
  703cateFallback([
  704       cateid=gensym(arule),
  705       graph = 'default',
  706       precall = 'true',
  707       topic = '*',
  708       that = '*',
  709       request = '*',
  710       pattern='*',
  711       flags= '*',
  712       call = 'true',
  713       guard = '*',
  714       userdict = 'user',
  715       % template = [],  ['Nothing'] ?
  716       % template= [gensym(template)],
  717       srcinfo=missinginfo,
  718       srcfile=missingfile,
  719       withCategory=[writeqnl,assert_cate_in_load]]).
  720       %%|MORE]):-findall(N=V,defaultPredicates(N,V),MORE).
  721
  722pathAttrib(S):-pathAttribS(SS),member(S,SS).
  723pathAttribS([filename,uri,loc,url,path,dir,file,pathname,src,srcfile,location])