Qt 4.8
Public Types | Public Functions | Private Functions | Properties | List of all members
QPatternist::XsdStateMachine< TransitionType > Class Template Reference

A state machine used for evaluation. More...

#include <qxsdstatemachine_p.h>

Public Types

typedef qint32 StateId
 
enum  StateType { StartState, StartEndState, InternalState, EndState }
 

Public Functions

void addEpsilonTransition (StateId start, StateId end)
 
StateId addState (StateType type)
 
void addTransition (StateId start, TransitionType transition, StateId end)
 
void clear ()
 
bool inEndState () const
 
template<>
bool inputEqualsTransition (QXmlName name, XsdTerm::Ptr term) const
 
template<typename InputType >
bool inputEqualsTransition (InputType input, TransitionType transition) const
 
template<typename InputType >
bool inputEqualsTransition (InputType input, TransitionType transition) const
 
TransitionType lastTransition () const
 
bool outputGraph (QIODevice *device, const QString &graphName) const
 
QList< TransitionType > possibleTransitions () const
 
template<typename TransitionType >
bool proceed (TransitionType transition)
 
bool proceed (TransitionType transition)
 
template<typename InputType >
bool proceed (InputType input)
 
template<typename InputType >
bool proceed (InputType input)
 
void reset ()
 
StateId startState () const
 
QHash< StateId, StateTypestates () const
 
XsdStateMachine< TransitionType > toDFA () const
 
QHash< StateId, QHash< TransitionType, QVector< StateId > > > transitions () const
 
template<>
QString transitionTypeToString (XsdTerm::Ptr term) const
 
QString transitionTypeToString (TransitionType type) const
 
 XsdStateMachine ()
 
 XsdStateMachine (const NamePool::Ptr &namePool)
 

Private Functions

StateId dfaStateForNfaState (QSet< StateId > nfaState, QList< QPair< QSet< StateId >, StateId > > &stateTable, XsdStateMachine< TransitionType > &dfa) const
 
QSet< StateIdepsilonClosure (const QSet< StateId > &input) const
 
QSet< StateIdmove (const QSet< StateId > &states, TransitionType input) const
 

Properties

qint32 m_counter
 
StateId m_currentState
 
QHash< StateId, QVector< StateId > > m_epsilonTransitions
 
TransitionType m_lastTransition
 
NamePool::Ptr m_namePool
 
QHash< StateId, StateTypem_states
 
QHash< StateId, QHash< TransitionType, QVector< StateId > > > m_transitions
 

Detailed Description

template<typename TransitionType>
class QPatternist::XsdStateMachine< TransitionType >

A state machine used for evaluation.

Author
Tobias Koenig tobia.nosp@m.s.ko.nosp@m.enig@.nosp@m.noki.nosp@m.a.com

Definition at line 76 of file qxsdstatemachine_p.h.

Typedefs

◆ StateId

template<typename TransitionType>
typedef qint32 QPatternist::XsdStateMachine< TransitionType >::StateId

Definition at line 79 of file qxsdstatemachine_p.h.

Enumerations

◆ StateType

template<typename TransitionType>
enum QPatternist::XsdStateMachine::StateType

Describes the type of state.

Enumerator
StartState 

The state the machine will start with.

StartEndState 

The state the machine will start with, can be end state as well.

InternalState 

Any state that is not start or end state.

EndState 

Any state where the machine is allowed to stop.

Definition at line 84 of file qxsdstatemachine_p.h.

85  {
86  StartState,
89  EndState
90  };
Any state that is not start or end state.
The state the machine will start with, can be end state as well.
The state the machine will start with.
Any state where the machine is allowed to stop.

Constructors and Destructors

◆ XsdStateMachine() [1/2]

template<typename TransitionType >
QPatternist::XsdStateMachine< TransitionType >::XsdStateMachine ( )

Creates a new state machine object.

Definition at line 48 of file qxsdstatemachine.cpp.

49  : m_counter(50)
50 {
51 }

◆ XsdStateMachine() [2/2]

