From 0020bd87b35bb925ead39d1c421f0e6f4d7e3374 Mon Sep 17 00:00:00 2001 From: Nicacioneto Date: Thu, 28 Sep 2017 10:27:45 -0300 Subject: [PATCH] =?UTF-8?q?Aplicando=20refatora=C3=A7=C3=A3o=20objeto-m?= =?UTF-8?q?=C3=A9todo=20para=20a=20classe=20ParamWrapper?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/paramwrapper/Formula.java | 90 +++++++++++++++++++++++++++++ src/paramwrapper/ParamWrapper.java | 91 ++++++++++++++---------------- 2 files changed, 133 insertions(+), 48 deletions(-) create mode 100644 src/paramwrapper/Formula.java diff --git a/src/paramwrapper/Formula.java b/src/paramwrapper/Formula.java new file mode 100644 index 0000000..58046b6 --- /dev/null +++ b/src/paramwrapper/Formula.java @@ -0,0 +1,90 @@ +package paramwrapper; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.logging.Logger; + +import com.sun.media.sound.PortMixerProvider; + +import java.util.logging.Level; + +public class Formula { + private static final Logger LOGGER = Logger.getLogger(Formula.class.getName()); + private ParamWrapper paramWrapper; + File modelFile; + FileWriter modelWriter; + File propertyFile; + FileWriter propertyWriter; + File resultsFile; + String formula; + + public Formula(ParamWrapper paramWrapper) { + this.paramWrapper = paramWrapper; + } + + public void setModelFile(String model, String param) { + try { + modelFile = File.createTempFile(model, param); + FileWriter modelWriter = new FileWriter(modelFile); + modelWriter.write(model); + modelWriter.flush(); + modelWriter.close(); + } catch (IOException e) { + LOGGER.log(Level.SEVERE, e.toString(), e); + } + } + + public File getModelFile() { + return modelFile; + } + + public void setPropertyFile(String property, String prop) { + try { + propertyWriter = new FileWriter(propertyFile); + propertyWriter.write(property); + propertyWriter.flush(); + propertyWriter.close(); + } catch (IOException e) { + LOGGER.log(Level.SEVERE, e.toString(), e); + } + } + + public File getPropertyFile() { + return propertyFile; + } + + public void setResultsFile(String result) { + try { + resultsFile = File.createTempFile(result, null); + } catch (IOException e) { + LOGGER.log(Level.SEVERE, e.toString(), e); + } + } + + public File getResultsFile() { + return resultsFile; + } + + public String getFormula() { + return formula.trim().replaceAll("\\s+", ""); + } + + public void setFormula() { + try { + formula = paramWrapper.invokeModelChecker(modelFile.getAbsolutePath(), propertyFile.getAbsolutePath(), + resultsFile.getAbsolutePath()); + }catch (IOException e){ + LOGGER.log(Level.SEVERE, e.toString(), e); + } + } + + public void setParametricFormula() { + try { + formula = paramWrapper.invokeParametricModelChecker(modelFile.getAbsolutePath(), propertyFile.getAbsolutePath(), + resultsFile.getAbsolutePath()); + }catch (IOException e){ + LOGGER.log(Level.SEVERE, e.toString(), e); + } + } +} diff --git a/src/paramwrapper/ParamWrapper.java b/src/paramwrapper/ParamWrapper.java index 1b34cb8..742e285 100644 --- a/src/paramwrapper/ParamWrapper.java +++ b/src/paramwrapper/ParamWrapper.java @@ -22,14 +22,14 @@ * */ public class ParamWrapper implements ParametricModelChecker { - private static final Logger LOGGER = Logger.getLogger(ParamWrapper.class.getName()); + private static final Logger LOGGER = Logger.getLogger(ParamWrapper.class.getName()); private String paramPath; private final String prismPath = "/opt/prism-4.2.1-src/bin/prism"; private boolean usePrism = false; public ParamWrapper(String paramPath) { - this.paramPath = paramPath; + this.paramPath = paramPath; } public String fdtmcToParam(FDTMC fdtmc) { @@ -46,60 +46,55 @@ public String getReliability(FDTMC fdtmc) { } private String evaluate(String model, String property) { - try { - File modelFile = File.createTempFile("model", "param"); - FileWriter modelWriter = new FileWriter(modelFile); - modelWriter.write(model); - modelWriter.flush(); - modelWriter.close(); - - File propertyFile = File.createTempFile("property", "prop"); - FileWriter propertyWriter = new FileWriter(propertyFile); - propertyWriter.write(property); - propertyWriter.flush(); - propertyWriter.close(); - - File resultsFile = File.createTempFile("result", null); - - String formula; - if (usePrism && !model.contains("param")) { - formula = invokeModelChecker(modelFile.getAbsolutePath(), - propertyFile.getAbsolutePath(), - resultsFile.getAbsolutePath()); - } else { - formula = invokeParametricModelChecker(modelFile.getAbsolutePath(), - propertyFile.getAbsolutePath(), - resultsFile.getAbsolutePath()); - } - return formula.trim().replaceAll("\\s+", ""); - } catch (IOException e) { - LOGGER.log(Level.SEVERE, e.toString(), e); + Formula formula = new Formula(this); + + // File modelFile = File.createTempFile("model", "param"); + // FileWriter modelWriter = new FileWriter(modelFile); + // modelWriter.write(model); + // modelWriter.flush(); + // modelWriter.close(); + formula.setModelFile("model", "param"); + + // File propertyFile = File.createTempFile("property", "prop"); + // FileWriter propertyWriter = new FileWriter(propertyFile); + // propertyWriter.write(property); + // propertyWriter.flush(); + // propertyWriter.close(); + + formula.setPropertyFile("property", "prop"); + + // File resultsFile = File.createTempFile("result", null); + formula.setResultsFile("result"); + + // String formula; + if (usePrism && !model.contains("param")) { + // formula = invokeModelChecker(modelFile.getAbsolutePath(), + // propertyFile.getAbsolutePath(), + // resultsFile.getAbsolutePath()); + formula.setFormula(); + } else { + // formula = invokeParametricModelChecker(modelFile.getAbsolutePath(), + // propertyFile.getAbsolutePath(), + // resultsFile.getAbsolutePath()); + formula.setParametricFormula(); } - return ""; + return formula.getFormula(); + } - private String invokeParametricModelChecker(String modelPath, - String propertyPath, - String resultsPath) throws IOException { - String commandLine = paramPath+" " - +modelPath+" " - +propertyPath+" " - +"--result-file "+resultsPath; - return invokeAndGetResult(commandLine, resultsPath+".out"); + protected String invokeParametricModelChecker(String modelPath, String propertyPath, String resultsPath) + throws IOException { + String commandLine = paramPath + " " + modelPath + " " + propertyPath + " " + "--result-file " + resultsPath; + return invokeAndGetResult(commandLine, resultsPath + ".out"); } - private String invokeModelChecker(String modelPath, - String propertyPath, - String resultsPath) throws IOException { - String commandLine = prismPath+" " - +modelPath+" " - +propertyPath+" " - +"-exportresults "+resultsPath; + String invokeModelChecker(String modelPath, String propertyPath, String resultsPath) throws IOException { + String commandLine = prismPath + " " + modelPath + " " + propertyPath + " " + "-exportresults " + resultsPath; return invokeAndGetResult(commandLine, resultsPath); } private String invokeAndGetResult(String commandLine, String resultsPath) throws IOException { - LOGGER.fine(commandLine); + LOGGER.fine(commandLine); Process program = Runtime.getRuntime().exec(commandLine); int exitCode = 0; try { @@ -110,7 +105,7 @@ private String invokeAndGetResult(String commandLine, String resultsPath) throws } List lines = Files.readAllLines(Paths.get(resultsPath), Charset.forName("UTF-8")); // Formula - return lines.get(lines.size()-1); + return lines.get(lines.size() - 1); } }