/*
* Copyright (C) 2011-2015 Julian Jarecki (jareckij@informatik.uni-freiburg.de)
* Copyright (C) 2011-2015 Matthias Heizmann (heizmann@informatik.uni-freiburg.de)
* Copyright (C) 2009-2015 University of Freiburg
*
* This file is part of the ULTIMATE Automata Library.
*
* The ULTIMATE Automata Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ULTIMATE Automata Library 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the ULTIMATE Automata Library. If not, see .
*
* Additional permission under GNU GPL version 3 section 7:
* If you modify the ULTIMATE Automata Library, or any covered work, by linking
* or combining it with Eclipse RCP (or a modified version of Eclipse RCP),
* containing parts covered by the terms of the Eclipse Public License, the
* licensors of the ULTIMATE Automata Library grant you additional permission
* to convey the resulting work.
*/
package de.uni_freiburg.informatik.ultimate.automata.petrinet.unfolding;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import de.uni_freiburg.informatik.ultimate.automata.petrinet.netdatastructures.ISuccessorTransitionProvider;
import de.uni_freiburg.informatik.ultimate.util.datastructures.relation.HashRelation;
/**
* Represents an incomplete Event. A Candidate consists of
*
* - the transition which belongs to the event
* - a list of {@link Condition}s that represents predecessor places of the transition that have already been
* instantiated by (these) condition
* - a list of {@link Place}s that represent predecessor places of the transition that have not yet been
* instantiated.
*
*
* @author Julian Jarecki (jareckij@informatik.uni-freiburg.de)
* @author Matthias Heizmann (heizmann@informatik.uni-freiburg.de)
**/
public class Candidate {
private final ISuccessorTransitionProvider mSuccTransProvider;
private final LinkedList> mInstantiated;
private final LinkedList mNotInstantiated;
private final Map>> mPossibleInstantiationsMap;
private final LinkedList> mInstantiatedButNotInitially;
public Candidate(final ISuccessorTransitionProvider succTransProvider,
final Collection> conditionsForImmediateInstantiation,
final HashRelation> place2coRelatedConditions) {
mSuccTransProvider = succTransProvider;
mInstantiated = new LinkedList<>();
mNotInstantiated = new LinkedList<>(mSuccTransProvider.getPredecessorPlaces());
mInstantiatedButNotInitially = new LinkedList<>();
// instantiate the places with the given conditions
for (final Condition condition : conditionsForImmediateInstantiation) {
final boolean wasContained = mNotInstantiated.remove(condition.getPlace());
if (wasContained) {
mInstantiated.add(condition);
}
}
mPossibleInstantiationsMap =
mNotInstantiated.stream().collect(Collectors.toMap(x -> x, place2coRelatedConditions::getImage));
}
public Candidate(final ISuccessorTransitionProvider succTransProvider,
final LinkedList notInstantiated, final LinkedList> instantiated,
final Map>> possibleInstantiationsMap) {
mSuccTransProvider = succTransProvider;
mInstantiated = instantiated;
mNotInstantiated = notInstantiated;
mPossibleInstantiationsMap = possibleInstantiationsMap;
mInstantiatedButNotInitially = null;
}
public ISuccessorTransitionProvider getTransition() {
return mSuccTransProvider;
}
public boolean isFullyInstantiated() {
return mNotInstantiated.isEmpty();
}
public PLACE getNextUninstantiatedPlace() {
return mNotInstantiated.getLast();
}
public Map>> getPossibleInstantiationsMap() {
return mPossibleInstantiationsMap;
}
public List getNotInstantiated() {
return mNotInstantiated;
}
public Set> getPossibleInstantiations(final PLACE p) {
assert mPossibleInstantiationsMap.get(p) != null : "p must be contained in the map";
return mPossibleInstantiationsMap.get(p);
}
public void instantiateNext(final Condition condition) {
if (!getNextUninstantiatedPlace().equals(condition.getPlace())) {
throw new IllegalStateException();
}
mInstantiatedButNotInitially.add(condition);
mInstantiated.add(condition);
mNotInstantiated.remove(mNotInstantiated.size() - 1);
}
public void undoOneInstantiation() {
final Condition last = mInstantiated.remove(mInstantiated.size() - 1);
mInstantiatedButNotInitially.remove(mInstantiatedButNotInitially.size() - 1);
mNotInstantiated.add(last.getPlace());
}
public List> getInstantiatedButNotInitially() {
return mInstantiatedButNotInitially;
}
public List> getInstantiated() {
return Collections.unmodifiableList(mInstantiated);
}
@Override
public String toString() {
return mSuccTransProvider.toString();
}
}