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(pl_ast_to_abba, [	write_facts_to_abba/1, 
   15							pl_test/0,
   16							pl_test/2]).   17								
   18:- ensure_loaded(pdt_builder_analyzer('../parse_util')).   19
   20/*
   21 * write_facts_to_abba(+File)
   22 *   Arg1 has to be a full qualified file name. The file may not exist.
   23 *   The predicated collects the relevant informations about the following 
   24 *   facts and converts them into abba-sources for Bashaars Tool. This 
   25 *   sources are written into the file specified by arg1-
   26 *   The facts that are considered are:
   27 *   - fileT
   28 *	 - onlo
   29 **/
   30write_facts_to_abba(File):-
   31    open(File,write,OutStream,[type(text)]),
   32    write_files(OutStream),
   33    nl(OutStream),
   34    flush_output(OutStream),
   35    write_onloades(OutStream),
   36    nl(OutStream),
   37    flush_output(OutStream),
   38    write_predicates(OutStream),
   39    nl(OutStream),
   40    flush_output(OutStream),
   41    write_directives(OutStream),
   42    nl(OutStream),
   43    flush_output(OutStream),
   44    write_clauses(OutStream),
   45    nl(OutStream),
   46    flush_output(OutStream),
   47    write_hirarchy(OutStream),
   48    nl(OutStream),
   49    flush_output(OutStream),
   50    write_edges(OutStream),
   51    close(OutStream).
   52    
   53/*
   54 * write_files(+Stream)
   55 *    writes #### dito ####
   56 */
   57write_files(Stream):-
   58    forall(	fileT(Id,File,_),
   59    		write_node(Stream,Id,prolog_file,File)
   60    	  ).
   61		
   62write_predicates(Stream):-
   63	forall(	predicateT(Id,_,Functor,Arity,Module),
   64			(	write_node(Stream,Id,prolog_predicate,(Module:Functor/Arity)),
   65				filePosT(Id,Begin,Length),
   66				write_position(Stream,Id,Begin,Length)
   67			)
   68		).
   69
   70write_onloades(Stream):-
   71	forall(	onloadT(Id,_,Module),
   72			(	write_node(Stream,Id,prolog_onload,Module),
   73				filePosT(Id,Begin,Length),
   74				write_position(Stream,Id,Begin,Length)
   75			)
   76		).
   77    
   78   	  
   79write_clauses(Stream):-
   80	forall(	headT(HeadId,Id,_,_,_,_),     
   81			(	termT(HeadId,Term),
   82				write_node(Stream,Id,prolog_clause,Term),
   83				filePosT(Id,Begin,Length),
   84				write_position(Stream,Id,Begin,Length)
   85			)
   86    	  ).      	  
   87
   88write_directives(Stream):-
   89	forall(	directiveT(Id,_,_),
   90			(	termT(Id,Term),
   91				write_node(Stream,Id,prolog_directive,Term),
   92				filePosT(Id,Begin,Length),
   93				write_position(Stream,Id,Begin,Length)
   94			)
   95    	  ).  
   96 
   97write_hirarchy(Stream):-
   98    forall( onloadT(PredId,FileId,_),
   99    		write_within(Stream,FileId,PredId,'parent-child')
  100    	),
  101    forall(	predicateT(PredId,FileId,_,_,_),
  102 			write_within(Stream,FileId,PredId,'parent-child')
  103    	),
  104    forall( /*directiveT(DirectId,_,_), */onload_edge(DirectId,OnloadId),
  105    		write_within(Stream,OnloadId,DirectId,'parent-child')
  106    	),
  107    forall(	pred_edge(ClauseId,Id),
  108    		write_within(Stream,Id,ClauseId,'parent-child')
  109    	).
  110 
  111    	
  112write_edges(Stream):-
  113    call_edge(LId,CalleeId),
  114    literalT(LId,_,CallerId,_,_,_),
  115    termT(LId,Term),
  116    	write_edge(Stream,LId,CalleeId,CallerId,Term),
  117    fail.
  118write_edges(_).		
  119
  120write_node(Stream,Id,Type,Attr):-
  121    write(Stream, 'node("'),
  122    write(Stream, Id),
  123    write(Stream, '", "'),
  124    write(Stream, Type),
  125    write(Stream,'", "'),
  126    write(Stream, Attr),
  127    write(Stream, '").'),
  128    nl(Stream).
  129    
  130write_position(Stream,Id,Begin,End):-
  131    write(Stream, 'property("'),
  132    write(Stream, Id),
  133    write(Stream, '", "position('),
  134    write(Stream, Begin),
  135    write(Stream,', '),
  136    write(Stream, End),
  137    write(Stream, ')").'),
  138    nl(Stream).
  139
  140write_within(Stream,Id1,Id2,Text):-
  141    write(Stream, 'within("'),
  142    write(Stream, Id1),
  143    write(Stream, '", "'),
  144    write(Stream, Id2),
  145    write(Stream,'", "'),
  146    write(Stream, Text),
  147    write(Stream, '").'),
  148    nl(Stream).
  149    
  150write_edge(Stream,Id,Callee,Caller,Term):-
  151    write(Stream, 'edge("'),
  152    write(Stream, Id),
  153    write(Stream, '", "edge_call", "'),
  154    write(Stream, Term),
  155    write(Stream,'", "'),
  156    write(Stream, Callee),
  157    write(Stream,'", "'),
  158    write(Stream, Caller),
  159    write(Stream, '").'),
  160    nl(Stream).
  161
  162
  163pl_test:-
  164%    pl_test(['Z:/WorkspacePDT/pdt.runtime/library/pdt/xref'],'Z:/WorkspaceTeaching/bla/test.pl'). 
  165%    pl_test(['Z:/WorkspaceTeaching/svf.examples.prolog'],'Z:/WorkspaceTeaching/bla/test.pl'). 
  166%     pl_test(['Z:/WorkspaceTeaching/svf.examples.prolog/pl/Load.pl'],'Z:/WorkspaceTeaching/bla/test.pl'). 
  167    pl_test(['Z:/WorkspacePDT'],'Z:/WorkspaceTeaching/bla/test.txt').   
  168%    pl_test('Z:/WorkspacePDT/pdt.runtime/library/attic/org/cs3/pdt/metadata/mi_meta_ops.pl','Z:/WorkspaceTeaching/bla/test.pl').     
  169%    pl_test(['Z:/WorkspacePDT/pdt.runtime/library/attic/spike/socket/pifcom_codec.pl'],'Z:/WorkspaceTeaching/bla/test.pl').
  170%    pl_test(['Z:/WorkspaceTeaching/bla/seltsam.pl'],'Z:/WorkspaceTeaching/bla/test.pl').   
  171%    pl_test(['Z:/WorkspaceTeaching/bla/automat.pl'],'Z:/WorkspaceTeaching/bla/test.pl').   
  172%    pl_test(['Z:/WorkspacePDT/pdt.runtime/library/pdt/xref/parse_util_quick.pl'],'Z:/WorkspaceTeaching/bla/test.pl'). 
  173%   pl_test(['Z:/WorkspacePDT/jtransformer'],'Z:/WorkspaceTeaching/bla/test.pl'). 
  174      
  175 
  176pl_test(Project,Output):-
  177	parse_util:generate_facts(Project),
  178	writeln('generate abba sources'),
  179    time(write_facts_to_abba(Output)).     %Ergebnisdatei (abba-Format)