Switch to unified view

a b/ConvertArchi2OWL_v2.3.php
1
<?php
2
3
/*
4
 * Script that create the Archinsurance part of an Ontology.
5
 * This script generate a xml file to add in the Archimate with extensions 
6
 * Ontology file, the generated file add the Archinsurance concepts
7
 * (Individuals and Object Properties) in the Archimate with extensions OWL file.
8
 * 
9
 * The script take as source the CSV file exported using the Archi Tool.
10
 * @author INESC-ID
11
 */
12
13
Header('Content-type: text/xml');
14
15
//Files
16
$csvFile = "D:/Dropbox/11_ScriptsTese/Archi_files/lnec.csv";
17
//If you provide the DIO file, the Model you be added to DIO file.
18
$DIOFile = "D:/Dropbox/11_ScriptsTese/PhpProject/DIO-Archimate-V0.1.owl";
19
20
21
$csv = fopen($csvFile, "r");
22
23
if ($DIOFile) {
24
25
    $xmlArchimate = simplexml_load_file(/* getInput() . */ $DIOFile);
26
    $newXML = new SimpleXMLElement($xmlArchimate->asXML());
27
} else {
28
    $newXML = new SimpleXMLElement("<RDF/>");
29
}
30
31
//Read Archi File --------------------------------------------------------------
32
$arrayRelationAccessType = array('hasAccessTypeWrite', 'hasAccessTypeRead', 'hasAccessTypeAccess', 'hasAccessTypeRead_Write');
33
$arrayJunctions = array();
34
35
//Write AccessTypes as subProperties
36
foreach ($arrayRelationAccessType as $value) {
37
38
    $property = $newXML->addChild('SubObjectPropertyOf');
39
    $subProp = $property->addChild("ObjectProperty");
40
    $subProp->addAttribute("IRI", "#" . $value);
41
    $subPropOf = $property->addChild("ObjectProperty");
42
    $subPropOf->addAttribute("IRI", "#accesses");
43
}
44
45
//Read CSV File-----------------------------------------------------------------
46
$arrayOperations = array();
47
$firstline = true;
48
while (!feof($csv)) {
49
50
    $line = fgetcsv($csv);
51
    //skip first line
52
    if ($firstline) {
53
        $firstline = false;
54
        continue;
55
    }
56
57
    //skip empty line
58
    if (empty($line)) {
59
        continue;
60
    }
61
62
    $elementName = format_string($line[1]);
63
    $sourceType = format_string($line[3]);
64
    $sourceName = format_string($line[4]);
65
    $targetType = format_string($line[5]);
66
    $targetName = format_string($line[6]);
67
    $type = str_replace("Relationship", "", $line[0]);
68
69
    //is individual
70
    if ($line[3] == "" && $type != "") {
71
72
        $newXML = writeIndividual($newXML, $type, $elementName);
73
74
        //has property
75
        if (!($line[8] == "")) {
76
            $props = split(";", $line[8]);
77
            foreach ($props as $prop) {
78
                // '|' is operator in php
79
                $keyvalue = split("\|", $prop);
80
                list($propKey, $propValue) = $keyvalue;
81
82
                $newXML = writeDataProperty($newXML, $elementName, $propKey, $propValue);
83
            }
84
        }
85
    } else {//is relation
86
        $type = convertObjectPropertyName($type);
87
88
        if ($elementName) {
89
90
            if (array_search("$elementName.$type", $arrayOperations) === false) {
91
92
                $newXML = writeSubProperty($newXML, $type, $elementName);
93
                array_push($arrayOperations, "$elementName.$type");
94
            }
95
        }
96
97
        if (($sourceType == 'AndJunction' or $targetType == 'AndJunction') 
98
                and array_search('AndJunction', $arrayJunctions) === false) {
99
            
100
            $newXML = writeSubClass($newXML, 'Junction', 'AndJunction');
101
            array_push($arrayJunctions, 'AndJunction');
102
        }
103
        if (($sourceType == 'OrJunction' or $targetType == 'OrJunction') 
104
                and array_search('OrJunction', $arrayJunctions) === false) {
105
            
106
            $newXML = writeSubClass($newXML, 'Junction', 'OrJunction');
107
            array_push($arrayJunctions, 'OrJunction');
108
        }
109
110
        $operation = $elementName ? $elementName : $type;
111
112
        $newXML = writeProperty($newXML, $operation, $sourceName, $targetName);
113
114
        if ($type == 'accesses') {
115
116
            $newXML = writeProperty($newXML, $arrayRelationAccessType[$line[7]], $sourceName, $targetName);
117
        }
118
    }
119
}
120
121
//path to output file
122
//Format XML to save indented tree rather than one line
123
$dom = new DOMDocument('1.0');
124
$dom->preserveWhiteSpace = false;
125
$dom->formatOutput = true;
126
$dom->loadXML($newXML->asXML());
127
//echo $dom->saveXML();
128
$dom->save('output.owl');
129
130
function format_string($string) {
131
132
    $elementName = str_replace(" ", "_", $string);
133
    $elementName = str_replace("(", "-", $elementName);
134
    $elementName = str_replace(")", "-", $elementName);
135
    $elementName = str_replace("/", "_", $elementName);
136
    return $elementName;
137
}
138
139
function getInput($fileDescription) {
140
    fwrite(STDOUT, 'Type the file location of ' . $fileDescription . ':');
141
    $input = fgets(STDIN);
142
    return rtrim($input);
143
}
144
145
function convertObjectPropertyName($oldObjectProperty) {
146
147
    $oldObjectProperty = strtolower($oldObjectProperty);
148
    switch ($oldObjectProperty) {
149
        case 'access':
150
            $newObjectProperty = 'accesses';
151
            break;
152
        case 'aggregation':
153
            $newObjectProperty = 'aggregates';
154
            break;
155
        case 'assignment':
156
            $newObjectProperty = 'assignedFrom';
157
            break;
158
        case 'composition':
159
            $newObjectProperty = 'composedOf';
160
            break;
161
        case 'flow':
162
            $newObjectProperty = 'flowTo';
163
            break;
164
        case 'influence':
165
            $newObjectProperty = 'influencedBy';
166
            break;
167
        case 'realisation':
168
            $newObjectProperty = 'realizes';
169
            break;
170
        case 'specialisation':
171
            $newObjectProperty = 'specialization';
172
            break;
173
        case 'triggering':
174
            $newObjectProperty = 'triggers';
175
            break;
176
        case 'usedby':
177
            $newObjectProperty = 'usedBy';
178
            break;
179
180
        default:
181
            $newObjectProperty = 'association';
182
            break;
183
    }
184
    return $newObjectProperty;
185
}
186
187
function writeSubProperty($newXML, $superProperty, $subProperty) {
188
189
    $property = $newXML->addChild('SubObjectPropertyOf');
190
    $subProp = $property->addChild("ObjectProperty");
191
    $subProp->addAttribute("IRI", "#" . $subProperty);
192
    $subPropOf = $property->addChild("ObjectProperty");
193
    $subPropOf->addAttribute("IRI", "#" . $superProperty);
194
195
    return $newXML;
196
}
197
198
function writeProperty($newXML, $relation, $source, $target) {
199
200
    $objectPropertyAssertion = $newXML->addChild("ObjectPropertyAssertion");
201
    $op = $objectPropertyAssertion->addChild("ObjectProperty");
202
    $op->addAttribute("IRI", "#" . $relation);
203
    $name1 = $objectPropertyAssertion->addChild("NamedIndividual");
204
    $name1->addAttribute("IRI", "#" . $source);
205
    $name2 = $objectPropertyAssertion->addChild("NamedIndividual");
206
    $name2->addAttribute("IRI", "#" . $target);
207
208
    return $newXML;
209
}
210
211
function writeDataProperty($newXML, $elementName, $propKey, $propValue) {
212
213
    $datatypePropertyAssertion = $newXML->addChild("DataPropertyAssertion");
214
    $dataProperty = $datatypePropertyAssertion->addChild("DataProperty");
215
216
    $dataProperty->addAttribute("IRI", "#" . format_string($propKey));
217
    $nameIndividual = $datatypePropertyAssertion->addChild("NamedIndividual");
218
    $nameIndividual->addAttribute("IRI", "#" . $elementName);
219
    $literal = $datatypePropertyAssertion->addChild("Literal", $propValue);
220
221
    return $newXML;
222
}
223
224
function writeIndividual($newXML, $type, $elementName) {
225
226
    $classAssertion = $newXML->addChild("ClassAssertion");
227
    $entity = $classAssertion->addChild("Class");
228
    $entity->addAttribute('IRI', "#" . $type);
229
    $individual = $classAssertion->addChild("NamedIndividual");
230
    $individual->addAttribute('IRI', "#" . $elementName);
231
232
    return $newXML;
233
}
234
235
function writeSubClass($newXML, $class, $subClass) {
236
237
    $subClassOf = $newXML->addChild('SubClassOf');
238
    $classSub = $subClassOf->addChild("Class");
239
    $classSub->addAttribute('IRI', "#" . $subClass);
240
    $classSup = $subClassOf->addChild("Class");
241
    $classSup->addAttribute('IRI', "#" . $class);
242
    
243
    return $newXML;
244
}
245
246
?>