-
Notifications
You must be signed in to change notification settings - Fork 1
/
generateShex4HCLS.php
177 lines (153 loc) · 7.34 KB
/
generateShex4HCLS.php
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
<?php
// parameters
$basefilename = "hcls";
$filesuffix = ".shex";
$forceSHOULD = false;
$forceMAY = false;
$force = false;
//////
$options = getopt("f::");
if(isset($options['f'])) {
$v = trim($options['f']);
if($v == "SHOULD") $forceSHOULD = true;
elseif($v == "MAY") $forceMAY = true;
elseif($v == "ALL") $force = true;
elseif(empty($v));
else {
echo "Options for -f are SHOULD, MAY, or ALL";exit;
}
}
$filename = $basefilename.$filesuffix;
if($forceSHOULD === true) $filename = "hcls-force-SHOULD".$filesuffix;
if($forceMAY === true) $filename = "hcls-force-MAY".$filesuffix;
if($force === true) {
$forceSHOULD = true; $forceMAY = true;
$filename = "hcls-force-ALL".$filesuffix;
}
$table = file_get_contents("table.html" );
$xml = "<?xml version='1.0' encoding='UTF-8'?>\n".$table;
$xml=simplexml_load_string($xml) or die("Error: Cannot create object");
foreach($xml->children() as $e => $o) {
if(isset($o->th)) continue;
$a = (array) $o->td;
if(count($a) !=6 ) continue;
$obj = array();
$obj['label'] = $a[0];
$obj['predicate'] = $a[1];
if(!is_object($a[2])) {
$obj['value'] = $a[2];
} else {
$b = $o->td[2];
$x = $b->saveXML();
$obj['value'] = $x;
}
$summary = $a[3];
$version = $a[4];
$distribution = $a[5];
$list['summary'][$summary][] = $obj;
$list['version'][$version][] = $obj;
$list['distribution'][$distribution][] = $obj;
unset($obj);
}
$zeroOrOne = array(
"dct:title", "dct:description","dct:created","dct:issued","pav:createdOn","pav:authoredOn","pav:curatedOn","dct:issue","schemaorg:logo","dct:license","dct:rights","idot:preferredPrefix","pav:version","pav:previousVersion","pav:hasCurrentVersion","dct:accrualPeriodicity","dcat:distribution"
);
$zeroOrOne = array_flip($zeroOrOne);
$prefix =
"PREFIX : <http://purl.org/hcls-metadata-spec/>
PREFIX cito: <http://purl.org/spar/cito/>
PREFIX dcat: <http://www.w3.org/ns/dcat#>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX dctypes: <http://purl.org/dc/dcmitype/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX freq: <http://purl.org/cld/freq/>
PREFIX idot: <http://identifiers.org/idot/>
PREFIX lexvo: <http://lexvo.org/id/iso639-3/>
PREFIX pav: <http://purl.org/pav/>
PREFIX prov: <http://www.w3.org/ns/prov#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX schemaorg: <http://schema.org/>
PREFIX sd: <http://www.w3.org/ns/sparql-service-description#>
PREFIX sio: <http://semanticscience.org/resource/>
PREFIX void: <http://rdfs.org/ns/void#>
PREFIX void-ext: <http://ldf.fi/void-ext#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
";
$output = $prefix;
foreach($list AS $level => $a) {
$forcelabel = "";
if($forceSHOULD == true) $forcelabel = "ForceShould";
else if($forceMAY == true) $forcelabel = "ForceMay";
else if($force == true) $forcelabel = "ForceShouldMay";
$shapename = "HCLS".ucfirst($level).$forcelabel."Shape";
$annotations = "
// dct:creator \"Michel Dumontier\"
// dct:created \"".date("Y-m-d H:i:s")."\"
// prov:used <https://github.com/micheldumontier/hcls-shex>
";
$extra = "EXTRA rdf:type";
$output .= "<$shapename> IRI $annotations $extra
{".PHP_EOL;
foreach($a AS $requirement => $b) {
$safe_requirement= str_replace(" ","-",$requirement);
# if($safe_requirement === "MUST-NOT") continue;
foreach($b AS $c) {
$safe_value = $c['value'];
$expression = '';
if($c['predicate'] == "rdf:type") {
if($level == "summary" && $safe_requirement == "MUST") $expression = ' rdf:type [dctypes:Dataset] ';
if($level == "summary" && $safe_requirement == "MUST-NOT") $expression = ' rdf:type [void:Dataset dcat:Distribution] {0} ';
if($level == "version" && $safe_requirement == "MUST") $expression = ' rdf:type [dctypes:Dataset] ';
if($level == "version" && $safe_requirement == "MUST-NOT") $expression = ' rdf:type [void:Dataset dcat:Distribution] {0} ';
if($level == "distribution" && $safe_requirement == "SHOULD") $expression = ' rdf:type [dcat:Distribution]? ';
if($level == "distribution" && $safe_requirement == "MUST") $expression = ' rdf:type [void:Dataset dcat:Distribution] {2} ';
$output .= " $expression ".PHP_EOL;
$output .= ' // rdfs:comment "'.$level.' level '.$requirement.' use '. $c['predicate'].' with '.$safe_value.'"'.PHP_EOL;
$output .= ' // :metadata-element "'.$c['label'].'"'.PHP_EOL;
$output .= ' // :requirement-level :'.$safe_requirement.';'.PHP_EOL.PHP_EOL;
} else {
if(isset($alreadyseen[$level][$safe_requirement][$c['predicate']])) continue;
$predicates = explode(" or ", $c['predicate']);
$value_type = $safe_value;
$value_restriction = '';
$p = strrpos($safe_value, "[");
if($p !== FALSE) {
$value_type = trim(substr($safe_value, 0, $p));
$value_restriction = ' that is restricted to '.substr($safe_value, $p+1,-1);
}
foreach($predicates AS $predicate) {
$x = '';
$cardinality = '';
if($safe_requirement == "MUST-NOT") {
$expression = $predicate." . {0}";
} else {
if(($safe_requirement == "SHOULD") or ($safe_requirement == "MAY") or ($safe_requirement == "SHOULD-NOT")) {
$x = "*";
if($safe_requirement == "SHOULD" and $forceSHOULD == true) $x = "+";
if($safe_requirement == "MAY" and $forceMAY == true) $x = "+";
if(isset($zeroOrOne[$predicate])) {
$x = "?";
if($safe_requirement == "SHOULD" and $forceSHOULD == true) $x = "";
if($safe_requirement == "MAY" and $forceMAY == true) $x = "";
}
}
$expression = $predicate.' '.$value_type.$x;
if($x == "*") $cardinality = " zero or more";
elseif($x == "?") $cardinality = " zero or one";
elseif($x == "+") $cardinality = " one or more";
elseif($x == '') $cardinality = " one";
}
$output .= " $expression ".PHP_EOL;
$output .= ' // rdfs:comment "'.$level.' level '.$requirement.' use '. $predicate.' with'.$cardinality.' '.$value_type.''.$value_restriction.'"'.PHP_EOL;
$output .= ' // :metadata-element "'.$c['label'].'"'.PHP_EOL;
$output .= ' // :requirement-level :'.$safe_requirement.';'.PHP_EOL.PHP_EOL;
}
$alreadyseen[$level][$safe_requirement][$c['predicate']] = TRUE;
}
}
}
$output .= "}".PHP_EOL;
}
file_put_contents($filename,$output);
?>