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

net.automatalib.util.automata.copy.TraversalAutomatonCopy 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
/* 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.util.automata.copy;

import java.util.Collection;

import net.automatalib.automata.Automaton;
import net.automatalib.automata.MutableAutomaton;
import net.automatalib.commons.util.Holder;
import net.automatalib.ts.TransitionPredicate;
import net.automatalib.ts.TransitionSystem;
import net.automatalib.util.traversal.TraversalOrder;
import net.automatalib.util.ts.traversal.TSTraversal;
import net.automatalib.util.ts.traversal.TSTraversalAction;
import net.automatalib.util.ts.traversal.TSTraversalVisitor;

import com.google.common.base.Function;
import com.google.common.base.Predicate;


final class TraversalAutomatonCopy extends
		AbstractLowLevelAutomatonCopier> implements TSTraversalVisitor {
	
	final static class CopyMethod implements AutomatonCopyMethod {
		private final TraversalOrder traversalOrder;
		
		public CopyMethod(TraversalOrder traversalOrder) {
			this.traversalOrder = traversalOrder;
		}
		
		@Override
		public 
		LowLevelAutomatonCopier createLowLevelCopier(
				Automaton in,
				Collection inputs,
				MutableAutomaton out,
				Function inputsMapping,
				Function spMapping,
				Function tpMapping,
				Predicate stateFilter,
				TransitionPredicate transitionFilter) {
			return new TraversalAutomatonCopy(traversalOrder, in.size(), in, inputs, out, inputsMapping, spMapping, tpMapping, stateFilter, transitionFilter);
		}
		
	}

	private final TraversalOrder traversalOrder;
	private final int limit;
	
	public TraversalAutomatonCopy(TraversalOrder traversalOrder,
			int limit,
			TransitionSystem in,
			Collection inputs,
			MutableAutomaton out,
			Function inputsMapping,
			Function spMapping,
			Function tpMapping,
			Predicate stateFilter,
			TransitionPredicate transFilter) {
		super(in, inputs, out, inputsMapping, spMapping, tpMapping, stateFilter, transFilter);
		this.traversalOrder = traversalOrder;
		this.limit = limit;
	}

	/*
	 * (non-Javadoc)
	 * @see net.automatalib.util.automata.copy.AbstractAutomatonCopy#doCopy()
	 */
	@Override
	public void doCopy() {
		TSTraversal.traverse(traversalOrder, in, limit, inputs, this);
	}

	/*
	 * (non-Javadoc)
	 * @see net.automatalib.util.ts.traversal.TSTraversalVisitor#processInitial(java.lang.Object, net.automatalib.commons.util.Holder)
	 */
	@Override
	public TSTraversalAction processInitial(S1 state, Holder outData) {
		if(stateFilter.apply(state)) {
			outData.value = copyInitialState(state);
			return TSTraversalAction.EXPLORE;
		}
		return TSTraversalAction.IGNORE;
	}

	/*
	 * (non-Javadoc)
	 * @see net.automatalib.util.ts.traversal.TSTraversalVisitor#startExploration(java.lang.Object, java.lang.Object)
	 */
	@Override
	public boolean startExploration(S1 state, S2 data) {
		return true;
	}

	/*
	 * (non-Javadoc)
	 * @see net.automatalib.util.ts.traversal.TSTraversalVisitor#processTransition(java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object, net.automatalib.commons.util.Holder)
	 */
	@Override
	public TSTraversalAction processTransition(S1 source, S2 srcData, I1 input,
			T1 transition, S1 succ, Holder outData) {
		if(transFilter.apply(source, input, transition) && stateFilter.apply(succ)) {
			S2 succ2 = copyTransitionChecked(srcData, inputsMapping.apply(input), transition, succ);
			if(succ2 == null) {
				return TSTraversalAction.IGNORE;
			}
			outData.value = succ2;
			return TSTraversalAction.EXPLORE;
		}
		return TSTraversalAction.IGNORE;
	}

}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy