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

net.automatalib.util.ts.TSIterators Maven / Gradle / Ivy

Go to download

This artifact provides various common utility operations for analyzing and manipulating automata and graphs, such as traversal, minimization and copying.

There is a newer version: 0.11.0
Show newest version
package net.automatalib.util.ts;

import java.util.Collection;
import java.util.Iterator;

import net.automatalib.commons.util.collections.SimplifiedIterator;
import net.automatalib.commons.util.collections.TwoLevelIterator;
import net.automatalib.ts.DeterministicTransitionSystem;
import net.automatalib.ts.TransitionSystem;
import net.automatalib.util.ts.TS.TransRef;

abstract class TSIterators {
	static final class AllTransitionsIterator implements Iterator {
		private final TransitionSystem ts;
		private final S state;
		private final Iterator inputIt;
		private Iterator transIt;
		
		public AllTransitionsIterator(TransitionSystem ts, S state, Collection inputs) {
			this.ts = ts;
			this.state = state;
			this.inputIt = inputs.iterator();
			findNext();
		}

		@Override
		public boolean hasNext() {
			return transIt.hasNext();
		}

		@Override
		public T next() {
			T t = transIt.next();
			if(!transIt.hasNext())
				findNext();
			return t;
		}

		@Override
		public void remove() {
			transIt.remove();
		}
		
		private void findNext() {
			while(inputIt.hasNext()) {
				I input = inputIt.next();
				Collection trans = ts.getTransitions(state, input);
				if(trans == null || trans.isEmpty())
					continue;
				transIt = trans.iterator();
				break;
			}
		}
		
	}
	
	public static final class DefinedTransitionsIterator extends SimplifiedIterator {
		private final DeterministicTransitionSystem dts;
		private final Iterator inputsIt;
		private final S state;
		public DefinedTransitionsIterator(
				DeterministicTransitionSystem dts,
				S state,
				Iterator inputsIt) {
			this.dts = dts;
			this.inputsIt = inputsIt;
			this.state = state;
		}
		@Override
		public void remove() {
			throw new UnsupportedOperationException();
		}
		@Override
		protected boolean calculateNext() {
			while(inputsIt.hasNext()) {
				I input = inputsIt.next();
				if(dts.getTransition(state, input) != null) {
					this.nextValue = input;
					return true;
				}
			}
			return false;
		}	
	}
	
	public static final class AllDefinedTransitionsIterator
			extends TwoLevelIterator> {
		private final DeterministicTransitionSystem dts;
		private final Iterable inputs;
		public AllDefinedTransitionsIterator(
				Iterator stateIt,
				DeterministicTransitionSystem dts,
				Iterable inputs) {
			super(stateIt);
			this.dts = dts;
			this.inputs = inputs;
		}
		@Override
		protected Iterator l2Iterator(S state) {
			return TS.definedTransitionsIterator(dts, state, inputs.iterator());
		}
		@Override
		protected TransRef combine(S state, I input) {
			return new TransRef<>(state, input);
		}
	}

	public static final class UndefinedTransitionsIterator extends SimplifiedIterator {
		private final DeterministicTransitionSystem dts;
		private final Iterator inputsIt;
		private final S state;
		public UndefinedTransitionsIterator(DeterministicTransitionSystem dts, S state, Iterator inputsIt) {
			this.dts = dts;
			this.inputsIt = inputsIt;
			this.state = state;
		}
		@Override
		public void remove() {
			throw new UnsupportedOperationException();
		}
		@Override
		protected boolean calculateNext() {
			while(inputsIt.hasNext()) {
				I input = inputsIt.next();
				if(dts.getTransition(state, input) == null) {
					this.nextValue = input;
					return true;
				}
			}
			return false;
		}
	}
	
	
	public static final class AllUndefinedTransitionsIterator
			extends TwoLevelIterator> {
		private final DeterministicTransitionSystem dts;
		private final Iterable inputs;
		public AllUndefinedTransitionsIterator(
				Iterator stateIt,
				DeterministicTransitionSystem dts,
				Iterable inputs) {
			super(stateIt);
			this.dts = dts;
			this.inputs = inputs;
		}
		@Override
		protected Iterator l2Iterator(S state) {
			return TS.undefinedTransitionsIterator(dts, state, inputs.iterator());
		}
		@Override
		protected TransRef combine(S l1Object, I l2Object) {
			return new TransRef<>(l1Object, l2Object);
		}
	}


}