<?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/lnec.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);
//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($newXML->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($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;
}
?>