|
| 1 | +package org.processmining.placebasedlpmdiscovery.prom.plugins.exports; |
| 2 | + |
| 3 | +import com.csvreader.CsvWriter; |
| 4 | +import org.processmining.acceptingpetrinet.models.AcceptingPetriNet; |
| 5 | +import org.processmining.contexts.uitopia.annotations.UIExportPlugin; |
| 6 | +import org.processmining.framework.plugin.PluginContext; |
| 7 | +import org.processmining.framework.plugin.annotations.Plugin; |
| 8 | +import org.processmining.framework.plugin.annotations.PluginVariant; |
| 9 | +import org.processmining.placebasedlpmdiscovery.lpmevaluation.results.LPMEvaluationResult; |
| 10 | +import org.processmining.placebasedlpmdiscovery.lpmevaluation.results.LPMEvaluationResultId; |
| 11 | +import org.processmining.placebasedlpmdiscovery.lpmevaluation.results.StandardLPMEvaluationResultId; |
| 12 | +import org.processmining.placebasedlpmdiscovery.lpmevaluation.results.aggregateoperations.EvaluationResultAggregateOperation; |
| 13 | +import org.processmining.placebasedlpmdiscovery.lpmevaluation.undecided.Utils; |
| 14 | +import org.processmining.placebasedlpmdiscovery.model.LocalProcessModel; |
| 15 | +import org.processmining.placebasedlpmdiscovery.model.discovery.LPMDiscoveryResult; |
| 16 | +import org.processmining.placebasedlpmdiscovery.utils.LocalProcessModelUtils; |
| 17 | + |
| 18 | +import java.io.ByteArrayOutputStream; |
| 19 | +import java.io.File; |
| 20 | +import java.io.IOException; |
| 21 | +import java.io.OutputStreamWriter; |
| 22 | +import java.nio.file.Files; |
| 23 | +import java.util.zip.ZipEntry; |
| 24 | +import java.util.zip.ZipOutputStream; |
| 25 | + |
| 26 | +@Plugin( |
| 27 | + name = "Export local process models into an array of Petri nets", |
| 28 | + returnLabels = {}, |
| 29 | + returnTypes = {}, |
| 30 | + parameterLabels = {"LPMDiscovery", "Filename"}) |
| 31 | +@UIExportPlugin( |
| 32 | + description = "Exports local process models as Accepting Petri Nets together with a csv file that includes all filenames and aggregated score.", |
| 33 | + extension = "zip") |
| 34 | +public class LPMDiscoveryResultToZipWithCsvExportPlugin { |
| 35 | + @PluginVariant(variantLabel = "Export local process models into a file", requiredParameterLabels = {0, 1}) |
| 36 | + public void export(PluginContext context, LPMDiscoveryResult lpmResult, File file) throws IOException { |
| 37 | + String fileName = file.getName(); |
| 38 | + String prefix = fileName.substring(0, fileName.indexOf(".")); |
| 39 | + |
| 40 | + ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(file.toPath())); |
| 41 | + |
| 42 | + String csvFileName = prefix + ".csv"; |
| 43 | + ByteArrayOutputStream csvOS = new ByteArrayOutputStream(); |
| 44 | + CsvWriter csvWriter = new CsvWriter(new OutputStreamWriter(csvOS), ','); |
| 45 | + csvWriter.writeRecord(new String[]{"Name", "Fitting Windows Score", "Trace Support Score", "Aggregated Score"}); |
| 46 | + |
| 47 | + EvaluationResultAggregateOperation aggregateOperation = new EvaluationResultAggregateOperation(); |
| 48 | + StandardLPMEvaluationResultId[] ids = new StandardLPMEvaluationResultId[]{ |
| 49 | + StandardLPMEvaluationResultId.FittingWindowsEvaluationResult, |
| 50 | + StandardLPMEvaluationResultId.TraceSupportEvaluationResult |
| 51 | + }; |
| 52 | + |
| 53 | + for (LocalProcessModel lpm : lpmResult.getAllLPMs()) { |
| 54 | + // convert lpm to accepting petri net |
| 55 | + AcceptingPetriNet apn = LocalProcessModelUtils.getAcceptingPetriNetRepresentation(lpm); |
| 56 | + |
| 57 | + String zfName = prefix + "." + lpm.getId() + ".pnml"; |
| 58 | + |
| 59 | + // add the net file to the zip folder |
| 60 | + ByteArrayOutputStream oos = new ByteArrayOutputStream(); |
| 61 | + Utils.exportAcceptingPetriNetToOutputStream(context, apn, oos); |
| 62 | + addContentToZip(out, oos.toByteArray(), prefix + "." + lpm.getId() + ".pnml"); |
| 63 | + |
| 64 | + // write an entry in the csv |
| 65 | + csvWriter.write(zfName); |
| 66 | + for (LPMEvaluationResultId id : ids) { |
| 67 | + csvWriter.write(String.valueOf(lpm.getAdditionalInfo() |
| 68 | + .getEvaluationResult(id.name(), LPMEvaluationResult.class).getResult())); |
| 69 | + } |
| 70 | + csvWriter.write(String.valueOf(aggregateOperation.aggregate(lpm.getAdditionalInfo().getEvaluationResults().values()))); |
| 71 | + csvWriter.endRecord(); |
| 72 | + } |
| 73 | + // add csv file to zip |
| 74 | + csvWriter.close(); |
| 75 | + addContentToZip(out, csvOS.toByteArray(), csvFileName); |
| 76 | + out.close(); |
| 77 | + } |
| 78 | + |
| 79 | + private void addContentToZip(ZipOutputStream out, byte[] content, String fileName) throws IOException { |
| 80 | + ZipEntry e = new ZipEntry(fileName.substring(0, fileName.lastIndexOf(".")) + fileName.substring(fileName.lastIndexOf("."))); |
| 81 | + out.putNextEntry(e); |
| 82 | + out.write(content); |
| 83 | + out.closeEntry(); |
| 84 | + } |
| 85 | +} |
0 commit comments