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.ts.abstractimpl; 018 019import java.util.Collection; 020import java.util.Collections; 021import java.util.HashMap; 022import java.util.HashSet; 023import java.util.Set; 024 025import net.automatalib.commons.util.mappings.MapMapping; 026import net.automatalib.commons.util.mappings.MutableMapping; 027import net.automatalib.ts.DeterministicTransitionSystem; 028import net.automatalib.ts.TransitionSystem; 029import net.automatalib.ts.powerset.PowersetDTS; 030 031 032public abstract class AbstractTS<S, I, T> implements TransitionSystem<S, I, T> { 033 034 /** 035 * Provides a realization of 036 * {@link TransitionSystem#getSuccessors(Object, Object)} using 037 * {@link TransitionSystem#getTransitions(Object, Object)} and 038 * {@link TransitionSystem#getSuccessor(Object)}. 039 * @see TransitionSystem#getSuccessors(Object, Object) 040 */ 041 public static <S,I,T> Set<S> getSuccessors(TransitionSystem<S, I, T> $this, S state, I input) { 042 Collection<T> transitions = $this.getTransitions(state, input); 043 if(transitions == null) 044 return Collections.emptySet(); 045 Set<S> result = new HashSet<S>(transitions.size()); 046 for(T trans : transitions) 047 result.add($this.getSuccessor(trans)); 048 return result; 049 } 050 051 /** 052 * Provides a realization of 053 * {@link TransitionSystem#getSuccessors(Object, Iterable)} using 054 * {@link TransitionSystem#getSuccessors(Collection, Iterable)}. 055 * @see TransitionSystem#getSuccessors(Object, Iterable) 056 */ 057 public static <S,I,T> Set<S> getSuccessors(TransitionSystem<S, I, T> $this, S state, Iterable<I> input) { 058 return $this.getSuccessors(Collections.singleton(state), input); 059 } 060 061 /** 062 * Provides a realization of 063 * {@link TransitionSystem#getSuccessors(Collection, Iterable)} using 064 * {@link TransitionSystem#getSuccessors(Object, Object)}. 065 * @see TransitionSystem#getSuccessors(Collection, Iterable) 066 */ 067 public static <S,I,T> Set<S> getSuccessors(TransitionSystem<S, I, T> $this, Collection<S> states, Iterable<I> input) { 068 Set<S> current = new HashSet<S>(states); 069 Set<S> succs = new HashSet<S>(); 070 071 for(I sym : input) { 072 for(S state : current) { 073 Set<S> currSuccs = $this.getSuccessors(state, sym); 074 if(currSuccs != null) 075 succs.addAll(currSuccs); 076 } 077 078 Set<S> tmp = current; 079 current = succs; 080 succs = tmp; 081 succs.clear(); 082 } 083 084 return current; 085 } 086 087 /** 088 * Provides a realization of 089 * {@link TransitionSystem#getStates(Iterable)} using 090 * {@link TransitionSystem#getSuccessors(Collection, Iterable)} and 091 * {@link TransitionSystem#getInitialStates()}. 092 * @see TransitionSystem#getStates(Iterable) 093 */ 094 public static <S,I,T> Set<S> getStates(TransitionSystem<S, I, T> $this, Iterable<I> input) { 095 return $this.getSuccessors($this.getInitialStates(), input); 096 } 097 098 /** 099 * Provides a realization of 100 * {@link TransitionSystem#powersetView()}. 101 * @see TransitionSystem#powersetView() 102 * @see PowersetDTS 103 */ 104 public static <S,I,T> PowersetDTS<S,I,T> powersetView(TransitionSystem<S,I,T> $this) { 105 return new PowersetDTS<S,I,T>($this); 106 } 107 108 109 public static <S,I,T,V> MutableMapping<S,V> createStaticStateMapping(TransitionSystem<S,I,T> $this) { 110 return new MapMapping<>(new HashMap<S,V>()); 111 } 112 113 public static <S,I,T,V> MutableMapping<S,V> createDynamicStateMapping(TransitionSystem<S,I,T> $this) { 114 return new MapMapping<>(new HashMap<S,V>()); 115 } 116 117 118 ////////////////////////////////////////////////////////////////////////////////////////////// 119 120 121 /* 122 * (non-Javadoc) 123 * @see net.automatalib.ts.SimpleTS#getSuccessors(java.lang.Object, java.lang.Object) 124 */ 125 @Override 126 public Set<S> getSuccessors(S state, I input) { 127 return getSuccessors(this, state, input); 128 } 129 130 /* 131 * (non-Javadoc) 132 * @see net.automatalib.ts.SimpleTS#getSuccessors(java.lang.Object, java.lang.Iterable) 133 */ 134 @Override 135 public Set<S> getSuccessors(S state, Iterable<I> input) { 136 return getSuccessors(this, state, input); 137 } 138 139 /* 140 * (non-Javadoc) 141 * @see net.automatalib.ts.SimpleTS#getSuccessors(java.util.Collection, java.lang.Iterable) 142 */ 143 @Override 144 public Set<S> getSuccessors(Collection<S> states, Iterable<I> input) { 145 return getSuccessors(this, states, input); 146 } 147 148 /* 149 * (non-Javadoc) 150 * @see net.automatalib.ts.SimpleTS#getStates(java.lang.Iterable) 151 */ 152 @Override 153 public Set<S> getStates(Iterable<I> input) { 154 return getStates(this, input); 155 } 156 157 /* 158 * (non-Javadoc) 159 * @see net.automatalib.ts.TransitionSystem#powersetView() 160 */ 161 @Override 162 public DeterministicTransitionSystem<? extends Set<S>, I, ? extends Collection<T>> powersetView() { 163 return powersetView(this); 164 } 165 166 167 @Override 168 public <V> MutableMapping<S,V> createStaticStateMapping() { 169 return createStaticStateMapping(this); 170 } 171 172 @Override 173 public <V> MutableMapping<S,V> createDynamicStateMapping() { 174 return createDynamicStateMapping(this); 175 } 176 177 178}