69807
|
1 |
/* Title: Pure/General/rdf.scala
|
|
2 |
Author: Makarius
|
|
3 |
|
|
4 |
Support for RDF/XML representation, see also:
|
|
5 |
- https://www.w3.org/RDF
|
|
6 |
- https://www.w3.org/TR/rdf-primer
|
|
7 |
*/
|
|
8 |
|
|
9 |
package isabelle
|
|
10 |
|
|
11 |
|
|
12 |
object RDF
|
|
13 |
{
|
|
14 |
/* document */
|
|
15 |
|
|
16 |
val rdf: XML.Namespace = XML.Namespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
|
|
17 |
val dc: XML.Namespace = XML.Namespace("dc", "http://purl.org/dc/elements/1.1/")
|
|
18 |
|
|
19 |
val default_namespaces: List[XML.Namespace] = List(rdf, dc)
|
|
20 |
|
|
21 |
def document(body: XML.Body,
|
|
22 |
namespaces: List[XML.Namespace] = default_namespaces,
|
|
23 |
attributes: XML.Attributes = Nil): XML.Elem =
|
|
24 |
{
|
|
25 |
XML.Elem(Markup(rdf("RDF"), namespaces.map(_.attribute) ::: attributes), body)
|
|
26 |
}
|
|
27 |
|
|
28 |
|
|
29 |
/* multiple triples vs. compact description */
|
|
30 |
|
|
31 |
sealed case class Triple(subject: String, predicate: String, `object`: XML.Body)
|
|
32 |
{
|
|
33 |
def property: XML.Elem = XML.elem(predicate, `object`)
|
|
34 |
}
|
|
35 |
|
|
36 |
def triples(args: List[Triple]): XML.Body =
|
|
37 |
args.groupBy(_.subject).toList.map(
|
|
38 |
{ case (subject, ts) => description(subject, ts.map(_.property)) })
|
|
39 |
|
|
40 |
def description(subject: String, body: XML.Body, attributes: XML.Attributes = Nil): XML.Elem =
|
|
41 |
XML.Elem(Markup(rdf("Description"), (rdf("about"), subject) :: attributes), body)
|
69808
|
42 |
|
|
43 |
|
|
44 |
/* collections */
|
|
45 |
|
|
46 |
def collection(kind: String, items: List[XML.Body]): XML.Elem =
|
|
47 |
XML.elem(kind, items.map(item => XML.elem(rdf("li"), item)))
|
|
48 |
|
|
49 |
def bag(items: List[XML.Body]): XML.Elem = collection(rdf("Bag"), items)
|
|
50 |
def seq(items: List[XML.Body]): XML.Elem = collection(rdf("Seq"), items)
|
|
51 |
def alt(items: List[XML.Body]): XML.Elem = collection(rdf("Alt"), items)
|
69807
|
52 |
}
|