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

net.automatalib.ts.abstractimpl.AbstractTS Maven / Gradle / Ivy

/* Copyright (C) 2013-2014 TU Dortmund
 * This file is part of AutomataLib, http://www.automatalib.net/.
 * 
 * AutomataLib is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 3.0 as published by the Free Software Foundation.
 * 
 * AutomataLib 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 AutomataLib; if not, see
 * http://www.gnu.de/documents/lgpl.en.html.
 */
package net.automatalib.ts.abstractimpl;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import net.automatalib.commons.util.mappings.MapMapping;
import net.automatalib.commons.util.mappings.MutableMapping;
import net.automatalib.ts.PowersetViewTS;
import net.automatalib.ts.TransitionSystem;
import net.automatalib.ts.powerset.DirectPowersetDTS;


public abstract class AbstractTS implements TransitionSystem {
	
	/**
	 * Provides a realization of
	 * {@link TransitionSystem#getSuccessors(Object, Object)} using
	 * {@link TransitionSystem#getTransitions(Object, Object)} and
	 * {@link TransitionSystem#getSuccessor(Object)}.
	 * @see TransitionSystem#getSuccessors(Object, Object)
	 */
	public static  Set getSuccessors(TransitionSystem $this, S state, I input) {
		Collection transitions = $this.getTransitions(state, input);
		if(transitions.isEmpty()) {
			return Collections.emptySet();
		}
		Set result = new HashSet(transitions.size());
		for(T trans : transitions)
			result.add($this.getSuccessor(trans));
		return result;
	}
	
	/**
	 * Provides a realization of
	 * {@link TransitionSystem#getSuccessors(Object, Iterable)} using
	 * {@link TransitionSystem#getSuccessors(Collection, Iterable)}.
	 * @see TransitionSystem#getSuccessors(Object, Iterable)
	 */
	public static  Set getSuccessors(TransitionSystem $this, S state, Iterable input) {
		return $this.getSuccessors(Collections.singleton(state), input);
	}
	
	/**
	 * Provides a realization of
	 * {@link TransitionSystem#getSuccessors(Collection, Iterable)} using
	 * {@link TransitionSystem#getSuccessors(Object, Object)}.
	 * @see TransitionSystem#getSuccessors(Collection, Iterable)
	 */
	public static  Set getSuccessors(TransitionSystem $this, Collection states, Iterable input) {
		Set current = new HashSet(states);
		Set succs = new HashSet();
		
		for(I sym : input) {
			for(S state : current) {
				Set currSuccs = $this.getSuccessors(state, sym);
				succs.addAll(currSuccs);
			}
					
			Set tmp = current;
			current = succs;
			succs = tmp;
			succs.clear();
		}
		
		return current;
	}
	
	/**
	 * Provides a realization of
	 * {@link TransitionSystem#getStates(Iterable)} using
	 * {@link TransitionSystem#getSuccessors(Collection, Iterable)} and
	 * {@link TransitionSystem#getInitialStates()}.
	 * @see TransitionSystem#getStates(Iterable)
	 */
	public static  Set getStates(TransitionSystem $this, Iterable input) {
		return $this.getSuccessors($this.getInitialStates(), input);
	}
	
	/**
	 * Provides a realization of
	 * {@link TransitionSystem#powersetView()}.
	 * @see TransitionSystem#powersetView()
	 * @see DirectPowersetDTS
	 */
	public static  DirectPowersetDTS powersetView(TransitionSystem $this) {
		return new DirectPowersetDTS($this);
	}
	
	
	public static  MutableMapping createStaticStateMapping(TransitionSystem $this) {
		return new MapMapping<>(new HashMap());
	}
	
	public static  MutableMapping createDynamicStateMapping(TransitionSystem $this) {
		return new MapMapping<>(new HashMap());
	}
	
	
	//////////////////////////////////////////////////////////////////////////////////////////////


	/*
	 * (non-Javadoc)
	 * @see net.automatalib.ts.SimpleTS#getSuccessors(java.lang.Object, java.lang.Object)
	 */
	@Override
	public Set getSuccessors(S state, I input) {
		return getSuccessors(this, state, input);
	}

	/*
	 * (non-Javadoc)
	 * @see net.automatalib.ts.SimpleTS#getSuccessors(java.lang.Object, java.lang.Iterable)
	 */
	@Override
	public Set getSuccessors(S state, Iterable input) {
		return getSuccessors(this, state, input);
	}

	/*
	 * (non-Javadoc)
	 * @see net.automatalib.ts.SimpleTS#getSuccessors(java.util.Collection, java.lang.Iterable)
	 */
	@Override
	public Set getSuccessors(Collection states, Iterable input) {
		return getSuccessors(this, states, input);
	}

	/*
	 * (non-Javadoc)
	 * @see net.automatalib.ts.SimpleTS#getStates(java.lang.Iterable)
	 */
	@Override
	public Set getStates(Iterable input) {
		return getStates(this, input);
	}

	/*
	 * (non-Javadoc)
	 * @see net.automatalib.ts.TransitionSystem#powersetView()
	 */
	@Override
	public PowersetViewTS powersetView() {
		return powersetView(this);
	}
	
	
	@Override
	public  MutableMapping createStaticStateMapping() {
		return createStaticStateMapping(this);
	}
	
	@Override
	public  MutableMapping createDynamicStateMapping() {
		return createDynamicStateMapping(this);
	}
	

}