package net.timbusproject.iermtoowl.utils;
import net.timbusproject.iermtoowl.epc.Connections;
import net.timbusproject.iermtoowl.epc.EPCModel;
import net.timbusproject.iermtoowl.epc.Elements;
import net.timbusproject.iermtoowl.risks.BusinessActivity;
import net.timbusproject.iermtoowl.risks.BusinessProcess;
import net.timbusproject.iermtoowl.risks.Resource;
import net.timbusproject.iermtoowl.risks.Risk;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.*;
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 uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl;
import java.io.*;
import java.util.HashMap;
import java.util.UUID;
/**
* Created by miguel on 25-06-2014.
*/
public class OntologyMapper {
private Ontology ontology;
private OWLDataFactory df;
private OWLOntologyManager m;
public OntologyMapper() {
try {
ontology = Ontology.createOntology("http://timbus.teco.edu/ontologies/examples/ierm-ontology.owl");
df = OWLManager.getOWLDataFactory();
m = new OWLOntologyManagerImpl(df);
} catch (OntologyCreationException e) {
e.printStackTrace();
}
}
public String run(BusinessProcess bp, EPCModel epc, String[] imports) throws IOException, OWLOntologyStorageException {
System.out.println("Starting ontology conversion engine..");
System.out.println("Adding default import http://timbus.teco.edu/ontologies/DIO.owl");
ontology.addImport("http://timbus.teco.edu/ontologies/DIO.owl");
System.out.println("Adding default import http://timbus.teco.edu/public/ontologies/DSOs/risk.owl");
ontology.addImport("http://timbus.teco.edu/public/ontologies/DSOs/risk.owl");
if (imports != null)
for (String importt : imports) {
System.out.println("Adding provided import " + importt);
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"),
new Prefix("rdfs", "http://www.w3.org/2000/01/rdf-schema")
);
if (bp != null)
setBusinessProcessPart(bp);
if (epc != null)
setEPCPart(epc);
return getOntology(ontology);
}
private void setBusinessProcessPart(BusinessProcess bp) {
for (BusinessActivity ba : bp.getBusinessActivity()) {
addActivity(ba);
}
for (Risk r : bp.getRisk()) {
addRisk(r);
}
}
private void setEPCPart(EPCModel epc) {
for (Elements e : epc.getElements()) {
if (e.getType().equals("epc:Event"))
addBusinessEvent(e);
if (e.getType().equals("epc:JoinLogicGate"))
addAndJunction(e);
if (e.getType().equals("epc:SplitLogicGate")) {
if (e.getGateType() != null && e.getGateType().equals("XOR"))
addJunction(e);
else
addAndJunction(e);
}
if (e.getType().equals("epc:Function")) {
if (e.getBusinessActivityInstance() == null) {
ontology.addAxiom(true, new ClassAxiom(e.getUuid(), "dio:BusinessProcess"));
if (e.getName() != null)
ontology.addAxiom(true, new DataPropertyAxiom<>(e.getUuid(), "rdfs:label", e.getName()));
}
}
}
HashMap<String, Elements> idToElement = new HashMap<>();
for (Elements e : epc.getElements())
idToElement.put(e.getUuid(), e);
for (Connections c : epc.getConnections()) {
String from;
String to;
if (idToElement.containsKey(c.getSource())) {
if (idToElement.get(c.getSource()).getBusinessActivityInstance() != null)
from = idToElement.get(c.getSource()).getBusinessActivityInstance().getId();
else
from = idToElement.get(c.getSource()).getUuid();
} else
throw new IllegalArgumentException("FlowConnection Source " + c.getSource() + " does not exist in this scope");
if (idToElement.containsKey(c.getTarget())) {
if (idToElement.get(c.getTarget()).getBusinessActivityInstance() != null)
to = idToElement.get(c.getTarget()).getBusinessActivityInstance().getId();
else
to = idToElement.get(c.getTarget()).getUuid();
} else
throw new IllegalArgumentException("FlowConnection Target " + c.getSource() + " does not exist in this scope");
addFlow(from, to);
}
}
private void addFlow(String idFrom, String idTo) {
ontology.addAxiom(true, new ObjectPropertyAxiom(idFrom, "dio:FlowTo", idTo));
}
private void addAndJunction(Elements e) {
ontology.addAxiom(true, new ClassAxiom(e.getUuid(), "dio:AndJunction"));
if (e.getName() != null)
ontology.addAxiom(true, new DataPropertyAxiom<>(e.getUuid(), "rdfs:label", e.getName()));
}
private void addJunction(Elements e) {
ontology.addAxiom(true, new ClassAxiom(e.getUuid(), "dio:Junction"));
if (e.getName() != null)
ontology.addAxiom(true, new DataPropertyAxiom<>(e.getUuid(), "rdfs:label", e.getName()));
}
private void addBusinessEvent(Elements e) {
ontology.addAxiom(true, new ClassAxiom(e.getUuid(), "dio:BusinessEvent"));
if (e.getName() != null)
ontology.addAxiom(true, new DataPropertyAxiom<>(e.getUuid(), "rdfs:label", e.getName()));
}
private void addActivity(BusinessActivity activity) {
ontology.addAxiom(true, new ClassAxiom(activity.getId(), "dio:BusinessProcess"));
if (activity.getName() != null)
ontology.addAxiom(true, new DataPropertyAxiom<>(activity.getId(), "rdfs:label", activity.getName()));
for (Resource r : activity.getRequiredResourceInstance()) {
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(), "rdfs: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.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 riskPath = "/home/miguel/Documentos/ierm-bpmn-owl-project/ierm-owl/risk-output-forDPES.xml";
String epcPath = "/home/miguel/Documentos/ierm-bpmn-owl-project/ierm-owl/drugfusion.bpmn.epc";
RiskReader reader = new RiskReader();
EPCReader epcReader = new EPCReader();
OntologyMapper mapper = new OntologyMapper();
BusinessProcess bp = reader.readFromFile(riskPath);
EPCModel epc = epcReader.readFromFile(epcPath, bp);
String owlString = mapper.run(bp, epc, new String[]{"http://timbus.teco.edu/ontologies/examples/WP9-eHealth/y3review.owl"});
File file = new File("outputTest.owl");
FileUtils.writeStringToFile(file, owlString);
}
}