LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
automaton.h
Go to the documentation of this file.
1// Copyright 2002-2018 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6/************************************************************************
7 *
8 * @file automaton.h
9 * @author besancon (besanconr@zoe.cea.fr)
10 * @date Fri Oct 04 2002
11 * copyright Copyright (C) 2002 by CEA LIST
12 * Project Automaton
13 *
14 * @brief A class for the description of automata
15 *
16 *
17 ***********************************************************************/
18
19#ifndef AUTOMATON_H
20#define AUTOMATON_H
21
22#include "AutomatonExport.h"
23#include "transitionUnit.h"
24#include "searchGraph.h"
25#include "transition.h"
27#include "recognizerMatch.h"
28#include "constraint.h"
31
32#include <fstream>
33#include <vector>
34#include <set>
35#include <string>
36
37namespace Lima {
38namespace LinguisticProcessing {
39namespace Automaton {
40
45 public:
48
49 uint64_t getMaxDepthStack() const
50 { return m_maxDepthStack; }
52 { return m_maxTransitionsExplored; }
53 uint64_t getMaxNbResults() const
54 { return m_maxNbResults; }
55 uint64_t getMaxResultSize() const
56 { return m_maxResultSize; }
57
58 void setMaxDepthStack(const uint64_t val)
59 { m_maxDepthStack=val; }
60 void setMaxTransitionsExplored(const uint64_t val)
61 { m_maxTransitionsExplored=val; }
62 void setMaxNbResults(const uint64_t val)
63 { m_maxNbResults=val; }
64 void setMaxResultSize(const uint64_t val)
65 { m_maxResultSize=val; }
66
67 private:
68 uint64_t m_maxDepthStack;
69 uint64_t m_maxTransitionsExplored;
70 uint64_t m_maxNbResults;
71 uint64_t m_maxResultSize;
72};
73
87{
88friend class AutomatonReader;
89friend class AutomatonWriter;
90
91public:
96 Automaton( const std::string& automId = "" );
97
101 Automaton(const Automaton& a);
102
109 Automaton(const Tstate nbStates);
110
114 ~Automaton();
115
119 Automaton& operator= (const Automaton& a);
120
121
122 //**********************************************************************
127 Tstate numberOfStates() const;
128
133 uint64_t numberOfTransitions() const;
134
141 uint64_t numberOfTransitions(const Tstate state) const;
142
149 Transition const& nthTransition(const Tstate state,
150 const uint64_t n) const;
151
157 bool isFinalState(const Tstate state) const;
162 std::vector<Tstate> finalStates() const;
168 std::vector<Transition> const& getTransitionsState(const Tstate state) const;
169
173 bool hasTransitionsState(const Tstate state) const;
174
179 bool isDeterministic() const;
180
185 void reinit();
186
191 void setActionHash(const std::vector<std::pair<LimaString,Constraint> >& actionsWithOneArgument);
196 typedef std::pair<RecognizerMatch,ConstraintCheckList> AutomatonMatch;
198 public:
199 bool operator()(const AutomatonMatch& r1,
200 const AutomatonMatch& r2) const;
201 };
202 typedef std::set<AutomatonMatch,CompareAutomatonMatch>
219 bool getBestMatch(const LinguisticAnalysisStructure::AnalysisGraph& graph,
220 const LinguisticGraphVertex& begin,
221 const LinguisticGraphVertex& limit,
222 AnalysisContent& analysis,
223 RecognizerMatch& longestMatch,
224 ConstraintCheckList& checkList,
225 const SearchGraphSense sense,
226 const AutomatonControlParams& controlParams) const;
227
247 bool getAllMatches(const LinguisticAnalysisStructure::AnalysisGraph& graph,
248 const LinguisticGraphVertex& begin,
249 const LinguisticGraphVertex& limit,
250 AnalysisContent& analysis,
251 AutomatonMatchSet& allMatches,
252 ConstraintCheckList& checkList,
253 ForwardSearch& forward,
254 BackwardSearch& backward,
255 const SearchGraphSense sense,
256 const AutomatonControlParams& controlParams) const;
257
258
259 //----------------------------------------------------------------------
260 // to build the automaton
261 //----------------------------------------------------------------------
262
269 Tstate addState(bool is_final=false);
278 bool addTransition(Tstate initialState, Tstate finalState,
279 TransitionUnit* transition);
280
281 void removeState(const Tstate state);
282 void removeTransition(const Tstate initialState,
283 const TransitionUnit& transition);
288 void makeFinal(const Tstate state);
293 void unMakeFinal(const Tstate state);
301 void setDeterministic(const bool det);
307 Automaton subsets() const;
308
314 Automaton reverse() const;
315
316
323 Automaton brzozowskiMinimize() const;
324
325 // for the output
329 friend LIMA_AUTOMATON_EXPORT std::ostream& operator << (std::ostream& os, const Automaton& a);
330 friend LIMA_AUTOMATON_EXPORT QDebug& operator << (QDebug& os, const Automaton& a);
331
332 void initializeSearchStructures(MediaId language);
333 bool getMatchingTransitions(const LinguisticAnalysisStructure::AnalysisGraph& graph,
334 const LinguisticGraphVertex& vertex,
335 AnalysisContent& analysis,
336 const SearchGraph* searchGraph,
337 const Tstate& state,
338 std::vector<std::pair<std::deque<LinguisticGraphVertex>,const Transition*> >& matchingTransitions,
339 const LinguisticGraphVertex& limit) const;
340
341 protected:
343 std::vector<bool> m_finalStates;
344 std::vector< std::vector<Transition> > m_transitions;
345 std::vector< TransitionSearchStructure<Transition>* > m_searchStructures;
348 std::string m_id;
350 //private methods
351 //**********************************************************************
352 // helper functions for constructors and destructors
353 void init();
354 void copy(const Automaton& a);
355 void freeMem();
356
357 class DFSStack;
358 friend LIMA_AUTOMATON_EXPORT std::ostream& operator << (std::ostream& os, const DFSStack& x);
359 friend LIMA_AUTOMATON_EXPORT QDebug& operator << (QDebug& os, const DFSStack& x);
360
361 bool testFromState(const Tstate firstState,
363 const LinguisticGraphVertex& begin,
364 const LinguisticGraphVertex& limit,
365 AnalysisContent& analysis,
366 AutomatonMatchSet& results,
367 ConstraintCheckList& checkList,
368 DFSStack& stack,
369 const AutomatonControlParams& controlParams) const;
370
371 //************************************************************
372 // helper functions for automaton construction and
373 // determinization
374 typedef std::set<Tstate> SubSet;
375
376 bool existsEpsilonPathToFinal(const Tstate state) const;
377 std::vector<TransitionUnit*> collectTransitions() const;
378 bool isFinalSubset (const SubSet& v) const;
379 void reachableStates(const SubSet& states,
380 const TransitionUnit& t,
381 SubSet& reachable) const;
382 void reachableStates(const Tstate& state,
383 const TransitionUnit& t,
384 SubSet& reachable) const;
385 std::string subsetString(const SubSet& subset) const; // for debug
386};
387/***********************************************************************/
388// inline access functions
389/***********************************************************************/
391 return m_numberStates;
392}
393
394inline uint64_t Automaton::numberOfTransitions(const Tstate state) const {
395 return m_transitions[state].size();
396}
397
399 const uint64_t n) const {
400 return m_transitions[state][n];
401}
402
403inline bool Automaton::isFinalState(const Tstate state) const {
404 if (state >= m_numberStates) { return false; }
405 return m_finalStates[state];
406}
407
408
409inline bool Automaton::isDeterministic() const {
410 return m_deterministic;
411}
412
413inline std::vector<Transition> const&
415 return m_transitions[state];
416}
417
418} // end namespace
419} // end namespace
420} // end namespace
421
422#endif
#define LIMA_AUTOMATON_EXPORT
QDebug & operator<<(QDebug &qd, const std::string &str)
Definition QsLog.cpp:39
LinguisticGraph::vertex_descriptor LinguisticGraphVertex
Holds all data that pass through the ProcessUnits Analysis data are shared pointers,...
␈rief a class for control parameters for the search using the automata
Definition automaton.h:44
␈rief A class for the description of automata
Definition automaton.h:87
bool m_deterministic
a boolean flag indicating if the automaton is deterministic or not
Definition automaton.h:346
std::pair< RecognizerMatch, ConstraintCheckList > AutomatonMatch
types defined to store the result (or results of the application of the automaton on a graph
Definition automaton.h:196
std::vector< Transition > const & getTransitionsState(const Tstate state) const
get the list of transitions leaving from a given state
Definition automaton.h:414
uint64_t numberOfTransitions() const
get the number of transitions in the automaton
std::vector< bool > m_finalStates
which states are final states
Definition automaton.h:343
Tstate numberOfStates() const
get the number of states of the automaton
Definition automaton.h:390
Transition const & nthTransition(const Tstate state, const uint64_t n) const
find the nth transition leaving from a particular state
Definition automaton.h:398
bool isFinalState(const Tstate state) const
test if a given state is a final state of the automaton
Definition automaton.h:403
std::vector< std::vector< Transition > > m_transitions
the transitions
Definition automaton.h:344
Tstate m_numberStates
number of states in the automaton
Definition automaton.h:342
bool isDeterministic() const
test if the automaton is deterministic or not
Definition automaton.h:409
std::vector< TransitionSearchStructure< Transition > * > m_searchStructures
Definition automaton.h:345
std::set< AutomatonMatch, CompareAutomatonMatch > AutomatonMatchSet
Definition automaton.h:203
An AnalysisData containing a LinguisticGraph with a language and an id.
SearchGraphSense
enumerated type to indicate in which sense the automaton should be built or searched
Definition searchGraph.h:35
std::vector< ConstraintCheckListElement > ConstraintCheckList
NAUTITIA.
PUGI__FN void reverse(I begin, I end)
Definition pugixml.cpp:7457