Browse Source
Refactor explicit SCC computation, splitting SCCConsumer from SCCComputer.
Refactor explicit SCC computation, splitting SCCConsumer from SCCComputer.
To obtain the previous behaviour, change SCCComputer sccs = SCCComputer.createSCCComputer(parent, model); sccs.computeSCCs(); ... = sccs.getSCCs(); to SCCConsumerStore sccs = new SCCConsumerStore(); SCCComputer sccComp = SCCComputer.createSCCComputer(parent, model, sccs); sccComp.computeSCCs(); ... = sccs.getSCCs(); This additional flexibility in how SCCs can be consumed can be used in the future for example to handle SCCs on-the-fly, without having to store all of them at the same time. git-svn-id: https://www.prismmodelchecker.org/svn/prism/prism/trunk@12110 bbc10eb1-c90d-0410-af57-cb519fbb1720master
13 changed files with 510 additions and 145 deletions
-
10prism/src/automata/DASimplifyAcceptance.java
-
11prism/src/automata/LTL2WDBA.java
-
25prism/src/explicit/DTMCModelChecker.java
-
5prism/src/explicit/ECComputerDefault.java
-
7prism/src/explicit/LTLModelChecker.java
-
5prism/src/explicit/NonProbModelChecker.java
-
58prism/src/explicit/SCCComputer.java
-
105prism/src/explicit/SCCComputerTarjan.java
-
69prism/src/explicit/SCCConsumer.java
-
77prism/src/explicit/SCCConsumerBSCCs.java
-
94prism/src/explicit/SCCConsumerBitSet.java
-
162prism/src/explicit/SCCConsumerStore.java
-
27prism/src/prism/Prism.java
@ -0,0 +1,69 @@ |
|||||
|
//============================================================================== |
||||
|
// |
||||
|
// Copyright (c) 2016- |
||||
|
// Authors: |
||||
|
// * Joachim Klein <klein@tcs.inf.tu-dresden.de> (TU Dresden) |
||||
|
// |
||||
|
//------------------------------------------------------------------------------ |
||||
|
// |
||||
|
// This file is part of PRISM. |
||||
|
// |
||||
|
// PRISM is free software; you can redistribute it and/or modify |
||||
|
// it under the terms of the GNU General Public License as published by |
||||
|
// the Free Software Foundation; either version 2 of the License, or |
||||
|
// (at your option) any later version. |
||||
|
// |
||||
|
// PRISM is distributed in the hope that it will be useful, |
||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
|
// GNU General Public License for more details. |
||||
|
// |
||||
|
// You should have received a copy of the GNU General Public License |
||||
|
// along with PRISM; if not, write to the Free Software Foundation, |
||||
|
// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||||
|
// |
||||
|
//============================================================================== |
||||
|
|
||||
|
package explicit; |
||||
|
|
||||
|
import prism.PrismException; |
||||
|
|
||||
|
/** |
||||
|
* Interface for a consumer of SCC information, for use with an {@code SCCComputer}. |
||||
|
* When a new SCC is discovered, first {@code notifyStartSCC()} will be called. |
||||
|
* Subsequently, for each state of the SCC, {@code notifyStateInSCC()} will be called. |
||||
|
* When all states of the SCC have been notified, {@code notifyEndSCC()} will be called. |
||||
|
* When the whole SCC computation is finished, {@code notifyDone()} will be called once. |
||||
|
*/ |
||||
|
public interface SCCConsumer { |
||||
|
/** |
||||
|
* Call-back function, will be called once at the start. |
||||
|
* Default implementation: Ignore. |
||||
|
*/ |
||||
|
public default void notifyStart(Model model) |
||||
|
{ |
||||
|
// ignore |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Call-back function, will be called when a new SCC is discovered. |
||||
|
**/ |
||||
|
public void notifyStartSCC() throws PrismException; |
||||
|
|
||||
|
/** |
||||
|
* Call-back function, will be called once for each state in the SCC. |
||||
|
*/ |
||||
|
public void notifyStateInSCC(int stateIndex) throws PrismException; |
||||
|
|
||||
|
/** |
||||
|
* Call-back function, will be called when all states of the SCC have been |
||||
|
* discovered. |
||||
|
**/ |
||||
|
public void notifyEndSCC() throws PrismException; |
||||
|
|
||||
|
/** |
||||
|
* Call-back function. Will be called after SCC computation is complete. |
||||
|
*/ |
||||
|
public default void notifyDone() {} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,77 @@ |
|||||
|
//============================================================================== |
||||
|
// |
||||
|
// Copyright (c) 2014- |
||||
|
// Authors: |
||||
|
// * Joachim Klein <klein@tcs.inf.tu-dresden.de> (TU Dresden) |
||||
|
// |
||||
|
//------------------------------------------------------------------------------ |
||||
|
// |
||||
|
// This file is part of PRISM. |
||||
|
// |
||||
|
// PRISM is free software; you can redistribute it and/or modify |
||||
|
// it under the terms of the GNU General Public License as published by |
||||
|
// the Free Software Foundation; either version 2 of the License, or |
||||
|
// (at your option) any later version. |
||||
|
// |
||||
|
// PRISM is distributed in the hope that it will be useful, |
||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
|
// GNU General Public License for more details. |
||||
|
// |
||||
|
// You should have received a copy of the GNU General Public License |
||||
|
// along with PRISM; if not, write to the Free Software Foundation, |
||||
|
// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||||
|
// |
||||
|
//============================================================================== |
||||
|
|
||||
|
package explicit; |
||||
|
|
||||
|
import java.util.BitSet; |
||||
|
|
||||
|
import prism.PrismComponent; |
||||
|
import prism.PrismException; |
||||
|
|
||||
|
/** |
||||
|
* Abstract base class for an SCC consumer that is only interested in |
||||
|
* the bottom strongly-connected components (BSCCs). <br/> |
||||
|
* |
||||
|
* Intercepts the {@code notifyNextSCC()} call, checks whether the SCC |
||||
|
* is a BSCC and calls {@code notifyNextBSCC()} if that is the case. |
||||
|
*/ |
||||
|
public abstract class SCCConsumerBSCCs extends SCCConsumerBitSet { |
||||
|
|
||||
|
protected Model model; |
||||
|
|
||||
|
/** Constructor */ |
||||
|
public SCCConsumerBSCCs() |
||||
|
{ |
||||
|
model = null; // will be set by notifyStart call |
||||
|
} |
||||
|
|
||||
|
public void notifyStart(Model model) |
||||
|
{ |
||||
|
this.model = model; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Call-back function. Called upon discovery of a BSCC. |
||||
|
*/ |
||||
|
public abstract void notifyNextBSCC(BitSet bscc) throws PrismException; |
||||
|
|
||||
|
@Override |
||||
|
public void notifyNextSCC(BitSet scc) throws PrismException |
||||
|
{ |
||||
|
boolean bottom = true; |
||||
|
// BSCC <=> for all states s, all successors are again in the SCC. |
||||
|
for (int s = scc.nextSetBit(0); s >= 0; s = scc.nextSetBit(s + 1)) { |
||||
|
if (!model.allSuccessorsInSet(s, scc)) { |
||||
|
bottom = false; |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
if (bottom) { |
||||
|
notifyNextBSCC(scc); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,94 @@ |
|||||
|
//============================================================================== |
||||
|
// |
||||
|
// Copyright (c) 2016- |
||||
|
// Authors: |
||||
|
// * Joachim Klein <klein@tcs.inf.tu-dresden.de> (TU Dresden) |
||||
|
// |
||||
|
//------------------------------------------------------------------------------ |
||||
|
// |
||||
|
// This file is part of PRISM. |
||||
|
// |
||||
|
// PRISM is free software; you can redistribute it and/or modify |
||||
|
// it under the terms of the GNU General Public License as published by |
||||
|
// the Free Software Foundation; either version 2 of the License, or |
||||
|
// (at your option) any later version. |
||||
|
// |
||||
|
// PRISM is distributed in the hope that it will be useful, |
||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
|
// GNU General Public License for more details. |
||||
|
// |
||||
|
// You should have received a copy of the GNU General Public License |
||||
|
// along with PRISM; if not, write to the Free Software Foundation, |
||||
|
// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||||
|
// |
||||
|
//============================================================================== |
||||
|
|
||||
|
package explicit; |
||||
|
|
||||
|
import java.util.BitSet; |
||||
|
|
||||
|
import prism.PrismException; |
||||
|
|
||||
|
/** |
||||
|
* Abstract base class for a consumer of SCC information, for use with an {@code SCCComputer}, |
||||
|
* where each SCC is stored as a BitSet. |
||||
|
* <br> |
||||
|
* When a new SCC is discovered, {@code notifyNextSCC()} will be called with a {@code BitSet} of the |
||||
|
* states in the SCC. When the SCC computation is finished, {@code notifyDone()} will be |
||||
|
* called once. |
||||
|
* <br> |
||||
|
* By default, for each SCC a fresh BitSet is created. If the BitSets that are |
||||
|
* passed to {@code notifyNextSCC} can be reused for the next call, the {@code reuseBitSet} |
||||
|
* flag can be set in the constructor. |
||||
|
*/ |
||||
|
public abstract class SCCConsumerBitSet implements SCCConsumer |
||||
|
{ |
||||
|
private BitSet curSCC = null; |
||||
|
private boolean reuseBitSet = false; |
||||
|
|
||||
|
/** Default constructor. Don't reuse the BitSets */ |
||||
|
public SCCConsumerBitSet() |
||||
|
{ |
||||
|
this(false); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Constructor. If {@code reuseBitSet} is set, reuse the same BitSet for all |
||||
|
* calls to {@code notifyNextSCC}. |
||||
|
* @param reuseBitSet allow reuse of BitSets |
||||
|
*/ |
||||
|
public SCCConsumerBitSet(boolean reuseBitSet) |
||||
|
{ |
||||
|
this.reuseBitSet = reuseBitSet; |
||||
|
if (reuseBitSet) |
||||
|
curSCC = new BitSet(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void notifyStartSCC() throws PrismException |
||||
|
{ |
||||
|
if (reuseBitSet) { |
||||
|
curSCC.clear(); |
||||
|
} else { |
||||
|
curSCC = new BitSet(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void notifyStateInSCC(int stateIndex) throws PrismException |
||||
|
{ |
||||
|
curSCC.set(stateIndex); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void notifyEndSCC() throws PrismException |
||||
|
{ |
||||
|
notifyNextSCC(curSCC); |
||||
|
if (!reuseBitSet) { |
||||
|
curSCC = null; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public abstract void notifyNextSCC(BitSet scc) throws PrismException; |
||||
|
} |
||||
@ -0,0 +1,162 @@ |
|||||
|
//============================================================================== |
||||
|
// |
||||
|
// Copyright (c) 2014- |
||||
|
// Authors: |
||||
|
// * Joachim Klein <klein@tcs.inf.tu-dresden.de> (TU Dresden) |
||||
|
// |
||||
|
//------------------------------------------------------------------------------ |
||||
|
// |
||||
|
// This file is part of PRISM. |
||||
|
// |
||||
|
// PRISM is free software; you can redistribute it and/or modify |
||||
|
// it under the terms of the GNU General Public License as published by |
||||
|
// the Free Software Foundation; either version 2 of the License, or |
||||
|
// (at your option) any later version. |
||||
|
// |
||||
|
// PRISM is distributed in the hope that it will be useful, |
||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
|
// GNU General Public License for more details. |
||||
|
// |
||||
|
// You should have received a copy of the GNU General Public License |
||||
|
// along with PRISM; if not, write to the Free Software Foundation, |
||||
|
// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||||
|
// |
||||
|
//============================================================================== |
||||
|
|
||||
|
package explicit; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
import java.util.BitSet; |
||||
|
import java.util.List; |
||||
|
|
||||
|
/** |
||||
|
* SCC consumer that stores lists of the discovered SCCs and BSCCs. |
||||
|
* The list of BSCCs is computed on demand from the list of SCCs. |
||||
|
*/ |
||||
|
public class SCCConsumerStore extends SCCConsumerBitSet { |
||||
|
/* Computed list of SCCs */ |
||||
|
private List<BitSet> sccs = new ArrayList<BitSet>(); |
||||
|
/* Computed list of BSCCs */ |
||||
|
private List<BitSet> bsccs; |
||||
|
/* States not in any BSCC */ |
||||
|
private BitSet notInBSCCs; |
||||
|
/* States not in any SCC */ |
||||
|
private BitSet notInSCCs; |
||||
|
|
||||
|
/** Is the SCC computation finished? */ |
||||
|
private boolean finished = false; |
||||
|
|
||||
|
private Model model; |
||||
|
|
||||
|
/** Constructor */ |
||||
|
public SCCConsumerStore() |
||||
|
{ |
||||
|
model = null; // will be set by notifyStart call |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void notifyStart(Model model) |
||||
|
{ |
||||
|
this.model = model; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void notifyNextSCC(BitSet scc) |
||||
|
{ |
||||
|
sccs.add(scc); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void notifyDone() |
||||
|
{ |
||||
|
finished = true; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Get a list of the SCCs. Can only be called once the SCC computation is finished. |
||||
|
*/ |
||||
|
public List<BitSet> getSCCs() |
||||
|
{ |
||||
|
if (!finished) |
||||
|
throw new UnsupportedOperationException("SCC computation is not yet finished."); |
||||
|
|
||||
|
return sccs; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Get a list of the BSCCs. Can only be called once the SCC computation is finished. |
||||
|
*/ |
||||
|
public List<BitSet> getBSCCs() |
||||
|
{ |
||||
|
if (!finished) |
||||
|
throw new UnsupportedOperationException("SCC computation is not yet finished."); |
||||
|
|
||||
|
// If we don't have the list of BSCCs already, compute it. |
||||
|
if (bsccs == null) { |
||||
|
computeBSCCs(); |
||||
|
} |
||||
|
return bsccs; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Get the set of states not in any BSCCs. Can only be called once the SCC computation is finished. |
||||
|
*/ |
||||
|
public BitSet getNotInBSCCs() |
||||
|
{ |
||||
|
if (!finished) |
||||
|
throw new UnsupportedOperationException("SCC computation is not yet finished."); |
||||
|
|
||||
|
// If we don't have the set already, compute it. |
||||
|
if (notInBSCCs == null) { |
||||
|
computeBSCCs(); |
||||
|
} |
||||
|
return notInBSCCs; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Compute the list of BSCCs from the list of SCCs. |
||||
|
*/ |
||||
|
private void computeBSCCs() |
||||
|
{ |
||||
|
if (!finished) |
||||
|
throw new UnsupportedOperationException("SCC computation is not yet finished."); |
||||
|
|
||||
|
bsccs = new ArrayList<BitSet>(); |
||||
|
notInBSCCs = (BitSet) getNotInSCCs().clone(); |
||||
|
for (BitSet scc : sccs) { |
||||
|
boolean bottom = true; |
||||
|
// BSCC <=> for all states s, all successors are again in SCC |
||||
|
for (int s = scc.nextSetBit(0); s >= 0; s = scc.nextSetBit(s + 1)) { |
||||
|
if (!model.allSuccessorsInSet(s, scc)) { |
||||
|
bottom = false; |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
if (bottom) |
||||
|
// store SCC as a BSCC |
||||
|
bsccs.add(scc); |
||||
|
else |
||||
|
// add states in scc to notInBSCCs |
||||
|
notInBSCCs.or(scc); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public BitSet getNotInSCCs() |
||||
|
{ |
||||
|
if (!finished) |
||||
|
throw new UnsupportedOperationException("SCC computation is not yet finished."); |
||||
|
|
||||
|
if (notInSCCs != null) { |
||||
|
return notInSCCs; |
||||
|
} |
||||
|
BitSet result = new BitSet(); |
||||
|
for (BitSet scc : getSCCs()) { |
||||
|
result.or(scc); |
||||
|
} |
||||
|
result.flip(0, model.getNumStates()); |
||||
|
notInSCCs = result; |
||||
|
return notInSCCs; |
||||
|
} |
||||
|
|
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue