Switch to unified view

a b/ConvertArchi2OWL_v2.1.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
//Files
14
$csv = fopen("D:/Dropbox/11_ScriptsTese/Archi_files/lnec.csv", "r");
15
$xml = simplexml_load_file("D:/Dropbox/11_ScriptsTese/Archi_files/LNEC.archimate");
16
$xmlArchimate = simplexml_load_file(/* getInput() . */ "D:/Dropbox/11_ScriptsTese/PhpProject/DIO-Archimate-V0.1.owl");
17
18
19
20
Header('Content-type: text/xml');
21
$newXML = new SimpleXMLElement($xmlArchimate->asXML());
22
23
//Read Archi File --------------------------------------------------------------
24
$arrayRelationAccessType = array('hasAccessTypeWrite', 'hasAccessTypeRead', 'hasAccessTypeAccess', 'hasAccessTypeRead_Write');
25
$hashAccessRelations = null;
26
27
//Write AccessTypes as subProperties
28
foreach ($arrayRelationAccessType as $value) {
29
30
    $property = $newXML->addChild('SubObjectPropertyOf');
31
    $subProp = $property->addChild("ObjectProperty");
32
    $subProp->addAttribute("IRI", "#" . $value);
33
    $subPropOf = $property->addChild("ObjectProperty");
34
    $subPropOf->addAttribute("IRI", "#accesses");
35
}
36
37
//Get DataProperties
38
$arrayProperty = $xml->xpath("//element/property/parent::*");
39
40
//Write DataProperties
41
foreach ($arrayProperty as $elementWithProperty) {
42
    
43
    $elementName = $elementWithProperty->attributes()['name'];
44
45
    if ($elementName) {
46
        $property = $elementWithProperty->property->attributes()['key'];
47
        $propertyValue = $elementWithProperty->property->attributes()['value'];
48
49
        $dataPropertyAssertion = $newXML->addChild("DataPropertyAssertion");
50
        $dataProperty = $dataPropertyAssertion->addChild("DataProperty");
51
        $dataProperty->addAttribute('IRI', "#" . $property);
52
        $individual = $dataPropertyAssertion->addChild("NamedIndividual");
53
        $individual->addAttribute('IRI', "#" . $elementName);
54
        $literal = $dataPropertyAssertion->addChild("Literal", $propertyValue);
55
        //$literal->addAttribute('datatypeIRI', "");
56
    }
57
}
58
59
//Read AccessType
60
$elementsWithAccessTypes = $xml->xpath('//element[@accessType]');
61
62
//Write AccessType
63
foreach ($elementsWithAccessTypes as $element) {
64
65
    $source = $element->attributes()['source'];
66
    $sourceElementObject = $xml->xpath(sprintf("//element[@id='%s']", $source));
67
    $sourceElement = format_string($sourceElementObject[0]->attributes()['name']);
68
69
    $target = $element->attributes()['target'];
70
    $targetElementObject = $xml->xpath(sprintf("//element[@id='%s']", $target));
71
    $targetElement = format_string($targetElementObject[0]->attributes()['name']);
72
73
    $accessType = $element->attributes()['accessType'];
74
75
    $objectPropertyAssertion = $newXML->addChild("ObjectPropertyAssertion");
76
    $op = $objectPropertyAssertion->addChild("ObjectProperty");
77
    $op->addAttribute("IRI", "#" . $arrayRelationAccessType[(int) $accessType]);
78
    $name1 = $objectPropertyAssertion->addChild("NamedIndividual");
79
    $name1->addAttribute("IRI", "#" . $sourceElement);
80
    $name2 = $objectPropertyAssertion->addChild("NamedIndividual");
81
    $name2->addAttribute("IRI", "#" . $targetElement);
82
83
    $hashAccessRelations[$sourceElement][$targetElement] = true;
84
}
85
86
//Read CSV File-----------------------------------------------------------------
87
$arrayOperations = array();
88
$firstline = true;
89
while (!feof($csv)) {
90
91
    $line = fgetcsv($csv);
92
    if ($firstline) {
93
        $firstline = false;
94
        continue;
95
    }
96
97
    if (empty($line)){
98
        continue;
99
    }
100
101
    $elementName = format_string($line[1]);
102
    $sourceName = format_string($line[4]);
103
    $targetName = format_string($line[6]);
104
    $type = str_replace("Relationship", "", $line[0]);
105
106
    if ($line[3] == "" && $type != "") {
107
        $classAssertion = $newXML->addChild("ClassAssertion");
108
        $entity = $classAssertion->addChild("Class");
109
        $entity->addAttribute('IRI', "#" . $type);
110
        $individual = $classAssertion->addChild("NamedIndividual");
111
        $individual->addAttribute('IRI', "#" . $elementName);
112
    } else {
113
        
114
        $type = convertObjectPropertyName($type);
115
        
116
        if ($elementName) {
117
118
            if (array_search("$elementName.$type", $arrayOperations) === false) {
119
                $property = $newXML->addChild('SubObjectPropertyOf');
120
                $subProp = $property->addChild("ObjectProperty");
121
                $subProp->addAttribute("IRI", "#" . $elementName);
122
                $subPropOf = $property->addChild("ObjectProperty");
123
                $subPropOf->addAttribute("IRI", "#" . $type);
124
                array_push($arrayOperations, "$elementName.$type");
125
            }
126
        }
127
128
        $operation = $elementName ? $elementName : $type;
129
130
131
        $objectPropertyAssertion = $newXML->addChild("ObjectPropertyAssertion");
132
        $op = $objectPropertyAssertion->addChild("ObjectProperty");
133
        $op->addAttribute("IRI", "#" . $operation);
134
        $name1 = $objectPropertyAssertion->addChild("NamedIndividual");
135
        $name1->addAttribute("IRI", "#" . $sourceName);
136
        $name2 = $objectPropertyAssertion->addChild("NamedIndividual");
137
        $name2->addAttribute("IRI", "#" . $targetName);
138
        
139
        if ($type == 'accesses' &&
140
                !$hashAccessRelations[$sourceName][$targetName]) {
141
142
            $objectPropertyAssertion = $newXML->addChild("ObjectPropertyAssertion");
143
            $op = $objectPropertyAssertion->addChild("ObjectProperty");
144
            $op->addAttribute("IRI", "#hasAccessTypeWrite");
145
            $name1 = $objectPropertyAssertion->addChild("NamedIndividual");
146
            $name1->addAttribute("IRI", "#" . $sourceName);
147
            $name2 = $objectPropertyAssertion->addChild("NamedIndividual");
148
            $name2->addAttribute("IRI", "#" . $targetName);
149
        }
150
    }
151
}
152
153
//path to output file
154
//Format XML to save indented tree rather than one line
155
$dom = new DOMDocument('1.0');
156
$dom->preserveWhiteSpace = false;
157
$dom->formatOutput = true;
158
$dom->loadXML($newXML->asXML());
159
//echo $dom->saveXML();
160
$dom->save('output.owl');
161
162
function format_string($string) {
163
164
    $elementName = str_replace(" ", "_", $string);
165
    $elementName = str_replace("(", "-", $elementName);
166
    $elementName = str_replace(")", "-", $elementName);
167
    $elementName = str_replace("/", "_", $elementName);
168
    return $elementName;
169
}
170
171
function getInput($fileDescription) {
172
    fwrite(STDOUT, 'Type the file location of '.$fileDescription.':');
173
    $input = fgets(STDIN);
174
    return rtrim($input);
175
}
176
177
function convertObjectPropertyName($oldObjectProperty) {
178
    
179
    $oldObjectProperty = strtolower($oldObjectProperty);
180
        switch ($oldObjectProperty) {
181
            case 'access':
182
                $newObjectProperty = 'accesses';
183
                break;
184
            case 'aggregation':
185
                $newObjectProperty = 'aggregates';
186
                break;
187
            case 'assignment':
188
                $newObjectProperty = 'assignedFrom';
189
                break;
190
            case 'composition':
191
                $newObjectProperty = 'composedOf';
192
                break;
193
            case 'flow':
194
                $newObjectProperty = 'flowTo';
195
                break;
196
            case 'influence':
197
                $newObjectProperty = 'influencedBy';
198
                break;
199
            case 'realisation':
200
                $newObjectProperty = 'realizes';
201
                break;
202
            case 'specialisation':
203
                $newObjectProperty = 'specialization';
204
                break;
205
            case 'triggering':
206
                $newObjectProperty = 'triggers';
207
                break;
208
            case 'usedby':
209
                $newObjectProperty = 'usedBy';
210
                break;
211
212
            default:
213
                $newObjectProperty = 'association';
214
                break;
215
        }
216
    return $newObjectProperty;
217
}
218
219
?>