template<typename TransitionType >
QPatternist::XsdStateMachine< TransitionType >::XsdStateMachine ( const NamePool::Ptr namePool)

Creates a new state machine object.

The name pool to use for accessing object names.

Definition at line 54 of file qxsdstatemachine.cpp.

55  : m_namePool(namePool)
56  , m_counter(50)
57 {
58 }

Functions

◆ addEpsilonTransition()

template<typename TransitionType >
void QPatternist::XsdStateMachine< TransitionType >::addEpsilonTransition ( StateId  start,
StateId  end 
)

Adds a new epsilon transition to the state machine.

Parameters
startThe start state.
endThe end state.

Definition at line 95 of file qxsdstatemachine.cpp.

96 {
98  states.append(end);
99 }
QHash< StateId, StateType > states() const
void append(const T &t)
Inserts value at the end of the vector.
Definition: qvector.h:573
QHash< StateId, QVector< StateId > > m_epsilonTransitions
static const KeyPair *const end

◆ addState()

template<typename TransitionType >
XsdStateMachine< TransitionType >::StateId QPatternist::XsdStateMachine< TransitionType >::addState ( StateType  type)

Adds a new state of the given type to the state machine.

Returns
The id of the new state.

Definition at line 61 of file qxsdstatemachine.cpp.

Referenced by QPatternist::XsdStateMachine< XsdSchemaToken::NodeName >::dfaStateForNfaState(), and QPatternist::XsdSchemaParser::setupStateMachines().

62 {
63 #ifndef QT_NO_DEBUG
64  // make sure we don't have two start states
65  if (type == StartState) {
66  QHashIterator<StateId, StateType> it(m_states);
67  while (it.hasNext()) {
68  it.next();
69  Q_ASSERT(it.value() != StartState && it.value() != StartEndState);
70  }
71  }
72 #endif // QT_NO_DEBUG
73 
74  // reserve new state id
75  const StateId id = ++m_counter;
76  m_states.insert(id, type);
77 
78  // if it is a start state, we make it to our current state
79  if (type == StartState || type == StartEndState)
80  m_currentState = id;
81 
82  return id;
83 }
int type
Definition: qmetatype.cpp:239
#define it(className, varName)
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition: qhash.h:753
The state the machine will start with, can be end state as well.
QHash< StateId, StateType > m_states
The state the machine will start with.

◆ addTransition()

template<typename TransitionType>
void QPatternist::XsdStateMachine< TransitionType >::addTransition ( StateId  start,
TransitionType  transition,
StateId  end 
)

Adds a new transition to the state machine.

Parameters
startThe start state.
transitionThe transition to come from the start to the end state.
endThe end state.

Definition at line 86 of file qxsdstatemachine.cpp.

Referenced by QPatternist::XsdSchemaParser::setupStateMachines(), and QPatternist::XsdStateMachine< XsdSchemaToken::NodeName >::toDFA().

87 {
89  QVector<StateId> &states = hash[transition];
90  if (!states.contains(end))
91  states.append(end);
92 }
static uint hash(const uchar *p, int n)
Definition: qhash.cpp:68
QHash< StateId, QHash< TransitionType, QVector< StateId > > > m_transitions
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66
QHash< StateId, StateType > states() const
void append(const T &t)
Inserts value at the end of the vector.
Definition: qvector.h:573
bool contains(const T &t) const
Returns true if the vector contains an occurrence of value; otherwise returns false.
Definition: qvector.h:731
static const KeyPair *const end

◆ clear()

template<typename TransitionType >
void QPatternist::XsdStateMachine< TransitionType >::clear ( void  )

Removes all states and transitions from the state machine.

Definition at line 118 of file qxsdstatemachine.cpp.

119 {
120  m_states.clear();
123  m_currentState = -1;
124  m_counter = 50;
125 }
void clear()
Removes all items from the hash.
Definition: qhash.h:574
QHash< StateId, QHash< TransitionType, QVector< StateId > > > m_transitions
QHash< StateId, QVector< StateId > > m_epsilonTransitions
QHash< StateId, StateType > m_states

