All Downloads are FREE. Search and download functionalities are using the official Maven repository.

be.unamur.transitionsystem.TransitionSystemCleaner Maven / Gradle / Ivy

There is a newer version: 2.0.5
Show newest version
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.Iterator;
import java.util.Queue;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Queues;
import com.google.common.collect.Sets;

public class TransitionSystemCleaner {

	private static final Logger logger = LoggerFactory
			.getLogger(TransitionSystemCleaner.class);

	/**
	 * Remove inaccessible states from the {@link TransitionSystem}.
	 * 
	 * @param ts The transition system to clean.
	 */
	public static void removeIsolatedStates(TransitionSystem ts) {
		Queue toCheck = Queues.newArrayDeque();
		@SuppressWarnings("rawtypes")
		Iterator itStates = ts.states();
		while(itStates.hasNext()){
			toCheck.add((State) itStates.next());
		}
		Set toRemove = Sets.newHashSet();
		while (!toCheck.isEmpty()) {
			State s = toCheck.poll();
			if (s.incomingSize() == 0 && s != ts.getInitialState()) {
				toRemove.add(s);
				// Check that other states have not to be removed
				for (Iterator it = s.outgoingTransitions(); it.hasNext();) {
					Transition tr = (Transition) it.next();
					toCheck.add(tr.getTo());
				}
			}
		}
		for (State s : toRemove) {
			if (!(s == ts.getInitialState())) {
				ts.removeState(s);
			}
		}
	}

	/**
	 * Try to remove unused actions from the transition system. If the remove
	 * method is not implemented in the {@link TransitionSystem}.actions()
	 * iterator, this method has no effect.
	 * 
	 * @param ts The transition system to clean.
	 */
	@SuppressWarnings("unchecked")
	public static void removeUnusedActions(TransitionSystem ts) {
		Set toKeep = Sets.newHashSet();
		for (Iterator itState = ts.states(); itState.hasNext();) {
			State s = (State) itState.next();
			for (Iterator it = s.outgoingTransitions(); it.hasNext();) {
				Transition tr = (Transition) it.next();
				toKeep.add(tr.getAction());
			}
		}
		for (Iterator itAct = ts.actions(); itAct.hasNext();) {
			Action act = itAct.next();
			if (!toKeep.contains(act)) {
				try {
					itAct.remove();
				} catch (UnsupportedOperationException ex) {
					logger.debug("Impossible to remove actions from system", ex);
					break;
				}
			}
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy