Switch to side-by-side view

--- a
+++ b/ConvertArchi2OWL_v2.4.php
@@ -0,0 +1,279 @@
+<?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
+ */
+
+Header('Content-type: text/xml');
+
+//Files
+$csvFile = "D:/Dropbox/11_ScriptsTese/Archi_files/eHealth_Model.csv";
+//If you provide the DIO file, the Model you be added to DIO file.
+$DIOFile = "D:/Dropbox/11_ScriptsTese/PhpProject/DIO-Archimate-V0.1.owl";
+
+
+$csv = fopen($csvFile, "r");
+
+
+if ($DIOFile) {
+
+    $xmlArchimate = simplexml_load_file(/* getInput() . */ $DIOFile);
+    $newXML = new SimpleXMLElement($xmlArchimate->asXML());
+} else {
+    $newXML = new SimpleXMLElement("<RDF/>");
+}
+
+//Read Archi File --------------------------------------------------------------
+$arrayRelationAccessType = array('hasAccessTypeWrite', 'hasAccessTypeRead', 'hasAccessTypeAccess', 'hasAccessTypeRead_Write');
+$arrayJunctions = array();
+
+//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");
+}
+
+//Read CSV File-----------------------------------------------------------------
+$arrayOperations = array();
+$firstline = true;
+while (!feof($csv)) {
+
+    $line = fgetcsv($csv);
+    $line = to_utf8($line);
+    //skip first line
+    if ($firstline) {
+        $firstline = false;
+        continue;
+    }
+
+    //skip empty line
+    if (empty($line)) {
+        continue;
+    }
+
+    $elementName = format_string($line[1]);
+    $sourceType = format_string($line[3]);
+    $sourceName = format_string($line[4]);
+    $targetType = format_string($line[5]);
+    $targetName = format_string($line[6]);
+    $type = str_replace("Relationship", "", $line[0]);
+
+    //is individual
+    if ($line[3] == "" && $type != "") {
+
+        $newXML = writeIndividual($newXML, $type, $elementName);
+
+        //has property
+        if (!($line[8] == "")) {
+            $props = split(";", $line[8]);
+            foreach ($props as $prop) {
+                // '|' is operator in php
+                $keyvalue = split("\|", $prop);
+                list($propKey, $propValue) = $keyvalue;
+                $newXML = writeDataProperty($newXML, $elementName, $propKey, $propValue);
+            }
+        }
+    } else {//is relation
+        $type = convertObjectPropertyName($type);
+
+        if ($elementName) {
+
+            if (array_search("$elementName.$type", $arrayOperations) === false) {
+
+                $newXML = writeSubProperty($newXML, $type, $elementName);
+                array_push($arrayOperations, "$elementName.$type");
+            }
+        }
+
+        if (($sourceType == 'AndJunction' or $targetType == 'AndJunction')
+                and array_search('AndJunction', $arrayJunctions) === false) {
+
+            $newXML = writeSubClass($newXML, 'Junction', 'AndJunction');
+            array_push($arrayJunctions, 'AndJunction');
+        }
+        if (($sourceType == 'OrJunction' or $targetType == 'OrJunction')
+                and array_search('OrJunction', $arrayJunctions) === false) {
+
+            $newXML = writeSubClass($newXML, 'Junction', 'OrJunction');
+            array_push($arrayJunctions, 'OrJunction');
+        }
+
+        $operation = $elementName ? $elementName : $type;
+
+        $newXML = writeProperty($newXML, $operation, $sourceName, $targetName);
+
+        if ($type == 'accesses') {
+
+            $newXML = writeProperty($newXML, $arrayRelationAccessType[$line[7]], $sourceName, $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(replace_special_chars($newXML->asXML()));
+//echo $newXML->asXML();
+$dom->save('eHealth_Model.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;
+}
+
+function writeSubProperty($newXML, $superProperty, $subProperty) {
+
+    $property = $newXML->addChild('SubObjectPropertyOf');
+    $subProp = $property->addChild("ObjectProperty");
+    $subProp->addAttribute("IRI", "#" . $subProperty);
+    $subPropOf = $property->addChild("ObjectProperty");
+    $subPropOf->addAttribute("IRI", "#" . $superProperty);
+
+    return $newXML;
+}
+
+function writeProperty($newXML, $relation, $source, $target) {
+
+    $objectPropertyAssertion = $newXML->addChild("ObjectPropertyAssertion");
+    $op = $objectPropertyAssertion->addChild("ObjectProperty");
+    $op->addAttribute("IRI", "#" . $relation);
+    $name1 = $objectPropertyAssertion->addChild("NamedIndividual");
+    $name1->addAttribute("IRI", "#" . $source);
+    $name2 = $objectPropertyAssertion->addChild("NamedIndividual");
+    $name2->addAttribute("IRI", "#" . $target);
+
+    return $newXML;
+}
+
+function writeDataProperty($newXML, $elementName, $propKey, $propValue) {
+
+    $datatypePropertyAssertion = $newXML->addChild("DataPropertyAssertion");
+    $dataProperty = $datatypePropertyAssertion->addChild("DataProperty");
+
+    $dataProperty->addAttribute("IRI", "#" . format_string($propKey));
+    $nameIndividual = $datatypePropertyAssertion->addChild("NamedIndividual");
+    $nameIndividual->addAttribute("IRI", "#" . $elementName);
+    $literal = $datatypePropertyAssertion->addChild("Literal", $propValue);
+
+    return $newXML;
+}
+
+function writeIndividual($newXML, $type, $elementName) {
+
+    $classAssertion = $newXML->addChild("ClassAssertion");
+    $entity = $classAssertion->addChild("Class");
+    $entity->addAttribute('IRI', "#" . $type);
+    $individual = $classAssertion->addChild("NamedIndividual");
+    $individual->addAttribute('IRI', "#" . $elementName);
+
+    return $newXML;
+}
+
+function writeSubClass($newXML, $class, $subClass) {
+
+    $subClassOf = $newXML->addChild('SubClassOf');
+    $classSub = $subClassOf->addChild("Class");
+    $classSub->addAttribute('IRI', "#" . $subClass);
+    $classSup = $subClassOf->addChild("Class");
+    $classSup->addAttribute('IRI', "#" . $class);
+
+    return $newXML;
+}
+
+function to_utf8($in) {
+    if (is_array($in)) {
+        foreach ($in as $key => $value) {
+            $out[to_utf8($key)] = to_utf8($value);
+        }
+    } elseif (is_string($in)) {
+        return utf8_encode($in);
+    } else {
+        return $in;
+    }
+    return $out;
+}
+//http://konfiguracja.c0.pl/webpl/index_en.html
+//http://en.sourceforge.jp/projects/nucleus-jp/scm/git/nucleus-jp-ancient/blobs/branch-3-6/utf8/nucleus/libs/entity.php
+function replace_special_chars($in){
+    
+    //cp1251
+    $_entities = array (
+          '&#x80;'                => '&#x20AC;',  '&#x82;'                => '&#x201A;',  '&#x83;'                => '&#x192;',   
+          '&#x84;'                => '&#x201E;',  '&#x85;'                => '&#x2026;',  '&#x86;'                => '&#x2020;',  
+          '&#x87;'                => '&#x2021;',  '&#x88;'                => '&#x2C6;',   '&#x89;'                => '&#x2030;',  
+          '&#x8A;'                => '&#x160;',   '&#x8B;'                => '&#x2039;',  '&#x8C;'                => '&#x152;',   
+          '&#x8E;'                => '&#x17D;',   '&#x91;'                => '&#x2018;',  '&#x92;'                => '&#x2019;',  
+          '&#x93;'                => '&#x201C;',  '&#x94;'                => '&#x201D;',  '&#x95;'                => '&#x2022;',  
+          '&#x96;'                => '&#x2013;',  '&#x97;'                => '&#x2014;',  '&#x98;'                => '&#x2DC;',   
+          '&#x99;'                => '&#x2122;',  '&#x9A;'                => '&#x161;',   '&#x9B;'                => '&#x203A;',  
+          '&#x9C;'                => '&#x153;',   '&#x9E;'                => '&#x17E;',   '&#x9F;'                => '&#x178;',   
+  );
+    $out = str_replace(array_keys($_entities), array_values($_entities), $in);
+    return $out;
+}
+
+?>