◆ dfaStateForNfaState()

template<typename TransitionType>
XsdStateMachine< TransitionType >::StateId QPatternist::XsdStateMachine< TransitionType >::dfaStateForNfaState ( QSet< StateId nfaState,
QList< QPair< QSet< StateId >, StateId > > &  stateTable,
XsdStateMachine< TransitionType > &  dfa 
) const
private

Returns the DFA state for the given nfaStat from the given stateTable. If there is no corresponding DFA state yet, a new one is created.

Definition at line 294 of file qxsdstatemachine.cpp.

Referenced by QPatternist::XsdStateMachine< XsdSchemaToken::NodeName >::transitions().

297 {
298  // check whether we have the given state in our lookup table
299  // already, in that case simply return it
300  for (int i = 0; i < stateTable.count(); ++i) {
301  if (stateTable.at(i).first == nfaState)
302  return stateTable.at(i).second;
303  }
304 
305  // check if the NFA state set contains a Start or End
306  // state, in that case our new DFA state will be a
307  // Start or End state as well
309  QSetIterator<StateId> it(nfaState);
310  bool hasStartState = false;
311  bool hasEndState = false;
312  while (it.hasNext()) {
313  const StateId state = it.next();
314  if (m_states.value(state) == EndState) {
315  hasEndState = true;
316  } else if (m_states.value(state) == StartState) {
317  hasStartState = true;
318  }
319  }
320  if (hasStartState) {
321  if (hasEndState)
322  type = StartEndState;
323  else
324  type = StartState;
325  } else if (hasEndState) {
326  type = EndState;
327  }
328 
329  // create the new DFA state
330  const StateId dfaState = dfa.addState(type);
331 
332  // add the new DFA state to the lookup table
333  stateTable.append(qMakePair<QSet<StateId>, StateId>(nfaState, dfaState));
334 
335  return dfaState;
336 }
int type
Definition: qmetatype.cpp:239
#define it(className, varName)
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
Any state that is not start or end state.
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The state the machine will start with, can be end state as well.
QHash< StateId, StateType > m_states
Q_OUTOFLINE_TEMPLATE QPair< T1, T2 > qMakePair(const T1 &x, const T2 &y)
Definition: qpair.h:102
The state the machine will start with.
Any state where the machine is allowed to stop.

◆ epsilonClosure()

template<typename TransitionType>
QSet<StateId> QPatternist::XsdStateMachine< TransitionType >::epsilonClosure ( const QSet< StateId > &  input) const
inlineprivate

Returns the set of all states that can be reached from the set of input states by the epsilon transition.

The implementation is inlined in order to workaround a compiler bug on Symbian/winscw.

Definition at line 230 of file qxsdstatemachine_p.h.

231  {
232  // every state can reach itself by epsilon transition, so include the input states
233  // in the result as well
234  QSet<StateId> result = input;
235 
236  // add the input states to the list of to be processed states
237  QList<StateId> workStates = input.toList();
238  while (!workStates.isEmpty()) { // while there are states to be processed left...
239 
240  // dequeue one state from list
241  const StateId state = workStates.takeFirst();
242 
243  // get the list of states that can be reached by the epsilon transition
244  // from the current 'state'
245  const QVector<StateId> targetStates = m_epsilonTransitions.value(state);
246  for (int i = 0; i < targetStates.count(); ++i) {
247  // if we have this target state not in our result set yet...
248  if (!result.contains(targetStates.at(i))) {
249  // ... add it to the result set
250  result.insert(targetStates.at(i));
251 
252  // add the target state to the list of to be processed states as well,
253  // as we want to have the epsilon transitions not only for the first
254  // level of following states
255  workStates.append(targetStates.at(i));
256  }
257  }
258  }
259 
260  return result;
261  }
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
bool contains(const T &value) const
Definition: qset.h:91
T takeFirst()
Removes the first item in the list and returns it.
Definition: qlist.h:489
QList< T > toList() const
Definition: qset.h:296
const_iterator insert(const T &value)
Definition: qset.h:179
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
QHash< StateId, QVector< StateId > > m_epsilonTransitions
The QList class is a template class that provides lists.
Definition: qdatastream.h:62

