Download this file

ConvertArchi2OWL_v2.4.php    280 lines (225 with data), 9.7 kB

  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
279
<?php
/*
* Script that create the Archinsurance part of an Ontology.
* This script generate a xml file to add in the Archimate with extensions
* Ontology file, the generated file add the Archinsurance concepts
* (Individuals and Object Properties) in the Archimate with extensions OWL file.
*
* The script take as source the CSV file exported using the Archi Tool.
* @author INESC-ID
*/
Header('Content-type: text/xml');
//Files
$csvFile = "D:/Dropbox/11_ScriptsTese/Archi_files/eHealth_Model.csv";
//If you provide the DIO file, the Model you be added to DIO file.
$DIOFile = "D:/Dropbox/11_ScriptsTese/PhpProject/DIO-Archimate-V0.1.owl";
$csv = fopen($csvFile, "r");
if ($DIOFile) {
$xmlArchimate = simplexml_load_file(/* getInput() . */ $DIOFile);
$newXML = new SimpleXMLElement($xmlArchimate->asXML());
} else {
$newXML = new SimpleXMLElement("<RDF/>");
}
//Read Archi File --------------------------------------------------------------
$arrayRelationAccessType = array('hasAccessTypeWrite', 'hasAccessTypeRead', 'hasAccessTypeAccess', 'hasAccessTypeRead_Write');
$arrayJunctions = array();
//Write AccessTypes as subProperties
foreach ($arrayRelationAccessType as $value) {
$property = $newXML->addChild('SubObjectPropertyOf');
$subProp = $property->addChild("ObjectProperty");
$subProp->addAttribute("IRI", "#" . $value);
$subPropOf = $property->addChild("ObjectProperty");
$subPropOf->addAttribute("IRI", "#accesses");
}
//Read CSV File-----------------------------------------------------------------
$arrayOperations = array();
$firstline = true;
while (!feof($csv)) {
$line = fgetcsv($csv);
$line = to_utf8($line);
//skip first line
if ($firstline) {
$firstline = false;
continue;
}
//skip empty line
if (empty($line)) {
continue;
}
$elementName = format_string($line[1]);
$sourceType = format_string($line[3]);
$sourceName = format_string($line[4]);
$targetType = format_string($line[5]);
$targetName = format_string($line[6]);
$type = str_replace("Relationship", "", $line[0]);
//is individual
if ($line[3] == "" && $type != "") {
$newXML = writeIndividual($newXML, $type, $elementName);
//has property
if (!($line[8] == "")) {
$props = split(";", $line[8]);
foreach ($props as $prop) {
// '|' is operator in php
$keyvalue = split("\|", $prop);
list($propKey, $propValue) = $keyvalue;
$newXML = writeDataProperty($newXML, $elementName, $propKey, $propValue);
}
}
} else {//is relation
$type = convertObjectPropertyName($type);
if ($elementName) {
if (array_search("$elementName.$type", $arrayOperations) === false) {
$newXML = writeSubProperty($newXML, $type, $elementName);
array_push($arrayOperations, "$elementName.$type");
}
}
if (($sourceType == 'AndJunction' or $targetType == 'AndJunction')
and array_search('AndJunction', $arrayJunctions) === false) {
$newXML = writeSubClass($newXML, 'Junction', 'AndJunction');
array_push($arrayJunctions, 'AndJunction');
}
if (($sourceType == 'OrJunction' or $targetType == 'OrJunction')
and array_search('OrJunction', $arrayJunctions) === false) {
$newXML = writeSubClass($newXML, 'Junction', 'OrJunction');
array_push($arrayJunctions, 'OrJunction');
}
$operation = $elementName ? $elementName : $type;
$newXML = writeProperty($newXML, $operation, $sourceName, $targetName);
if ($type == 'accesses') {
$newXML = writeProperty($newXML, $arrayRelationAccessType[$line[7]], $sourceName, $targetName);
}
}
}
//path to output file
//Format XML to save indented tree rather than one line
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML(replace_special_chars($newXML->asXML()));
//echo $newXML->asXML();
$dom->save('eHealth_Model.owl');
function format_string($string) {
$elementName = str_replace(" ", "_", $string);
$elementName = str_replace("(", "-", $elementName);
$elementName = str_replace(")", "-", $elementName);
$elementName = str_replace("/", "_", $elementName);
return $elementName;
}
function getInput($fileDescription) {
fwrite(STDOUT, 'Type the file location of ' . $fileDescription . ':');
$input = fgets(STDIN);
return rtrim($input);
}
function convertObjectPropertyName($oldObjectProperty) {
$oldObjectProperty = strtolower($oldObjectProperty);
switch ($oldObjectProperty) {
case 'access':
$newObjectProperty = 'accesses';
break;
case 'aggregation':
$newObjectProperty = 'aggregates';
break;
case 'assignment':
$newObjectProperty = 'assignedFrom';
break;
case 'composition':
$newObjectProperty = 'composedOf';
break;
case 'flow':
$newObjectProperty = 'flowTo';
break;
case 'influence':
$newObjectProperty = 'influencedBy';
break;
case 'realisation':
$newObjectProperty = 'realizes';
break;
case 'specialisation':
$newObjectProperty = 'specialization';
break;
case 'triggering':
$newObjectProperty = 'triggers';
break;
case 'usedby':
$newObjectProperty = 'usedBy';
break;
default:
$newObjectProperty = 'association';
break;
}
return $newObjectProperty;
}
function writeSubProperty($newXML, $superProperty, $subProperty) {
$property = $newXML->addChild('SubObjectPropertyOf');
$subProp = $property->addChild("ObjectProperty");
$subProp->addAttribute("IRI", "#" . $subProperty);
$subPropOf = $property->addChild("ObjectProperty");
$subPropOf->addAttribute("IRI", "#" . $superProperty);
return $newXML;
}
function writeProperty($newXML, $relation, $source, $target) {
$objectPropertyAssertion = $newXML->addChild("ObjectPropertyAssertion");
$op = $objectPropertyAssertion->addChild("ObjectProperty");
$op->addAttribute("IRI", "#" . $relation);
$name1 = $objectPropertyAssertion->addChild("NamedIndividual");
$name1->addAttribute("IRI", "#" . $source);
$name2 = $objectPropertyAssertion->addChild("NamedIndividual");
$name2->addAttribute("IRI", "#" . $target);
return $newXML;
}
function writeDataProperty($newXML, $elementName, $propKey, $propValue) {
$datatypePropertyAssertion = $newXML->addChild("DataPropertyAssertion");
$dataProperty = $datatypePropertyAssertion->addChild("DataProperty");
$dataProperty->addAttribute("IRI", "#" . format_string($propKey));
$nameIndividual = $datatypePropertyAssertion->addChild("NamedIndividual");
$nameIndividual->addAttribute("IRI", "#" . $elementName);
$literal = $datatypePropertyAssertion->addChild("Literal", $propValue);
return $newXML;
}
function writeIndividual($newXML, $type, $elementName) {
$classAssertion = $newXML->addChild("ClassAssertion");
$entity = $classAssertion->addChild("Class");
$entity->addAttribute('IRI', "#" . $type);
$individual = $classAssertion->addChild("NamedIndividual");
$individual->addAttribute('IRI', "#" . $elementName);
return $newXML;
}
function writeSubClass($newXML, $class, $subClass) {
$subClassOf = $newXML->addChild('SubClassOf');
$classSub = $subClassOf->addChild("Class");
$classSub->addAttribute('IRI', "#" . $subClass);
$classSup = $subClassOf->addChild("Class");
$classSup->addAttribute('IRI', "#" . $class);
return $newXML;
}
function to_utf8($in) {
if (is_array($in)) {
foreach ($in as $key => $value) {
$out[to_utf8($key)] = to_utf8($value);
}
} elseif (is_string($in)) {
return utf8_encode($in);
} else {
return $in;
}
return $out;
}
//http://konfiguracja.c0.pl/webpl/index_en.html
//http://en.sourceforge.jp/projects/nucleus-jp/scm/git/nucleus-jp-ancient/blobs/branch-3-6/utf8/nucleus/libs/entity.php
function replace_special_chars($in){
//cp1251
$_entities = array (
'&#x80;' => '&#x20AC;', '&#x82;' => '&#x201A;', '&#x83;' => '&#x192;',
'&#x84;' => '&#x201E;', '&#x85;' => '&#x2026;', '&#x86;' => '&#x2020;',
'&#x87;' => '&#x2021;', '&#x88;' => '&#x2C6;', '&#x89;' => '&#x2030;',
'&#x8A;' => '&#x160;', '&#x8B;' => '&#x2039;', '&#x8C;' => '&#x152;',
'&#x8E;' => '&#x17D;', '&#x91;' => '&#x2018;', '&#x92;' => '&#x2019;',
'&#x93;' => '&#x201C;', '&#x94;' => '&#x201D;', '&#x95;' => '&#x2022;',
'&#x96;' => '&#x2013;', '&#x97;' => '&#x2014;', '&#x98;' => '&#x2DC;',
'&#x99;' => '&#x2122;', '&#x9A;' => '&#x161;', '&#x9B;' => '&#x203A;',
'&#x9C;' => '&#x153;', '&#x9E;' => '&#x17E;', '&#x9F;' => '&#x178;',
);
$out = str_replace(array_keys($_entities), array_values($_entities), $in);
return $out;
}
?>