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

net.automatalib.util.automata.copy.AbstractLowLevelAutomatonCopier 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) 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.ArrayList;
import java.util.Collection;
import java.util.List;

import net.automatalib.automata.MutableAutomaton;
import net.automatalib.commons.util.mappings.Mapping;
import net.automatalib.commons.util.mappings.MutableMapping;
import net.automatalib.ts.TransitionPredicate;
import net.automatalib.ts.TransitionSystem;

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

public abstract class AbstractLowLevelAutomatonCopier> implements LowLevelAutomatonCopier {

	protected final TS1 in;
	protected final Collection inputs;
	protected final MutableAutomaton out;
	protected final MutableMapping stateMapping;
	protected final Function inputsMapping;
	protected final Function spMapping;
	protected final Function tpMapping;
	protected final Predicate stateFilter;
	protected final TransitionPredicate transFilter;
	
	
	public AbstractLowLevelAutomatonCopier(TS1 in,
			Collection inputs,
			MutableAutomaton out,
			Function inputsMapping,
			Function spMapping,
			Function tpMapping,
			Predicate stateFilter,
			TransitionPredicate transFilter) {
		this.in = in;
		this.inputs = inputs;
		this.out = out;
		this.stateMapping = in.createStaticStateMapping();
		this.inputsMapping = inputsMapping;
		this.spMapping = spMapping;
		this.tpMapping = tpMapping;
		this.stateFilter = stateFilter;
		this.transFilter = transFilter;
	}
	
	@Override
	public Mapping getStateMapping() {
		return stateMapping;
	}

	protected S2 copyState(S1 s1) {
		SP2 prop = spMapping.apply(s1);
		S2 s2 = out.addState(prop);
		stateMapping.put(s1, s2);
		return s2;
	}
	
	protected S2 copyInitialState(S1 s1) {
		SP2 prop = spMapping.apply(s1);
		S2 s2 = out.addInitialState(prop);
		stateMapping.put(s1, s2);
		return s2;
	}
	
	protected T2 copyTransition(S2 src2, I2 input2, T1 trans1, S1 succ1) {
		TP2 prop = tpMapping.apply(trans1);
		
		S2 succ2 = stateMapping.get(succ1);
		
		T2 trans2 = out.createTransition(succ2, prop);
		out.addTransition(src2, input2, trans2);
		return trans2;
	}
	
	protected void copyTransitions(S2 src2, I2 input2, Iterable transitions1) {
		List transitions2 = new ArrayList<>();
		
		for(T1 trans1 : transitions1) {
			S1 succ1 = in.getSuccessor(trans1);
			S2 succ2 = stateMapping.get(succ1);
			TP2 prop = tpMapping.apply(trans1);
			T2 trans2 = out.createTransition(succ2, prop);
			transitions2.add(trans2);
		}
		
		out.addTransitions(src2, input2, transitions2);
	}
	
	protected S2 copyTransitionChecked(S2 src2, I2 input2, T1 trans1, S1 succ1) {
		TP2 prop = tpMapping.apply(trans1);
		
		S2 succ2 = stateMapping.get(succ1);
		S2 freshSucc = null;
		if(succ2 == null) {
			freshSucc = succ2 = copyState(succ1);
		}
		
		T2 trans2 = out.createTransition(succ2, prop);
		out.addTransition(src2, input2, trans2);
		return freshSucc;
	}
	
	/* (non-Javadoc)
	 * @see net.automatalib.util.automata.copy.AutomatonCopier#doCopy()
	 */
	@Override
	public abstract void doCopy();
	
	protected final void updateInitials() {
		for(S1 init1 : in.getInitialStates()) {
			S2 init2 = stateMapping.get(init1);
			if(init2 == null) {
				continue;
			}
			out.setInitial(init2, true);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy