00001 #ifndef _AUTOMATACREATOR_H_32627795824_
00002 #define _AUTOMATACREATOR_H_32627795824_
00003
00004 #include "iautomaton.h"
00005
00006 class State;
00007 class Transition;
00008
00009 #include <QStringList>
00010 #include <QMap>
00011 #include <QList>
00012 class QPainterPath;
00013
00014 class TransitionImpl : public ITransition
00015 {
00016 private:
00017 Q_DISABLE_COPY(TransitionImpl)
00018
00019 public:
00020 typedef QList<QSharedPointer<TransitionImpl> > TTransitionList;
00021
00022 TransitionImpl(const QString &source, const QString &destination,
00023 const ITransition::TCharSet characters,
00024 Transition *graphicsTransition = NULL);
00025
00026 ~TransitionImpl();
00027
00028 void setSourceState(const QString &stateName);
00029 void setDestinationState(const QString &stateName);
00030
00031 QString getSourceState() const;
00032 QString getDestinationState() const;
00033
00034 bool passOn(const QString &character) const;
00035
00036 ITransition::TCharSet getCharacters() const;
00037 void setCharacters(const ITransition::TCharSet &characters);
00038
00039 Transition* getGraphicsTransition();
00040
00041 protected:
00042 QString m_sourceState;
00043 QString m_destinationState;
00044
00045 ITransition::TCharSet m_characters;
00046 Transition* m_graphicsTransition;
00047 };
00048
00049 inline bool operator==(const ITransition &lhs, const ITransition &rhs)
00050 {
00051 return (lhs.getSourceState() == rhs.getSourceState() &&
00052 lhs.getDestinationState() == rhs.getDestinationState() &&
00053 lhs.getCharacters() == rhs.getCharacters());
00054 }
00055
00056 inline uint qHash(const ITransition &key)
00057 {
00058 uint hash = qHash(key.getSourceState()+key.getDestinationState());
00059
00060 foreach(const QString &character, key.getCharacters())
00061 {
00062 hash ^= qHash(character);
00063 }
00064
00065 return hash;
00066 }
00067
00068 class StateImpl : public IState
00069 {
00070 private:
00071 Q_DISABLE_COPY(StateImpl)
00072
00073 public:
00074 typedef QList<QSharedPointer<StateImpl> > TStateList;
00075 typedef QMap<QString, QSharedPointer<StateImpl> > TStateMap;
00076
00077 StateImpl(const QString &name, const QString &label, bool initial, bool final,
00078 State *graphicsState = NULL);
00079 ~StateImpl();
00080
00081
00082
00083
00084 void setName(const QString &name);
00085 QString getName() const;
00086
00087 void setLabel(const QString &label);
00088 QString getLabel() const;
00089
00090 void setInitial(bool is = true);
00091 void setFinal(bool is = true);
00092
00093 bool isInitial() const;
00094 bool isFinal() const;
00095
00096 IState::TIStateNameSet getStatesOn(const QString &character) const;
00097 IState::TIStateNameList getAdjacentStates() const;
00098
00099 ITransition::TITransitionList getTransitions() const;
00100 ITransition::TITransitionList getTransitionsTo() const;
00101
00102
00103 void addTransition(const QSharedPointer<TransitionImpl> &tr);
00104 void addTransitionTo(const QSharedPointer<TransitionImpl> &tr);
00105
00106 void removeTransition(const QSharedPointer<TransitionImpl> &tr);
00107 void removeTransitionTo(const QSharedPointer<TransitionImpl> &tr);
00108
00109 TransitionImpl::TTransitionList getPrivateTransitions() const;
00110 TransitionImpl::TTransitionList getPrivateTransitionsTo() const;
00111
00112 IState::TIStateNameSet getStatesAndPathsOn(const QString &character, QList<QPainterPath> &paths) const;
00113
00114 State* getGraphicsState() const;
00115
00116 protected:
00117 QString m_name;
00118 QString m_label;
00119 bool m_initial;
00120 bool m_final;
00121
00122 TransitionImpl::TTransitionList m_transitions;
00123 TransitionImpl::TTransitionList m_transitionsTo;
00124
00125 State* m_graphicsState;
00126 };
00127
00128
00129
00130
00131
00132
00133 class AutomatonImpl : public IAutomaton
00134 {
00135 private:
00136 Q_DISABLE_COPY(AutomatonImpl)
00137
00138 public:
00139 typedef QList<QSharedPointer<AutomatonImpl> > TAutomatonList;
00140
00141 AutomatonImpl(const ITransition::TCharSet &alphabet, const QString &alphabetSymbol,
00142 const QString &epsilonSymbol);
00143
00144 ~AutomatonImpl();
00145
00146 virtual QSharedPointer<IState> createState
00147 (const QString &name, const QString &label, bool initial, bool final);
00148
00149 virtual QSharedPointer<ITransition> createTransition
00150 (const QString &source, const QString &dest, const ITransition::TCharSet &characters);
00151
00152 virtual IState::TIStateList getStates() const;
00153 virtual IState::TIStateList getInitialStates() const;
00154 virtual IState::TIStateList getFinalStates() const;
00155
00156 virtual IState::TIStateNameList getStateNameList() const;
00157
00158 StateImpl::TStateList getPrivateStates() const;
00159 StateImpl::TStateList getPrivateInitialStates() const;
00160 StateImpl::TStateList getPrivateFinalStates() const;
00161
00162 virtual bool hasPositions() const { return !m_positioningMap.isEmpty(); }
00163 virtual IAutomaton::TPositioningMap getPositioningMap() const;
00164 virtual void setPositioningMap(const IAutomaton::TPositioningMap &map);
00165
00166 virtual ITransition::TITransitionList getTransitions() const;
00167 virtual QSharedPointer<ITransitionTable> getTransitionTable() const;
00168
00169 TransitionImpl::TTransitionList getPrivateTransitions() const;
00170
00171 virtual void setAlphabet(const ITransition::TCharSet &alphabet);
00172 virtual void setAlphabetSymbol(const QString &alphabetSymbol);
00173 virtual void setEpsilonSymbol(const QString &epsilonSymbol);
00174
00175 virtual ITransition::TCharSet getAlphabet() const;
00176 virtual QString getAlphabetSymbol() const;
00177 virtual QString getEpsilonSymbol() const ;
00178
00179 virtual QSharedPointer<IState> getState(const QString &name) const;
00180 virtual bool removeState(const QSharedPointer<IState> &state);
00181 virtual bool removeState(const QString &stateName);
00182 virtual bool renameState(const QSharedPointer<IState> &state,
00183 const QString &newName);
00184
00185 virtual bool addState(const QSharedPointer<StateImpl> &state);
00186 QSharedPointer<StateImpl> getPrivateState(const QString &name) const;
00187
00188 virtual bool addTransition(const QSharedPointer<TransitionImpl> &tr);
00189 virtual bool removeTransition(const QSharedPointer<TransitionImpl> &tr);
00190 virtual bool removeTransition(const QSharedPointer<ITransition> &tr);
00191
00192 virtual void addLists(const StateImpl::TStateList &states,
00193 const TransitionImpl::TTransitionList &transitions);
00194
00195 virtual bool hasMultipleInitials() const;
00196 virtual bool hasEpsilonTransitions() const;
00197 virtual bool isDeterministic() const;
00198 virtual bool hasState(const QString &name) const;
00199 virtual ITransition::TCharSet getTransitionSymbols(const QString &source,
00200 const QString &dest) const;
00201
00202 virtual bool allowRedundantTransitions() const { return m_allowRenundancies; }
00203
00204 virtual void setAllowRedundantTransitions(bool allow);
00205
00206 virtual void reset();
00207 virtual bool processCharacter(const QString &character);
00208 virtual bool isConfigurationAccepted() const;
00209 virtual IState::TIStateList getActiveStates() const;
00210 virtual QStringList getActiveStatesNames() const;
00211 virtual QStringList getProcessedString() const;
00212
00213 StateImpl::TStateList getPrivateActiveStates() const;
00214 IState::TIStateNameSet processCharacterWithInfo(const QString &character,
00215 QList<QPainterPath> &paths);
00216
00217
00218
00219
00220 QSharedPointer<TransitionImpl> createPrivateTransition
00221 (const QString &source, const QString &dest, const ITransition::TCharSet &characters,
00222 const TransitionImpl::TTransitionList &transitions, bool &created, Transition *graphicsTransition);
00223
00224
00225 QSharedPointer<TransitionImpl> createAndAddPrivateTransition
00226 (const QString &source, const QString &dest, const ITransition::TCharSet &characters, Transition *graphicsTransition);
00227
00228 protected:
00229
00230 QSharedPointer<TransitionImpl> createPrivateTransitionInternal
00231 (const QString &source, const QString &dest, const ITransition::TCharSet &characters, bool &created);
00232 QSharedPointer<TransitionImpl> createPrivateTransitionIfNotExists
00233 (const QString &source, const QString &dest, const ITransition::TCharSet &characters,
00234 const TransitionImpl::TTransitionList &transitions, bool &created, Transition *graphicsTransition = NULL);
00235 QSharedPointer<TransitionImpl> createEpsilonTransitionIfNotExists
00236 (const QString &source, const QString &dest, const QString &epsilonSymbol,
00237 const TransitionImpl::TTransitionList &transitions, bool &created, Transition *graphicsTransition = NULL);
00238 QSharedPointer<TransitionImpl> createCharactersTransitionIfNotExists
00239 (const QString &source, const QString &dest, ITransition::TCharSet &characters,
00240 const TransitionImpl::TTransitionList &transitions, bool &created, Transition *graphicsTransition = NULL);
00241
00242 private:
00243
00244 void fillRowData(const QSharedPointer<IState> &state, ITTRow &row) const;
00245
00246 StateImpl::TStateMap m_stateMap;
00247 IAutomaton::TPositioningMap m_positioningMap;
00248
00249 TransitionImpl::TTransitionList m_transitions;
00250
00251 ITransition::TCharSet m_alphabet;
00252 QString m_alphabetSymbol;
00253 QString m_epsilonSymbol;
00254
00255 StateImpl::TStateList m_activeStates;
00256 QStringList m_processedString;
00257
00258 bool m_allowRenundancies;
00259 };
00260
00261
00262
00263
00264
00265
00266 class AutomataCreator : public IAutomataCreator
00267 {
00268 public:
00269 ~AutomataCreator() {}
00270
00271 QSharedPointer<AutomatonImpl> createPrivateAutomaton
00272 (const ITransition::TCharSet &alphabet,
00273 const QString &alphabetSymbol,
00274 const QString &epsilonSymbol);
00275
00276
00277
00278 QSharedPointer<StateImpl> createPrivateState
00279 (const QString &name, const QString &label, bool initial, bool final,
00280 State *graphicsState);
00281
00282
00283
00284
00285 QSharedPointer<IAutomaton> createAutomaton
00286 (const ITransition::TCharSet &alphabet,
00287 const QString &alphabetSymbol,
00288 const QString &epsilonSymbol);
00289
00290
00291
00292 static const QStringList defaultAlphabetSymbList;
00293
00294 static const QStringList defaultEpsilonSymbList;
00295 };
00296
00297 #endif