diff --git a/C/scanner.c b/C/scanner.c index b008a0295..5eb618bc1 100755 --- a/C/scanner.c +++ b/C/scanner.c @@ -2024,9 +2024,13 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments, return l; default: + { + char err[1024]; + snprintf( err, 1023, "\n++++ token: unrecognised char %c (%d), type %c\n", ch, ch, chtype(ch) ); #if DEBUG - fprintf(stderr, "\n++++ token: wrong char type %c %d\n", ch, chtype(ch)); + fprintf(stderr, "%s", err); #endif + } t->Tok = Ord(kind = eot_tok); } #if DEBUG diff --git a/misc/analysis/graphs.yap b/misc/analysis/graphs.yap deleted file mode 100644 index 580a13ae7..000000000 --- a/misc/analysis/graphs.yap +++ /dev/null @@ -1,161 +0,0 @@ -pl_graphs(Dir - Mod) :- - atom( Dir ), - format(' ************* GRAPH: ~a ***********************/~n', [Dir]), - atom_concat([Dir,'/*'], Pattern), % */ - expand_file_name( Pattern, Files ), - member( File, Files ), - ( ( sub_atom(File,_,_,0,'.yap') ; sub_atom(File,_,_,0,'.pl') ) -> - build_graph( File , Mod ) - ; - exists_directory( File ), - \+ atom_concat(_, '/.', File), - \+ atom_concat(_, '/..', File), - \+ atom_concat(_, '/.git', File), - pl_graphs( File - Mod ) - ), - fail. -pl_graphs(_). - -%% -% pl_graph( File, Mod) -% adds a node to the file graph and marks which files are modules -% -% main side-effect facts like edge( F0-Mod:File ) -% exported( F-M , N/A ) or exported(F- M. Op ), -% module_on ( M, File ) -% pred ( M :N/A ) -% -build_graph(F, Mod) :- - % writeln(F), - preprocess_file( F, PF ), - catch( open(PF, read, S, [scripting(true)]), _, fail ), - repeat, - nb_getval( current_module, MR ), - catch(read_term( S, T, [term_position(Pos),module(MR),comments(Cs)] ), Throw, (writeln(Throw))), - ( - T == end_of_file - -> - !,% also, clo ops defined in the module M, if M \= Mod - % ( sub_atom(F,_,_,_,'/matrix.yap') -> start_low_level_trace ; nospyall ), - close(S) - ; - stream_position_data( line_count, Pos, Line ), - maplist( comment, Cs ), - nb_setval( line, Line ), - nb_getval( current_module, MC0 ), - ( Mod == prolog -> MC = prolog ; MC = MC0 ), - get_graph( T, F, Pos, MC ), - fail - ). - - -get_graph( V , _F, _Pos, _M ) :- - var( V ), - !, - error( instantiation_error ). -get_graph( T, _F, _Pos, _M0 ) :- - var(T), - !. -get_graph( M:T, F, _Pos, _M0 ) :- !, - get_graph( T, F, _Pos, M ). -get_graph( ( M:H :- B), F, _Pos, M0 ) :- - !, - get_graph( (H :- M0:B), F, _Pos, M ). -get_graph( ( M:H --> B), F, _Pos, M0 ) :- - !, - get_graph( ( H --> M0:B), F, _Pos, M ). -get_graph( ( A, _ --> B), F, _Pos, M ) :- - get_graph( ( A --> B), F, _Pos, M ). -get_graph( (H --> B), F, _Pos, M ) :- - !, - functor( H, N, Ar), - Ar2 is Ar+2, - add_deps( B, M, M:N/Ar2, F, _Pos, 2 ). -get_graph( (H :- B), F, _Pos, M ) :- - !, - functor( H, N, Ar), - add_deps( B, M, M:N/Ar, F, _Pos, 0 ). -%% switches to new file n -get_graph( (:-include( Fs ) ), F, _Pos, M ) :- - !, - source_graphs( M, F, Fs ). -get_graph( (?- _ ), _F, _Pos, _M ) :- !. -get_graph( (:- _ ), _F, _Pos, _M ) :- !. - -source_graphs( M, F, Fs ) :- - maplist( source_graph( M, F ), Fs ), !. -source_graphs( M, F, Fs ) :- - search_file( Fs, F, pl, NF ), - build_graph( NF , M ), !. - -add_deps(V, _M, _P, _F, _Pos, _) :- - var(V), !. -add_deps(M1:G, _M, _P, _F, _Pos,L) :- - !, - always_strip_module(M1:G, M2, G2), - add_deps(G2, M2, _P, _F, _Pos, L). -add_deps((A,B), M, P, F, _Pos, L) :- - !, - add_deps(A, M, P, F, _Pos, L), - add_deps(B, M, P, F, _Pos, L). -add_deps((A;B), M, P, F, _Pos, L) :- !, - add_deps(A, M, P, F, _Pos, L), - add_deps(B, M, P, F, _Pos, L). -add_deps((A|B), M, P, F, _Pos, L) :- !, - add_deps(A, M, P, F, _Pos, L), - add_deps(B, M, P, F, _Pos, L). -add_deps((A->B), M, P, F, _Pos, L) :- !, - add_deps(A, M, P, F, _Pos, L), - add_deps(B, M, P, F, _Pos, L). -add_deps((A*->B), M, P, F, _Pos, L) :- !, - add_deps(A, M, P, F, _Pos, L), - add_deps(B, M, P, F, _Pos, L). -add_deps(once(A), M, P, F, _Pos, L) :- !, - add_deps(A, M, P, F, _Pos, L). -add_deps({A}, M, P, F, _Pos, 2) :- !, - add_deps(A, M, P, F, _Pos, 0). -add_deps([_|_], M, P, F, Pos, 2) :- - !, - put_dep( (F-M:P :- prolog:'C'/3 ), Pos ). -add_deps(String, _M, _P, _F, _Pos, _) :- string(String), !. -add_deps([], _M, _P, _F, _Pos, 2) :- !. -add_deps(!, _M, _P, _F, _Pos, _) :- !. -add_deps(true, _M, _P, _F, _Pos, 0) :- !. -add_deps(false, _M, _P, _F, _Pos, 0) :- !. -add_deps(fail, _M, _P, _F, _Pos, 0) :- !. -add_deps(repeat, _M, _P, _F, _Pos, 0) :- !. -add_deps(A, M, P, F, Pos, L) :- - % we're home, M:N/Ar -> P=M1:N1/A1 - functor(A, N, Ar0), - Ar is Ar0+L, - put_dep( ( F-M:P :- F-M:N/Ar ), Pos ). - -put_dep( (Target :- F0-M:Goal ), Pos ) :- -exported( ( F0-M:Goal :- F1-M1:N/Ar ) ), !, - %follow ancestor chain -ancestor( ( F1-M1:N/Ar :- FA-MA:NA/Ar ) ), -put_dep( ( Target :- FA-MA:NA/Ar ), Pos ). - % the base case, copying from the same module ( but maybe not same file 0. -put_dep( ( Target :- _F-M:N/Ar ) , _ ) :- -m_exists(M:N/Ar, F0), -!, -assert_new( edge( ( Target :- F0-M:N/Ar ) ) ). - % prolog is visible ( but maybe not same file ). -put_dep( ( Target :- _F-_prolog:N/Ar ), _ ) :- -m_exists(prolog:N/Ar, F0), -!, -assert_new( edge( ( Target :- F0-prolog:N/Ar ) ) ). -put_dep( ( _Target :- _F-Mod:_N/_Ar ), _Pos) :- -var( Mod ), !. -put_dep( ( Target :- F-Mod:N/Ar ), Pos) :- -atom( Mod ), -stream_position_data( line_count, Pos, Line ), -assert_new( undef( (Target :- F-Mod:N/Ar ), Line) ). - -ancestor( ( Younger :- Older) ) :- -exported( ( Mid :- Older ) ), !, -ancestor( ( Younger :- Mid) ). -ancestor( (Older :- Older) ). - -m_exists(P, F) :- private( F, P ), !. -m_exists(P, F) :- public( F, P ). diff --git a/misc/analysis/load.yap b/misc/analysis/load.yap deleted file mode 100644 index a0539e0b4..000000000 --- a/misc/analysis/load.yap +++ /dev/null @@ -1,694 +0,0 @@ -/*********************************************************** - - load a program into a graph, but do not actually consult it. - - ***********************************************************/ - -load( D, _OMAP ) :- - working_directory(_, D), - fail. -load( _, _Map ) :- - % from libraries outside the current directories - assert( node( attributes, woken_att_do/4, 'library/atts.yap', prolog ) ), - fail. -load( _ , Dirs ) :- - dirs( Dirs ), - %%% phase 1: find modules - nb_setval( current_module, user ), - nb_setval( private, false ), - nb_setval( file_entry, user:user ), - init_loop( Dirs ), - maplist( pl_interfs, Dirs ), - %%% phase 2: find C-code predicates - maplist( c_preds, Dirs ). - -dirs( Roots ) :- - member( Root-_, Roots ), -% (Root = 'OPTYap' -> start_low_level_trace ; true ), - absolute_file_name( Root, FRoot ), - rdir( FRoot ), - fail. -dirs( _Roots ). - -rdir( FRoot ) :- - absolute_file_name( FRoot, [glob(*), solutions(all), file_errors(fail)], File ), - \+ doskip( File ), - ( - file_property( File, type(directory) ) - -> - rdir( File ) - ; - assert_new( dir( FRoot, File )) - ), - fail. -rdir(_). - -c_preds(Dir - Mod) :- - atom( Dir ), - absolute_file_name( Dir, [glob(*), solutions(all), file_errors(fail)], File ), - ( ( sub_atom(File,_,_,0,'.c') - ; - sub_atom(File,_,_,0,'.i') - ; - sub_atom(File,_,_,0,'.C') - ; - sub_atom(File,_,_,0,'.cpp') - ; - sub_atom(File,_,_,0,'.icc') - ; - sub_atom(File,_,_,0,'.h') - ) -> - \+ doskip( File ), - c_file( File , Mod ) - ; - exists_directory( File ), - \+ doskip( File ), - c_preds( File - Mod ) - ), - fail. -c_preds(_). - - -c_file(F, _Mod) :- - consulted( F, _ ), - !. -c_file(F, Mod) :- - % writeln(F), - assert( consulted( F, Mod ) ), - nb_setval( current_module, Mod ), - open(F, read, S, [alias(c_file)]), - repeat, - read_line_to_string( S, String ), - ( String == end_of_file - -> - !, - close(S) - ; - sub_string(String, _, _, _, `PL_extension`), - %writeln(Fields), - c_ext(S, Mod, F), - fail - ; - split_string(String, `,; ()\t\"\'`, Fields), %' - %writeln(Fields), - line_count(S, Lines), - c_line(Fields , Mod, F:Lines), - fail - ). - -c_line([`}`], Mod, _) :- !, - nb_setval( current_module, Mod ). -c_line(Line, _Mod, _) :- - append( _, [ `CurrentModule`, `=`, M|_], Line), - system_mod(M, _Mod, Mod, _), - nb_setval( current_module, Mod ). -c_line(Line, Mod, F: LineP) :- - break_line( Line, N/A, Fu), - assert( node( Mod, N/A, F-LineP, Fu ) ), - handle_pred( Mod, N, A, F ). - -c_ext( S, Mod, F ) :- - repeat, - read_line_to_string( S, String ), - ( - sub_string( String, _, _, _, `NULL` ), - ! - ; - split_string(String, `,; (){}\t\"\'`, [`FRG`, NS,AS,FS|_]), %' - atom_string(N,NS), - atom_string(Fu,FS), - number_string(A, AS), - stream_property( S, position( Pos ) ), - stream_position_data( line_count, Pos, Line ), - assert( node( Mod , N/A, F-Line, Fu ) ), - handle_pred( Mod, N, A, F ) - ; - split_string(String, `,; (){}\t\"\'`, [NS,AS,FS|_]), %' - atom_string(N,NS), - atom_string(Fu,FS), - number_string(A, AS), - stream_property( S, position( Pos ) ), - stream_position_data( line_count, Pos, Line ), - Line0 is Line-1, - assert( node( Mod, N/A, F-Line0, Fu ) ), - handle_pred( Mod, N, A, F ) - ). - - -break_line( Line, N/A, c(Fu)) :- - take_line( Line, NS, AS, FS ), !, - atom_string(N,NS), - atom_string(Fu,FS), - number_string(A, AS). -break_line( Line, N/A, swi(Fu)) :- - take_line( Line, NS, AS, FS ), !, - atom_string(N,NS), - number_string(A, AS), - atomic_concat([`pl_`,FS,`_`,A,`_va`], Fu). -break_line( Line, N/A, bp(Fu)) :- - take_line( Line, NS, AS, FS ), !, - atom_string(N,NS), - number_string(A, AS), - atomic_concat([`pc_`,FS,`_`,A], Fu). -break_line( Line, N/A, c(FuE, FuB)) :- - take_line( Line, NS, AS, FSE, FSB ), !, - atom_string(N,NS), - atom_string(FuE,FSE), - atom_string(FuB,FSB), - number_string(A, AS). - -take_line( Line, NS, AS, FS ) :- - append( _, [ `Yap_InitCPred`, NS, AS, FS|_], Line), !. -take_line( Line, NS, AS, FS ) :- - append( _, [ `Yap_InitAsmPred`, NS, AS, _, FS|_], Line), !. -take_line( Line, NS, AS, FS ) :- - append( _, [ `Yap_InitCmpPred`, NS, AS, FS|_], Line), !. -take_line( Line, NS, AS, FS ) :- - append( _, [ `Yap_InitCmpPred`, NS, AS, FS|_], Line), !. -take_line( Line, NS, AS, FS ) :- - append( _, [ `YAP_UserCPredicate`, NS, FS, AS|_], Line), !. -take_line( Line, NS, AS, FS ) :- - append( _, [ `PRED`, NS0, AS, FS|_], Line), !, - append( [`pl_`, NS0, AS, `_va`], NS ). -take_line( Line, NS, AS, FS ) :- - append( _, [ `PRED_IMPL`, NS0, AS, FS|_], Line), !, - append( [`pl_`, NS0, AS, `_va`], NS ). -take_line( Line, NS, AS, FS ) :- - append( _, [ `PL_register_foreign`, NS, AS, FS|_], Line), !. -take_line( Line, NS, AS, FS ) :- - append( _, [ `PRED_DEF`, NS0, AS, FS|_], Line), !, - append( [`pl_`, NS0, AS, `_va`], NS ). -take_line( Line, NS, AS, FS ) :- - append( _, [ `FRG`, NS, AS, FS|_], Line), !. - % from odbc -take_line( Line, NS, AS, FS ) :- - append( _, [ `NDET`, NS, AS, FS|_], Line), !. -take_line( Line, NS, AS, FS ) :- - append( _, [ `DET`, NS, AS, FS|_], Line), !. - - -take_line( Line, AS, FS ) :- - append( _, [ `REGISTER_CPRED`, FS, AS], Line), !. - - -take_line( Line, NS, AS, FSE, FSB ) :- - append( _, [ `Yap_InitCPredBack`, NS, AS, _, FSE, FSB|_], Line), !. - -system_mod(`ATTRIBUTES_MODULE`, _, attributes, user ). -system_mod(`HACKS_MODULE`, _, '$hacks' , sys ). -system_mod(`USER_MODULE`, _, user, user ). -system_mod(`DBLOAD_MODULE`, _, '$db_load', sys ). -system_mod(`GLOBALS_MODULE`, _, globals, sys ). -system_mod(`ARG_MODULE`, _, arg, sys ). -system_mod(`PROLOG_MODULE`, _ , prolog, sys ). -system_mod(`RANGE_MODULE`, _, range, user ). -system_mod(`SWI_MODULE`, _, swi, sys ). -system_mod(`OPERATING_SYSTEM_MODULE`, _, system , sys ). -system_mod(`TERMS_MODULE`, _, terms , sys). -system_mod(`SYSTEM_MODULE`, _, system, sys ). -system_mod(`IDB_MODULE`, _, idb, user ). -system_mod(`CHARSIO_MODULE`, _, charsio, sys ). -system_mod(`cm`, M, M, user ). - -call_c_files( File, Mod, _Fun, [CFile] ) :- - search_file( CFile, File, c, F ), - c_file(F, Mod). -call_c_files( File, Mod, _Fun, CFile ) :- - CFile \= [_|_], - search_file( CFile, File, c, F ), - c_file(F, Mod). - - -pl_interfs(Dir - Mod) :- - \+ doskip( Dir ), - format('% ************* ~a\n', [Dir]), - nb_setval( current_module, Mod ), - atom( Dir ), - directory_files( Dir , Files), - member( File, Files ), - atom_concat([Dir,'/',File], Path), - ( ( sub_atom(File,_,_,0,'.yap') ; sub_atom(File,_,_,0,'.pl') ; sub_atom(File,_,_,0,'.ypp') ) -> - ops_restore, - absolute_file_name( Path, APath ), - pl_interf( APath , Mod ) - ; - exists_directory( Path ), - \+ atom_concat(_, '/.', Path), - \+ atom_concat(_, '/..', Path), - \+ atom_concat(_, '/.git', Path), - absolute_file_name( Path, APath ), - \+ doskip( APath ), - pl_interfs( APath - Mod ) - ), - fail. -pl_interfs(_). - -%% - % pl_interf( File, Mod) - % adds a node to the file graph and marks which files are modules - % - % main side-effect facts like edge( F0-Mod:File ) - % exported( ( FMNATarget :- FMNASource ) ) ou exported(F-M, Op ), - % module_on ( M, File ) - % -pl_interf(F, _Mod) :- - module_on( F , _M, _Is), - !. -pl_interf(F, Mod) :- - consulted(F, Mod ), - !. -pl_interf(F, Mod) :- - % ( sub_atom(F,_,_,_,'matrix.yap') -> spy get_interf ; true ), - % ( sub_atom( F, _, _, 0, 'gecode.yap' ) -> spy user_deps; true ), - assert_new(consulted(F, Mod ) ), - nb_getval( private, Default ), - nb_setval( private, false ), - nb_getval( file_entry, OF:OMod ), - nb_setval( file_entry, F:Mod ), - preprocess_file( F, PF ), - catch( open(PF, read, S, [scripting(true)]) , _, fail ), - repeat, - nb_getval( current_module, MR ), - %( sub_atom(F,_,_,_,'e.yap') -> spy get_interf ; nospyall ), - catch( read_term( S, T, [module( MR ),term_position(Pos)] ), Throw, (ypp(F,Throw), fail)), - ( - T == end_of_file - -> - !, - close(S), - ( - c_dep( F, Fc), - c_file( Fc, MR ), - fail - ; - build_graph( F, MR ), - fail - % cleanup - ; - module_on( F , _M, _Is) - -> - % also, close ops defined in the module M, if M \= Mod - nb_setval( current_module, Mod ), - nb_setval( private, Default ), - nb_setval( file_entry, OF:OMod ) - ; - true - ) - ; - nb_getval( current_module, MC0 ), - stream_position_data( line_count, Pos, Line ), - nb_setval( line, Line ), - ( Mod == prolog -> MC = prolog ; MC = MC0 ), - get_interf( T, F, MC ), - fail - ). - -get_interf( T, _F, _M0 ) :- - var(T), - !. -get_interf( T, _F, _M0 ) :- - % ( T = (:- op(_,_,_)) -> trace ; true ), - var(T), - !. -get_interf( M:T, F, _M0 ) :- !, - get_interf( T, F, M ). -get_interf( goal_expansion(G, M, _) , F, _M0 ) :- - nonvar( G ), - !, - ( var( M ) -> M1 = prolog ; M = M1 ), - functor( G, N, A ), - handle_pred( M1, N, A, F ). -get_interf( goal_expansion(G, _) , F, _M0 ) :- - nonvar( G ), - !, - functor( G, N, A ), - handle_pred( prolog, N, A, F ). -get_interf( ( M:H :- _B), F, _M ) :- -!, -get_interf( H, F, M ). -get_interf( ( goal_expansion(G, M, _) :- _) , F, _M0 ) :- -nonvar( G ), -!, -( var( M ) -> M1 = prolog ; M = M1 ), -functor( G, N, A ), -handle_pred( M1, N, A, F ). -get_interf( ( goal_expansion(G, _) :- _) , F, _M0 ) :- -nonvar( G ), -!, -functor( G, N, A ), -handle_pred( prolog, N, A, F ). -get_interf( ( M:H --> _B), F, _ ) :- -!, -get_interf( ( H --> _B), F, M ). -get_interf( ( A, _ --> _B), F, M ) :- -get_interf( ( A --> _B), F, M ). -get_interf( (H --> _B), F, M ) :- -!, -functor( H, N, Ar), -Ar2 is Ar+2, -functor( H2, N, Ar2), -get_interf( H2, F, M ). -get_interf( (H :- _B), F, M ) :- -!, -get_interf( H, F, M ). -%% switches to new file n -get_interf( (:- V ), _F, _M ) :- -var( V ), -!. -get_interf( (:- module( NM, Is ) ), F, _M ) :- -!, -assert(module_file( F, NM ) ), -nb_setval( current_module, NM ), -assert( module_on( F , NM, Is) ), -maplist( public(F, NM), Is ), -nb_setval( private, true ). -get_interf( (:- reexport( Loc, Is ) ), F, M ) :- -!, - % find the file -search_file( Loc, F, pl, NF ), -include_files( F, M, Is, NF ), - % extend the interface.rg -retract( module_on( F , M, Is0) ), -append( Is0, Is, NIs ), -assert( module_on( F , M, NIs) ), -maplist( public(F, M), NIs ). -get_interf( (:- use_module( Loc, Is ) ), F, M ) :- !, -!, -include_files( F, M, Is, Loc ). -get_interf( (:- use_module( Loc ) ), F, M ) :- !, -!, -include_files( F, M, Loc ). - % nb_getval(current_module,MM), writeln(NM:MM:M). -get_interf( (:- use_module( Loc, Is, _ ) ), F, M ) :- !, -!, -include_files( F, M, Is, Loc ). -get_interf( (:- consult( Files ) ), F, M ) :- -!, -include_files( F, M, Files ). -get_interf( (:- reconsult( Files ) ), F, M ) :- -!, -include_files( F, M, Files ). -get_interf( (:- ensure_loaded( Files ) ), F, M ) :- -!, -include_files( F, M, Files ). -get_interf( (:- include( Files ) ), F, M ) :- -!, -source_files( F, M, Files ). -get_interf( (:- load_files( Files , [_|_] ) ), F, M ) :- -!, -include_files( F, M, Files ). -get_interf( ( :- ( G -> _ ; _ ) ) , F, M) :- -!, -get_interf( (:- G ) , F, M). -get_interf( (:- catch( G , _, _ ) ) , F, M) :- -!, -get_interf( (:- G ) , F, M). -get_interf( (:- initialization( G , now ) ) , F, M) :- -!, -get_interf( (:- G ) , F, M). -get_interf( (:- load_files( Files , [_|_] ) ), F, M ) :- -!, -include_files( F, M, Files ). -get_interf( (:- [F1|Fs] ), F, M ) :- -!, -include_files( F, M, [F1|Fs] ). - % don't actually use this one. -get_interf( (:- load_foreign_files(Fs, _, Fun) ), F, M ) :- -!, -call_c_files( F, M, Fun, Fs ). -get_interf( (:- load_foreign_library(F) ), F0, M ) :- -!, -always_strip_module(M:F, M1, F1), -call_c_files( F0, M1, '', F1 ). -get_interf( (:- load_foreign_library(F,Fun) ), F0, M ) :- -!, -always_strip_module(M:F, M1, F1), -call_c_files( F0, M1, Fun, F1 ). -get_interf( (:- use_foreign_library(F) ), F0, M ) :- -!, -always_strip_module(M:F, M1, F1), -call_c_files( F0, M1, '', F1 ). -get_interf( (:- system_module( _NM, _Publics, _Hiddens) ), _F, _M ) :- -!. -get_interf( (:- style_checker( _ ) ), _F, _M ) :- -!. -get_interf( (:- dynamic T), F, M ) :- -!, -declare_functors( T, F, M ). -get_interf( (:- multifile T), F, M ) :- % public? -!, -declare_functors( T, F, M ). -get_interf( (:- meta_predicate T), F, M ) :-!, -declare_terms( T, F, M ), % public? -!. -get_interf( (:- '$install_meta_predicate'( H, M) ), F, __M ) :- -!, -declare_functors( H, F, M ). -get_interf( (:- thread_local T), F, M ) :- -!, -declare_functors( T, F, M ). -get_interf( (:- op( X, Y, Z) ), F, M ) :- -!, -always_strip_module(M:Z, M1, Z1), -handle_op( F, M1, op( X, Y, Z1) ). -get_interf( (:- record( Records ) ), F, M ) :- -!, -handle_record( Records, F, M). -get_interf( (:- set_prolog_flag(dollar_as_lower_case,On) ), _F, _M ) :- -!, -set_prolog_flag(dollar_as_lower_case,On). -get_interf( (:- _ ), _F, _M ) :- !. -get_interf( (?- _ ), _F, _M ) :- !. -get_interf( V , _F, _M ) :- - var( V ), - !, - error( instantiation_error ). -get_interf( G , F, M ) :- - functor( G, N, A), - handle_pred( M, N, A, F ), - !. - - % support SWI package record -handle_record( (Records1, Records2), F, M ) :- - !, - handle_record( Records1, F, M ), - handle_record( Records2, F, M ). -handle_record( Record, F, M ) :- - Record =.. [Constructor|Fields], - atom_concat(Constructor, '_data', Data), - handle_pred( M, Data, 3, F), - atom_concat(default_, Constructor, New), - handle_pred( M, New, 1, F), - atom_concat(is_, Constructor, Is), - handle_pred( M, Is, 1, F), - atom_concat(make_, Constructor, Make), - handle_pred( M, Make, 2, F), - handle_pred( M, Make, 3, F), - atom_concat([set_, Constructor,'_fields'], Sets), - handle_pred( M, Sets, 3, F), - handle_pred( M, Sets, 4, F), - atom_concat([set_, Constructor,'_field'], Set), - handle_pred( M, Set, 3, F), - maplist( handle_record_field( Constructor, F, M) , Fields ). - -handle_record_field( Constructor, F, M, Name:_=_ ) :- - !, - handle_record_field_name( Constructor, F, M, Name). -handle_record_field( Constructor, F, M, Name:_ ) :- - !, - handle_record_field_name( Constructor, F, M, Name). -handle_record_field( Constructor, F, M, Name=_ ) :- - !, - handle_record_field_name( Constructor, F, M, Name). -handle_record_field( Constructor, F, M, Name ) :- - handle_record_field_name( Constructor, F, M, Name). - -handle_record_field_name( Constructor, F, M, Name) :- - atom_concat([ Constructor,'_', Name], Val), - handle_pred( M, Val, 2, F), - atom_concat([ set_, Name, '_of_', Constructor ], Set), - handle_pred( M, Set, 3, F), - handle_pred( M, Set, 2, F), - atom_concat([ nb_set_, Name, '_of_', Constructor ], Set), - handle_pred( M, Set, 3, F), - handle_pred( M, Set, 2, F). - -handle_pred( M, N, A, F ) :- - ( - system_mod( _, _, M, sys ) - -> - ( - atom_concat('$',_,N) - -> - private( F, M, N/A ) - ; - public( F, M, N/A ) - ) - ; - ( nb_getval( private, false ) - -> - public( F, M, N/A ) - ; - private( F, M, N/A ) - ) - ). - -handle_op( F, M, Op ) :- - ( nb_getval( private, false ) - -> - public( F, M, Op ) - ; - private( F, M, Op ) - ), - Op = op(X, Y, Z ), - ( ( M == user ; M == prolog ) - -> - op( X, Y, prolog:Z ) - ; - op( X, Y, M:Z ) - ). - -exported( NF, F, NM, M, op(X,Y,Z)) :- - !, - public( NF , NM:op(X,Y,Z) ), - handle_op( F, M , op(X,Y,Z) ). -exported( NF, F, NM, M, N/A) :- !, - % sink no more - retractall( exported(( _ :- F-M:N/A) ) ), - assert_new( exported( (F-M:N/A :- NF-NM:N/A )) ). -exported( NF, F, NM, M, N/A as NN) :- !, - % sink no more - retractall( exported(( _ :- F-M:N/A) ) ), - assert_new( exported( ( F-M:NN/A :- NF-NM:N/A ) ) ). -exported( NF, F, NM, M, N//A) :- !, - A2 is A+2, - % sink no more - retractall( exported(( _ :- F-M:N/A2) ) ), - assert_new( exported( (F-M:N/A2 :- NF-NM:N/A2) ) ). -exported( NF, F, NM, M, N//A as NN) :- !, - A2 is A+2, - % sink no more - retractall( exported(( _ :- F-M:N/A2) ) ), - assert_new( exported( ( F-M:NN/A2 :- NF-NM:N/A2 )) ). - -import_publics( F, ProducerMod, ConsumerMod ) :- - public(F, ProducerMod:op(X,Y,Z) ), - handle_op( F, ConsumerMod, op(X,Y,Z) ), - fail. -import_publics( _F, _ProducerMod, _ConsumerMod ). - -all_imported( ProducerFile, ConsumerFile, ProducerMod, ConsumerMod ) :- - public(ProducerFile, ProducerMod:op(X,Y,Z) ), - handle_op( ConsumerFile, ConsumerMod, op(X,Y,Z) ), - fail. -all_imported( ProducerFile, ConsumerFile, ProducerMod, ConsumerMod ) :- - public(ProducerFile, ProducerMod:N/A ), - exported( ProducerFile, ConsumerFile, ProducerMod, ConsumerMod, N/A ), - fail. -all_imported( _ProducerFile, _ConsumerFile, _ProducerMod, _ConsumerMod ). - - -include_files( F, M, Files ) :- - include_files( F, M, _Is, Files ). - -include_files( F, M, Is, Files ) :- - maplist( include_files( F, M, Is ), Files ), - !. -include_files( F, M, Is, -Files ) :- - !, - include_files( F, M, Is, Files). -include_files( F, M, Is, Files ) :- - !, - always_strip_module(M:Files, M1, NFiles), - include_file( F, M1, Is, NFiles ). -include_files( F, M, Is, Loc ) :- - include_file( F, M, Is, Loc ). - -include_file( F, M, Is, Loc ) :- - is_list( Loc ), !, - maplist( include_file( F, M, Is), Loc ). -include_file( F, M, Is0, Loc ) :- - nb_getval( private, Private ), - % find the file - once( search_file( Loc, F, pl, NF ) ), - % depth visit - pl_interf(NF, M), % should verify Is in _Is - % link b - ( module_on(NF, NM, Is) - -> - ( var(Is0) -> Is = Is0 ; true ), - maplist( exported( NF, F, NM, M) , Is0 ) - ; - all_imported( NF, F, NM, M) - ), - nb_setval( private, Private ). - - -source_files( F, M, Files ) :- - maplist( source_files( F, M ), Files ), - !. -source_files( F, M, Loc ) :- - source_file( F, M, Loc ). - -source_file( F, M, Loc ) :- - once( search_file( Loc, F, pl, NF ) ), - % depth visit - pl_source(NF, F, M). % should verify Is in _Is - -pl_source(F, F0, Mod) :- - % writeln( -F ), - preprocess_file( F, PF ), - catch( open(PF, read, S, []) , _, fail ), - repeat, - nb_getval( current_module, MR ), - %( sub_atom(F,_,_,_,'examples/matrix.yap') -> spy get_interf ; nospyall ), - catch( read_term( S, T, [module( MR ),term_position(Pos)] ), Throw, (writeln(F:MR:Throw), break, fail)), - ( - T == end_of_file - -> - !, - close(S) - ; - nb_getval( current_module, MC0 ), - stream_position_data( line_count, Pos, Line ), - nb_setval( line, Line ), - ( Mod == prolog -> MC = prolog ; MC = MC0 ), - get_interf( T, F0, MC ), - fail - ). - -declare_functors( T, _F, _M1) :- var(T), !, - error( unbound_variable ). -declare_functors( M:T, F, _M1) :- !, - declare_functors( T, F, M). -declare_functors( (T1,T2), F, M1) :- !, - declare_functors( T1, F, M1), - declare_functors( T2, F, M1). -declare_functors( Ts, F, M1) :- - maplist( declare_functor( F, M1), Ts ), !. -declare_functors( T, F, M1) :- - declare_functor( F, M1, T). - -declare_functor(File, M, N/A) :- - handle_pred( M, N, A, File ). - -declare_terms( T, _F, _M1) :- var(T), !, - error( unbound_variable ). -declare_terms( M:T, F, _M1) :- !, - declare_functors( T, F, M). -declare_terms( (N1,N2), F, M) :- - number(N1), - number(N2), - !, - declare_term( F, M, (N1,N2)). -declare_terms( (T1,T2), F, M1) :- !, - declare_terms( T1, F, M1), - declare_terms( T2, F, M1). -declare_terms( Ts, F, M1) :- - maplist( declare_term( F, M1), Ts ), !. -declare_terms( T, F, M1) :- - declare_term( F, M1, T). - -declare_term(F, M, S) :- - functor(S, N, A), - handle_pred( M, N, A, F ). diff --git a/packages/bdd/simplecudd_lfi/problogbdd_lfi.c b/packages/bdd/simplecudd_lfi/problogbdd_lfi.c index 667d8aa0e..9cc09b0d5 100644 --- a/packages/bdd/simplecudd_lfi/problogbdd_lfi.c +++ b/packages/bdd/simplecudd_lfi/problogbdd_lfi.c @@ -190,7 +190,7 @@ #include "iqueue.h" #include #include -#define VERSION "2.0.0" +#define VERSION "2.0.0" int all_loaded_for_deterministic_variables(namedvars varmap, int disp); @@ -236,16 +236,20 @@ void termhandler(int num); void myexpand(extmanager MyManager, DdNode *Current); double CalcProbability(extmanager MyManager, DdNode *Current); double CalcProbabilitySigmoid(extmanager MyManager, DdNode *Current); -gradientpair CalcGradient(extmanager MyManager, DdNode *Current, int TargetVar, char *TargetPattern, int type); -double CalcExpectedCountsUp(extmanager * MyManager, DdNode *Current, char *query_id) ; -double CalcExpectedCountsDown(extmanager * MyManager, DdNode *Current, char *query_id); -double CalcExpectedCounts(extmanager * MyManager, DdNode *Current, char *query_id, int calcdown_needed); +gradientpair CalcGradient(extmanager MyManager, DdNode *Current, int TargetVar, + char *TargetPattern, int type); +double CalcExpectedCountsUp(extmanager *MyManager, DdNode *Current, + char *query_id); +double CalcExpectedCountsDown(extmanager *MyManager, DdNode *Current, + char *query_id); +double CalcExpectedCounts(extmanager *MyManager, DdNode *Current, + char *query_id, int calcdown_needed); int patterncalculated(char *pattern, extmanager MyManager, int loc); -char * extractpattern(const char *thestr); +char *extractpattern(const char *thestr); int main(int argc, char **arg) { extmanager MyManager; - DdNode *bdd = NULL, **forest = NULL, *bakbdd= NULL; + DdNode *bdd = NULL, **forest = NULL, *bakbdd = NULL; bddfileheader fileheader; int i, ivarcnt, code, curbdd; gradientpair tvalue; @@ -258,7 +262,8 @@ int main(int argc, char **arg) { if (params.errorcnt > 0) { printhelp(argc, arg); for (i = 0; i < params.errorcnt; i++) { - fprintf(stderr, "Error: not known or error at parameter %s.\n", arg[params.error[i]]); + fprintf(stderr, "Error: not known or error at parameter %s.\n", + arg[params.error[i]]); } return -1; } @@ -269,19 +274,32 @@ int main(int argc, char **arg) { return -1; } - if (params.method != 0 && arg[params.method][0] != 'g' && arg[params.method][0] != 'p' && arg[params.method][0] != 'o' && arg[params.method][0] != 'l' && arg[params.method][0] != 'e' && arg[params.method][0] != 'd') { + if (params.method != 0 && arg[params.method][0] != 'g' && + arg[params.method][0] != 'p' && arg[params.method][0] != 'o' && + arg[params.method][0] != 'l' && arg[params.method][0] != 'e' && + arg[params.method][0] != 'd') { printhelp(argc, arg); - fprintf(stderr, "Error: you must choose a calculation method beetween [p]robability, [g]radient, [l]ine search, [o]nline, [e]xpected counts, probability with [d]eterministic nodes.\n"); + fprintf(stderr, "Error: you must choose a calculation method beetween " + "[p]robability, [g]radient, [l]ine search, [o]nline, " + "[e]xpected counts, probability with [d]eterministic " + "nodes.\n"); return -1; } - if (params.method != 0 && (arg[params.method][0] == 'g' || arg[params.method][0] == 'p' || arg[params.method][0] == 'l'|| arg[params.method][0] == 'e'|| arg[params.method][0] == 'd') && params.inputfile == -1) { + if (params.method != 0 && + (arg[params.method][0] == 'g' || arg[params.method][0] == 'p' || + arg[params.method][0] == 'l' || arg[params.method][0] == 'e' || + arg[params.method][0] == 'd') && + params.inputfile == -1) { printhelp(argc, arg); - fprintf(stderr, "Error: an input file is necessary for probability, gradient, line search calculation or expected counts methods.\n"); + fprintf(stderr, "Error: an input file is necessary for probability, " + "gradient, line search calculation or expected counts " + "methods.\n"); return -1; } - if (params.debug) DEBUGON; + if (params.debug) + DEBUGON; RAPIDLOADON; SETMAXBUFSIZE(params.maxbufsize); @@ -304,43 +322,44 @@ int main(int argc, char **arg) { bdd = OnlineGenerateBDD(MyManager.manager, &MyManager.varmap); ivarcnt = GetVarCount(MyManager.manager); } else { - //fprintf(stderr,"reading file \n"); + // fprintf(stderr,"reading file \n"); fileheader = ReadFileHeader(arg[params.loadfile]); - switch(fileheader.filetype) { - case BDDFILE_SCRIPT: - // fprintf(stderr," ..... %i \n",fileheader.varcnt); - MyManager.manager = simpleBDDinit(fileheader.varcnt); - MyManager.t = HIGH(MyManager.manager); - MyManager.f = LOW(MyManager.manager); - MyManager.varmap = InitNamedVars(fileheader.varcnt, fileheader.varstart); - if (fileheader.version > 1) { - forest = FileGenerateBDDForest(MyManager.manager, MyManager.varmap, fileheader); - bdd = forest[0]; - bakbdd = bdd; - } else { - forest = NULL; - bdd = FileGenerateBDD(MyManager.manager, MyManager.varmap, fileheader); - bakbdd = bdd; - } - ivarcnt = fileheader.varcnt; - break; - case BDDFILE_NODEDUMP: - MyManager.manager = simpleBDDinit(fileheader.varcnt); - MyManager.t = HIGH(MyManager.manager); - MyManager.f = LOW(MyManager.manager); - MyManager.varmap = InitNamedVars(fileheader.varcnt, fileheader.varstart); - bdd = LoadNodeDump(MyManager.manager, MyManager.varmap, fileheader.inputfile); - ivarcnt = fileheader.varcnt; - break; - default: - fprintf(stderr, "Error: not a valid file format to load.\n"); - return -1; - break; + switch (fileheader.filetype) { + case BDDFILE_SCRIPT: + // fprintf(stderr," ..... %i \n",fileheader.varcnt); + MyManager.manager = simpleBDDinit(fileheader.varcnt); + MyManager.t = HIGH(MyManager.manager); + MyManager.f = LOW(MyManager.manager); + MyManager.varmap = InitNamedVars(fileheader.varcnt, fileheader.varstart); + if (fileheader.version > 1) { + forest = FileGenerateBDDForest(MyManager.manager, MyManager.varmap, + fileheader); + bdd = forest[0]; + bakbdd = bdd; + } else { + forest = NULL; + bdd = FileGenerateBDD(MyManager.manager, MyManager.varmap, fileheader); + bakbdd = bdd; + } + ivarcnt = fileheader.varcnt; + break; + case BDDFILE_NODEDUMP: + MyManager.manager = simpleBDDinit(fileheader.varcnt); + MyManager.t = HIGH(MyManager.manager); + MyManager.f = LOW(MyManager.manager); + MyManager.varmap = InitNamedVars(fileheader.varcnt, fileheader.varstart); + bdd = LoadNodeDump(MyManager.manager, MyManager.varmap, + fileheader.inputfile); + ivarcnt = fileheader.varcnt; + break; + default: + fprintf(stderr, "Error: not a valid file format to load.\n"); + return -1; + break; } // fprintf(stderr,"bdd built\n"); } - alarm(0); // problem specifics @@ -349,104 +368,136 @@ int main(int argc, char **arg) { ivarcnt = RepairVarcnt(&MyManager.varmap); code = 0; if (params.inputfile != -1) { - if (LoadVariableData(MyManager.varmap, arg[params.inputfile]) == -1) return -1; + if (LoadVariableData(MyManager.varmap, arg[params.inputfile]) == -1) + return -1; // if (!all_loaded(MyManager.varmap, 1)) return -1; all_loaded_for_deterministic_variables(MyManager.varmap, 1); } // impose a predifined order good for debugging - // can be used with a partial number of variables to impose ordering at beggining of BDD + // can be used with a partial number of variables to impose ordering at + // beggining of BDD if (params.orderfile != -1) { - ImposeOrder(MyManager.manager, MyManager.varmap, GetVariableOrder(arg[params.orderfile], MyManager.varmap.varcnt)); + ImposeOrder( + MyManager.manager, MyManager.varmap, + GetVariableOrder(arg[params.orderfile], MyManager.varmap.varcnt)); } curbdd = 0; do { MyManager.his = InitHistory(ivarcnt); if (params.method != 0) { - switch(arg[params.method][0]) { - case 'g': - for (i = 0; i < MyManager.varmap.varcnt; i++) { - if (MyManager.varmap.vars[i] != NULL) { + switch (arg[params.method][0]) { + case 'g': + for (i = 0; i < MyManager.varmap.varcnt; i++) { + if (MyManager.varmap.vars[i] != NULL) { - // check whether this is a continues fact - if (MyManager.varmap.dynvalue[i] == NULL) { // nope, regular fact - varpattern = extractpattern(MyManager.varmap.vars[i]); - if ((varpattern == NULL) || (!patterncalculated(varpattern, MyManager, i))) { - tvalue = CalcGradient(MyManager, bdd, i + MyManager.varmap.varstart, varpattern, 0); - probability = tvalue.probability; - if (varpattern == NULL) { - printf("query_gradient(%s,%s,p,%e).\n", arg[params.queryid], MyManager.varmap.vars[i], tvalue.gradient); - } else { - varpattern[strlen(varpattern) - 2] = '\0'; - printf("query_gradient(%s,%s,p,%e).\n", arg[params.queryid], varpattern, tvalue.gradient); - } - ReInitHistory(MyManager.his, MyManager.varmap.varcnt); - if (varpattern != NULL) free(varpattern); - } - } else { // it is! let's do the Hybrid Problog Magic - // first for mu - varpattern = extractpattern(MyManager.varmap.vars[i]); - if ((varpattern == NULL) || (!patterncalculated(varpattern, MyManager, i))) { - tvalue = CalcGradient(MyManager, bdd, i + MyManager.varmap.varstart, varpattern, 1); - probability = tvalue.probability; - if (varpattern == NULL) { - printf("query_gradient(%s,%s,mu,%e).\n", arg[params.queryid], MyManager.varmap.vars[i], tvalue.gradient); - } else { - varpattern[strlen(varpattern) - 2] = '\0'; - printf("query_gradient(%s,%s,mu,%e).\n", arg[params.queryid], varpattern, tvalue.gradient); - } + // check whether this is a continues fact + if (MyManager.varmap.dynvalue[i] == NULL) { // nope, regular fact + varpattern = extractpattern(MyManager.varmap.vars[i]); + if ((varpattern == NULL) || + (!patterncalculated(varpattern, MyManager, i))) { + tvalue = CalcGradient(MyManager, bdd, + i + MyManager.varmap.varstart, + varpattern, 0); + probability = tvalue.probability; + if (varpattern == NULL) { + printf("query_gradient(%s,%s,p,%e).\n", arg[params.queryid], + MyManager.varmap.vars[i], tvalue.gradient); + } else { + varpattern[strlen(varpattern) - 2] = '\0'; + printf("query_gradient(%s,%s,p,%e).\n", arg[params.queryid], + varpattern, tvalue.gradient); } ReInitHistory(MyManager.his, MyManager.varmap.varcnt); - if (varpattern != NULL) free(varpattern); - - // then for sigma - varpattern = extractpattern(MyManager.varmap.vars[i]); - if ((varpattern == NULL) || (!patterncalculated(varpattern, MyManager, i))) { - tvalue = CalcGradient(MyManager, bdd, i + MyManager.varmap.varstart, varpattern, 2); - probability = tvalue.probability; - if (varpattern == NULL) { - printf("query_gradient(%s,%s,sigma,%e).\n", arg[params.queryid], MyManager.varmap.vars[i], tvalue.gradient); - } else { - varpattern[strlen(varpattern) - 2] = '\0'; - printf("query_gradient(%s,%s,sigma,%e).\n", arg[params.queryid], varpattern, tvalue.gradient); - } - } - ReInitHistory(MyManager.his, MyManager.varmap.varcnt); - if (varpattern != NULL) free(varpattern); + if (varpattern != NULL) + free(varpattern); } + } else { // it is! let's do the Hybrid Problog Magic + // first for mu + varpattern = extractpattern(MyManager.varmap.vars[i]); + if ((varpattern == NULL) || + (!patterncalculated(varpattern, MyManager, i))) { + tvalue = CalcGradient(MyManager, bdd, + i + MyManager.varmap.varstart, + varpattern, 1); + probability = tvalue.probability; + if (varpattern == NULL) { + printf("query_gradient(%s,%s,mu,%e).\n", + arg[params.queryid], MyManager.varmap.vars[i], + tvalue.gradient); + } else { + varpattern[strlen(varpattern) - 2] = '\0'; + printf("query_gradient(%s,%s,mu,%e).\n", + arg[params.queryid], varpattern, tvalue.gradient); + } + } + ReInitHistory(MyManager.his, MyManager.varmap.varcnt); + if (varpattern != NULL) + free(varpattern); - } else { - fprintf(stderr, "Error: no variable name given for parameter.\n"); + // then for sigma + varpattern = extractpattern(MyManager.varmap.vars[i]); + if ((varpattern == NULL) || + (!patterncalculated(varpattern, MyManager, i))) { + tvalue = CalcGradient(MyManager, bdd, + i + MyManager.varmap.varstart, + varpattern, 2); + probability = tvalue.probability; + if (varpattern == NULL) { + printf("query_gradient(%s,%s,sigma,%e).\n", + arg[params.queryid], MyManager.varmap.vars[i], + tvalue.gradient); + } else { + varpattern[strlen(varpattern) - 2] = '\0'; + printf("query_gradient(%s,%s,sigma,%e).\n", + arg[params.queryid], varpattern, tvalue.gradient); + } + } + ReInitHistory(MyManager.his, MyManager.varmap.varcnt); + if (varpattern != NULL) + free(varpattern); } + + } else { + fprintf(stderr, "Error: no variable name given for parameter.\n"); } - if (probability < 0.0) { - // no nodes, so we have to calculate probability ourself - tvalue = CalcGradient(MyManager, bdd, 0 + MyManager.varmap.varstart, NULL, 0); - probability = tvalue.probability; - } - printf("query_probability(%s,%e).\n", arg[params.queryid], probability); - break; - case 'l': - tvalue = CalcGradient(MyManager, bdd, 0 + MyManager.varmap.varstart, NULL, 0); + } + if (probability < 0.0) { + // no nodes, so we have to calculate probability ourself + tvalue = CalcGradient(MyManager, bdd, 0 + MyManager.varmap.varstart, + NULL, 0); probability = tvalue.probability; - printf("query_probability(%s,%e).\n", arg[params.queryid], probability); - break; - case 'e': - //fprintf(stderr,"start calc exp count\n"); - printf("query_probability(%s,%30.30e).\n", arg[params.queryid],CalcExpectedCounts(&MyManager, bdd,arg[params.queryid],1)); - break; - case 'd': - //fprintf(stderr,"start calc exp count\n"); - printf("query_probability(%s,%30.30e).\n", arg[params.queryid],CalcExpectedCounts(&MyManager, bdd,arg[params.queryid],0)); - break; - case 'p': - printf("query_probability(%s,%e).\n", arg[params.queryid], CalcProbability(MyManager, bdd)); - break; - case 'o': - onlinetraverse(MyManager.manager, MyManager.varmap, MyManager.his, bdd); - break; - default: - myexpand(MyManager, bdd); - break; + } + printf("query_probability(%s,%e).\n", arg[params.queryid], + probability); + break; + case 'l': + tvalue = CalcGradient(MyManager, bdd, 0 + MyManager.varmap.varstart, + NULL, 0); + probability = tvalue.probability; + printf("query_probability(%s,%e).\n", arg[params.queryid], + probability); + break; + case 'e': + // fprintf(stderr,"start calc exp count\n"); + printf("query_probability(%s,%30.30e).\n", arg[params.queryid], + CalcExpectedCounts(&MyManager, bdd, arg[params.queryid], 1)); + break; + case 'd': + // fprintf(stderr,"start calc exp count\n"); + printf("query_probability(%s,%30.30e).\n", arg[params.queryid], + CalcExpectedCounts(&MyManager, bdd, arg[params.queryid], 0)); + break; + case 'p': + printf("query_probability(%s,%e).\n", arg[params.queryid], + CalcProbability(MyManager, bdd)); + break; + case 'o': + onlinetraverse(MyManager.manager, MyManager.varmap, MyManager.his, + bdd); + break; + default: + myexpand(MyManager, bdd); + break; } } else { myexpand(MyManager, bdd); @@ -458,10 +509,14 @@ int main(int argc, char **arg) { bdd = NULL; } ReInitHistory(MyManager.his, MyManager.varmap.varcnt); - } while(bdd != NULL); + } while (bdd != NULL); bdd = bakbdd; - if (params.savedfile > -1) SaveNodeDump(MyManager.manager, MyManager.varmap, bdd, arg[params.savedfile]); - if (params.exportfile > -1) simpleNamedBDDtoDot(MyManager.manager, MyManager.varmap, bdd, arg[params.exportfile]); + if (params.savedfile > -1) + SaveNodeDump(MyManager.manager, MyManager.varmap, bdd, + arg[params.savedfile]); + if (params.exportfile > -1) + simpleNamedBDDtoDot(MyManager.manager, MyManager.varmap, bdd, + arg[params.exportfile]); free(MyManager.his); } if (MyManager.manager != NULL) { @@ -469,70 +524,113 @@ int main(int argc, char **arg) { free(MyManager.varmap.dvalue); free(MyManager.varmap.ivalue); if (MyManager.varmap.dynvalue != NULL) { - for(i = 0; i < MyManager.varmap.varcnt; i++) + for (i = 0; i < MyManager.varmap.varcnt; i++) if (MyManager.varmap.dynvalue[i] != NULL) { free(MyManager.varmap.dynvalue[i]); } free(MyManager.varmap.dynvalue); } for (i = 0; i < MyManager.varmap.varcnt; i++) - free((const char *)MyManager.varmap.vars[i]); + free((void *)MyManager.varmap.vars[i]); free(MyManager.varmap.vars); } - if (params.error != NULL) free(params.error); + if (params.error != NULL) + free(params.error); return code; - } /* Shell Parameters handling */ int argtype(const char *arg) { - if (strcmp(arg, "-l") == 0 || strcmp(arg, "--load") == 0) return 0; - if (strcmp(arg, "-e") == 0 || strcmp(arg, "--export") == 0) return 2; - if (strcmp(arg, "-m") == 0 || strcmp(arg, "--method") == 0) return 3; - if (strcmp(arg, "-i") == 0 || strcmp(arg, "--input") == 0) return 4; - if (strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0) return 5; - if (strcmp(arg, "-d") == 0 || strcmp(arg, "--debug") == 0) return 6; - if (strcmp(arg, "-id") == 0 || strcmp(arg, "--queryid") == 0) return 7; - if (strcmp(arg, "-t") == 0 || strcmp(arg, "--timeout") == 0) return 8; - if (strcmp(arg, "-sd") == 0 || strcmp(arg, "--savedump") == 0) return 9; - if (strcmp(arg, "-sl") == 0 || strcmp(arg, "--slope") == 0) return 10; - if (strcmp(arg, "-o") == 0 || strcmp(arg, "--online") == 0) return 11; - if (strcmp(arg, "-bs") == 0 || strcmp(arg, "--bufsize") == 0) return 12; - if (strcmp(arg, "-pid") == 0 || strcmp(arg, "--pid") == 0) return 13; - if (strcmp(arg, "-ord") == 0 || strcmp(arg, "--order") == 0) return 14; + if (strcmp(arg, "-l") == 0 || strcmp(arg, "--load") == 0) + return 0; + if (strcmp(arg, "-e") == 0 || strcmp(arg, "--export") == 0) + return 2; + if (strcmp(arg, "-m") == 0 || strcmp(arg, "--method") == 0) + return 3; + if (strcmp(arg, "-i") == 0 || strcmp(arg, "--input") == 0) + return 4; + if (strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0) + return 5; + if (strcmp(arg, "-d") == 0 || strcmp(arg, "--debug") == 0) + return 6; + if (strcmp(arg, "-id") == 0 || strcmp(arg, "--queryid") == 0) + return 7; + if (strcmp(arg, "-t") == 0 || strcmp(arg, "--timeout") == 0) + return 8; + if (strcmp(arg, "-sd") == 0 || strcmp(arg, "--savedump") == 0) + return 9; + if (strcmp(arg, "-sl") == 0 || strcmp(arg, "--slope") == 0) + return 10; + if (strcmp(arg, "-o") == 0 || strcmp(arg, "--online") == 0) + return 11; + if (strcmp(arg, "-bs") == 0 || strcmp(arg, "--bufsize") == 0) + return 12; + if (strcmp(arg, "-pid") == 0 || strcmp(arg, "--pid") == 0) + return 13; + if (strcmp(arg, "-ord") == 0 || strcmp(arg, "--order") == 0) + return 14; return -1; } void printhelp(int argc, char **arg) { fprintf(stderr, "\n\nProbLogBDD Tool Version: %s\n\n", VERSION); - fprintf(stderr, "SimpleCUDD library (www.cs.kuleuven.be/~theo/tools/simplecudd.html)\n"); - fprintf(stderr, "SimpleCUDD was developed at Katholieke Universiteit Leuven(www.kuleuven.be)\n"); + fprintf( + stderr, + "SimpleCUDD library (www.cs.kuleuven.be/~theo/tools/simplecudd.html)\n"); + fprintf(stderr, "SimpleCUDD was developed at Katholieke Universiteit " + "Leuven(www.kuleuven.be)\n"); fprintf(stderr, "Copyright Katholieke Universiteit Leuven 2008\n"); - fprintf(stderr, "Authors: Theofrastos Mantadelis, Angelika Kimmig, Bernd Gutmann\n"); + fprintf(stderr, + "Authors: Theofrastos Mantadelis, Angelika Kimmig, Bernd Gutmann\n"); fprintf(stderr, "This package falls under the: Artistic License 2.0\n"); - fprintf(stderr, "\nUsage: %s -l [filename] -i [filename] -o (-s(d) [filename] -e [filename] -m [method] -id [queryid] -sl [double]) (-t [seconds] -d -h)\n", arg[0]); + fprintf(stderr, "\nUsage: %s -l [filename] -i [filename] -o (-s(d) " + "[filename] -e [filename] -m [method] -id [queryid] -sl " + "[double]) (-t [seconds] -d -h)\n", + arg[0]); fprintf(stderr, "Generates and traverses a BDD\nMandatory parameters:\n"); - fprintf(stderr, "\t-l [filename]\t->\tfilename to load supports two formats:\n\t\t\t\t\t\t1. script with generation instructions\n\t\t\t\t\t\t2. node dump saved file\n"); - fprintf(stderr, "\t-i [filename]\t->\tfilename to input problem specifics (mandatory with file formats 1, 2)\n"); - fprintf(stderr, "\t-o\t\t->\tgenerates the BDD in online mode instead from a file can be used instead of -l\n"); + fprintf(stderr, "\t-l [filename]\t->\tfilename to load supports two " + "formats:\n\t\t\t\t\t\t1. script with generation " + "instructions\n\t\t\t\t\t\t2. node dump saved file\n"); + fprintf(stderr, "\t-i [filename]\t->\tfilename to input problem specifics " + "(mandatory with file formats 1, 2)\n"); + fprintf(stderr, "\t-o\t\t->\tgenerates the BDD in online mode instead from a " + "file can be used instead of -l\n"); fprintf(stderr, "Optional parameters:\n"); - fprintf(stderr, "\t-sd [filename]\t->\tfilename to save generated BDD in node dump format (fast loading, traverse valid only)\n"); - fprintf(stderr, "\t-e [filename]\t->\tfilename to export generated BDD in dot format\n"); - fprintf(stderr, "\t-m [method]\t->\tthe calculation method to be used: none(default), [p]robability, [g]radient, [l]ine search, [o]nline, [e]xpexted counts, prob. with [d]eterministic nodes\n"); - fprintf(stderr, "\t-id [queryid]\t->\tthe queries identity name (used by gradient) default: %s\n", arg[0]); - fprintf(stderr, "\t-sl [double]\t->\tthe sigmoid slope (used by gradient) default: 1.0\n"); + fprintf(stderr, "\t-sd [filename]\t->\tfilename to save generated BDD in " + "node dump format (fast loading, traverse valid only)\n"); + fprintf( + stderr, + "\t-e [filename]\t->\tfilename to export generated BDD in dot format\n"); + fprintf(stderr, "\t-m [method]\t->\tthe calculation method to be used: " + "none(default), [p]robability, [g]radient, [l]ine search, " + "[o]nline, [e]xpexted counts, prob. with [d]eterministic " + "nodes\n"); + fprintf(stderr, "\t-id [queryid]\t->\tthe queries identity name (used by " + "gradient) default: %s\n", + arg[0]); + fprintf(stderr, "\t-sl [double]\t->\tthe sigmoid slope (used by gradient) " + "default: 1.0\n"); fprintf(stderr, "Extra parameters:\n"); - fprintf(stderr, "\t-t [seconds]\t->\tthe seconds (int) for BDD generation timeout default 0 = no timeout\n"); - fprintf(stderr, "\t-pid [pid]\t->\ta process id (int) to check for termination default 0 = no process to check\n"); - fprintf(stderr, "\t-bs [bytes]\t->\tthe bytes (int) to use as a maximum buffer size to read files default 0 = no max\n"); - fprintf(stderr, "\t-ord [filename]\t->\tUse the [filename] to define a specific BDD variable order\n"); - fprintf(stderr, "\t-d\t\t->\tRun in debug mode (gives extra messages in stderr)\n"); + fprintf(stderr, "\t-t [seconds]\t->\tthe seconds (int) for BDD generation " + "timeout default 0 = no timeout\n"); + fprintf(stderr, "\t-pid [pid]\t->\ta process id (int) to check for " + "termination default 0 = no process to check\n"); + fprintf(stderr, "\t-bs [bytes]\t->\tthe bytes (int) to use as a maximum " + "buffer size to read files default 0 = no max\n"); + fprintf(stderr, "\t-ord [filename]\t->\tUse the [filename] to define a " + "specific BDD variable order\n"); + fprintf(stderr, + "\t-d\t\t->\tRun in debug mode (gives extra messages in stderr)\n"); fprintf(stderr, "\t-h\t\t->\tHelp (displays this message)\n"); - fprintf(stderr, "Extra notes:\nSupports a forest of BDDs in one shared BDD.\nSelected computational methods will be applied to each BDD seperately.\nFile operations will be applied only to the first BDD.\n"); - fprintf(stderr, "\nExample: %s -l testbdd -i input.txt -m g -id testbdd\n", arg[0]); + fprintf(stderr, "Extra notes:\nSupports a forest of BDDs in one shared " + "BDD.\nSelected computational methods will be applied to " + "each BDD seperately.\nFile operations will be applied only " + "to the first BDD.\n"); + fprintf(stderr, "\nExample: %s -l testbdd -i input.txt -m g -id testbdd\n", + arg[0]); } parameters loadparam(int argc, char **arg) { @@ -552,122 +650,122 @@ parameters loadparam(int argc, char **arg) { params.maxbufsize = 0; params.ppid = NULL; params.orderfile = -1; - params.error = (int *) malloc(argc * sizeof(int)); + params.error = (int *)malloc(argc * sizeof(int)); for (i = 1; i < argc; i++) { - switch(argtype(arg[i])) { - case 0: - if (argc > i + 1) { - i++; - params.loadfile = i; - } else { - params.error[params.errorcnt] = i; - params.errorcnt++; - } - break; - case 2: - if (argc > i + 1) { - i++; - params.exportfile = i; - } else { - params.error[params.errorcnt] = i; - params.errorcnt++; - } - break; - case 3: - if (argc > i + 1) { - i++; - params.method = i; - } else { - params.error[params.errorcnt] = i; - params.errorcnt++; - } - break; - case 4: - if (argc > i + 1) { - i++; - params.inputfile = i; - } else { - params.error[params.errorcnt] = i; - params.errorcnt++; - } - break; - case 5: - printhelp(argc, arg); - break; - case 6: - params.debug = 1; - break; - case 7: - if (argc > i + 1) { - i++; - params.queryid = i; - } else { - params.error[params.errorcnt] = i; - params.errorcnt++; - } - break; - case 8: - if ((argc > i + 1) && (IsPosNumber(arg[i + 1]))) { - i++; - params.timeout = atoi(arg[i]); - } else { - params.error[params.errorcnt] = i; - params.errorcnt++; - } - break; - case 9: - if (argc > i + 1) { - i++; - params.savedfile = i; - } else { - params.error[params.errorcnt] = i; - params.errorcnt++; - } - break; - case 10: - if ((argc > i + 1) && (IsRealNumber(arg[i + 1]))) { - i++; - params.sigmoid_slope = atof(arg[i]); - } else { - params.error[params.errorcnt] = i; - params.errorcnt++; - } - break; - case 11: - params.online = 1; - break; - case 12: - if ((argc > i + 1) && (IsPosNumber(arg[i + 1]))) { - i++; - params.maxbufsize = atoi(arg[i]); - } else { - params.error[params.errorcnt] = i; - params.errorcnt++; - } - break; - case 13: - if ((argc > i + 1) && (IsPosNumber(arg[i + 1]))) { - i++; - params.ppid = (char *) malloc(sizeof(char) * (strlen(arg[i]) + 1)); - strcpy(params.ppid, arg[i]); - } else { - params.error[params.errorcnt] = i; - params.errorcnt++; - } - break; - case 14: - if (argc > i + 1) { - i++; - params.orderfile = i; - } else { - params.error[params.errorcnt] = i; - params.errorcnt++; - } - break; - default: + switch (argtype(arg[i])) { + case 0: + if (argc > i + 1) { + i++; + params.loadfile = i; + } else { params.error[params.errorcnt] = i; params.errorcnt++; - break; + } + break; + case 2: + if (argc > i + 1) { + i++; + params.exportfile = i; + } else { + params.error[params.errorcnt] = i; + params.errorcnt++; + } + break; + case 3: + if (argc > i + 1) { + i++; + params.method = i; + } else { + params.error[params.errorcnt] = i; + params.errorcnt++; + } + break; + case 4: + if (argc > i + 1) { + i++; + params.inputfile = i; + } else { + params.error[params.errorcnt] = i; + params.errorcnt++; + } + break; + case 5: + printhelp(argc, arg); + break; + case 6: + params.debug = 1; + break; + case 7: + if (argc > i + 1) { + i++; + params.queryid = i; + } else { + params.error[params.errorcnt] = i; + params.errorcnt++; + } + break; + case 8: + if ((argc > i + 1) && (IsPosNumber(arg[i + 1]))) { + i++; + params.timeout = atoi(arg[i]); + } else { + params.error[params.errorcnt] = i; + params.errorcnt++; + } + break; + case 9: + if (argc > i + 1) { + i++; + params.savedfile = i; + } else { + params.error[params.errorcnt] = i; + params.errorcnt++; + } + break; + case 10: + if ((argc > i + 1) && (IsRealNumber(arg[i + 1]))) { + i++; + params.sigmoid_slope = atof(arg[i]); + } else { + params.error[params.errorcnt] = i; + params.errorcnt++; + } + break; + case 11: + params.online = 1; + break; + case 12: + if ((argc > i + 1) && (IsPosNumber(arg[i + 1]))) { + i++; + params.maxbufsize = atoi(arg[i]); + } else { + params.error[params.errorcnt] = i; + params.errorcnt++; + } + break; + case 13: + if ((argc > i + 1) && (IsPosNumber(arg[i + 1]))) { + i++; + params.ppid = (char *)malloc(sizeof(char) * (strlen(arg[i]) + 1)); + strcpy(params.ppid, arg[i]); + } else { + params.error[params.errorcnt] = i; + params.errorcnt++; + } + break; + case 14: + if (argc > i + 1) { + i++; + params.orderfile = i; + } else { + params.error[params.errorcnt] = i; + params.errorcnt++; + } + break; + default: + params.error[params.errorcnt] = i; + params.errorcnt++; + break; } } return params; @@ -689,9 +787,12 @@ void pidhandler(int num) { exit(-1); } } - s = (char *) malloc(sizeof(char) * (19 + strlen(params.ppid))); - strcpy(s, "ps "); strcat(s, params.ppid); strcat(s, " >/dev/null"); - if (system(s) != 0) exit(4); + s = (char *)malloc(sizeof(char) * (19 + strlen(params.ppid))); + strcpy(s, "ps "); + strcat(s, params.ppid); + strcat(s, " >/dev/null"); + if (system(s) != 0) + exit(4); #ifndef __MINGW32__ signal(SIGALRM, pidhandler); #endif @@ -699,9 +800,7 @@ void pidhandler(int num) { free(s); } -void termhandler(int num) { - exit(3); -} +void termhandler(int num) { exit(3); } /* Debugging traverse function */ @@ -712,7 +811,8 @@ void myexpand(extmanager MyManager, DdNode *Current) { curnode = GetNodeVarNameDisp(MyManager.manager, MyManager.varmap, Current); printf("%s\n", curnode); if ((Current != MyManager.t) && (Current != MyManager.f) && - ((Found = GetNode(MyManager.his, MyManager.varmap.varstart, Current)) == NULL)) { + ((Found = GetNode(MyManager.his, MyManager.varmap.varstart, Current)) == + NULL)) { l = LowNodeOf(MyManager.manager, Current); h = HighNodeOf(MyManager.manager, Current); printf("l(%s)->", curnode); @@ -739,27 +839,33 @@ double CalcProbability(extmanager MyManager, DdNode *Current) { // gcc stupidly complains. curnode = NULL; } - if (Current == MyManager.t) return 1.0; - if (Current == MyManager.f) return 0.0; + if (Current == MyManager.t) + return 1.0; + if (Current == MyManager.f) + return 0.0; - if ((Found = GetNode(MyManager.his, MyManager.varmap.varstart, Current)) != NULL) return Found->dvalue; + if ((Found = GetNode(MyManager.his, MyManager.varmap.varstart, Current)) != + NULL) + return Found->dvalue; l = LowNodeOf(MyManager.manager, Current); h = HighNodeOf(MyManager.manager, Current); - if (params.debug) fprintf(stderr, "l(%s)->", curnode); + if (params.debug) + fprintf(stderr, "l(%s)->", curnode); lvalue = CalcProbability(MyManager, l); - if (params.debug) fprintf(stderr, "h(%s)->", curnode); + if (params.debug) + fprintf(stderr, "h(%s)->", curnode); hvalue = CalcProbability(MyManager, h); - - tvalue = MyManager.varmap.dvalue[GetIndex(Current) - MyManager.varmap.varstart]; + tvalue = + MyManager.varmap.dvalue[GetIndex(Current) - MyManager.varmap.varstart]; tvalue = tvalue * hvalue + lvalue * (1.0 - tvalue); AddNode(MyManager.his, MyManager.varmap.varstart, Current, tvalue, 0, NULL); return tvalue; } - -double CalcExpectedCounts(extmanager * MyManager, DdNode *Current, char *query_id, int calcdown_needed) { +double CalcExpectedCounts(extmanager *MyManager, DdNode *Current, + char *query_id, int calcdown_needed) { // fprintf(stderr,"%%calcing up\n"); double ret = CalcExpectedCountsUp(MyManager, Current, query_id); @@ -769,14 +875,13 @@ double CalcExpectedCounts(extmanager * MyManager, DdNode *Current, char *query_i if (calcdown_needed != 0) { // double retd=CalcExpectedCountsDown(MyManager,Current, query_id); } -/* if(1 != retd){ */ -/* fprintf(stderr,"down %e != up %e/%e\n",ret,retd,ret); */ -/* exit(1); */ -/* } */ + /* if(1 != retd){ */ + /* fprintf(stderr,"down %e != up %e/%e\n",ret,retd,ret); */ + /* exit(1); */ + /* } */ return ret; } - /* ComparisonFunction compare_nodes ( extmanager MyManager) */ /* { */ /* //fprintf(stderr,"creating comparator for %p\n",MyManager); */ @@ -790,11 +895,14 @@ double CalcExpectedCounts(extmanager * MyManager, DdNode *Current, char *query_i /* int aindex,bindex, aperm, bperm; */ /* aindex=GetIndex(a); */ /* bindex=GetIndex(b); */ -/* aperm=Cudd_IsConstant(a) ? CUDD_CONST_INDEX : Cudd_ReadPerm(MyManager.manager,aindex); */ +/* aperm=Cudd_IsConstant(a) ? CUDD_CONST_INDEX : + * Cudd_ReadPerm(MyManager.manager,aindex); */ /* Cudd_ReadPerm(MyManager.manager,bindex); */ -/* bperm=Cudd_IsConstant(b) ? CUDD_CONST_INDEX : Cudd_ReadPerm(MyManager.manager,bindex); */ +/* bperm=Cudd_IsConstant(b) ? CUDD_CONST_INDEX : + * Cudd_ReadPerm(MyManager.manager,bindex); */ /* int temp = aperm-bperm; */ -/* //-Cudd_ReadPerm(MyManager.manager,(*b).index);//-Cudd_ReadPerm(MyManager,b);// - Cudd_ReadPerm(b); */ +/* //-Cudd_ReadPerm(MyManager.manager,(*b).index);//-Cudd_ReadPerm(MyManager,b);// + * - Cudd_ReadPerm(b); */ /* //fprintf(stderr,"comparing3 %p %p %p\n",a,b,MyManager); */ /* // return -1; */ /* if (temp < 0) */ @@ -810,244 +918,314 @@ double CalcExpectedCounts(extmanager * MyManager, DdNode *Current, char *query_i /* } */ #define NODE_VALUE 1001 -#define LOG_EXPECTED 0 +#define LOG_EXPECTED 0 +static void PrintNodeQueue(Queue q, extmanager MyManager) { -static -void PrintNodeQueue(Queue q , extmanager MyManager){ - - QueueIterator qiter = QueueIteratorNew(q, 1); - fprintf(stderr,"Queue %p is [", q); - - while (qiter->currentItem != NULL) { - DdNode* val = (DdNode*) qiter->currentItem->element; - QueueIteratorAdvance(qiter); - fprintf(stderr," %s %s", GetNodeVarNameDisp(MyManager.manager, MyManager.varmap, val), - (qiter->currentItem !=NULL)?",":"]\n"); - } + QueueIterator qiter = QueueIteratorNew(q, 1); + fprintf(stderr, "Queue %p is [", q); + while (qiter->currentItem != NULL) { + DdNode *val = (DdNode *)qiter->currentItem->element; + QueueIteratorAdvance(qiter); + fprintf(stderr, " %s %s", + GetNodeVarNameDisp(MyManager.manager, MyManager.varmap, val), + (qiter->currentItem != NULL) ? "," : "]\n"); + } } - /** also nesting in CalcExpected seems to not work (must be here nested only valid within function frame)*/ +/** also nesting in CalcExpected seems to not work (must be here nested only + * valid within function frame)*/ /* will be changed at later stage */ -static extmanager * ineedtostorethatsomehow; +static extmanager *ineedtostorethatsomehow; -static -int comparator(void *av, void *bv){ - int ret = 0; - DdNode* a = (DdNode*)av; - DdNode* b = (DdNode*)bv; - int aindex,bindex, aperm, bperm; - aindex=GetIndex(a); - bindex=GetIndex(b); - aperm=Cudd_IsConstant(a) ? CUDD_CONST_INDEX : Cudd_ReadPerm(ineedtostorethatsomehow->manager,aindex); - Cudd_ReadPerm(ineedtostorethatsomehow->manager,bindex); - bperm=Cudd_IsConstant(b) ? CUDD_CONST_INDEX : Cudd_ReadPerm(ineedtostorethatsomehow->manager,bindex); - int temp = -aperm+bperm; - if (temp < 0) - ret= 1; - else if (temp > 0) - ret= -1; - // else //never return zero otherwise one is pruned away, or(?) - // return 0; - if(LOG_EXPECTED){ - fprintf(stderr,"perm(%s,%i)=%i perm(%s,%i)=%i => %i\n",GetNodeVarNameDisp(ineedtostorethatsomehow->manager, ineedtostorethatsomehow->varmap, a), - aindex, - aperm, - GetNodeVarNameDisp(ineedtostorethatsomehow->manager, ineedtostorethatsomehow->varmap, b), - bindex,bperm, - ret);} +static int comparator(void *av, void *bv) { + int ret = 0; + DdNode *a = (DdNode *)av; + DdNode *b = (DdNode *)bv; + int aindex, bindex, aperm, bperm; + aindex = GetIndex(a); + bindex = GetIndex(b); + aperm = Cudd_IsConstant(a) + ? CUDD_CONST_INDEX + : Cudd_ReadPerm(ineedtostorethatsomehow->manager, aindex); + Cudd_ReadPerm(ineedtostorethatsomehow->manager, bindex); + bperm = Cudd_IsConstant(b) + ? CUDD_CONST_INDEX + : Cudd_ReadPerm(ineedtostorethatsomehow->manager, bindex); + int temp = -aperm + bperm; + if (temp < 0) + ret = 1; + else if (temp > 0) + ret = -1; + // else //never return zero otherwise one is pruned away, or(?) + // return 0; + if (LOG_EXPECTED) { + fprintf(stderr, "perm(%s,%i)=%i perm(%s,%i)=%i => %i\n", + GetNodeVarNameDisp(ineedtostorethatsomehow->manager, + ineedtostorethatsomehow->varmap, a), + aindex, aperm, + GetNodeVarNameDisp(ineedtostorethatsomehow->manager, + ineedtostorethatsomehow->varmap, b), + bindex, bperm, ret); + } - return ret; + return ret; } -static void -skip_nodes_cnt(extmanager * MyManager, double (*counts)[] , int skipcnt, DdNode* l,double dprob, char *query_id); +static void skip_nodes_cnt(extmanager *MyManager, double (*counts)[], + int skipcnt, DdNode *l, double dprob, + char *query_id); /** output information for skipped nodes **/ -static void -skip_nodes(extmanager * MyManager, double (*counts)[] , DdNode* node, DdNode* l,double dprob, char *query_id){ +static void skip_nodes(extmanager *MyManager, double (*counts)[], DdNode *node, + DdNode *l, double dprob, char *query_id) { int skipcnt; - skipcnt = Cudd_ReadPerm(MyManager->manager,GetIndex(node))+1; - if(LOG_EXPECTED){fprintf(stderr,">> skipper >> %s=%i@%i of %i -> %i@%i %i\n", - (char *)(MyManager->varmap.dynvalue[GetIndex(node) - MyManager->varmap.varstart]), - GetIndex(node), - Cudd_ReadPerm(MyManager->manager,GetIndex(node)), - Cudd_ReadSize( MyManager->manager), - GetIndex(l), - Cudd_ReadPerm(MyManager->manager,GetIndex(l)), - Cudd_IsConstant(l) - ); + skipcnt = Cudd_ReadPerm(MyManager->manager, GetIndex(node)) + 1; + if (LOG_EXPECTED) { + fprintf(stderr, ">> skipper >> %s=%i@%i of %i -> %i@%i %i\n", + (char *)(MyManager->varmap.dynvalue[GetIndex(node) - + MyManager->varmap.varstart]), + GetIndex(node), Cudd_ReadPerm(MyManager->manager, GetIndex(node)), + Cudd_ReadSize(MyManager->manager), GetIndex(l), + Cudd_ReadPerm(MyManager->manager, GetIndex(l)), Cudd_IsConstant(l)); } - skip_nodes_cnt( MyManager, counts, skipcnt, l, dprob, query_id); + skip_nodes_cnt(MyManager, counts, skipcnt, l, dprob, query_id); } -static void -skip_nodes_cnt(extmanager * MyManager, double (*counts)[] , int skipcnt, DdNode* l,double dprob, char *query_id){ - if(LOG_EXPECTED) fprintf(stderr,"====================\n"); +static void skip_nodes_cnt(extmanager *MyManager, double (*counts)[], + int skipcnt, DdNode *l, double dprob, + char *query_id) { + if (LOG_EXPECTED) + fprintf(stderr, "====================\n"); double p; int ivalue; // fprintf(stderr, " skip (:%i) \n",__LINE__); - while(Cudd_IsConstant(l) ? - skipcnt < Cudd_ReadSize( MyManager->manager) // the terminals/leafs/constants will be ignored - : - skipcnt < Cudd_ReadPerm(MyManager->manager,GetIndex(l) ) - ){ + while (Cudd_IsConstant(l) + ? skipcnt < Cudd_ReadSize(MyManager->manager) // the + // terminals/leafs/constants + // will be ignored + : skipcnt < Cudd_ReadPerm(MyManager->manager, GetIndex(l))) { skipcnt++; - if(LOG_EXPECTED){ fprintf(stderr,"skipcnt %i\n",skipcnt-1);} - int idx=Cudd_ReadInvPerm(MyManager->manager,skipcnt-1); - if(LOG_EXPECTED){fprintf(stderr,"index %i %i\n",idx,MyManager->varmap.varstart);} - //fprintf(stdout,"%i %s.\n",skipcnt,MyManager->varmap.dynvalue[GetIndex(node) - MyManager->varmap.varstart]); - if(LOG_EXPECTED){fprintf(stderr,"Node skipped level %i index: %i name: %s (dprob is %e)\n", - skipcnt,idx, - MyManager->varmap.vars[idx - MyManager->varmap.varstart], - dprob);} - //notiz + if (LOG_EXPECTED) { + fprintf(stderr, "skipcnt %i\n", skipcnt - 1); + } + int idx = Cudd_ReadInvPerm(MyManager->manager, skipcnt - 1); + if (LOG_EXPECTED) { + fprintf(stderr, "index %i %i\n", idx, MyManager->varmap.varstart); + } + // fprintf(stdout,"%i + // %s.\n",skipcnt,MyManager->varmap.dynvalue[GetIndex(node) - + // MyManager->varmap.varstart]); + if (LOG_EXPECTED) { + fprintf(stderr, + "Node skipped level %i index: %i name: %s (dprob is %e)\n", + skipcnt, idx, + MyManager->varmap.vars[idx - MyManager->varmap.varstart], dprob); + } + // notiz ivalue = MyManager->varmap.ivalue[idx - MyManager->varmap.varstart]; //+ new{ - //double tvalue; // probability of prob fact corresp to node - //tvalue = MyManager->varmap.dvalue[idx - MyManager->varmap.varstart]; + // double tvalue; // probability of prob fact corresp to node + // tvalue = MyManager->varmap.dvalue[idx - MyManager->varmap.varstart]; //} - if(ivalue==1){ - p=dprob*MyManager->varmap.dvalue[idx - MyManager->varmap.varstart]; + if (ivalue == 1) { + p = dprob * MyManager->varmap.dvalue[idx - MyManager->varmap.varstart]; //+ new{ - //p=dprob*MyManager->varmap.dvalue[idx - MyManager->varmap.varstart] *tvalue; + // p=dprob*MyManager->varmap.dvalue[idx - MyManager->varmap.varstart] + // *tvalue; //} - if(p>0){// probability is zero, don't follow this branch - (*counts)[idx - MyManager->varmap.varstart]+=p; - // fprintf(stdout,"oec(%s,%s,%e). %%2\n",query_id,MyManager->varmap.vars[idx - MyManager->varmap.varstart],p); - if(LOG_EXPECTED) fprintf(stderr,"ec -> %s,%s,%e . %%2_1\n",query_id,MyManager->varmap.vars[idx - MyManager->varmap.varstart],p); - }else{ - if(LOG_EXPECTED){fprintf(stdout,"%% ec(%s,%s,%30.30e). %%2_2\n",query_id,MyManager->varmap.vars[idx - MyManager->varmap.varstart],p);} + if (p > 0) { // probability is zero, don't follow this branch + (*counts)[idx - MyManager->varmap.varstart] += p; + // fprintf(stdout,"oec(%s,%s,%e). + //%%2\n",query_id,MyManager->varmap.vars[idx - + // MyManager->varmap.varstart],p); + if (LOG_EXPECTED) + fprintf(stderr, "ec -> %s,%s,%e . %%2_1\n", query_id, + MyManager->varmap.vars[idx - MyManager->varmap.varstart], p); + } else { + if (LOG_EXPECTED) { + fprintf(stdout, "%% ec(%s,%s,%30.30e). %%2_2\n", query_id, + MyManager->varmap.vars[idx - MyManager->varmap.varstart], p); + } } } } // fprintf(stderr, " skip %i \n",__LINE__); - if(LOG_EXPECTED){fprintf(stderr,"skipped\n");} + if (LOG_EXPECTED) { + fprintf(stderr, "skipped\n"); + } } - -double CalcExpectedCountsDown(extmanager * MyManager, DdNode *Current, char *query_id) { - ineedtostorethatsomehow=MyManager; +double CalcExpectedCountsDown(extmanager *MyManager, DdNode *Current, + char *query_id) { + ineedtostorethatsomehow = MyManager; Queue q = QueueNew(); - //fprintf(stderr", =====> queue is: %p \n",q); + // fprintf(stderr", =====> queue is: %p \n",q); int i; - char *curnode, *curh, *curl,*dynvalue; + char *curnode, *curh, *curl, *dynvalue; DdNode *h, *l, *node; ComparisonFunction fun; - hisnode *Found = NULL,*lfound, *hfound; - double dprob; //downward probability of current node + hisnode *Found = NULL, *lfound, *hfound; + double dprob; // downward probability of current node double tvalue; // probability of prob fact corresp to node int ivalue; - double retval; //last value of true + double retval; // last value of true - double counts[MyManager->varmap.varcnt] ; - double (*pcnt)[MyManager->varmap.varcnt]; + double counts[MyManager->varmap.varcnt]; + double(*pcnt)[MyManager->varmap.varcnt]; pcnt = &counts; - for( i = 0 ;i< MyManager->varmap.varcnt ; i++){ - (*pcnt)[i]=0; + for (i = 0; i < MyManager->varmap.varcnt; i++) { + (*pcnt)[i] = 0; } // skip everything before the first node: - skip_nodes_cnt(MyManager,pcnt,0,Current,1,query_id); - + skip_nodes_cnt(MyManager, pcnt, 0, Current, 1, query_id); fun = *comparator; - if(LOG_EXPECTED){fprintf(stderr," ##############################\n");} - if(LOG_EXPECTED){ fprintf(stderr," ##############################\n fun is %p\n",fun);} - if(!Cudd_IsConstant(Current)){ - QueuePutOnPriority(q, Current, NODE_VALUE,fun); - Found = GetNode(MyManager->his, MyManager->varmap.varstart, Current); - (*Found).dvalue2=1.0/((*Found).dvalue); - dynvalue = (*Found).dynvalue; + if (LOG_EXPECTED) { + fprintf(stderr, " ##############################\n"); } - Current= NULL; // not used anymore or should not be - retval=0; + if (LOG_EXPECTED) { + fprintf(stderr, " ##############################\n fun is %p\n", fun); + } + if (!Cudd_IsConstant(Current)) { + QueuePutOnPriority(q, Current, NODE_VALUE, fun); + Found = GetNode(MyManager->his, MyManager->varmap.varstart, Current); + (*Found).dvalue2 = 1.0 / ((*Found).dvalue); + dynvalue = (*Found).dynvalue; + } + Current = NULL; // not used anymore or should not be + retval = 0; - while(QueueSize(q)>0){ - if(LOG_EXPECTED){fprintf(stderr,"\n");} - if(LOG_EXPECTED){PrintNodeQueue(q,*MyManager);} - node=QueueGet(q); + while (QueueSize(q) > 0) { + if (LOG_EXPECTED) { + fprintf(stderr, "\n"); + } + if (LOG_EXPECTED) { + PrintNodeQueue(q, *MyManager); + } + node = QueueGet(q); curnode = GetNodeVarNameDisp(MyManager->manager, MyManager->varmap, node); // int level = Cudd_ReadPerm(MyManager->manager,GetIndex(node)); - if(!Cudd_IsConstant(node)){ - tvalue = MyManager->varmap.dvalue[GetIndex(node) - MyManager->varmap.varstart]; - ivalue = MyManager->varmap.ivalue[GetIndex(node) - MyManager->varmap.varstart]; - dynvalue = MyManager->varmap.vars[GetIndex(node) - MyManager->varmap.varstart]; - Found = GetNode(MyManager->his, MyManager->varmap.varstart, node); - dprob=(*Found).dvalue2; - l = LowNodeOf(MyManager->manager, node); - h = HighNodeOf(MyManager->manager, node); - lfound = GetNode(MyManager->his, MyManager->varmap.varstart, l); - hfound = GetNode(MyManager->his, MyManager->varmap.varstart, h) ; - curh = GetNodeVarNameDisp(MyManager->manager, MyManager->varmap, h); - curl = GetNodeVarNameDisp(MyManager->manager, MyManager->varmap, l); + if (!Cudd_IsConstant(node)) { + tvalue = + MyManager->varmap.dvalue[GetIndex(node) - MyManager->varmap.varstart]; + ivalue = + MyManager->varmap.ivalue[GetIndex(node) - MyManager->varmap.varstart]; + dynvalue = + MyManager->varmap.vars[GetIndex(node) - MyManager->varmap.varstart]; + Found = GetNode(MyManager->his, MyManager->varmap.varstart, node); + dprob = (*Found).dvalue2; + l = LowNodeOf(MyManager->manager, node); + h = HighNodeOf(MyManager->manager, node); + lfound = GetNode(MyManager->his, MyManager->varmap.varstart, l); + hfound = GetNode(MyManager->his, MyManager->varmap.varstart, h); + curh = GetNodeVarNameDisp(MyManager->manager, MyManager->varmap, h); + curl = GetNodeVarNameDisp(MyManager->manager, MyManager->varmap, l); - if(LOG_EXPECTED){fprintf(stderr, "%s (%i)--> %s %s\n", curnode,(*node).index,curh,curl);} - /** low node */ - if((*lfound).dvalue2<-0.1){ //only if not seen before == dvalue2=0 (almost) otherwise requing does not harm - if(LOG_EXPECTED){fprintf(stderr,"queueing l(%s)=%s \n",curnode,curl);} - QueuePutOnPriority(q, l, NODE_VALUE,fun); - (*lfound).dvalue2=0; + if (LOG_EXPECTED) { + fprintf(stderr, "%s (%i)--> %s %s\n", curnode, (*node).index, curh, + curl); + } + /** low node */ + if ((*lfound).dvalue2 < -0.1) { // only if not seen before == dvalue2=0 + // (almost) otherwise requing does not + // harm + if (LOG_EXPECTED) { + fprintf(stderr, "queueing l(%s)=%s \n", curnode, curl); + } + QueuePutOnPriority(q, l, NODE_VALUE, fun); + (*lfound).dvalue2 = 0; + } + ((*lfound).dvalue2) = + ((*lfound).dvalue2) + (ivalue == 0 ? dprob : dprob * (1 - tvalue)); + if (LOG_EXPECTED) { + fprintf(stderr, "l(%s)=%s %e \n", curnode, curl, (*lfound).dvalue2); + } + if (LOG_EXPECTED) { + fprintf(stderr, "l(%s)=%s %e %e %e\n", curnode, curl, (*lfound).dvalue2, + tvalue, dprob); } - ((*lfound).dvalue2)=((*lfound).dvalue2)+(ivalue==0? dprob : dprob*(1-tvalue)); - if(LOG_EXPECTED){fprintf(stderr, "l(%s)=%s %e \n", curnode,curl,(*lfound).dvalue2);} - if(LOG_EXPECTED){fprintf(stderr, "l(%s)=%s %e %e %e\n", curnode,curl,(*lfound).dvalue2,tvalue,dprob);} /** high node */ - if((*hfound).dvalue2<-0.1){ //only if not seen before == dvalue2=0 (almost) otherwise requing does not harm - fun = *comparator; - (*fun)(l,l); - if(LOG_EXPECTED){ - PrintNodeQueue(q,*MyManager); - fprintf(stderr,"-> %p\n",h); - } - QueuePutOnPriority(q, h, NODE_VALUE,fun); - (*hfound).dvalue2=0; + if ((*hfound).dvalue2 < -0.1) { // only if not seen before == dvalue2=0 + // (almost) otherwise requing does not + // harm + fun = *comparator; + (*fun)(l, l); + if (LOG_EXPECTED) { + PrintNodeQueue(q, *MyManager); + fprintf(stderr, "-> %p\n", h); + } + QueuePutOnPriority(q, h, NODE_VALUE, fun); + (*hfound).dvalue2 = 0; + } + (*hfound).dvalue2 = + (*hfound).dvalue2 + (ivalue == 0 ? dprob : (dprob * (tvalue))); + if (LOG_EXPECTED) { + fprintf(stderr, "h(%s)=%s %e %e %e\n", curnode, curh, (*hfound).dvalue2, + tvalue, dprob); } - (*hfound).dvalue2=(*hfound).dvalue2+(ivalue==0? dprob : (dprob*(tvalue))); - if(LOG_EXPECTED){fprintf(stderr, "h(%s)=%s %e %e %e\n", curnode,curh,(*hfound).dvalue2,tvalue,dprob);} /** output expected counts current node */ - if(ivalue==1){ - (*pcnt)[GetIndex(node) - MyManager->varmap.varstart]+=dprob * tvalue * (*hfound).dvalue; - //fprintf(stdout,"oec(%s,%s,%e). %% 1_1\n",query_id,dynvalue,dprob * tvalue * (*hfound).dvalue); - if(LOG_EXPECTED) fprintf(stderr,"ec -> %s,%s,%e . %% 1_1\n",query_id,dynvalue,dprob * tvalue * (*hfound).dvalue); - }else{ - (*pcnt)[GetIndex(node) - MyManager->varmap.varstart]+=dprob * tvalue * (*hfound).dvalue; - if(LOG_EXPECTED) fprintf(stderr,"ec -> %s,%s,%e . %% 1_2\n",query_id,dynvalue,dprob * tvalue * (*hfound).dvalue); + if (ivalue == 1) { + (*pcnt)[GetIndex(node) - MyManager->varmap.varstart] += + dprob * tvalue * (*hfound).dvalue; + // fprintf(stdout,"oec(%s,%s,%e). %% 1_1\n",query_id,dynvalue,dprob * + // tvalue * (*hfound).dvalue); + if (LOG_EXPECTED) + fprintf(stderr, "ec -> %s,%s,%e . %% 1_1\n", query_id, dynvalue, + dprob * tvalue * (*hfound).dvalue); + } else { + (*pcnt)[GetIndex(node) - MyManager->varmap.varstart] += + dprob * tvalue * (*hfound).dvalue; + if (LOG_EXPECTED) + fprintf(stderr, "ec -> %s,%s,%e . %% 1_2\n", query_id, dynvalue, + dprob * tvalue * (*hfound).dvalue); } /** output expected counts of skipped nodes for low branch*/ - skip_nodes(MyManager,pcnt,node,l,dprob*((ivalue==0)?1:(1-tvalue))*(*lfound).dvalue,query_id); - skip_nodes(MyManager,pcnt,node,h,dprob*((ivalue==0)?1:(tvalue))*(*hfound).dvalue,query_id); - }else{ - if(LOG_EXPECTED){fprintf(stderr,"here: retval %s %e=>%e\n",curnode,retval,(*Found).dvalue2);} - if(node==(MyManager->t)){ - if(LOG_EXPECTED){fprintf(stderr,"updating retval %e=>%e\n",retval,(*Found).dvalue2);} - retval=(*Found).dvalue2; + skip_nodes(MyManager, pcnt, node, l, + dprob * ((ivalue == 0) ? 1 : (1 - tvalue)) * (*lfound).dvalue, + query_id); + skip_nodes(MyManager, pcnt, node, h, + dprob * ((ivalue == 0) ? 1 : (tvalue)) * (*hfound).dvalue, + query_id); + } else { + if (LOG_EXPECTED) { + fprintf(stderr, "here: retval %s %e=>%e\n", curnode, retval, + (*Found).dvalue2); + } + if (node == (MyManager->t)) { + if (LOG_EXPECTED) { + fprintf(stderr, "updating retval %e=>%e\n", retval, (*Found).dvalue2); + } + retval = (*Found).dvalue2; } - } } - for( i = 0 ;i< MyManager->varmap.varcnt ; i++){ + for (i = 0; i < MyManager->varmap.varcnt; i++) { ivalue = MyManager->varmap.ivalue[i]; /* fprintf(stderr,"Node level %i index: %i name: %s (dprob is %e)\n", */ /* i,idx, */ /* MyManager->varmap.vars[idx - MyManager->varmap.varstart], */ /* dprob); */ - //fprintf(stderr,"Node idx: %i level: %i \n",i,Cudd_ReadPerm(MyManager->manager,i)); - if(ivalue==0){ - fprintf(stdout,"%% det: ec(%s,%s,%30.30e).\n",query_id,MyManager->varmap.vars[i],(counts)[i]); - }else{ - fprintf(stdout,"ec(%s,%s,%30.30e).\n",query_id,MyManager->varmap.vars[i],(counts)[i]); - + // fprintf(stderr,"Node idx: %i level: %i + // \n",i,Cudd_ReadPerm(MyManager->manager,i)); + if (ivalue == 0) { + fprintf(stdout, "%% det: ec(%s,%s,%30.30e).\n", query_id, + MyManager->varmap.vars[i], (counts)[i]); + } else { + fprintf(stdout, "ec(%s,%s,%30.30e).\n", query_id, + MyManager->varmap.vars[i], (counts)[i]); } } // free(counts); - if(LOG_EXPECTED){ -fprintf(stderr,"retval is %e\n",retval); -} + if (LOG_EXPECTED) { + fprintf(stderr, "retval is %e\n", retval); + } return retval; } -double CalcExpectedCountsUp(extmanager * MyManager, DdNode *Current, char *query_id) { +double CalcExpectedCountsUp(extmanager *MyManager, DdNode *Current, + char *query_id) { // fprintf(stderr,"--------------------- the manager 2 %p \n",&MyManager); DdNode *h, *l; @@ -1057,52 +1235,62 @@ double CalcExpectedCountsUp(extmanager * MyManager, DdNode *Current, char *query // tvalue=0.0; int ivalue; if (params.debug) { - curnode = GetNodeVarNameDisp(MyManager->manager, MyManager->varmap, Current); + curnode = + GetNodeVarNameDisp(MyManager->manager, MyManager->varmap, Current); fprintf(stderr, "%s\n", curnode); } - if (Current == MyManager->t){ - // if ((Found = GetNode(MyManager->his, MyManager->varmap.varstart, Current)) == NULL) { + if (Current == MyManager->t) { + // if ((Found = GetNode(MyManager->his, MyManager->varmap.varstart, + // Current)) == NULL) { // fprintf(stderr,"adding true \n"); - AddNode(MyManager->his, MyManager->varmap.varstart, MyManager->t, 1, 0, NULL);//}//needed in down + AddNode(MyManager->his, MyManager->varmap.varstart, MyManager->t, 1, 0, + NULL); //}//needed in down return 1.0; } - if (Current == MyManager->f){ + if (Current == MyManager->f) { // fprintf(stderr,"adding false \n"); - // if ((Found = GetNode(MyManager->his, MyManager->varmap.varstart, Current)) == NULL) { - AddNode(MyManager->his, MyManager->varmap.varstart, MyManager->f, 0, 0, NULL);//}//needed in down + // if ((Found = GetNode(MyManager->his, MyManager->varmap.varstart, + // Current)) == NULL) { + AddNode(MyManager->his, MyManager->varmap.varstart, MyManager->f, 0, 0, + NULL); //}//needed in down return 0.0; } - if ((Found = GetNode(MyManager->his, MyManager->varmap.varstart, Current)) != NULL) return Found->dvalue; + if ((Found = GetNode(MyManager->his, MyManager->varmap.varstart, Current)) != + NULL) + return Found->dvalue; l = LowNodeOf(MyManager->manager, Current); h = HighNodeOf(MyManager->manager, Current); - if (params.debug) fprintf(stderr, "l(%s)->", curnode); - lvalue = CalcExpectedCountsUp(MyManager, l,query_id); - if (params.debug) fprintf(stderr, "h(%s)->", curnode); - hvalue = CalcExpectedCountsUp(MyManager, h,query_id); + if (params.debug) + fprintf(stderr, "l(%s)->", curnode); + lvalue = CalcExpectedCountsUp(MyManager, l, query_id); + if (params.debug) + fprintf(stderr, "h(%s)->", curnode); + hvalue = CalcExpectedCountsUp(MyManager, h, query_id); + tvalue = + MyManager->varmap.dvalue[GetIndex(Current) - MyManager->varmap.varstart]; + // notiz + ivalue = + MyManager->varmap.ivalue[GetIndex(Current) - MyManager->varmap.varstart]; - tvalue = MyManager->varmap.dvalue[GetIndex(Current) - MyManager->varmap.varstart]; - //notiz - ivalue = MyManager->varmap.ivalue[GetIndex(Current) - MyManager->varmap.varstart]; - - if(ivalue == 1){ + if (ivalue == 1) { tvalue = tvalue * hvalue + lvalue * (1.0 - tvalue); - }else if (ivalue == 0){ - tvalue = hvalue + lvalue ; + } else if (ivalue == 0) { + tvalue = hvalue + lvalue; } // fprintf(stderr," ---> %e \n",tvalue); AddNode(MyManager->his, MyManager->varmap.varstart, Current, tvalue, 0, NULL); return tvalue; } - /* Bernds Algorithm */ // type=0 regular probabilistic fact // type=1 derive gradient for mu // type=2 derive gradient for sigma -gradientpair CalcGradient(extmanager MyManager, DdNode *Current, int TargetVar, char *TargetPattern, int type) { +gradientpair CalcGradient(extmanager MyManager, DdNode *Current, int TargetVar, + char *TargetPattern, int type) { DdNode *h, *l; hisnode *Found; char *curnode = NULL, *dynvalue; @@ -1126,55 +1314,78 @@ gradientpair CalcGradient(extmanager MyManager, DdNode *Current, int TargetVar, tvalue.gradient = 0.0; return tvalue; } - //node is in cache - if ((Found = GetNode(MyManager.his, MyManager.varmap.varstart, Current)) != NULL) { + // node is in cache + if ((Found = GetNode(MyManager.his, MyManager.varmap.varstart, Current)) != + NULL) { tvalue.probability = Found->dvalue; - tvalue.gradient = *((double *) Found->dynvalue); + tvalue.gradient = *((double *)Found->dynvalue); return tvalue; } - //inductive case + // inductive case l = LowNodeOf(MyManager.manager, Current); h = HighNodeOf(MyManager.manager, Current); - if (params.debug) fprintf(stderr, "l(%s)->", curnode); - lowvalue = CalcGradient(MyManager, l, TargetVar, TargetPattern,type); - if (params.debug) fprintf(stderr, "h(%s)->", curnode); - highvalue = CalcGradient(MyManager, h, TargetVar, TargetPattern,type); - dynvalue = (char*) MyManager.varmap.dynvalue[GetIndex(Current) - MyManager.varmap.varstart]; + if (params.debug) + fprintf(stderr, "l(%s)->", curnode); + lowvalue = CalcGradient(MyManager, l, TargetVar, TargetPattern, type); + if (params.debug) + fprintf(stderr, "h(%s)->", curnode); + highvalue = CalcGradient(MyManager, h, TargetVar, TargetPattern, type); + dynvalue = (char *)MyManager.varmap + .dynvalue[GetIndex(Current) - MyManager.varmap.varstart]; if (dynvalue == NULL) { // no dynvalue, it's a regular probabilistic fact - memset( &dynvalue_parsed, 0, sizeof(dynvalue_parsed) ); - this_probability = sigmoid(MyManager.varmap.dvalue[GetIndex(Current) - MyManager.varmap.varstart], params.sigmoid_slope); - } else { // there is a dynvalue, it's a continuous fact! let's do the hybrid ProbLog magic here + memset(&dynvalue_parsed, 0, sizeof(dynvalue_parsed)); + this_probability = sigmoid( + MyManager.varmap.dvalue[GetIndex(Current) - MyManager.varmap.varstart], + params.sigmoid_slope); + } else { // there is a dynvalue, it's a continuous fact! let's do the hybrid + // ProbLog magic here curnode = GetNodeVarNameDisp(MyManager.manager, MyManager.varmap, Current); dynvalue_parsed = parse_density_integral_string(dynvalue, curnode); - this_probability=cumulative_normal(dynvalue_parsed.low,dynvalue_parsed.high,dynvalue_parsed.mu,dynvalue_parsed.sigma); + this_probability = + cumulative_normal(dynvalue_parsed.low, dynvalue_parsed.high, + dynvalue_parsed.mu, dynvalue_parsed.sigma); } - tvalue.probability = this_probability * highvalue.probability + (1 - this_probability) * lowvalue.probability; - tvalue.gradient = this_probability * highvalue.gradient + (1 - this_probability) * lowvalue.gradient; + tvalue.probability = this_probability * highvalue.probability + + (1 - this_probability) * lowvalue.probability; + tvalue.gradient = this_probability * highvalue.gradient + + (1 - this_probability) * lowvalue.gradient; if ((GetIndex(Current) == TargetVar) || - ((TargetPattern != NULL) && patternmatch(TargetPattern, MyManager.varmap.vars[GetIndex(Current)]))) { - if (type == 0) { // current node is normal probabilistic fact - tvalue.gradient += (highvalue.probability - lowvalue.probability) * this_probability * (1 - this_probability) * params.sigmoid_slope; + ((TargetPattern != NULL) && + patternmatch(TargetPattern, MyManager.varmap.vars[GetIndex(Current)]))) { + if (type == 0) { // current node is normal probabilistic fact + tvalue.gradient += (highvalue.probability - lowvalue.probability) * + this_probability * (1 - this_probability) * + params.sigmoid_slope; } else if (type == 1) { // it's a continues fact and we need d/dmu - tvalue.gradient += cumulative_normal_dmu(dynvalue_parsed.low, dynvalue_parsed.high, dynvalue_parsed.mu, dynvalue_parsed.sigma) * (highvalue.probability + lowvalue.probability); + tvalue.gradient += + cumulative_normal_dmu(dynvalue_parsed.low, dynvalue_parsed.high, + dynvalue_parsed.mu, dynvalue_parsed.sigma) * + (highvalue.probability + lowvalue.probability); } else if (type == 2) { // it's a continues fact and we need d/dsigma - tvalue.gradient += cumulative_normal_dsigma(dynvalue_parsed.low, dynvalue_parsed.high, dynvalue_parsed.mu, dynvalue_parsed.sigma) * (highvalue.probability + lowvalue.probability); + tvalue.gradient += + cumulative_normal_dsigma(dynvalue_parsed.low, dynvalue_parsed.high, + dynvalue_parsed.mu, dynvalue_parsed.sigma) * + (highvalue.probability + lowvalue.probability); } } - gradient = (double *) malloc(sizeof(double)); + gradient = (double *)malloc(sizeof(double)); *gradient = tvalue.gradient; - AddNode(MyManager.his, MyManager.varmap.varstart, Current, tvalue.probability, 0, gradient); + AddNode(MyManager.his, MyManager.varmap.varstart, Current, tvalue.probability, + 0, gradient); return tvalue; } -char * extractpattern(const char *thestr) { +char *extractpattern(const char *thestr) { char *p; int i = 0, sl = strlen(thestr); - while((thestr[i] != '_') && (i < sl)) i++; - if (i == sl) return NULL; + while ((thestr[i] != '_') && (i < sl)) + i++; + if (i == sl) + return NULL; i++; - p = (char *) malloc(sizeof(char) * (i + 2)); + p = (char *)malloc(sizeof(char) * (i + 2)); strncpy(p, thestr, i); p[i] = '*'; p[i + 1] = '\0'; @@ -1183,8 +1394,10 @@ char * extractpattern(const char *thestr) { int patterncalculated(char *pattern, extmanager MyManager, int loc) { int i; - if (pattern == NULL) return 0; + if (pattern == NULL) + return 0; for (i = loc - 1; i > -1; i--) - if (patternmatch(pattern, MyManager.varmap.vars[i])) return 1; + if (patternmatch(pattern, MyManager.varmap.vars[i])) + return 1; return 0; } diff --git a/packages/jpl/CMakeLists.txt b/packages/jpl/CMakeLists.txt index b1f131a1a..b0d66caa9 100644 --- a/packages/jpl/CMakeLists.txt +++ b/packages/jpl/CMakeLists.txt @@ -53,6 +53,13 @@ if (Java_Development_FOUND) target_link_libraries(jplYap libYap ${JAVA_LIBRARIES} ${JNI_LIBRARIES}) + #SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) + LIST(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${libdir};${dlls};${JAVA_JVM_LIBRARY}/..;${JAVA_JVM_LIBRARY}/..;" isSystemDir) + IF("${isSystemDir}" STREQUAL "-1") + SET(CMAKE_INSTALL_RPATH "${libdir};${dlls}") + ENDIF("${isSystemDir}" STREQUAL "-1") + + set_target_properties(jplYap PROPERTIES OUTPUT_NAME jpl ) diff --git a/pl/consult.yap b/pl/consult.yap index 727939a56..22c060519 100644 --- a/pl/consult.yap +++ b/pl/consult.yap @@ -473,9 +473,10 @@ load_files(Files,Opts) :- % check if there is a qly file % start_low_level_trace, '$absolute_file_name'(File,[access(read),file_type(qly),file_errors(fail),solutions(first),expand(true)],F,qload_file(File)), - open( F, read, Stream , [type(binary)] ), + open( F, read, Stream , [type(binary)] ), ( '$q_header'( Stream, Type ), + writeln(File:Type), Type == file -> ! diff --git a/pl/protect.yap b/pl/protect.yap index cdba357d5..86875b775 100755 --- a/pl/protect.yap +++ b/pl/protect.yap @@ -1,4 +1,4 @@ -xc/************************************************************************* +/************************************************************************* * * * YAP Prolog * * * @@ -20,7 +20,7 @@ xc/************************************************************************* % This protects all code from further changes % and also makes it impossible from some predicates to be seen '$protect' :- - '$current_predicate'(Name,M,P,_),riteln(P), + '$current_predicate'(Name,M,P,_), M \= user, functor(P,Name,Arity), '$new_system_predicate'(Name,Arity,M),