Download this file

ConvertArchi2OWL_v2.0.php    174 lines (149 with data), 6.2 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
<?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
*/
//path to input file
$csv = fopen("input.csv", "r");
Header('Content-type: text/xml');
$xml = simplexml_load_file(getInput() . ".archimate");
$newsXML = new SimpleXMLElement('<RDF/>');
//Read Archi File --------------------------------------------------------------
$arrayRelationAccessType = array('Write', 'Read', 'Access', 'Read_Write');
$hashAccessRelations = null;
foreach ($arrayRelationAccessType as $value) {
$property = $newsXML->addChild('SubObjectPropertyOf');
$subProp = $property->addChild("ObjectProperty");
$subProp->addAttribute("IRI", "#" . $value);
$subPropOf = $property->addChild("ObjectProperty");
$subPropOf->addAttribute("IRI", "#accesses");
}
$att = $xml->xpath('//element[@accessType]');
foreach ($att as $simpleXmlObject) {
$source = $simpleXmlObject->attributes()['source'];
$sourceElementObject = $xml->xpath(sprintf("//element[@id='%d']", $source));
$sourceElement = format_string($sourceElementObject[0]->attributes()['name']);
$target = $simpleXmlObject->attributes()['target'];
$targetElementObject = $xml->xpath(sprintf("//element[@id='%d']", $target));
$targetElement = format_string($targetElementObject[0]->attributes()['name']);
$accessType = $simpleXmlObject->attributes()['accessType'];
$relation = $newsXML->addChild("ObjectPropertyAssertion");
$op = $relation->addChild("ObjectProperty");
$op->addAttribute("IRI", "#" . $arrayRelationAccessType[(int) $accessType]);
$name1 = $relation->addChild("NamedIndividual");
$name1->addAttribute("IRI", "#" . $sourceElement);
$name2 = $relation->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;
}
$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 = $newsXML->addChild("ClassAssertion");
$entity = $classAssertion->addChild("Class");
$entity->addAttribute('IRI', "#" . $type);
$individual = $classAssertion->addChild("NamedIndividual");
$individual->addAttribute('IRI', "#" . $elementName);
} else {
$type = strtolower($type);
switch ($type) {
case 'access':
$type = 'accesses';
break;
case 'aggregation':
$type = 'aggregates';
break;
case 'assignment':
$type = 'assignedFrom';
break;
case 'composition':
$type = 'composedOf';
break;
case 'flow':
$type = 'flowTo';
break;
case 'influence':
$type = 'influencedBy';
break;
case 'realisation':
$type = 'realizes';
break;
case 'specialisation':
$type = 'specialization';
break;
case 'triggering':
$type = 'triggers';
break;
case 'usedby':
$type = 'usedBy';
break;
default:
break;
}
if ($elementName) {
if (array_search("$elementName.$type", $arrayOperations) === false) {
$property = $newsXML->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;
$relation = $newsXML->addChild("ObjectPropertyAssertion");
$op = $relation->addChild("ObjectProperty");
$op->addAttribute("IRI", "#" . $operation);
$name1 = $relation->addChild("NamedIndividual");
$name1->addAttribute("IRI", "#" . $sourceName);
$name2 = $relation->addChild("NamedIndividual");
$name2->addAttribute("IRI", "#" . $targetName);
if ($type == 'accesses' &&
!$hashAccessRelations[$sourceName][$targetName]) {
$relation = $newsXML->addChild("ObjectPropertyAssertion");
$op = $relation->addChild("ObjectProperty");
$op->addAttribute("IRI", "#Write");
$name1 = $relation->addChild("NamedIndividual");
$name1->addAttribute("IRI", "#" . $sourceName);
$name2 = $relation->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($newsXML->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() {
fwrite(STDOUT, 'Type the .archimate file name:');
$input = fgets(STDIN);
return rtrim($input);
}
?>