2/*  LogicMOO User Modules Setup
    3%
    4%
    5% Dec 13, 2035
    6% Douglas Miles
    7
    8*/
    9:- module(logicmoo_lib, [logicmoo_webbot/0,setup_logicmoo_operators/0]).   10:- set_module(class(library)).   11
   12
   13% ==============================================
   14% [Required] Load the Logicmoo Common Utils
   15% ==============================================
   16:- ensure_loaded(library(logicmoo_common)).   17
   18
   19% ==============================================
   20% SETUP KB EXTENSIONS
   21% ==============================================
   22
   23:- use_module(library(logicmoo_utils)).   24
   25app_argv_local(X):- current_predicate(app_argv/2),app_argv(X).
   26
   27% :- multifile prolog:message//1, prolog:message_hook/3.
   28% prolog:message(ignored_weak_import(Into, From:PI))--> { nonvar(Into),Into \== system,dtrace(dmsg(ignored_weak_import(Into, From:PI))),fail}.
   29% prolog:message(Into)--> { nonvar(Into),functor(Into,_F,A),A>1,arg(1,Into,N),\+ number(N),dtrace(wdmsg(Into)),fail}.
   30% prolog:message_hook(T,error,Warn):- dtrace(wdmsg(nessage_hook(T,warning,Warn))),fail.
   31% prolog:message_hook(T,warning,Warn):- dtrace(wdmsg(nessage_hook(T,warning,Warn))),fail.
   32
   33:- if(app_argv_local('--pdt')).   34:- if(\+ app_argv_local('-nopce')).   35:- if(\+ (getenv('DISPLAY',X) -> \+ atom_string(X,""))).   36%:- guitracer.
   37:- else.   38%:- noguitracer.
   39:- endif.   40:- endif.   41:- endif.   42
   43:- if(app_argv_local('--wamcl');app_argv_local('--lispsock')).   44:- user:use_module(library(wamclrt)).   45:- endif.   46
   47%:- if(app_argv_local('--lispsock 3301')).
   48%:- start_lspsrv(repl,3301,"Lisp Repl").
   49%:- endif.
   50
   51:- if(app_argv_local('--pdt')).   52:- user:use_module(library(logicmoo_pdt)).   53:- endif.   54
   55
   56/*
   57:- flag_call(unsafe_speedups=true).
   58:- flag_call(runtime_debug=0).
   59:- flag_call(runtime_debug=2).
   60% ?- current_prolog_flag(unsafe_speedups , true) .
   61:- flag_call(unsafe_speedups=false).
   62*/
   63%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   64:- dmsg("SET TOPLEVEL OPTIONS").   65% ?- listing.  (uses varaibles)
   66% slows the system startup down consideraly
   67%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   68
   69% :- set_prolog_flag(toplevel_print_factorized,true). % default false
   70%:- set_prolog_flag(toplevel_mode,backtracking). % OR recursive 
   71%:- after_init(dmsg(qconsult_kb7166)).
   72% :- use_listing_vars.
   73%:- set_prolog_flag(write_attributes,portray).
   74% :- debug.
   75
   76/*
   77:- set_prolog_flag(fileerrors,false).
   78:- set_prolog_flag(debug,true).
   79%:- set_prolog_flag(gc,false).
   80%:- set_prolog_flag(gc,true).
   81:- debug.
   82
   83:- set_prolog_flag(report_error,true).
   84:- set_prolog_flag(access_level,system).
   85:- set_prolog_flag(toplevel_print_anon,true).
   86:- set_prolog_flag(debug_on_error,true).
   87:- set_prolog_flag(optimise,false).
   88:- set_prolog_flag(last_call_optimisation,false).
   89
   90*/
   91
   92
   93%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   94:- dmsg("SETUP KB EXTENSIONS").   95%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   96
   97%:- '$set_typein_module'(baseKB).
   98%:- nop('$set_source_module'( baseKB)).
   99
  100:- use_module(library(plunit)).  101:- kb_global(plunit:loading_unit/4).  102
  103% :- ['/home/prologmud_server/lib/swipl/pack/prologmud_samples/prolog/prologmud_sample_games/run_clio'].
  104
  105%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  106:- dmsg("PACK LOADER").  107%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  108:- user:use_module(library(logicmoo_packs)).  109:- dmsg("AUTOLOAD PACKAGES").  110:- before_boot(rescan_pack_autoload_packages).  111
  112%:- reload_library_index.
  113%:- autoload([verbose(true)]).
  114%:- reload_library_index.
  115
  116:- if(\+ current_module( baseKB)).  117:- set_prolog_flag(logicmoo_qsave,true).  118:- else.  119:- set_prolog_flag(logicmoo_qsave,false).  120:- endif.  121
  122/*
  123:- if(exists_source(library(yall))).
  124:-  multifile(yall:lambda_functor/1),
  125   dynamic(yall:lambda_functor/1),
  126   with_no_mpred_expansions(use_module(yall:library(yall),[])),
  127   show_call(retractall(yall:lambda_functor('/'))).
  128:- endif.
  129*/
  130
  131/*
  132set_default_argv:- dmsg("SETTING DEFAULT ARGV!!!!"),
  133   set_prolog_flag(os_argv,[swipl, '-f', '/dev/null','--nonet','--unsafe','--']).
  134*/
  135
  136set_full_argv :-
  137 current_prolog_flag(argv,WasArgV),
  138 ignore((  
  139           \+ ((member(E,WasArgV), 
  140                atom_concat('--',_,E))),
  141   append(WasArgV,[
  142   '--',   
  143   '--mud', % Load MUD server
  144   '--world', % Load MUD server World
  145   %'--nonet' '--noworld',
  146
  147   '--clif', % Support for CLIF
  148   '--sumo', % Support for SUMO
  149   '--nlkb', % Load CYC NL 
  150   '--cyckb', % Load CYC KB 
  151   '--tinykb', % Support for LarKC
  152
  153   '--www', % https://logicmoo.org/*
  154   '--no-fork', '--workers=16', '--port=3020',
  155   %'--user=www-data',
  156   '--sigma', % Sigma Inference Engine Server  https://logicmoo.org/logicmoo/
  157   '--cliop',  % https://logicmoo.org/cliopatria/
  158   '--irc', % Launch IRC Eggdrop Client
  159   '--swish', % https://logicmoo.org/swish/
  160   '--docs', % https://logicmoo.org/pldoc/
  161   '--plweb',   % https://logicmoo.org/plweb/
  162   
  163   % '--lispsock', % '--wamcl', % is already implied by --lispsock
  164
  165   '--logtalk', % Support Logtalk
  166   '--elfinder', % Support Filesystem Browser   https://logicmoo.org/ef/
  167   '--nopdt', % Prolog Development for Eclipse
  168   '--planner', % Load any planners
  169
  170   '--all', % all default options (in case there are new ones!)
  171   '--defaults'
  172   ], NewArgV),
  173   set_prolog_flag('argv',NewArgV))),
  174 current_prolog_flag(argv,Is),
  175 (\+ lmconf:saved_app_argv(_) -> asserta(lmconf:saved_app_argv(Is)) ; true),
  176 writeq(set_prolog_flag('argv',Is)),!,nl.
  177
  178%:- (current_prolog_flag(os_argv,[swipl]) ; current_prolog_flag(argv,[])) -> set_full_argv; true.
  179
  180
  181
  182%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  183:- dmsg("LOAD PARTS OF SYSTEM EARLY").  184%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  185 % :- set_prolog_flag(subclause_expansion,default).
  186 % :- set_prolog_flag(subclause_expansion,false).
  187 % :- set_prolog_flag(dialect_pfc,default).
  188
  189
  190:- user:load_library_system(logicmoo_swilib).  191
  192:- if( current_prolog_flag(xpce,true) ).  193:- if(exists_source(library(pce_emacs))).  194% :- user:use_module(library(pce_emacs)).
  195:- endif.  196:- endif.  197
  198
  199:- multifile(swish_trace:installed/1).  200:-   dynamic(swish_trace:installed/1).  201:-  volatile(swish_trace:installed/1).  202
  203:- if(exists_source(library(semweb/rdf_db))).  204%:- use_module(pengine_sandbox:library(semweb/rdf_db)).
  205:- endif.  206
  207
  208%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  209:- dmsg("SETUP LOGICMOO OPERATORS").  210%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  211setup_logicmoo_operators:- locally(set_prolog_flag(access_level,system),
  212 ((op(200,fy,'-'),op(300,fx,'-'),
  213 op(1190,xfx,('::::')),
  214 op(1180,xfx,('==>')),
  215 op(1170,xfx,'<==>'),
  216 op(1160,xfx,('<-')),
  217 op(1150,xfx,'=>'),
  218 op(1140,xfx,'<='),
  219 op(1130,xfx,'<=>'),
  220 op(600,yfx,'&'),
  221 op(600,yfx,'v'),
  222 op(350,xfx,'xor'),
  223 op(300,fx,'~'),
  224 op(300,fx,'-'),
  225 op(1199,fx,('==>'))))).
  226
  227
  228
  229
  230%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  231:- dmsg("SETUP PATHS FOR PROLOGMUD/LOGICMOO").  232%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  233
  234% :- before_boot((user:ensure_loaded(setup_paths))).
  235
  236% :- user:use_module(library('file_scope')).
  237% :- use_module(library('clause_expansion')).
  238
  239 % :- set_prolog_flag(subclause_expansion,true).
  240
  241% :- during_boot((sanity((lmce:current_smt(SM,M),writeln(current_smt(SM,M)))))).
  242
  243%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  244:- dmsg("LOAD LOGICMOO UTILS").  245%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  246
  247libhook:maybe_save_lm:- \+ current_prolog_flag(logicmoo_qsave,true),!.
  248libhook:maybe_save_lm:- current_predicate(lmcache:qconsulted_kb7166/0),call(call,lmcache:qconsulted_kb7166),!.
  249libhook:maybe_save_lm:- qsave_lm(lm_repl4),!.
  250
  251%:- user:ensure_loaded(library(logicmoo_utils)).
  252
  253%:- multifile(prolog:make_hook/2).
  254%:- dynamic(prolog:make_hook/2).
  255% prolog:make_hook(before, C):- wdmsg(prolog:make_hook(before, C)),fail.
  256% prolog:make_hook(after, C):- wdmsg(prolog:make_hook(after, C)),libhook:maybe_save_lm,fail.
  257
  258%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  259:- dmsg("LOGICMOO/CYC Alignment util").  260%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  261:- set_prolog_flag(do_renames,restore).  262:- gripe_time(60,baseKB:ensure_loaded(library('logicmoo/plarkc/logicmoo_i_cyc_rewriting'))).  263
  264logicmoo_webbot:- whenever_flag_permits(load_network,
  265load_library_system(library(logicmoo_webbot))).
  266/*
  267%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  268:- dmsg("[Optional] Load the Logicmoo Web System").
  269%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  270:- user:use_module(library(logicmoo_common)).
  271
  272:- if(\+ app_argv_local('--nonet')).
  273:- logicmoo_webbot.
  274:- endif.
  275*/
  276
  277%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  278:- dmsg("[Required] Load the Logicmoo Type System").  279%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  280:- ensure_loaded(library(logicmoo_typesystem)).  281
  282%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  283:- dmsg("[Mostly Required] Load the Logicmoo Plan Generator System").  284%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  285:- if(app_argv_local('--planner')).  286:- if(exists_source(library(logicmoo_planner))).  287:- load_library_system(library(logicmoo_planner)).  288:- endif.  289:- endif.  290
  291%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  292:- dmsg("[Mostly Required] Load the Prolog LarKC System").  293% LOAD CYC KB EXTENSIONS
  294%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  295:- load_library_system(library(logicmoo_plarkc)).  296%:- use_module(logicmoo_plarkc).
  297:- check_clause_counts.  298
  299%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  300:- dmsg("[Mostly Required] logicmoo_clif").  301%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  302:- load_library_system(library(logicmoo_clif)).  303%:- use_module(llibrary(logicmoo_clif)).
  304
  305%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  306:- dmsg("SETUP CYC KB EXTENSIONS (TINYKB)").  307%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  308:- before_boot(
  309    (set_prolog_flag(do_renames,restore),
  310      gripe_time(60,baseKB:ensure_loaded(library('logicmoo/plarkc/logicmoo_u_cyc_kb_tinykb.pl'))))).  311
  312%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  313:- dmsg("SETUP CYC KB EXTENSIONS (FULLKB)").  314%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  315:- during_boot(set_prolog_flag(do_renames,restore)).  316%:- gripe_time(60,baseKB:ensure_loaded(library('logicmoo/plarkc/logicmoo_i_cyc_kb.pl'))).
  317:- check_clause_counts.  318
  319
  320%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  321:- dmsg("[Required] Load the CYC Network Client and Logicmoo CycServer Emulator (currently server is disabled)").  322%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  323% :- load_library_system(library(plark/logicmoo/logicmoo_u_cyc_api)).
  324
  325
  326%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  327:- dmsg("[Optional] NOT YET Load the Logicmoo RDF/OWL Browser System").  328%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  329% :- load_library_system(logicmoo(mpred_online/mpred_rdf)).
  330
  331
  332%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  333:- dmsg("[Debugging] Normarily this set as 'true' can interfere with debugging").  334%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  335% :- set_prolog_flag(gc,false).
  336% Yet turning it off we cant even startup without crashing
  337% :- set_prolog_flag(gc,true).
  338
  339
  340% :- sanity(doall(printAll(current_prolog_flag(_N,_V)))).
  341% :- after_boot(during_net_boot(kill_unsafe_preds)).
  342
  343%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  344:- dmsg("[Mostly Required] Load the Logicmoo Parser/Generator System").  345%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  346%:- load_library_system(library(parser_all)).
  347:- if(\+ current_module(logicmoo_nlu)).  348% :- load_library_system(library(logicmoo_nlu)).
  349%:- noguitracer.
  350:- endif.  351%:- load_library_system(library(parser_e2c)).
  352
  353%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  354:- dmsg("MAYBE QSAVE THIS").  355%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  356
  357:- set_prolog_flag(logicmoo_qsave,false).  358
  359:- if( \+ current_prolog_flag(address_bits, 32)).  360%:- before_boot(set_prolog_stack_gb(16)).
  361:- endif.  362
  363:- fixup_exports.  364
  365:- if(false).  366:- statistics.  367:- listing(qsave_lm/1).  368:- endif.  369
  370
  371
  372%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  373% Regression tests that first run whenever a person starts the MUD on the public server
  374%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  375
  376%:- load_library_system(pack(logicmoo_base/t/examples/pfc/'sanity_col_as_unary.pfc')).
  377%:- load_library_system(pack(logicmoo_base/t/examples/pfc/'sanity_birdt.pfc')).
  378%:- load_library_system(pack(logicmoo_base/t/examples/pfc/'sanity_sv.pfc')).
  379%:- load_library_system(pack(logicmoo_base/t/examples/pfc/'sanity_foob.pfc')).