◆ inEndState()

template<typename TransitionType >
bool QPatternist::XsdStateMachine< TransitionType >::inEndState ( ) const

Returns whether the machine is in an allowed end state.

Definition at line 192 of file qxsdstatemachine.cpp.

193 {
194  // check if current state is an end state
196 }
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
The state the machine will start with, can be end state as well.
QHash< StateId, StateType > m_states
Any state where the machine is allowed to stop.

◆ inputEqualsTransition() [1/3]

template<>
bool QPatternist::XsdStateMachine< XsdTerm::Ptr >::inputEqualsTransition< QXmlName > ( QXmlName  name,
XsdTerm::Ptr  term 
) const

Definition at line 81 of file qxsdvalidatinginstancereader.cpp.

82  {
83  if (term->isElement()) {
84  return (XsdElement::Ptr(term)->name(m_namePool) == name);
85  } else if (term->isWildcard()) {
86  // wildcards using XsdWildcard::absentNamespace, so we have to fix that here
89  }
90 
92  }
93 
94  return false;
95  }
static QString absentNamespace()
QXmlName::NamespaceCode allocateNamespace(const QString &uri)
Definition: qnamepool_p.h:202
virtual bool isWildcard() const
Definition: qxsdterm.cpp:58
virtual bool isElement() const
Definition: qxsdterm.cpp:48
const char * name
virtual QXmlName name(const NamePool::Ptr &namePool) const
NamespaceCode namespaceURI() const
Definition: qnamepool_p.h:503
static bool wildcardAllowsExpandedName(const QXmlName &name, const XsdWildcard::Ptr &wildcard, const NamePool::Ptr &namePool)
void setNamespaceURI(const NamespaceCode c)
Definition: qnamepool_p.h:518

◆ inputEqualsTransition() [2/3]

template<typename TransitionType>
template<typename InputType >
bool QPatternist::XsdStateMachine< TransitionType >::inputEqualsTransition ( InputType  input,
TransitionType  transition 
) const

Returns whether the given input matches the given transition.

Definition at line 187 of file qxsdstatemachine_p.h.

