Child: [05f15f] (diff)

Download this file

OntologyMapper.java    139 lines (111 with data), 6.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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);
}
}