001/* Copyright (C) 2013 TU Dortmund 002 * This file is part of AutomataLib, http://www.automatalib.net/. 003 * 004 * AutomataLib is free software; you can redistribute it and/or 005 * modify it under the terms of the GNU Lesser General Public 006 * License version 3.0 as published by the Free Software Foundation. 007 * 008 * AutomataLib is distributed in the hope that it will be useful, 009 * but WITHOUT ANY WARRANTY; without even the implied warranty of 010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 011 * Lesser General Public License for more details. 012 * 013 * You should have received a copy of the GNU Lesser General Public 014 * License along with AutomataLib; if not, see 015 * http://www.gnu.de/documents/lgpl.en.html. 016 */ 017package net.automatalib.automata.graphs; 018 019import java.util.ArrayList; 020import java.util.Collection; 021import java.util.List; 022 023import net.automatalib.automata.Automaton; 024import net.automatalib.automata.FiniteAlphabetAutomaton; 025import net.automatalib.automata.UniversalAutomaton; 026import net.automatalib.automata.abstractimpl.AbstractAutomaton; 027import net.automatalib.commons.util.Pair; 028import net.automatalib.commons.util.mappings.MutableMapping; 029import net.automatalib.graphs.UniversalGraph; 030import net.automatalib.graphs.concepts.NodeIDs; 031 032public abstract class AbstractAutomatonGraph<S,I,T,SP,TP> 033 extends AbstractAutomaton<S, I, T> implements FiniteAlphabetAutomaton<S,I,T>, UniversalAutomaton<S,I,T,SP,TP>, UniversalGraph<S,Pair<I,T>,SP,Pair<I,TP>> { 034 035 036 public static <S,I,T> Collection<Pair<I,T>> createOutgoingEdges(Automaton<S,I,T> automaton, Collection<? extends I> inputs, S state) { 037 List<Pair<I,T>> result 038 = new ArrayList<Pair<I,T>>(); 039 040 041 for(I input : inputs) { 042 Collection<T> transitions = automaton.getTransitions(state, input); 043 if(transitions == null) 044 continue; 045 for(T t : transitions) 046 result.add(Pair.make(input, t)); 047 } 048 049 return result; 050 } 051 052 053 public static <S,I,T> Collection<S> getNodes(Automaton<S,I,T> $this) { 054 return $this.getStates(); 055 } 056 057 public static <S,I,T> NodeIDs<S> nodeIDs(Automaton<S,I,T> $this) { 058 return new StateAsNodeIDs<>($this.stateIDs()); 059 } 060 061 public static <S,I,T> Collection<Pair<I,T>> getOutgoingEdges(FiniteAlphabetAutomaton<S,I,T> $this, S node) { 062 return createOutgoingEdges($this, $this.getInputAlphabet(), node); 063 } 064 065 public static <S,I,T> S getTarget(Automaton<S,I,T> $this, Pair<I,T> edge) { 066 return $this.getSuccessor(edge.getSecond()); 067 } 068 069 public static <S,I,T,V> MutableMapping<S, V> createStaticNodeMapping(Automaton<S,I,T> $this) { 070 return $this.createStaticStateMapping(); 071 } 072 073 public static <S,I,T,V> MutableMapping<S, V> createDynamicNodeMapping(Automaton<S,I,T> $this) { 074 return $this.createDynamicStateMapping(); 075 } 076 077 public static <S,I,T,SP,TP> SP getNodeProperties(UniversalAutomaton<S, I, T, SP, TP> $this, S node) { 078 return $this.getStateProperty(node); 079 } 080 081 082 public static <S,I,T,SP,TP> Pair<I, TP> getEdgeProperties(UniversalAutomaton<S,I,T,SP,TP> $this, Pair<I, T> edge) { 083 TP transProp = $this.getTransitionProperty(edge.getSecond()); 084 return Pair.make(edge.getFirst(), transProp); 085 } 086 087 @Override 088 public Collection<S> getNodes() { 089 return getNodes(this); 090 } 091 092 @Override 093 public NodeIDs<S> nodeIDs() { 094 return nodeIDs(this); 095 } 096 097 @Override 098 public Collection<Pair<I, T>> getOutgoingEdges(S node) { 099 return getOutgoingEdges(this, node); 100 } 101 102 @Override 103 public S getTarget(Pair<I, T> edge) { 104 return getTarget(this, edge); 105 } 106 107 @Override 108 public <V> MutableMapping<S, V> createStaticNodeMapping() { 109 return createStaticNodeMapping(this); 110 } 111 112 @Override 113 public <V> MutableMapping<S, V> createDynamicNodeMapping() { 114 return createDynamicNodeMapping(this); 115 } 116 117 118 @Override 119 public SP getNodeProperty(S node) { 120 return getNodeProperties(this, node); 121 } 122 123 124 @Override 125 public Pair<I, TP> getEdgeProperty(Pair<I, T> edge) { 126 return getEdgeProperties(this, edge); 127 } 128 129 130}