Download this file

ConvertArchi2OWL_v2.1.php    219 lines (178 with data), 8.1 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
<?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
*/
//Files
$csv = fopen("D:/Dropbox/11_ScriptsTese/Archi_files/lnec.csv", "r");
$xml = simplexml_load_file("D:/Dropbox/11_ScriptsTese/Archi_files/LNEC.archimate");
$xmlArchimate = simplexml_load_file(/* getInput() . */ "D:/Dropbox/11_ScriptsTese/PhpProject/DIO-Archimate-V0.1.owl");
Header('Content-type: text/xml');
$newXML = new SimpleXMLElement($xmlArchimate->asXML());
//Read Archi File --------------------------------------------------------------
$arrayRelationAccessType = array('hasAccessTypeWrite', 'hasAccessTypeRead', 'hasAccessTypeAccess', 'hasAccessTypeRead_Write');
$hashAccessRelations = null;
//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");
}
//Get DataProperties
$arrayProperty = $xml->xpath("//element/property/parent::*");
//Write DataProperties
foreach ($arrayProperty as $elementWithProperty) {
$elementName = $elementWithProperty->attributes()['name'];
if ($elementName) {
$property = $elementWithProperty->property->attributes()['key'];
$propertyValue = $elementWithProperty->property->attributes()['value'];
$dataPropertyAssertion = $newXML->addChild("DataPropertyAssertion");
$dataProperty = $dataPropertyAssertion->addChild("DataProperty");
$dataProperty->addAttribute('IRI', "#" . $property);
$individual = $dataPropertyAssertion->addChild("NamedIndividual");
$individual->addAttribute('IRI', "#" . $elementName);
$literal = $dataPropertyAssertion->addChild("Literal", $propertyValue);
//$literal->addAttribute('datatypeIRI', "");
}
}
//Read AccessType
$elementsWithAccessTypes = $xml->xpath('//element[@accessType]');
//Write AccessType
foreach ($elementsWithAccessTypes as $element) {
$source = $element->attributes()['source'];
$sourceElementObject = $xml->xpath(sprintf("//element[@id='%s']", $source));
$sourceElement = format_string($sourceElementObject[0]->attributes()['name']);
$target = $element->attributes()['target'];
$targetElementObject = $xml->xpath(sprintf("//element[@id='%s']", $target));
$targetElement = format_string($targetElementObject[0]->attributes()['name']);
$accessType = $element->attributes()['accessType'];
$objectPropertyAssertion = $newXML->addChild("ObjectPropertyAssertion");
$op = $objectPropertyAssertion->addChild("ObjectProperty");
$op->addAttribute("IRI", "#" . $arrayRelationAccessType[(int) $accessType]);
$name1 = $objectPropertyAssertion->addChild("NamedIndividual");
$name1->addAttribute("IRI", "#" . $sourceElement);
$name2 = $objectPropertyAssertion->addChild("NamedIndividual");
$name2->addAttribute("IRI", "#" . $targetElement);
$hashAccessRelations[$sourceElement][$targetElement] = true;
}
//Read CSV File-----------------------------------------------------------------
$arrayOperations = array();
$firstline = true;
while (!feof($csv)) {
$line = fgetcsv($csv);
if ($firstline) {
$firstline = false;
continue;
}
if (empty($line)){
continue;
}
$elementName = format_string($line[1]);
$sourceName = format_string($line[4]);
$targetName = format_string($line[6]);
$type = str_replace("Relationship", "", $line[0]);
if ($line[3] == "" && $type != "") {
$classAssertion = $newXML->addChild("ClassAssertion");
$entity = $classAssertion->addChild("Class");
$entity->addAttribute('IRI', "#" . $type);
$individual = $classAssertion->addChild("NamedIndividual");
$individual->addAttribute('IRI', "#" . $elementName);
} else {
$type = convertObjectPropertyName($type);
if ($elementName) {
if (array_search("$elementName.$type", $arrayOperations) === false) {
$property = $newXML->addChild('SubObjectPropertyOf');
$subProp = $property->addChild("ObjectProperty");
$subProp->addAttribute("IRI", "#" . $elementName);
$subPropOf = $property->addChild("ObjectProperty");
$subPropOf->addAttribute("IRI", "#" . $type);
array_push($arrayOperations, "$elementName.$type");
}
}
$operation = $elementName ? $elementName : $type;
$objectPropertyAssertion = $newXML->addChild("ObjectPropertyAssertion");
$op = $objectPropertyAssertion->addChild("ObjectProperty");
$op->addAttribute("IRI", "#" . $operation);
$name1 = $objectPropertyAssertion->addChild("NamedIndividual");
$name1->addAttribute("IRI", "#" . $sourceName);
$name2 = $objectPropertyAssertion->addChild("NamedIndividual");
$name2->addAttribute("IRI", "#" . $targetName);
if ($type == 'accesses' &&
!$hashAccessRelations[$sourceName][$targetName]) {
$objectPropertyAssertion = $newXML->addChild("ObjectPropertyAssertion");
$op = $objectPropertyAssertion->addChild("ObjectProperty");
$op->addAttribute("IRI", "#hasAccessTypeWrite");
$name1 = $objectPropertyAssertion->addChild("NamedIndividual");
$name1->addAttribute("IRI", "#" . $sourceName);
$name2 = $objectPropertyAssertion->addChild("NamedIndividual");
$name2->addAttribute("IRI", "#" . $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($newXML->asXML());
//echo $dom->saveXML();
$dom->save('output.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;
}
?>