forked from mthom/cl-psoatransrun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsoa-pprint.lisp
278 lines (216 loc) · 10.3 KB
/
psoa-pprint.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
(in-package #:psoa-pprint)
#|
This package contains functions for pretty-printing objects whose
supertype is ruleml-ast-node, all of which are defined in
psoa-ast.lisp (i.e., the content of the package #:psoa-ast).
While objects of supertype ruleml-ast-node are assembled from string
KBs into PSOA RuleML ASTs by the EBNF-like grammar rules of
psoa-grammar.lisp, the functions here perform the inverse function:
they convert PSOA RuleML ASTs back to the string representations of
the PSOA RuleML KBs approximately like those from which they were
produced.
Documentation of the Common Lisp Pretty Printing System, which is used
extensively in this package, is available here:
https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node253.html
|#
(defparameter *inside-psoa-rest* nil
"A contextual flag / boolean indicating whether the subterms of a
PSOA RuleML atom or expression are currently being printed.")
(defparameter *print-caret-before-expr* nil
"A boolean indicating whether exprs outside atoms should be
preceded by caret (^).")
(defun pprint-ruleml-document (stream document)
(pprint-logical-block (stream nil :prefix "RuleML(" :suffix ")")
(pprint-indent :block -4 stream)
(pprint-newline :mandatory stream)
(when-it (ruleml-document-base document)
(write it :stream stream)
(pprint-newline :mandatory stream)
(pprint-newline :mandatory stream))
(when-it (ruleml-document-prefixes document)
(pprint-linear stream it nil)
(pprint-newline :mandatory stream)
(pprint-newline :mandatory stream))
(when-it (ruleml-document-imports document)
(pprint-linear stream it nil)
(pprint-newline :mandatory stream)
(pprint-newline :mandatory stream))
(when-it (ruleml-document-performatives document)
(pprint-linear stream it nil))
(pprint-indent :block (- (length "RuleML(")) stream)
(pprint-newline :mandatory stream)))
(set-pprint-dispatch 'ruleml-document 'pprint-ruleml-document)
(defun pprint-ruleml-base (stream base)
(pprint-logical-block (stream nil :prefix "Base(" :suffix ")")
(write-char #\< stream)
(write (ruleml-base-iri-ref base) :stream stream :escape nil)
(write-char #\> stream)))
(set-pprint-dispatch 'ruleml-base 'pprint-ruleml-base)
(defun pprint-ruleml-prefix (stream prefix)
(pprint-logical-block (stream nil :prefix "Prefix(" :suffix ")")
(when (ruleml-prefix-name prefix)
(write (ruleml-prefix-name prefix) :stream stream :escape nil))
(write-char #\: stream)
(write (ruleml-prefix-iri-ref prefix) :stream stream :escape nil)))
(set-pprint-dispatch 'ruleml-prefix 'pprint-ruleml-prefix)
(defun pprint-ruleml-const (stream const)
(write (ruleml-const-contents const) :stream stream :escape nil))
(set-pprint-dispatch 'ruleml-const 'pprint-ruleml-const)
(defun pprint-ruleml-string (stream string)
(write-char #\" stream)
(write (ruleml-string-contents string) :stream stream)
(write-char #\" stream))
(set-pprint-dispatch 'ruleml-string 'pprint-ruleml-string)
(defun pprint-ruleml-import (stream import)
(pprint-logical-block (stream nil :prefix "Import(" :suffix ")")
(write-char #\< stream)
(write (ruleml-import-iri-ref import) :stream stream :escape nil)
(write-char #\> stream)
(pprint-newline :fill stream)
(when-it (ruleml-import-profile import)
(write-char #\Space stream)
(write it :stream stream))))
(set-pprint-dispatch 'ruleml-import 'pprint-ruleml-import)
(defun pprint-ruleml-assert (stream assert)
(pprint-logical-block (stream nil :prefix "Assert(" :suffix ")")
(pprint-indent :block -4 stream)
(pprint-newline :mandatory stream)
(pprint-logical-block (stream (ruleml-assert-items assert))
(pprint-exit-if-list-exhausted)
(loop (write (pprint-pop) :stream stream)
(pprint-exit-if-list-exhausted)
(pprint-newline :mandatory stream)
(pprint-newline :mandatory stream)))
(pprint-indent :block (- (length "Assert(")) stream)
(pprint-newline :mandatory stream)))
(set-pprint-dispatch 'ruleml-assert 'pprint-ruleml-assert)
(defun pprint-ruleml-query (stream query)
(pprint-logical-block (stream nil :prefix "Query(" :suffix ")")
(write (ruleml-query-term query) :stream stream)))
(set-pprint-dispatch 'ruleml-query 'pprint-ruleml-query)
(defun pprint-ruleml-naf (stream formula)
(pprint-logical-block (stream nil :prefix "Naf(" :suffix ")")
(write (ruleml-naf-formula formula) :stream stream)))
(set-pprint-dispatch 'ruleml-naf 'pprint-ruleml-naf)
(defun pprint-ruleml-var (stream var)
(write-char #\? stream)
(write (ruleml-var-name var) :stream stream :escape nil))
(set-pprint-dispatch 'ruleml-var 'pprint-ruleml-var)
(set-pprint-dispatch 'ruleml-genvar 'pprint-ruleml-var)
(defun pprint-ruleml-slot (stream slot)
(write (ruleml-slot-name slot) :stream stream)
(write-string (if (ruleml-slot-dep slot) "+>" "->") stream)
(write (ruleml-slot-filler slot) :stream stream))
(set-pprint-dispatch 'ruleml-slot 'pprint-ruleml-slot)
(defun pprint-ruleml-tuple (stream tuple)
(write-string (if (ruleml-tuple-dep tuple) "+" "-") stream)
(write-char #\[ stream)
(pprint-fill stream (ruleml-tuple-terms tuple) nil)
(write-char #\] stream))
(set-pprint-dispatch 'ruleml-tuple 'pprint-ruleml-tuple)
(defun pprint-ruleml-pname-ln (stream pname-ln)
(when-it (ruleml-pname-ln-name pname-ln)
(write it :stream stream :escape nil))
(write-char #\: stream)
(write (ruleml-pname-ln-url pname-ln) :stream stream :escape nil))
(set-pprint-dispatch 'ruleml-pname-ln 'pprint-ruleml-pname-ln)
(defun pprint-ruleml-expr (stream expr)
(unless (or *inside-psoa-rest* (not *print-caret-before-expr*))
(write-char #\^ stream))
(write (ruleml-expr-root expr) :stream stream)
(pprint-logical-block (stream nil :prefix "(" :suffix ")")
(let ((*inside-psoa-rest* t))
(pprint-fill stream (ruleml-expr-terms expr) nil))))
(set-pprint-dispatch 'ruleml-expr 'pprint-ruleml-expr)
(defun pprint-ruleml-subclass-rel (stream subclass-rel)
(write (ruleml-subclass-rel-sub subclass-rel) :stream stream)
(pprint-newline :fill stream)
(write-string "##" stream)
(pprint-newline :fill stream)
(write (ruleml-subclass-rel-super subclass-rel) :stream stream))
(set-pprint-dispatch 'ruleml-subclass-rel 'pprint-ruleml-subclass-rel)
(defun pprint-ruleml-equal (stream equal)
(write (ruleml-equal-left equal) :stream stream)
(write-char #\= stream)
(write (ruleml-equal-right equal) :stream stream))
(set-pprint-dispatch 'ruleml-equal 'pprint-ruleml-equal)
(defun pprint-ruleml-atom (stream atom)
(when *inside-psoa-rest*
(write-char #\# stream))
(write (ruleml-atom-root atom) :stream stream)
(when-it (ruleml-atom-descriptors atom)
(let ((*inside-psoa-rest* t))
(if (and (single it)
(ruleml-tuple-p (first it))
(ruleml-tuple-dep (first it)))
(pprint-fill stream (ruleml-tuple-terms (first it)))
(pprint-fill stream it))))
(pprint-newline :fill stream))
(set-pprint-dispatch 'ruleml-atom 'pprint-ruleml-atom)
(defun pprint-ruleml-oidful-atom (stream atom)
(write (ruleml-oidful-atom-oid atom) :stream stream)
(when (not *inside-psoa-rest*)
(write-char #\# stream))
(write (ruleml-oidful-atom-predicate atom) :stream stream))
(set-pprint-dispatch 'ruleml-oidful-atom 'pprint-ruleml-oidful-atom)
(defun pprint-ruleml-forall (stream clause)
(let* ((forall-prefix (format nil "Forall ~{~a~^ ~} (" (ruleml-forall-vars clause)))
(prefix-length (length forall-prefix)))
(pprint-logical-block (stream nil :prefix forall-prefix :suffix ")")
(pprint-indent :block (+ (- prefix-length) 4) stream)
(pprint-newline :mandatory stream)
(write (ruleml-forall-clause clause) :stream stream)
(pprint-indent :block (- prefix-length) stream)
(pprint-newline :mandatory stream))))
(set-pprint-dispatch 'ruleml-forall 'pprint-ruleml-forall)
(defun pprint-ruleml-implies (stream implies)
(write (ruleml-implies-conclusion implies) :stream stream)
(write-char #\Space stream)
(write-string ":-" stream)
(write-char #\Space stream)
(write (ruleml-implies-condition implies) :stream stream))
(set-pprint-dispatch 'ruleml-implies 'pprint-ruleml-implies)
(defun pprint-ruleml-and (stream item)
(pprint-logical-block (stream nil :prefix "And(" :suffix ")")
(pprint-indent :block -3 stream)
(pprint-newline :mandatory stream)
(pprint-linear stream (ruleml-and-terms item) nil)
(pprint-indent :block (- (length "And(")) stream)
(pprint-newline :mandatory stream)))
(set-pprint-dispatch 'ruleml-and 'pprint-ruleml-and)
(defun pprint-ruleml-or (stream item)
(pprint-logical-block (stream nil :prefix "Or(" :suffix ")")
(pprint-indent :block -2 stream)
(pprint-newline :mandatory stream)
(pprint-linear stream (ruleml-or-terms item) nil)
(pprint-indent :block (- (length "Or(")) stream)
(pprint-newline :mandatory stream)))
(set-pprint-dispatch 'ruleml-or 'pprint-ruleml-or)
(defun pprint-ruleml-exists (stream clause)
(let* ((exists-prefix (format nil "Exists ~{~a~^ ~} (" (ruleml-exists-vars clause)))
(prefix-length (length exists-prefix)))
(pprint-logical-block (stream nil :prefix exists-prefix :suffix ")")
(pprint-indent :block (+ (- prefix-length) 4) stream)
(pprint-newline :mandatory stream)
(write (ruleml-exists-formula clause) :stream stream)
(pprint-indent :block (- prefix-length) stream)
(pprint-newline :mandatory stream))))
(set-pprint-dispatch 'ruleml-exists 'pprint-ruleml-exists)
(defun pprint-ruleml-external (stream external)
(pprint-logical-block (stream nil :prefix "External(" :suffix ")")
(pprint-indent :block 4 stream)
(write (ruleml-external-atom external) :stream stream)))
(set-pprint-dispatch 'ruleml-external 'pprint-ruleml-external)
(defun pprint-ruleml-membership (stream membership)
(write (ruleml-membership-oid membership) :stream stream)
(write-char #\# stream)
(write (ruleml-membership-predicate membership) :stream stream))
(set-pprint-dispatch 'ruleml-membership 'pprint-ruleml-membership)
(defun pprint-ruleml-number (stream number)
(write (ruleml-number-value number) :stream stream))
(set-pprint-dispatch 'ruleml-number 'pprint-ruleml-number)
(defun pprint-ruleml-iri (stream iri)
(write-string "<" stream)
(write-string (ruleml-iri-contents iri) stream)
(write-string ">" stream))
(set-pprint-dispatch 'ruleml-iri 'pprint-ruleml-iri)