package de.uni_freiburg.informatik.ultimate.lib.pea; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; import java.util.List; import java.util.Vector; import de.uni_freiburg.informatik.ultimate.lib.pea.modelchecking.TCSWriter; public class VacuousAssignment extends TCSWriter { /** * FileWriter to output file. */ protected BufferedWriter writer; protected boolean rename; protected PhaseEventAutomata pea2write; private int i = 0; private final Vector possiblyVacuous; // list of formulas such that the pea this formula was generated from, // gets vacuously satisfied if this formula holds in every state of the parallel product private Vector possibleVacuousMaker; // list of formulas, such that the pea this formulas was generated from, // cannot be vacuously satisfied private Vector vacuousReqNames; // however it might lead to a vacuous satisfaction for other peas private Vector vacuous; // list of the formulae that are really vacuously satisfied public VacuousAssignment(final String fileName, final PhaseEventAutomata pea) { super(fileName); pea2write = pea; possiblyVacuous = new Vector<>(); setVacuous(new Vector()); setPossibleVacuousMaker(new Vector()); setVacuousReqNames(new Vector()); } /** * Constructor setting output file name and rename flag that indicates whether original location names have to be * used or whether the locations are renamed into default names. * * @param file * @param rename */ public VacuousAssignment(final String destfile, final boolean rename, final PhaseEventAutomata pea) { this(destfile, pea); this.rename = rename; setVacuous(new Vector()); setPossibleVacuousMaker(new Vector()); setVacuousReqNames(new Vector()); } public void getVacuousAssignments(final PhaseEventAutomata pea) throws IOException { System.out.println("*********************************************************"); System.out.println("Queries to check for a vacuous satisfaction for PEA " + pea.getName()); final List init = pea.getInit(); final int numberOfLocations = pea.getPhases().size(); if (numberOfLocations < 0) { System.out.println("ERROR: The pea is empty"); } if (numberOfLocations == 1) { final CDD cdd = pea.getPhases().get(0).getStateInvariant(); final int length = getPossibleVacuousMakerLength(); for (int i = 0; i < length; i++) { final CDD poss = getPossibleVacuousMaker().elementAt(i); if (cdd.implies(poss)) { addToVacuous(cdd); } else if (poss.implies(cdd)) { addToVacuous(poss); } } addToPossVacuousMaker(cdd); final String vacString = cdd.toString("uppaal", true); System.out.println("A[](" + vacString + ");"); writer.write("A[](" + vacString + ");"); writer.newLine(); writer.flush(); } else { final int numberOfInitStates = init.size(); for (int i = 0; i < numberOfInitStates; i++) { final Phase initState = init.get(0); // States that have a clockinvariant !=0 have to be left when the clock is expired // Thus such states cannot lead to a vacuous satisfaction if (initState.getClockInvariant() == CDD.TRUE) { final CDD vac = initState.getStateInvariant(); addToPossiblyVacuous(vac); final String vacString = vac.toString("uppaal", true); System.out.println("A[](" + vacString + ");"); writer.write("A[](" + vacString + ");"); writer.newLine(); writer.flush(); } } System.out.println("*********************************************************"); } } public void getVacuousAssignments(final PhaseEventAutomata pea, final String reqName) throws IOException { this.getVacuousAssignments(pea); // System.out.println("*********************************************************"); // System.out.println("Queries to check for a vacuous satisfaction for PEA "+pea.getName()); // Phase[] init = pea.getInit(); // int numberOfLocations = pea.getPhases().length; // if (numberOfLocations <0) { // System.out.println("ERROR: The pea is empty"); // } // if (numberOfLocations ==1){ // CDD cdd = pea.getPhases()[0].getStateInvariant(); // // if (this.getPossibleVacuousMaker().isEmpty()){ // this.addToPossVacuousMaker(cdd); // } // else { // int length = this.getPossibleVacuousMakerLength(); // for (int i=0; i vac = getPossiblyVacuous(); if (vac.isEmpty()) { System.out.println("Before checking for vacuous satisfaction, you need to built-up the vacuous-vector"); } else { checkPossiblyVacuous(pea); checkPossibleVacuousMaker(pea); } } private void checkPossiblyVacuous(final PhaseEventAutomata pea) { final List phases = pea.getPhases(); final Vector vac = getPossiblyVacuous(); if (phases.size() > 1) { for (int j = 0; j < vac.size(); j++) { boolean testVacuous = true; final CDD formula = vac.elementAt(j); for (final Phase a : phases) { final CDD state = a.getStateInvariant(); if (state.and(formula) == CDD.FALSE) { testVacuous = false; } } if (testVacuous) { addToVacuous(formula); } } } } private void checkPossibleVacuousMaker(final PhaseEventAutomata pea) { final Vector possMaker = getPossibleVacuousMaker(); final Vector vacCandidate = getPossiblyVacuous(); if (possMaker.isEmpty() || vacCandidate.isEmpty()) { System.out.println("no possible vacuous formulas known OR no possible vacuous maker known"); } else { boolean testVacuous = false; for (int j = 0; j < possMaker.size() && !testVacuous; j++) { final CDD formula = possMaker.elementAt(j); for (int q = 0; q < vacCandidate.size(); q++) { final CDD state = vacCandidate.elementAt(q); if (formula.implies(state)) { testVacuous = true; addToVacuous(state); addToVacuous(formula); } } } } } public void startWriting() { try { // this.writer = new FileWriter(fileName); writer = new BufferedWriter(new FileWriter(mFileName)); writer.write("Preamble:\n Queries to check for a vacuous satisfaction for PEA"); writer.newLine(); writer.flush(); } catch (final IOException e) { throw new RuntimeException(e); } } public void stopWriting() throws IOException { writer.close(); System.out.println("Successfully written to " + mFileName); } @Override public void write() { try { writer = new BufferedWriter(new FileWriter("fileName")); // init(); this.getVacuousAssignments(pea2write); writer.flush(); writer.close(); System.out.println("Successfully written to " + mFileName); } catch (final IOException e) { throw new RuntimeException(e); } } public void writePEA(final PhaseEventAutomata pea) { try { writer.write("Queries to check for requirement " + i); writer.newLine(); i++; this.getVacuousAssignments(pea); writer.newLine(); writer.flush(); } catch (final IOException e) { throw new RuntimeException(e); } } @Override protected void writeAndDelimiter(final Writer writer) throws IOException { // TODO Auto-generated method stub } @Override protected void writeDecision(final Decision decision, final int child, final Writer writer) throws IOException { // TODO Auto-generated method stub } public Vector getPossiblyVacuous() { return possiblyVacuous; } public void setVacuous(final Vector vacuous) { this.vacuous = vacuous; } public Vector getVacuous() { return vacuous; } public void addToVacuous(final CDD cdd) { if (vacuous.contains(cdd)) { return; } vacuous.add(cdd); } public void addToPossiblyVacuous(final CDD cdd) { if (possiblyVacuous.contains(cdd)) { return; } possiblyVacuous.add(cdd); } public void addToPossVacuousMaker(final CDD cdd) { if (possibleVacuousMaker.contains(cdd)) { return; } possibleVacuousMaker.add(cdd); } public void addToVacuousReqNames(final String name) { if (vacuousReqNames.contains(name)) { return; } vacuousReqNames.add(name); } public Vector getPossibleVacuousMaker() { return possibleVacuousMaker; } private int getPossibleVacuousMakerLength() { return possibleVacuousMaker.size(); } private void setPossibleVacuousMaker(final Vector possibleVacuousMaker) { this.possibleVacuousMaker = possibleVacuousMaker; } public Vector getVacuousReqNames() { return vacuousReqNames; } private void setVacuousReqNames(final Vector vacuousReqNames) { this.vacuousReqNames = vacuousReqNames; } }