
be.unamur.transitionsystem.AbstractTransitionSystem Maven / Gradle / Ivy
package be.unamur.transitionsystem;
/*
* #%L
* vibes-core
* %%
* Copyright (C) 2014 PReCISE, University of Namur
* %%
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* #L%
*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public abstract class AbstractTransitionSystem implements TransitionSystem {
protected TransitionSystemElementFactory factory;
private S initialState;
private Map states = new HashMap();
private Map actions = new HashMap();
protected AbstractTransitionSystem(TransitionSystemElementFactory factory){
assert(factory != null);
this.factory = factory;
}
protected AbstractTransitionSystem(AbstractTransitionSystem original){
this.factory = original.factory;
importActions(original.actions.values().iterator());
importStates(original.states.values().iterator());
importTransitions(original.states.values().iterator());
this.initialState = getState(original.getInitialState().getName());
}
protected void importActions(Iterator actions) {
while(actions.hasNext()){
addAction(actions.next().getName());
}
}
protected void importStates(Iterator states) {
S state;
while(states.hasNext()){
state = states.next();
this.addState(state.getName());
}
}
protected void importTransitions(Iterator states) {
S state;
@SuppressWarnings("rawtypes")
Iterator itTrans;
Transition trans;
while(states.hasNext()){
state = states.next();
itTrans = state.outgoingTransitions();
while(itTrans.hasNext()){
trans = (Transition) itTrans.next();
addTransition(getState(trans.getFrom().getName()), getState(trans.getTo().getName()), getAction(trans.getAction().getName()));
}
}
}
// Accessors
protected void setFactory(TransitionSystemElementFactory factory) {
assert(factory != null);
this.factory = factory;
}
@Override
public S getState(String name){
return this.states.get(name);
}
@Override
public A getAction(String name){
return this.actions.get(name);
}
@SuppressWarnings("unchecked")
@Override
public void setInitialState(State initialState){
assert(initialState != null);
this.initialState = (S) initialState;
}
@Override
public S getInitialState(){
return this.initialState;
}
@Override
public int numberOfStates(){
return this.states.size();
}
@Override
public int numberOfActions(){
return this.actions.size();
}
// Adding elements
@Override
public S addState(String name){
assert(name != null);
S state = this.states.get(name);
if(state == null){
state = this.factory.buildState();
state.setName(name);
this.states.put(name, state);
}
return state;
}
@Override
public T addTransition(State from, State to, Action action){
assert(this.states.containsKey(from.getName()));
assert(this.states.containsKey(to.getName()));
assert(this.actions.containsKey(action.getName()));
T tr = this.factory.buildTransition(getState(from.getName()), getState(to.getName()), getAction(action.getName()));
from.addOutgoing(tr);
to.addIncoming(tr);
return tr;
}
/**
* @param name
* @return
*/
@Override
public A addAction(String name){
assert(name != null);
A act = this.actions.get(name);
if(act == null){
act = this.factory.buildAction();
act.setName(name);
this.actions.put(name, act);
}
return act;
}
// Removing elements
@Override
@SuppressWarnings("unchecked")
public void removeState(State state){
assert(state != null);
if(this.initialState.equals(state)){
this.initialState = null;
}
List lst = new ArrayList();
lst.addAll((List extends T>) state.getIncoming());
lst.addAll((List extends T>) state.getOutgoing());
for(T tr: lst){
removeTransition(tr);
}
this.states.remove(state.getName());
}
@Override
public void removeTransition(Transition tr){
Transition toRemove = this.getState(tr.getFrom().getName()).getOutTransition(tr);
if(toRemove != null){ // If the transition has not been already removed (happens for loop transitions)
toRemove.getFrom().removeOutgoing(tr);
toRemove.getTo().removeIncoming(tr);
}
}
// Iterator over elements
@Override
public Iterator states(){
return this.states.values().iterator();
}
@Override
public Iterator actions(){
return this.actions.values().iterator();
}
@Override
public abstract TransitionSystem copy();
@Override
public String toString() {
return "AbstractTransitionSystem [initialState=" + initialState.getName() + ", states="
+ states + ", actions=" + actions + "]";
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy