-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.fsx
166 lines (131 loc) · 5.45 KB
/
build.fsx
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
#I "/tools/packages/NICE.Freya/tools"
#r "/tools/packages/NICE.Freya/tools/FSharp.Markdown.dll"
#r "/tools/packages/NICE.Freya/tools/freya.exe"
#r "/tools/packages/NICE.Freya/tools/ExtCore.dll"
#r "/tools/packages/NICE.Freya/tools/SharpYaml.dll"
#r "/tools/packages/NICE.Freya/tools/FSharp.RDF.dll"
open Freya
open Freya.Builder
open Freya.YamlParser
open FSharp.Markdown
open Freya.Markdown
open FSharp.RDF
let mkKey (x : string) = x.Replace(" ", "").ToLowerInvariant()
///Load all resources from uri and make a map of rdfs:label -> resource uri
let vocabLookup uri =
let rdfslbl = Uri.from "http://www.w3.org/2000/01/rdf-schema#label"
let gcd = Graph.loadFrom uri
let onlySome = List.choose id
Resource.fromPredicate rdfslbl gcd
|> List.map (fun r ->
match r with
| FunctionalDataProperty rdfslbl xsd.string x ->
Some(mkKey x, Resource.id r)
| r -> None)
|> onlySome
|> Map.ofList
///Map of annotation vocab name to vocabulary
let lookupVocab =
([ "setting",
vocabLookup "http://schema/ns/qualitystandard/setting.ttl"
"agegroup",
vocabLookup "http://schema/ns/qualitystandard/agegroup.ttl"
"lifestylecondition",
vocabLookup
"http://schema/ns/qualitystandard/lifestylecondition.ttl"
"conditiondisease",
vocabLookup "http://schema/ns/qualitystandard/conditiondisease.ttl"
"servicearea",
vocabLookup "http://schema/ns/qualitystandard/servicearea.ttl" ]
|> Map.ofList)
///Map of annotation vocabulary name to restricted property
let lookupProperty =
([ "setting", Uri.from "http://ld.nice.org.uk/ns/qualitystandard#setting"
"agegroup",
Uri.from "http://ld.nice.org.uk/ns/qualitystandard#targetPopulation"
"conditiondisease",
Uri.from "http://ld.nice.org.uk/ns/qualitystandard#targetPopulation"
"servicearea",
Uri.from "http://ld.nice.org.uk/ns/qualitystandard#serviceArea"
"lifestylecondition",
Uri.from "http://ld.nice.org.uk/ns/qualitystandard#targetPopulation" ]
|> Map.ofList)
type YNode = Freya.YamlParser.Node
open FSharp.RDF.Assertion
open rdf
let owlAllValuesFrom property = function
| [] -> []
| ranges -> [ for r in ranges -> objectProperty property r ]
let qsAnnotations ctx =
let message f x = f x (Tracing.fileLocation ctx.Path)
let info = message Tracing.info
let warn = message Tracing.warn
let onlySome = List.filter Option.isSome >> List.map Option.get
//Pairs of trace message / annotation uri
let lookUpScalar vocabKey =
function
| Node.Scalar(Scalar.String term) ->
printfn "%A %A" vocabKey term
match Map.tryFind (mkKey vocabKey) lookupVocab with
| Some vocab ->
match Map.tryFind (mkKey term) vocab with
| Some uri -> (info (sprintf "Annotating for term %s" term), Some uri)
| None -> (warn (sprintf "Cannot find '%s' in '%s'" term vocabKey), None)
| None -> (warn (sprintf "Cannot find vocabulary '%s'" vocabKey), None)
| _ -> (warn (sprintf "Malformed yaml"), None)
let extracted =
match ctx.Content with
| Map xs ->
xs
|> List.map (function
| k, YNode.List xv ->
(Map.tryFind (mkKey k) lookupProperty,
List.map (lookUpScalar (mkKey k)) xv)
| k, _ -> (None, []))
|> List.map (function
| Some k, xs ->
(List.map fst xs,
owlAllValuesFrom k ((List.map snd xs |> onlySome)) )
| _, xs -> (List.map fst xs, []))
| _ -> []
{ Trace = List.concat (List.map fst extracted)
Extracted = List.map snd extracted
|> List.map (owl.individual ctx.TargetId [] )}
let qsDC (ctx:ExtractionContext<FSharp.Markdown.MarkdownDocument>) =
let message f x = f x (Tracing.fileLocation ctx.Path)
let info = message Tracing.info
let warn = message Tracing.warn
let h1 = ctx.Content.Paragraphs
|> List.choose MarkdownParagraph.hAny
|> List.map MarkdownSpan.text
|> List.map (fun x -> rdf.dataProperty !!"http://purl.org/dc/terms/title" (x^^xsd.string) )
|> List.tryHead
let p1 = ctx.Content.Paragraphs
|> MarkdownParagraph.following
(MarkdownParagraph.h3 >>= (MarkdownSpan.re ".*(Q|q)uality.*(S|s)tatement.*"))
|> List.map MarkdownParagraph.text
|> List.map (fun x -> rdf.dataProperty !!"http://purl.org/dc/terms/abstract" (x^^xsd.string) )
|> List.tryHead
{Extracted = [rdf.resource ctx.TargetId (( Option.toList h1 ) @ (Option.toList p1 ))]
Trace = [
if Option.isNone h1 then yield (warn "No title found")
if Option.isNone p1 then yield (warn "No abstract found")
]}
markdownExtractor "QsDC" qsDC
yamlExtractor "QsAnnotations" qsAnnotations
target "QualityStandards" (dir "qualitystandards")
target "QualityStandardDir" (dir "qs$(QualityStandardId)")
target "QualityStatementDir" (dir "st$(QualityStatementId)")
target "QualityStatement" (file "Statement.md"
["Content";"QsDC";"QsAnnotations";"HtmlFragment"]
(Some "/templates/QualityStatement.md")
"owl:NamedIndividual")
target "QualityStandard" (file "Standard.md"
["Content"]
(Some "/templates/QualityStandard.md")
"owl:NamedIndividual")
"QualityStandards"
===> ["QualityStandardDir"
===> ["QualityStandard"
"QualityStatementDir"
===> ["QualityStatement"]]]