1/*****************************************************************************
    2 * This file is part of the Prolog Development Tool (PDT)
    3 * 
    4 * WWW: http://sewiki.iai.uni-bonn.de/research/pdt/start
    5 * Mail: pdt@lists.iai.uni-bonn.de
    6 * Copyright (C): 2004-2012, CS Dept. III, University of Bonn
    7 * 
    8 * All rights reserved. This program is  made available under the terms
    9 * of the Eclipse Public License v1.0 which accompanies this distribution,
   10 * and is available at http://www.eclipse.org/legal/epl-v10.html
   11 * 
   12 ****************************************************************************/
   13
   14:- module(parse_util, [	generate_facts/1, 
   15						update_facts/2,
   16						assert_new_node/4, 
   17						cleanup_nodes/0, 
   18						cleanup_nodes/1, 
   19						cleanup_computed_facts/0]).   20
   21:- reexport('pdt_factbase.pl').   22:- ensure_loaded('util/walking_prolog_files.pl').   23:- use_module(preparser).   24:- use_module(predicates).   25:- use_module(load_graph).   26:- use_module(modules_and_visibility).   27:- use_module(literal_parser).   28:- use_module(cross_reference_builder).   29
   30generate_facts(Project):-
   31    cleanup_nodes,
   32    walking_file_list(Project,parse,1),
   33	build_load_graph,
   34    derive_all_predicates,
   35	derive_directive_collections,
   36	compute_all_predicate_properties,
   37	compute_visibility_graph,
   38	parse_bodies,
   39	derive_edges.    
   40
   41%generate_facts(Project):-
   42%    writeln('cleaning up'),
   43%    cleanup_nodes,
   44%    writeln('start parsing clauses'),
   45%	time(walking_file_list(Project,parse,1)),
   46%	writeln('generating loadgraph'),
   47%	time(build_load_graph),
   48%    writeln('generating predicates'),
   49%	time(derive_all_predicates),
   50%	writeln('genereating directive collections'),
   51%	time(derive_directive_collections),
   52%	writeln('compute_predicate_properties'),
   53%	time(compute_all_predicate_properties),
   54%	writeln('compute_visibilities'),
   55%	time(compute_visibility_graph),
   56%	writeln('parse literals'),
   57%	time(parse_bodies),
   58%	writeln('generate edges'),
   59%	time(derive_edges).    
   60	
   61update_facts(File, Project):-				
   62	cleanup_nodes(File),
   63	cleanup_computed_facts,
   64   	walking_file_list([File|Project],parse,1),	
   65	build_load_graph,
   66	derive_predicates_of_files([File|Project]),
   67	derive_directive_collection_of_files([File|Project]),
   68	compute_predicate_properties_for_files([File|Project]),
   69	compute_visibility_graph,
   70	parse_bodies,
   71	derive_edges.
   72	
   73%update_facts(File, Project):-				
   74%	format('cleaning up facts for ~w~n',File),
   75%	cleanup_nodes(File),
   76%	cleanup_computed_facts,
   77%    writeln('start parsing clauses'),			
   78%	time(walking_file_list(Project,parse,1)),	
   79%	writeln('generating loadgraph'),
   80%	time(build_load_graph),
   81%    writeln('generating predicates'),
   82%	time(derive_all_predicates),
   83%	writeln('genereating directive collections'),
   84%	time(derive_onloads),
   85%	writeln('compute_predicate_properties'),
   86%	time(compute_all_predicate_properties),
   87%	writeln('compute_visibilities'),
   88%	time(compute_visibility_graph),
   89%	writeln('parse literals'),
   90%	time(parse_bodies),
   91%	writeln('generate edges'),
   92%	time(derive_edges).
   93	
   94
   95		
   96/*
   97 * cleanup_nodes/0 isdet
   98 * retracts everything a former run of parse_util:generate_facts/1 could have asserted.
   99 **/  
  100cleanup_nodes:-
  101	retractall(fileT(_,_,_)),
  102	retractall(literalT(_,_,_,_,_,_)),
  103	retractall(metaT(_,_,_,_,_,_)),
  104	retractall(headT(_,_,_,_,_)),
  105	retractall(clauseT(_,_,_,_,_)),
  106	retractall(directiveT(_,_,_)),
  107	retractall(operatorT(_,_,_,_,_,_,_,_)),
  108	retractall(predicateT(_,_,_,_,_)),
  109	retractall(onloadT(_,_,_)),
  110	retractall(dynamicT(_,_)),
  111	retractall(load_dir(_,_,_)),
  112	retractall(import_dir(_,_)),
  113	retractall(export_dir(_,_)),
  114	retractall(library_dir(_,_,_)),
  115	retractall(property_dir(_,_,_)),
  116	retractall(transparentT(_,_)),						
  117	retractall(multifileT(_,_)),	
  118	retractall(meta_predT(_,_)),
  119	retractall(termT(_,_)),
  120	retractall(filePosT(_,_,_)),
  121	retractall(literalT_ri(_,_,_,_)),     
  122	retractall(fileT_ri(_,_)),
  123	retractall(predicateT_ri(_,_,_,_)),
  124	retractall(pred_edge(_,_)),
  125	retractall(onload_edge(_,_)),
  126	retractall(pos_and_vars(_,_,_)),	
  127	retractall(error(_,_,_)),
  128	retractall(warning(_,_,_)),
  129	cleanup_computed_facts,
  130	ctc_id_init_pdt.
  131	
  132cleanup_computed_facts:-
  133    retractall(call_edge(_,_)),	
  134    
  135	retractall(load_edge(_,_,_,_)),
  136	retractall(call_built_in(_,_,_,_)),
  137
  138	retractall(meta_predT(_,found)).
  139    
  140
  141cleanup_nodes(File):-
  142    fileT_ri(File,Id), !,
  143    clean_file_entries(Id),
  144    retractall(fileT_ri(File,Id)),
  145    retractall(fileT(Id,_,_)),
  146    retractall(error(_,_,Id)).		%TODO: gegebenenfalls zu clean_general_references_to/1 schieben
  147cleanup_nodes(_).
  148   
  149   
  150    
  151clean_file_entries(FileId):-
  152	directiveT(DirId,FileId,_),
  153		termT(DirId,_),
  154		clean_directives(DirId),
  155%		retractall(directiveT(DirId,_,_)),
  156%		retractall(import_dir(_,DirId)),
  157%		retractall(export_dir(_,DirId)),
  158%		retractall(load_dir(DirId,_,_)),
  159		clean_general_references_to(DirId),
  160		retractall(directiveT(DirId,_,_)),
  161	fail.
  162clean_file_entries(FileId):-
  163	clauseT(ClauseId,FileId,_,_,_),
  164		clean_clause_references(ClauseId),
  165		clean_general_references_to(ClauseId),
  166		retractall(clauseT(ClauseId,_,_,_,_)),
  167	fail.	
  168clean_file_entries(FileId):-
  169    predicateT(PredId,FileId,_,_,_),
  170    	retractall(predicateT(PredId,_,_,_,_)),
  171    	retractall(predicateT_ri(_,_,_,PredId)),
  172    fail.
  173clean_file_entries(FileId):-
  174    onloadT(Id,FileId,_),
  175    	retractall(onloadT(Id,_,_)),
  176    fail.
  177clean_file_entries(_).
  178	
  179	
  180	
  181clean_clause_references(ClauseId):-
  182    headT(HeadId,ClauseId,_,_,_),
  183    	clean_clause_references(HeadId),
  184    	retractall(headT(HeadId,_,_,_,_,_)),
  185    fail.
  186clean_clause_references(ClauseId):-   
  187	literalT(LitId,_,ClauseId,_,_,_),
  188		clean_clause_references(LitId),
  189    	retractall(literalT(LitId,_,ClauseId,M,F,A)),
  190    	retractall(literalT_ri(F,A,M,LitId)),
  191    	retractall(metaT(LitId,_,ClauseId,_,_,_)),
  192    	retractall(pred_edge(ClauseId,_)),
  193    fail.
  194clean_clause_references(_).
  195
  196
  197
  198clean_directives(DirectiveId):-
  199    retractall(import_dir(_,DirectiveId)),
  200    retractall(export_dir(_,DirectiveId)),
  201    retractall(load_dir(DirectiveId,_,_)),
  202    retractall(property_dir(DirectiveId,_,_)),
  203    retractall(library_dir(_,_,DirectiveId)),
  204    retractall(meta_pred(_,DirectiveId)),
  205    retractall(onload_edge(DirectiveId,_)).
  206
  207
  208
  209clean_general_references_to(Id):-
  210	retractall(termT(Id,_)),
  211	retractall(filePosT(Id,_,_)),
  212	retractall(warning(Id,_,_)),
  213	retractall(pos_and_vars(Id,_,_)).
  214
  215
  216/*
  217 * assert_new_node(+Term,+From,+To,-Id)
  218 * 	creates new identity Arg4 and asserts termT and filePosT with the information given
  219 *  by Arg1-Arg3 to this identity. 
  220 *  the Arg6. 
  221 */   
  222assert_new_node(Term,From,To,Id):- 
  223    new_node_id_pdt(Id),	
  224	assert(termT(Id,Term)),
  225    Length is To - From,
  226    assert(filePosT(Id,From,Length))