package net.timbusproject.iermtoowl.utils;
import net.timbusproject.iermtoowl.BusinessActivity;
import net.timbusproject.iermtoowl.Resource;
import net.timbusproject.iermtoowl.Risk;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.model.OWLOntologyStorageException;
import pt.caixamagica.owl.owlapi.Ontology;
import pt.caixamagica.owl.owlapi.Prefix;
import pt.caixamagica.owl.owlapi.exceptions.OntologyCreationException;
import pt.caixamagica.owl.owlapi.impl.axioms.ClassAxiom;
import pt.caixamagica.owl.owlapi.impl.axioms.DataPropertyAxiom;
import pt.caixamagica.owl.owlapi.impl.axioms.ObjectPropertyAxiom;
import java.io.*;
import java.util.UUID;
/**
* Created by miguel on 25-06-2014.
*/
public class OntologyMapper {
private Ontology ontology;
public OntologyMapper() {
try {
ontology = Ontology.createOntology("http://timbus.teco.edu/ontologies/examples/ierm-ontology.owl");
} catch (OntologyCreationException e) {
e.printStackTrace();
}
}
public String run(String[] imports) throws IOException, OWLOntologyStorageException {
ontology.addImport("http://timbus.teco.edu/ontologies/DIO.owl");
ontology.addImport("http://timbus.teco.edu/public/ontologies/DSOs/risk.owl");
if (imports != null)
for (String importt : imports)
ontology.addImport(importt);
ontology.addPrefix(
new Prefix("risk", "http://timbus.teco.edu/ontologies/DSOs/risk.owl"),
new Prefix("dio", "http://timbus.teco.edu/ontologies/DIO.owl")
);
/*
System.out.println(ontology.getOntologyIRI());
System.exit(0);*/
for (BusinessActivity ba : UnifiedRiskReader.riskXml.getBusinessActivity()) {
addActivity(ba);
}
for (Risk r : UnifiedRiskReader.riskXml.getRisk()) {
addRisk(r);
}
return getOntology(ontology);
}
private void addActivity(BusinessActivity activity) {
ontology.addAxiom(true,
new ClassAxiom(activity.getId(), "dio:BusinessProcess"),
new DataPropertyAxiom<>(activity.getId(), "dio:label", activity.getName())
);
for (Resource r : activity.getRequiredResourceInstance()) {
System.out.println("hellO");
ontology.addAxiom(false,
new ObjectPropertyAxiom(ontology.getOntologyIRI() + "#" + activity.getId(), ontology.getPrefixManager().getIRI("dio:") + "uses", r.getContextModelURI())
);
}
}
private void addRisk(Risk risk) {
ontology.addAxiom(true, new ClassAxiom(risk.getId(), "risk:Risk"));
if (risk.getName() != null && !risk.getName().equals(""))
ontology.addAxiom(true, new DataPropertyAxiom<>(risk.getId(), "dio:label", risk.getName()));
if (risk.getLikelihood() != null && !risk.getLikelihood().equals(""))
ontology.addAxiom(true, new DataPropertyAxiom<>(risk.getId(), "risk:likelihood", risk.getLikelihood()));
if (risk.getPotentialLoss() != null && !risk.getPotentialLoss().equals(""))
ontology.addAxiom(true, new DataPropertyAxiom<>(risk.getId(), "risk:potentialLoss", risk.getPotentialLoss()));
if (risk.getStrategicImpact() != null && !risk.getStrategicImpact().equals(""))
ontology.addAxiom(true, new DataPropertyAxiom<>(risk.getId(), "risk:strategicImpact", risk.getStrategicImpact()));
if (risk.getCustomerImpact() != null && !risk.getCustomerImpact().equals(""))
ontology.addAxiom(true, new DataPropertyAxiom<>(risk.getId(), "risk:customerImpact", risk.getCustomerImpact()));
if (risk.getLegalImpact() != null && !risk.getLegalImpact().equals(""))
ontology.addAxiom(true, new DataPropertyAxiom<>(risk.getId(), "risk:legalImpact", risk.getLegalImpact()));
if (Double.valueOf(risk.getMaxRecoveryTime()) != null)
ontology.addAxiom(true, new DataPropertyAxiom<>(risk.getId(), "risk:maxRecoveryTime", risk.getMaxRecoveryTime()));
if (Double.valueOf(risk.getMinRecoveryTime()) != null)
ontology.addAxiom(true, new DataPropertyAxiom<>(risk.getId(), "risk:minRecoveryTime", risk.getMinRecoveryTime()));
/*if (risk.getName() != null && !risk.getName().equals(""))
ontology.addAxiom(true, new DataPropertyAxiom<>(risk.getId(), "risk:riskCategory", risk.getRiskCategory()));*/
if (risk.getOccurInBusinessActivity() != null && !risk.getOccurInBusinessActivity().equals("")) {
ontology.addAxiom(true, new ObjectPropertyAxiom(risk.getId(), "risk:occurInBusinessActivity", risk.getOccurInBusinessActivity()));
}
if (risk.getOccurInResourceInstance() != null) {
String uri = risk.getOccurInResourceInstance().getContextModelURI();
ontology.addAxiom(false, new ObjectPropertyAxiom(ontology.getOntologyIRI() + "#" + risk.getId(), ontology.getPrefixManager().getIRI("risk:") + "occurInResource", uri));
}
}
public String getOntology(Ontology api) throws IOException, OWLOntologyStorageException {
OWLOntology ontology = api.getOntology();
OWLOntologyManager manager = ontology.getOWLOntologyManager();
File file = File.createTempFile(UUID.randomUUID().toString(), ".tmp");
manager.saveOntology(ontology, new FileOutputStream(file));
StringWriter writer = new StringWriter();
IOUtils.copy(new FileInputStream(file), writer);
//noinspection ResultOfMethodCallIgnored
file.delete();
return writer.toString();
}
public static void main(String[] args) throws IOException, OWLOntologyStorageException {
String path = "/home/miguel/Documentos/ierm-bpmn-owl-project/ierm-owl/risk-output-forDPES.xml";
UnifiedRiskReader reader = new UnifiedRiskReader();
OntologyMapper mapper = new OntologyMapper();
reader.readFromFile(path);
String owlString = mapper.run(new String[]{"http://timbus.teco.edu/ontologies/examples/WP9-eHealth/y3review.owl"});
File file = new File("outputTest.owl");
FileUtils.writeStringToFile(file, owlString);
}
}