212  {

◆ inputEqualsTransition() [3/3]

template<typename TransitionType>
template<typename InputType >
bool QPatternist::XsdStateMachine< TransitionType >::inputEqualsTransition ( InputType  input,
TransitionType  transition 
) const

Definition at line 186 of file qxsdstatemachine.cpp.

187 {
188  return false;
189 }

◆ lastTransition()

template<typename TransitionType >
TransitionType QPatternist::XsdStateMachine< TransitionType >::lastTransition ( ) const

Returns the last transition that was taken.

Definition at line 199 of file qxsdstatemachine.cpp.

200 {
201  return m_lastTransition;
202 }

◆ move()

template<typename TransitionType>
QSet<StateId> QPatternist::XsdStateMachine< TransitionType >::move ( const QSet< StateId > &  states,
TransitionType  input 
) const
inlineprivate

Returns the set of all states that can be reached from the set of given states by the given input.

The implementation is inlined in order to workaround a compiler bug on Symbian/winscw.

Definition at line 270 of file qxsdstatemachine_p.h.

271  {
272  QSet<StateId> result;
273 
274  QSetIterator<StateId> it(states);
275  while (it.hasNext()) { // iterate over all given states
276  const StateId state = it.next();
277 
278  // get the transition table for the current state
280 
281  // get the target states for the given input
282  const QVector<StateId> targetStates = transitions.value(input);
283 
284  // add all target states to the result
285  for (int i = 0; i < targetStates.size(); ++i)
286  result.insert(targetStates.at(i));
287  }
288 
289  return result;
290  }
#define it(className, varName)
QHash< StateId, QHash< TransitionType, QVector< StateId > > > m_transitions
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
const_iterator insert(const T &value)
Definition: qset.h:179
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
QHash< StateId, QHash< TransitionType, QVector< StateId > > > transitions() const
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137

◆ outputGraph()

template<typename TransitionType >
bool QPatternist::XsdStateMachine< TransitionType >::outputGraph ( QIODevice device,
const QString graphName 
) const

Outputs the state machine in DOT format to the given output device.

Definition at line 227 of file qxsdstatemachine.cpp.

228 {
229  if (!device->isOpen()) {
230  qWarning("device must be open for writing");
231  return false;
232  }
233 
234  QByteArray graph;
235  QTextStream s(&graph);
236 
237  QHashIterator<StateId, QHash<TransitionType, QVector<StateId> > > it(m_transitions);
238  QHashIterator<StateId, StateType> it3(m_states);
239 
240  s << "digraph " << graphName << " {\n";
241  s << " mindist = 2.0\n";
242 
243  // draw edges
244  while (it.hasNext()) {
245  it.next();
246 
247  QHashIterator<TransitionType, QVector<StateId> > it2(it.value());
248  while (it2.hasNext()) {
249  it2.next();
250  for (int i = 0; i < it2.value().count(); ++i)
251  s << " " << it.key() << " -> " << it2.value().at(i) << " [label=\"" << transitionTypeToString(it2.key()) << "\"]\n";
252  }
253  }
254 
255  QHashIterator<StateId, QVector<StateId> > it4(m_epsilonTransitions);
256  while (it4.hasNext()) {
257  it4.next();
258 
259  const QVector<StateId> states = it4.value();
260  for (int i = 0; i < states.count(); ++i)
261  s << " " << it4.key() << " -> " << states.at(i) << " [label=\"&#949;\"]\n";
262  }
263 
264  // draw node infos
265  while (it3.hasNext()) {
266  it3.next();
267 
268  QString style;
269  if (it3.value() == StartState) {
270  style = QLatin1String("shape=circle, style=filled, color=blue");
271  } else if (it3.value() == StartEndState) {
272  style = QLatin1String("shape=doublecircle, style=filled, color=blue");
273  } else if (it3.value() == InternalState) {
274  style = QLatin1String("shape=circle, style=filled, color=red");
275  } else if (it3.value() == EndState) {
276  style = QLatin1String("shape=doublecircle, style=filled, color=green");
277  }
278 
279  s << " " << it3.key() << " [" << style << "]\n";
280  }
281 
282  s << "}\n";
283 
284  s.flush();
285 
286  if (device->write(graph) == -1)
287  return false;
288 
289  return true;
290 }
#define it(className, varName)
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
QHash< StateId, QHash< TransitionType, QVector< StateId > > > m_transitions
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
Any state that is not start or end state.
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
QString transitionTypeToString(TransitionType type) const
bool isOpen() const
Returns true if the device is open; otherwise returns false.
Definition: qiodevice.cpp:530
QHash< StateId, StateType > states() const
Q_CORE_EXPORT void qWarning(const char *,...)
The state the machine will start with, can be end state as well.
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
QHash< StateId, QVector< StateId > > m_epsilonTransitions
QHash< StateId, StateType > m_states
The QTextStream class provides a convenient interface for reading and writing text.
Definition: qtextstream.h:73
The state the machine will start with.
Any state where the machine is allowed to stop.
qint64 write(const char *data, qint64 len)
Writes at most maxSize bytes of data from data to the device.
Definition: qiodevice.cpp:1342

◆ possibleTransitions()

template<typename TransitionType >
QList< TransitionType > QPatternist::XsdStateMachine< TransitionType >::possibleTransitions ( ) const

Returns the list of transitions that are reachable from the current state.

Definition at line 147 of file qxsdstatemachine.cpp.

148 {
149  // check that we are not in an invalid state
151  return QList<TransitionType>();
152  }
153 
154  // fetch the transition entry for the current state
156 
157  return entry.keys();
158 }
QHash< StateId, QHash< TransitionType, QVector< StateId > > > m_transitions
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66
bool contains(const Key &key) const
Returns true if the hash contains an item with the key; otherwise returns false.
Definition: qhash.h:872
The QList class is a template class that provides lists.
Definition: qdatastream.h:62

