/
ArchiveCustomization.scala
152 lines (137 loc) · 5.2 KB
/
ArchiveCustomization.scala
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
package info.kwarc.mmt.api.backend
import scala.xml.{Elem, Node}
import info.kwarc.mmt.api._
import info.kwarc.mmt.api.objects._
import info.kwarc.mmt.api.presentation.ContentMathMLPresenter
/** Helper class to permit customizing archives */
abstract class ArchiveCustomization {
def mwsurl(p: Path) : String
/**
* Any post-processing on the query object should be done here
*/
def prepareQuery(t: Obj): scala.xml.Node
}
class DefaultCustomization extends ArchiveCustomization {
def mwsurl(p: Path) : String = p.toPath
def prepareQuery(t: Obj): scala.xml.Node = ContentMathMLPresenter(t)
}
/** Customization for Mizar */
class MML extends ArchiveCustomization {
def mwsurl(p: Path) : String = p match {
case par ?? ln =>
val thyName = par.name.toPath
val symbolName = ln.head.toPath
"http://www.mizar.org/version/current/html/" + thyName.toLowerCase() + ".html#" + symbolName
case _ => throw ImplementationError("Module level references not implemented yet")
// "http://www.mizar.org/version/current/html/" + "%m.html#o"
}
def prepareQuery(t: Obj): scala.xml.Node = {
makeHVars(removeLFApp(ContentMathMLPresenter(t)), Nil, Nil, true)
}
private def removeLFApp(n : scala.xml.Node) : scala.xml.Node = n match {
case <apply><csymbol>{x}</csymbol>{s @ _*}</apply> =>
if (x.toString == "http://cds.omdoc.org/foundational?LF?@")
<apply>{s.map(removeLFApp)}</apply>
else {
new scala.xml.Elem(n.prefix, n.label, n.attributes, n.scope, true, n.child.map(removeLFApp) : _*)
}
case _ =>
if (n.child.length == 0)
n
else
new scala.xml.Elem(n.prefix, n.label, n.attributes, n.scope, true, n.child.map(removeLFApp(_)) : _*)
}
private def makeHVars(n : scala.xml.Node, uvars : List[String], evars : List[String], negFlag : Boolean) : scala.xml.Node = n match {
case <apply><csymbol>{s}</csymbol><bvar><apply>{zz}<ci>{v}</ci>{a}{b}</apply>{rest @ _*}</bvar>{body}</apply> =>
if (s.toString == "http://cds.omdoc.org/foundational?LF?Pi") {//"http://cds.omdoc.org/foundations/lf/lf.omdoc?lf?Pi") {
var bd = body
if (rest.length > 0)
bd = <apply><csymbol>{s}</csymbol><bvar>{rest}</bvar>{body}</apply>
makeHVars(bd, v.toString :: uvars, evars, negFlag)
}
else {
new scala.xml.Elem(n.prefix, n.label, n.attributes, n.scope, true, n.child.map(makeHVars(_,uvars, evars, negFlag)) : _*)
}
case <apply><apply><csymbol>{s}</csymbol>{a1}</apply><bvar><apply>{zz}<ci>{v}</ci>{a}{b}</apply></bvar>{body}</apply> =>
if (s.toString == "http://latin.omdoc.org/foundations/mizar?mizar-curry?for") {
var uv = uvars
var ev = evars
if (negFlag)
uv = v.toString :: uvars
else
ev = v.toString :: evars
makeHVars(body, uv, ev, negFlag)
} else {
new scala.xml.Elem(n.prefix, n.label, n.attributes, n.scope, true, n.child.map(makeHVars(_,uvars, evars, negFlag)) : _*)
}
case <apply><csymbol>{s}</csymbol>{a}</apply> =>
if (s.toString == "http://latin.omdoc.org/foundations/mizar?mizar-curry?not") {
val firstq = s.toString.indexOf('?')
<apply><csymbol>{s.toString.substring(firstq + 1)}</csymbol>{makeHVars(a, uvars, evars, !negFlag)}</apply>
} else
new scala.xml.Elem(n.prefix, n.label, n.attributes, n.scope, true, n.child.map(makeHVars(_,uvars, evars, negFlag)) : _*)
case <ci>{v}</ci> =>
if (uvars.contains(v.toString))
<mws:uvar><ci>{v}</ci></mws:uvar>
else if (evars.contains(v.toString))
<mws:evar><ci>{v}</ci></mws:evar>
else
n
case <csymbol>{p}</csymbol> =>
val firstq = p.toString.indexOf('?')
<csymbol>{p.toString.substring(firstq + 1)}</csymbol>
case _ =>
if (n.child.length == 0)
n
else
new scala.xml.Elem(n.prefix, n.label, n.attributes, n.scope, true, n.child.map(makeHVars(_,uvars, evars, negFlag)) : _*)
}
}
/**
* TPTP customization
*/
class TPTP extends ArchiveCustomization {
def mwsurl(p: Path) : String = p.toPath
/**
* Traverse the node, fixing symbol names and translating $$ terms to qvar
*/
def prepareQuery(t: Obj): Node = process(ContentMathMLPresenter(t))
def process(n: Node) : Node = n match {
case <csymbol>{s}</csymbol> =>
val ss = getSymbolName(s.toString)
if (isQvar(ss)) {
<mws:qvar>{ss}</mws:qvar>
} else {
<csymbol>{ss}</csymbol>
}
case <ci>{s}</ci> =>
val ss = s.toString
if (isQvar(ss)) {
<mws:qvar>{ss}</mws:qvar>
} else {
<ci>{ss}</ci>
}
case n =>
if (n.child.length == 0)
n
else
new Elem(n.prefix, n.label, n.attributes, n.scope, true, n.child.map(process(_)) : _*)
}
/**
* Strips csymbol names to contain only the symbol name
*/
def getSymbolName(ss: String): String = {
val ind = ss.lastIndexOf("?")
if (ind != -1) {
if (ind == ss.length - 1)
return "?"
else
return ss.substring(ind + 1, ss.length)
}
return ss
}
/**
* qvars start with $$ followed by lower case character
*/
def isQvar(ss: String): Boolean = ss.startsWith("$$")
}