a b/ConvertArchi2OWL_v2.0.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
//path to input file
14
$csv = fopen("input.csv", "r");
15
Header('Content-type: text/xml');
16
17
$xml = simplexml_load_file(getInput() . ".archimate");
18
$newsXML = new SimpleXMLElement('<RDF/>');
19
20
//Read Archi File --------------------------------------------------------------
21
$arrayRelationAccessType = array('Write', 'Read', 'Access', 'Read_Write');
22
$hashAccessRelations = null;
23
foreach ($arrayRelationAccessType as $value) {
24
25
    $property = $newsXML->addChild('SubObjectPropertyOf');
26
    $subProp = $property->addChild("ObjectProperty");
27
    $subProp->addAttribute("IRI", "#" . $value);
28
    $subPropOf = $property->addChild("ObjectProperty");
29
    $subPropOf->addAttribute("IRI", "#accesses");
30
}
31
$att = $xml->xpath('//element[@accessType]');
32
foreach ($att as $simpleXmlObject) {
33
34
    $source = $simpleXmlObject->attributes()['source'];
35
    $sourceElementObject = $xml->xpath(sprintf("//element[@id='%d']", $source));
36
    $sourceElement = format_string($sourceElementObject[0]->attributes()['name']);
37
38
    $target = $simpleXmlObject->attributes()['target'];
39
    $targetElementObject = $xml->xpath(sprintf("//element[@id='%d']", $target));
40
    $targetElement = format_string($targetElementObject[0]->attributes()['name']);
41
    $accessType = $simpleXmlObject->attributes()['accessType'];
42
43
    $relation = $newsXML->addChild("ObjectPropertyAssertion");
44
    $op = $relation->addChild("ObjectProperty");
45
    $op->addAttribute("IRI", "#" . $arrayRelationAccessType[(int) $accessType]);
46
    $name1 = $relation->addChild("NamedIndividual");
47
    $name1->addAttribute("IRI", "#" . $sourceElement);
48
    $name2 = $relation->addChild("NamedIndividual");
49
    $name2->addAttribute("IRI", "#" . $targetElement);
50
51
    $hashAccessRelations[$sourceElement][$targetElement] = true;
52
}
53
54
//Read CSV File-----------------------------------------------------------------
55
$arrayOperations = array();
56
$firstline = true;
57
while (!feof($csv)) {
58
59
    $line = fgetcsv($csv);
60
    if ($firstline) {
61
        $firstline = false;
62
        continue;
63
    }
64
65
    $elementName = format_string($line[1]);
66
    $sourceName = format_string($line[4]);
67
    $targetName = format_string($line[6]);
68
    $type = str_replace("Relationship", "", $line[0]);
69
70
    if ($line[3] == "" && $type != "") {
71
        $classAssertion = $newsXML->addChild("ClassAssertion");
72
        $entity = $classAssertion->addChild("Class");
73
        $entity->addAttribute('IRI', "#" . $type);
74
        $individual = $classAssertion->addChild("NamedIndividual");
75
        $individual->addAttribute('IRI', "#" . $elementName);
76
    } else {
77
        $type = strtolower($type);
78
        switch ($type) {
79
            case 'access':
80
                $type = 'accesses';
81
                break;
82
            case 'aggregation':
83
                $type = 'aggregates';
84
                break;
85
            case 'assignment':
86
                $type = 'assignedFrom';
87
                break;
88
            case 'composition':
89
                $type = 'composedOf';
90
                break;
91
            case 'flow':
92
                $type = 'flowTo';
93
                break;
94
            case 'influence':
95
                $type = 'influencedBy';
96
                break;
97
            case 'realisation':
98
                $type = 'realizes';
99
                break;
100
            case 'specialisation':
101
                $type = 'specialization';
102
                break;
103
            case 'triggering':
104
                $type = 'triggers';
105
                break;
106
            case 'usedby':
107
                $type = 'usedBy';
108
                break;
109
110
            default:
111
                break;
112
        }
113
        if ($elementName) {
114
115
            if (array_search("$elementName.$type", $arrayOperations) === false) {
116
                $property = $newsXML->addChild('SubObjectPropertyOf');
117
                $subProp = $property->addChild("ObjectProperty");
118
                $subProp->addAttribute("IRI", "#" . $elementName);
119
                $subPropOf = $property->addChild("ObjectProperty");
120
                $subPropOf->addAttribute("IRI", "#" . $type);
121
                array_push($arrayOperations, "$elementName.$type");
122
            }
123
        }
124
125
        $operation = $elementName ? $elementName : $type;
126
127
128
        $relation = $newsXML->addChild("ObjectPropertyAssertion");
129
        $op = $relation->addChild("ObjectProperty");
130
        $op->addAttribute("IRI", "#" . $operation);
131
        $name1 = $relation->addChild("NamedIndividual");
132
        $name1->addAttribute("IRI", "#" . $sourceName);
133
        $name2 = $relation->addChild("NamedIndividual");
134
        $name2->addAttribute("IRI", "#" . $targetName);
135
136
        if ($type == 'accesses' &&
137
                !$hashAccessRelations[$sourceName][$targetName]) {
138
139
            $relation = $newsXML->addChild("ObjectPropertyAssertion");
140
            $op = $relation->addChild("ObjectProperty");
141
            $op->addAttribute("IRI", "#Write");
142
            $name1 = $relation->addChild("NamedIndividual");
143
            $name1->addAttribute("IRI", "#" . $sourceName);
144
            $name2 = $relation->addChild("NamedIndividual");
145
            $name2->addAttribute("IRI", "#" . $targetName);
146
        }
147
    }
148
}
149
150
//path to output file
151
//Format XML to save indented tree rather than one line
152
$dom = new DOMDocument('1.0');
153
$dom->preserveWhiteSpace = false;
154
$dom->formatOutput = true;
155
$dom->loadXML($newsXML->asXML());
156
//echo $dom->saveXML();
157
$dom->save('output.owl');
158
159
function format_string($string) {
160
161
    $elementName = str_replace(" ", "_", $string);
162
    $elementName = str_replace("(", "-", $elementName);
163
    $elementName = str_replace(")", "-", $elementName);
164
    $elementName = str_replace("/", "_", $elementName);
165
    return $elementName;
166
}
167
168
function getInput() {
169
    fwrite(STDOUT, 'Type the .archimate file name:');
170
    $input = fgets(STDIN);
171
    return rtrim($input);
172
}
173
174
?>