Parent: [9e28b1] (diff)

Child: [19506b] (diff)

Download this file

OntologyMapper.java    229 lines (190 with data), 10.5 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
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);
}
}