<?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
*/
//path to input file
$csv = fopen("input.csv", "r");
Header('Content-type: text/xml');
$xml = simplexml_load_file(getInput() . ".archimate");
$newsXML = new SimpleXMLElement('<RDF/>');
//Read Archi File --------------------------------------------------------------
$arrayRelationAccessType = array('Write', 'Read', 'Access', 'Read_Write');
$hashAccessRelations = null;
foreach ($arrayRelationAccessType as $value) {
$property = $newsXML->addChild('SubObjectPropertyOf');
$subProp = $property->addChild("ObjectProperty");
$subProp->addAttribute("IRI", "#" . $value);
$subPropOf = $property->addChild("ObjectProperty");
$subPropOf->addAttribute("IRI", "#accesses");
}
$att = $xml->xpath('//element[@accessType]');
foreach ($att as $simpleXmlObject) {
$source = $simpleXmlObject->attributes()['source'];
$sourceElementObject = $xml->xpath(sprintf("//element[@id='%d']", $source));
$sourceElement = format_string($sourceElementObject[0]->attributes()['name']);
$target = $simpleXmlObject->attributes()['target'];
$targetElementObject = $xml->xpath(sprintf("//element[@id='%d']", $target));
$targetElement = format_string($targetElementObject[0]->attributes()['name']);
$accessType = $simpleXmlObject->attributes()['accessType'];
$relation = $newsXML->addChild("ObjectPropertyAssertion");
$op = $relation->addChild("ObjectProperty");
$op->addAttribute("IRI", "#" . $arrayRelationAccessType[(int) $accessType]);
$name1 = $relation->addChild("NamedIndividual");
$name1->addAttribute("IRI", "#" . $sourceElement);
$name2 = $relation->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;
}
$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 = $newsXML->addChild("ClassAssertion");
$entity = $classAssertion->addChild("Class");
$entity->addAttribute('IRI', "#" . $type);
$individual = $classAssertion->addChild("NamedIndividual");
$individual->addAttribute('IRI', "#" . $elementName);
} else {
$type = strtolower($type);
switch ($type) {
case 'access':
$type = 'accesses';
break;
case 'aggregation':
$type = 'aggregates';
break;
case 'assignment':
$type = 'assignedFrom';
break;
case 'composition':
$type = 'composedOf';
break;
case 'flow':
$type = 'flowTo';
break;
case 'influence':
$type = 'influencedBy';
break;
case 'realisation':
$type = 'realizes';
break;
case 'specialisation':
$type = 'specialization';
break;
case 'triggering':
$type = 'triggers';
break;
case 'usedby':
$type = 'usedBy';
break;
default:
break;
}
if ($elementName) {
if (array_search("$elementName.$type", $arrayOperations) === false) {
$property = $newsXML->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;
$relation = $newsXML->addChild("ObjectPropertyAssertion");
$op = $relation->addChild("ObjectProperty");
$op->addAttribute("IRI", "#" . $operation);
$name1 = $relation->addChild("NamedIndividual");
$name1->addAttribute("IRI", "#" . $sourceName);
$name2 = $relation->addChild("NamedIndividual");
$name2->addAttribute("IRI", "#" . $targetName);
if ($type == 'accesses' &&
!$hashAccessRelations[$sourceName][$targetName]) {
$relation = $newsXML->addChild("ObjectPropertyAssertion");
$op = $relation->addChild("ObjectProperty");
$op->addAttribute("IRI", "#Write");
$name1 = $relation->addChild("NamedIndividual");
$name1->addAttribute("IRI", "#" . $sourceName);
$name2 = $relation->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($newsXML->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() {
fwrite(STDOUT, 'Type the .archimate file name:');
$input = fgets(STDIN);
return rtrim($input);
}
?>