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
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
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( nload_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:-
167 pl_test(['Z:/WorkspacePDT'],'Z:/WorkspaceTeaching/bla/test.txt').
174
175
176pl_test(Project,Output):-
177 parse_util:generate_facts(Project),
178 writeln('generate abba sources'),
179 time(write_facts_to_abba(Output)).