◆ proceed() [1/4]

template<typename TransitionType>
template<typename TransitionType >
bool QPatternist::XsdStateMachine< TransitionType >::proceed ( TransitionType  transition)

Definition at line 128 of file qxsdstatemachine.cpp.

129 {
130  // check that we are not in an invalid state
132  return false;
133  }
134 
135  // fetch the transition entry for the current state
137  if (entry.contains(transition)) { // is there an transition for the given input?
138  m_currentState = entry.value(transition).first();
139  m_lastTransition = transition;
140  return true;
141  } else {
142  return false;
143  }
144 }
QHash< StateId, QHash< TransitionType, QVector< StateId > > > m_transitions
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66
bool contains(const Key &key) const
Returns true if the hash contains an item with the key; otherwise returns false.
Definition: qhash.h:872
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606

◆ proceed() [2/4]

template<typename TransitionType>
bool QPatternist::XsdStateMachine< TransitionType >::proceed ( TransitionType  transition)

Continues execution of the machine with the given input transition.

Returns
true if the transition was successful, false otherwise.

Definition at line 129 of file qxsdstatemachine_p.h.

212  {

◆ proceed() [3/4]

template<typename TransitionType >
template<typename InputType >
bool QPatternist::XsdStateMachine< TransitionType >::proceed ( InputType  input)

Continues execution of the machine with the given input.

Note
To use this method, inputEqualsTransition must be implemented to find the right transition to use.
Returns
true if the transition was successful, false otherwise.

Definition at line 163 of file qxsdstatemachine_p.h.

212  {

◆ proceed() [4/4]

template<typename TransitionType>
template<typename InputType >
bool QPatternist::XsdStateMachine< TransitionType >::proceed ( InputType  input)

Definition at line 162 of file qxsdstatemachine.cpp.

163 {
164  // check that we are not in an invalid state
166  return false;
167  }
168 
169  // fetch the transition entry for the current state
171  QHashIterator<TransitionType, QVector<StateId> > it(entry);
172  while (it.hasNext()) {
173  it.next();
174  if (inputEqualsTransition(input, it.key())) {
175  m_currentState = it.value().first();
176  m_lastTransition = it.key();
177  return true;
178  }
179  }
180 
181  return false;
182 }
#define it(className, varName)
QHash< StateId, QHash< TransitionType, QVector< StateId > > > m_transitions
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66
bool contains(const Key &key) const
Returns true if the hash contains an item with the key; otherwise returns false.
Definition: qhash.h:872
bool inputEqualsTransition(InputType input, TransitionType transition) const

◆ reset()

template<typename TransitionType >
void QPatternist::XsdStateMachine< TransitionType >::reset ( )

Resets the machine to the start state.

Definition at line 102 of file qxsdstatemachine.cpp.

103 {
104  // reset the machine to the start state
105  QHashIterator<StateId, StateType> it(m_states);
106  while (it.hasNext()) {
107  it.next();
108  if (it.value() == StartState || it.value() == StartEndState) {
109  m_currentState = it.key();
110  return;
111  }
112  }
113 
114  Q_ASSERT(false);
115 }
#define it(className, varName)
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The state the machine will start with, can be end state as well.
QHash< StateId, StateType > m_states
The state the machine will start with.

◆ startState()

template<typename TransitionType >
XsdStateMachine< TransitionType >::StateId QPatternist::XsdStateMachine< TransitionType >::startState ( ) const

Returns the start state of the machine.

Definition at line 205 of file qxsdstatemachine.cpp.

Referenced by QPatternist::XsdParticleChecker::subsumes().

206 {
207  QHashIterator<StateId, StateType> it(m_states);
208  while (it.hasNext()) {
209  it.next();
210  if (it.value() == StartState || it.value() == StartEndState)
211  return it.key();
212  }
213 
214  Q_ASSERT(false); // should never be reached
215  return -1;
216 }
#define it(className, varName)
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The state the machine will start with, can be end state as well.
QHash< StateId, StateType > m_states
The state the machine will start with.

◆ states()

template<typename TransitionType >
QHash< typename XsdStateMachine< TransitionType >::StateId, typename XsdStateMachine< TransitionType >::StateType > QPatternist::XsdStateMachine< TransitionType >::states ( ) const

Returns the information of all states of the state machine.

Definition at line 410 of file qxsdstatemachine.cpp.

Referenced by QPatternist::XsdParticleChecker::isUPAConform(), and QPatternist::XsdParticleChecker::subsumes().

411 {
412  return m_states;
413 }
QHash< StateId, StateType > m_states

◆ toDFA()

template<typename TransitionType >
XsdStateMachine< TransitionType > QPatternist::XsdStateMachine< TransitionType >::toDFA ( ) const

Returns a DFA that is equal to the NFA of the state machine.

Definition at line 339 of file qxsdstatemachine.cpp.

Referenced by QPatternist::XsdValidatingInstanceReader::createAndPushStateMachine(), QPatternist::XsdParticleChecker::isUPAConform(), and QPatternist::XsdParticleChecker::subsumes().

340 {
341  XsdStateMachine<TransitionType> dfa(m_namePool);
342  dfa.m_counter = 100;
344  QList< QSet<StateId> > isMarked;
345 
346  // search the start state as the algorithm starts with it...
347  StateId startState = -1;
348  QHashIterator<StateId, StateType> stateTypeIt(m_states);
349  while (stateTypeIt.hasNext()) {
350  stateTypeIt.next();
351  if (stateTypeIt.value() == StartState) {
352  startState = stateTypeIt.key();
353  break;
354  }
355  }
356  Q_ASSERT(startState != -1);
357 
358  // our list of state set that still have to be processed
359  QList< QSet<StateId> > workStates;
360 
361  // add the start state to the list of to processed state sets
362  workStates.append(epsilonClosure(QSet<StateId>() << startState));
363 
364  while (!workStates.isEmpty()) { // as long as there are state sets to process left
365 
366  // enqueue set of states
367  const QSet<StateId> states = workStates.takeFirst();
368 
369  if (isMarked.contains(states)) // we processed this state set already
370  continue;
371 
372  // mark as processed
373  isMarked.append(states);
374 
375  // select a list of all inputs that are possible for
376  // the 'states' set
377  QList<TransitionType> input;
378 
379  {
380  QSetIterator<StateId> it(states);
381  while (it.hasNext()) {
382  input << m_transitions.value(it.next()).keys();
383  }
384  }
385 
386  // get the state in DFA that corresponds to the 'states' set in the NFA
387  const StateId dfaBegin = dfaStateForNfaState(states, table, dfa);
388 
389  for (int i = 0; i < input.count(); ++i) { // for each possible input
390  // retrieve the states that can be reached from the 'states' set by the
391  // given input or by epsilon transition
392  const QSet<StateId> followStates = epsilonClosure(move(states, input.at(i)));
393 
394  // get the state in DFA that corresponds to the 'followStates' set in the NFA
395  const StateId dfaEnd = dfaStateForNfaState(followStates, table, dfa);
396 
397  // adds a new transition to the DFA that corresponds to the transitions between
398  // 'states' and 'followStates' in the NFA
399  dfa.addTransition(dfaBegin, input.at(i), dfaEnd);
400 
401  // add the 'followStates' to the list of to be processed state sets
402  workStates.append(followStates);
403  }
404  }
405 
406  return dfa;
407 }
#define it(className, varName)
QHash< StateId, QHash< TransitionType, QVector< StateId > > > m_transitions
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QStringList keys
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
QBool contains(const T &t) const
Returns true if the list contains an occurrence of value; otherwise returns false.
Definition: qlist.h:880
T takeFirst()
Removes the first item in the list and returns it.
Definition: qlist.h:489
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
QHash< StateId, StateType > states() const
StateId dfaStateForNfaState(QSet< StateId > nfaState, QList< QPair< QSet< StateId >, StateId > > &stateTable, XsdStateMachine< TransitionType > &dfa) const
QHash< StateId, StateType > m_states
QSet< StateId > move(const QSet< StateId > &states, TransitionType input) const
The state the machine will start with.
QSet< StateId > epsilonClosure(const QSet< StateId > &input) const
The QList class is a template class that provides lists.
Definition: qdatastream.h:62

◆ transitions()

template<typename TransitionType>
QHash<StateId, QHash<TransitionType, QVector<StateId> > > QPatternist::XsdStateMachine< TransitionType >::transitions ( ) const
inline

Returns the information of all transitions of the state machine.

The implementation is inlined in order to workaround a compiler bug on Symbian/winscw.

Definition at line 211 of file qxsdstatemachine_p.h.

Referenced by QPatternist::XsdParticleChecker::isUPAConform(), QPatternist::XsdStateMachine< XsdSchemaToken::NodeName >::move(), and QPatternist::XsdParticleChecker::subsumes().

212  {
213  return m_transitions;
214  }
QHash< StateId, QHash< TransitionType, QVector< StateId > > > m_transitions

◆ transitionTypeToString() [1/2]

template<>
QString QPatternist::XsdStateMachine< XsdTerm::Ptr >::transitionTypeToString ( XsdTerm::Ptr  term) const

This template specialization is picked up by XsdStateMachine and allows us to print nice edge labels.

Definition at line 64 of file qxsdparticlechecker.cpp.

65  {
66  if (!term)
67  return QLatin1String("(empty)");
68 
69  if (term->isElement()) {
70  return XsdElement::Ptr(term)->displayName(m_namePool);
71  } else if (term->isWildcard()) {
72  const XsdWildcard::Ptr wildcard(term);
73  return QLatin1String("(wildcard)");
74  } else {
75  return QString();
76  }
77  }
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
virtual bool isWildcard() const
Definition: qxsdterm.cpp:58
virtual bool isElement() const
Definition: qxsdterm.cpp:48
virtual QString displayName(const NamePool::Ptr &namePool) const
QExplicitlySharedDataPointer< XsdElement > Ptr
Definition: qxsdelement_p.h:86

◆ transitionTypeToString() [2/2]

template<typename TransitionType>
QString QPatternist::XsdStateMachine< TransitionType >::transitionTypeToString ( TransitionType  type) const

This method should be redefined by template specialization for every concret TransitionType.

Definition at line 219 of file qxsdstatemachine.cpp.

220 {
221  Q_UNUSED(type)
222 
223  return QString();
224 }
int type
Definition: qmetatype.cpp:239
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

Properties

◆ m_counter

template<typename TransitionType>
qint32 QPatternist::XsdStateMachine< TransitionType >::m_counter
private

◆ m_currentState

template<typename TransitionType>
StateId QPatternist::XsdStateMachine< TransitionType >::m_currentState
private

Definition at line 296 of file qxsdstatemachine_p.h.

◆ m_epsilonTransitions

template<typename TransitionType>
QHash<StateId, QVector<StateId> > QPatternist::XsdStateMachine< TransitionType >::m_epsilonTransitions
private

◆ m_lastTransition

template<typename TransitionType>
TransitionType QPatternist::XsdStateMachine< TransitionType >::m_lastTransition
private

Definition at line 298 of file qxsdstatemachine_p.h.

◆ m_namePool

template<typename TransitionType>
NamePool::Ptr QPatternist::XsdStateMachine< TransitionType >::m_namePool
private

Definition at line 292 of file qxsdstatemachine_p.h.

◆ m_states

template<typename TransitionType>
QHash<StateId, StateType> QPatternist::XsdStateMachine< TransitionType >::m_states
private

Definition at line 293 of file qxsdstatemachine_p.h.

◆ m_transitions

template<typename TransitionType>
QHash<StateId, QHash<TransitionType, QVector<StateId> > > QPatternist::XsdStateMachine< TransitionType >::m_transitions
private

The documentation for this class was generated from the following files: