<?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
*/
//Files
$csv = fopen("D:/Dropbox/11_ScriptsTese/Archi_files/lnec.csv", "r");
$xml = simplexml_load_file("D:/Dropbox/11_ScriptsTese/Archi_files/LNEC.archimate");
$xmlArchimate = simplexml_load_file(/* getInput() . */ "D:/Dropbox/11_ScriptsTese/PhpProject/DIO-Archimate-V0.1.owl");
Header('Content-type: text/xml');
$newXML = new SimpleXMLElement($xmlArchimate->asXML());
//Read Archi File --------------------------------------------------------------
$arrayRelationAccessType = array('hasAccessTypeWrite', 'hasAccessTypeRead', 'hasAccessTypeAccess', 'hasAccessTypeRead_Write');
$hashAccessRelations = null;
//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");
}
//Get DataProperties
$arrayProperty = $xml->xpath("//element/property/parent::*");
//Write DataProperties
foreach ($arrayProperty as $elementWithProperty) {
$elementName = $elementWithProperty->attributes()['name'];
if ($elementName) {
$property = $elementWithProperty->property->attributes()['key'];
$propertyValue = $elementWithProperty->property->attributes()['value'];
$dataPropertyAssertion = $newXML->addChild("DataPropertyAssertion");
$dataProperty = $dataPropertyAssertion->addChild("DataProperty");
$dataProperty->addAttribute('IRI', "#" . $property);
$individual = $dataPropertyAssertion->addChild("NamedIndividual");
$individual->addAttribute('IRI', "#" . $elementName);
$literal = $dataPropertyAssertion->addChild("Literal", $propertyValue);
//$literal->addAttribute('datatypeIRI', "");
}
}
//Read AccessType
$elementsWithAccessTypes = $xml->xpath('//element[@accessType]');
//Write AccessType
foreach ($elementsWithAccessTypes as $element) {
$source = $element->attributes()['source'];
$sourceElementObject = $xml->xpath(sprintf("//element[@id='%s']", $source));
$sourceElement = format_string($sourceElementObject[0]->attributes()['name']);
$target = $element->attributes()['target'];
$targetElementObject = $xml->xpath(sprintf("//element[@id='%s']", $target));
$targetElement = format_string($targetElementObject[0]->attributes()['name']);
$accessType = $element->attributes()['accessType'];
$objectPropertyAssertion = $newXML->addChild("ObjectPropertyAssertion");
$op = $objectPropertyAssertion->addChild("ObjectProperty");
$op->addAttribute("IRI", "#" . $arrayRelationAccessType[(int) $accessType]);
$name1 = $objectPropertyAssertion->addChild("NamedIndividual");
$name1->addAttribute("IRI", "#" . $sourceElement);
$name2 = $objectPropertyAssertion->addChild("NamedIndividual");
$name2->addAttribute("IRI", "#" . $targetElement);
$hashAccessRelations[$sourceElement][$targetElement] = true;
}
//Read CSV File-----------------------------------------------------------------
$arrayOperations = array();
$firstline = true;
while (!feof($csv)) {
$line = fgetcsv($csv);
if ($firstline) {
$firstline = false;
continue;
}
if (empty($line)){
continue;
}
$elementName = format_string($line[1]);
$sourceName = format_string($line[4]);
$targetName = format_string($line[6]);
$type = str_replace("Relationship", "", $line[0]);
if ($line[3] == "" && $type != "") {
$classAssertion = $newXML->addChild("ClassAssertion");
$entity = $classAssertion->addChild("Class");
$entity->addAttribute('IRI', "#" . $type);
$individual = $classAssertion->addChild("NamedIndividual");
$individual->addAttribute('IRI', "#" . $elementName);
} else {
$type = convertObjectPropertyName($type);
if ($elementName) {
if (array_search("$elementName.$type", $arrayOperations) === false) {
$property = $newXML->addChild('SubObjectPropertyOf');
$subProp = $property->addChild("ObjectProperty");
$subProp->addAttribute("IRI", "#" . $elementName);
$subPropOf = $property->addChild("ObjectProperty");
$subPropOf->addAttribute("IRI", "#" . $type);
array_push($arrayOperations, "$elementName.$type");
}
}
$operation = $elementName ? $elementName : $type;
$objectPropertyAssertion = $newXML->addChild("ObjectPropertyAssertion");
$op = $objectPropertyAssertion->addChild("ObjectProperty");
$op->addAttribute("IRI", "#" . $operation);
$name1 = $objectPropertyAssertion->addChild("NamedIndividual");
$name1->addAttribute("IRI", "#" . $sourceName);
$name2 = $objectPropertyAssertion->addChild("NamedIndividual");
$name2->addAttribute("IRI", "#" . $targetName);
if ($type == 'accesses' &&
!$hashAccessRelations[$sourceName][$targetName]) {
$objectPropertyAssertion = $newXML->addChild("ObjectPropertyAssertion");
$op = $objectPropertyAssertion->addChild("ObjectProperty");
$op->addAttribute("IRI", "#hasAccessTypeWrite");
$name1 = $objectPropertyAssertion->addChild("NamedIndividual");
$name1->addAttribute("IRI", "#" . $sourceName);
$name2 = $objectPropertyAssertion->addChild("NamedIndividual");
$name2->addAttribute("IRI", "#" . $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;
}
?>