NCBI C++ ToolKit
macro_engine.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef GUI_OBJUTILS___MACRO_ENGINE__HPP
2 #define GUI_OBJUTILS___MACRO_ENGINE__HPP
3 /* $Id: macro_engine.hpp 47411 2023-03-20 19:18:06Z asztalos $
4  * ===========================================================================
5  *
6  * PUBLIC DOMAIN NOTICE
7  * National Center for Biotechnology Information
8  *
9  * This software/database is a "United States Government Work" under the
10  * terms of the United States Copyright Act. It was written as part of
11  * the author's official duties as a United States Government employee and
12  * thus cannot be copyrighted. This software/database is freely available
13  * to the public for use. The National Library of Medicine and the U.S.
14  * Government have not placed any restriction on its use or reproduction.
15  *
16  * Although all reasonable efforts have been taken to ensure the accuracy
17  * and reliability of the software and data, the NLM and the U.S.
18  * Government do not and cannot warrant the performance or results that
19  * may be obtained by using this software or data. The NLM and the U.S.
20  * Government disclaim all warranties, express or implied, including
21  * warranties of performance, merchantability or fitness for any particular
22  * purpose.
23  *
24  * Please cite the author in any work or product based on this material.
25  *
26  * ===========================================================================
27  *
28  * Authors: Anatoly Osipov
29  *
30  * File Description: Macro engine for macro processing
31  *
32  */
33 
34 /// @file macro_engine.hpp
35 /// Macro engine for macro execution
36 #include <gui/gui_export.h>
43 
44 /** @addtogroup GUI_MACRO_SCRIPTS_UTIL
45  *
46  * @{
47  */
48 
50 BEGIN_SCOPE(macro)
51 
52 ///////////////////////////////////////////////////////////////////////////////
53 /// CMacroStat - collecting statistics about a single macro
54 ///
55 /// collector for macro execution statistics,
56 /// such as execution time and number of changed qualifiers
57 /// everything is logged via LOG_POST so all standard logging conventions apply
59 {
60 public:
61  CMacroStat() { Reset(); }
62  /// reset the macro name, qualifier and iteration counts
63  void Reset();
64 
65  class CMacroLog
66  {
67  public:
68  const string& GetName() const { return m_MacroName; }
69  string GetLog() const { return m_Log; }
70 
71  void SetName(const string& name)
72  {
73  if (name.empty()) {
74  m_Log.clear();
75  }
76  m_MacroName.assign(name);
77  }
78 
79  void BuildReport(const string& rep)
80  {
81  if (!m_MacroName.empty()) {
82  m_Log.append(rep);
83  }
84  }
85 
86  void ClearAndBuildReport(const string& rep)
87  {
88  if (!m_MacroName.empty()){
89  m_Log = rep;
90  }
91  }
92 
93  void AppendError(const string& errmsg)
94  {
95  if (!errmsg.empty()) {
96  if (!m_ErrorLog.empty()) {
97  m_ErrorLog += "\n";
98  }
99  m_ErrorLog += errmsg;
100  }
101  }
102 
103  const string& GetErrorLog() const { return m_ErrorLog; }
104 
105  void Reset()
106  {
107  m_MacroName.resize(0);
108  m_Log.resize(0);
109  m_ErrorLog.resize(0);
110  }
111  private:
112  /// name of macro
113  string m_MacroName;
114  /// Logging all activities during this macro
115  string m_Log;
116  /// Logging errors encountered during macro execution
117  string m_ErrorLog;
118  };
119 
120 
121  const CMacroLog& GetMacroReport() const { return m_MacroReport; }
122 
123  /// function gradually builds the log report
124  void AddToReport(const string& report);
125 
126  /// store encountered errors
127  void AddToErrorReport(const string& error);
128 
129  /// Used when applying a table to the entry:
130  /// it stores the values and number of table entries that were not matched during execution
131  void GatherUnMatchedTableEntries(const string& report, const int count);
132 
133  const vector<pair<string, int>>& GetUnmatchedTableEntries() const { return m_UnmatchedList; }
134 
135  /// function to collect whether any data was changed during the execution of the macro
136  void AddDataChanged(bool change);
137  /// function to collect DO function names to be used for processing reporting in huge file mode
138  void AddFunctionName(const string& func_name);
139  const set<string>& GetDOFunctionNames() const { return m_FuncNames; }
140 
141  /// log start of macro execution
142  /// @param macro_name
143  /// name of the macro which is started
144  /// is printed out and memorized until the next Log_Start()/Reset() call
145  void LogStart(const string& macro_name);
146 
147  /// log end of macro execution
148  /// this includes time of execution, status, count of changed qualifiers
149  /// and an error message when applicable
150  /// result also goes to the GBench status bar
151  /// @param status
152  /// true if no errors occured, false otherwise
153  /// @param err_message
154  /// additional error message which is printed when status is false
155  void LogStop(bool status, const string& err_message);
156 
157  long GetExecutionTime() const { return static_cast<long>(m_StopTime - m_StartTime); }
158 
159  size_t GetCounter() const {return m_counter;}
160  bool HasDataUpdated() const { return m_DataUpdated; }
161 
162  void AddFunctionLog(const CRef<IFunctionLog>& log);
163  const vector<CRef<IFunctionLog>>& GetFunctionsLog() const { return m_FunctionsLog; }
164 
165 private:
166  time_t m_StartTime;
167  time_t m_StopTime;
169  size_t m_counter{ 0 };
170 
171  vector<CRef<IFunctionLog>> m_FunctionsLog;
172  bool m_DataUpdated{ false };
173  set<string> m_FuncNames; // DO function names
174 
175  // for storing list of values found in table that were not matched
176  vector<pair<string, int>> m_UnmatchedList;
177 private:
180 };
181 
182 ///////////////////////////////////////////////////////////////////////////////
183 /// IMacroEngine
184 ///
185 /// Provides a generic interface for common high level tasks:
186 /// - parsing single macro into its binary representation
187 /// - parsing library (file) of macros
188 /// - executing macro binary representation
189 /// - getting macro execution statistics
191 {
192 public:
193 
194  /// Parses a file containing macros and appends the results to a map.
195  ///
196  /// @param filename
197  /// Full name (with path) for a file to be parsed.
198  /// @param lib
199  /// The map that internally stores macros
200  /// @return
201  /// - true, if the file is read and parsed successfully
202  /// - false, otherwise.
203  virtual bool AppendToLibrary(const string& filename, CMacroLib::TMacroLibrary& lib) = 0;
204  /// Parses a file containing macros and stores them in a list
205  ///@param[in] filename
206  /// Name of the file to be parsed
207  ///@param[in,out] macro_list
208  /// Map in which the parsed macros to be stored
209  virtual bool ReadAndParseMacros(const string& filename, vector<CRef<CMacroRep>>& macro_list) = 0;
210 
211  /// Parses macro script into its binary representation. This may need to be preprocessed to
212  /// let user resolve "ask" and "choice" variables.
213  virtual CMacroRep* Parse(const string& macro_text) = 0;
214 
215  /// Executes a macro
216  ///@param[in] macro_rep
217  /// Macro in its binary representation. It should be free of unresolved "ask" and "choice" variables
218  ///@param[in] data
219  /// Data (top seq-entry and optionally seq-submit) which will be edited by the macro
220  ///@param[in,out] CmdComposite
221  /// Storage for editing commands
222  ///@param[in] throw_on_error
223  /// Flag, indicating whether to throw exceptions on errors
224  virtual bool Exec(const CMacroRep& macro_rep, const CMacroBioData& data, CRef<CMacroCmdComposite> CmdComposite, bool throw_on_error = false, CNcbiOstream* ostream = nullptr) = 0;
225 
226  /// Gets the most recently executed macro statistics
227  virtual const CMacroStat& GetStatistics() const = 0;
228  virtual ~IMacroEngine() {}
229 };
230 
231 
232 ///////////////////////////////////////////////////////////////////////////////
233 /// CMacroEngine
234 ///
235 /// Provides interfaces for:
236 /// - parsing single macro into its binary representation
237 /// - parsing library (file) of macros
238 /// - executing macro binary representation
239 /// - resolving identifier met in the macro into variable value or biodata object (qualifier) value
240 /// - calling function by name met in the macro.
241 ///
242 /// Additionally, this class keeps the run-time variables that are assigned a value in the Do-clause of a macro.
243 ///
244 /// This class keeps the context for execution. All variables in macro representation are to be resolved by GUI if required.
245 
247 {
248 public:
249  /// Constructor
250  CMacroEngine(bool lookup_synfile = false):
251  m_Status(false),
252  m_HugeFileMode(false),
253  m_ErrorMessage("Not executed"),
254  m_Line(0),
255  m_Column(0) { x_InitSetOfBuiltInFunctions(lookup_synfile); }
256 
257  CMacroEngine(bool huge_file_mode, bool lookup_synfile = false) :
258  m_Status(false),
259  m_HugeFileMode(huge_file_mode),
260  m_ErrorMessage("Not executed"),
261  m_Line(0),
262  m_Column(0) { x_InitSetOfBuiltInFunctions(lookup_synfile); }
263 
264  virtual ~CMacroEngine();
265 
266  /// Parses a file containing macros and appends the results to a map.
267  ///
268  /// @param filename
269  /// Full name (with path) for a file to be parsed.
270  /// @param lib
271  /// The map that internally stores macros
272  /// @return
273  /// - true, if the file is read and parsed successfully
274  /// - false, otherwise.
275  bool AppendToLibrary(const string& filename, CMacroLib::TMacroLibrary& lib);
276  /// Parses a file containing macros and stores them in a list
277  ///@param[in] filename
278  /// Name of the file to be parsed
279  ///@param[in,out] macro_list
280  /// Map in which the parsed macros to be stored
281  bool ReadAndParseMacros(const string& filename, vector<CRef<CMacroRep>>& macro_list);
282 
283  /// Extracts synonym files present in the list of macros
284  vector<string> GetSynonymFilenames(const vector<CRef<CMacroRep>>& macro_list) const;
285 
286  /// Parses macro script into its binary representation. This may need to be preprocessed to
287  /// let user resolve "ask" and "choice" variables.
288  CMacroRep* Parse(const string& macro_text);
289 
290  /// Executes a macro
291  ///@param[in] macro_rep
292  /// Macro in its binary representation. It should be free of unresolved "ask" and "choice" variables
293  ///@param[in] data
294  /// Data (top seq-entry and optionally seq-submit) which will be edited by the macro
295  ///@param[in,out] CmdComposite
296  /// Storage for editing commands
297  ///@param[in] throw_on_error
298  /// Flag, indicating whether to throw exceptions on errors
299  bool Exec(const CMacroRep& macro_rep, const CMacroBioData& data, CRef<CMacroCmdComposite> CmdComposite, bool throw_on_error = false, CNcbiOstream* ostream = nullptr);
300 
301  /// Get status of most recent command
302  bool GetStatus() const { return m_Status; }
303 
304  /// Get error message in case previous command was unsuccessful.
305  const string& GetErrorMessage() const { return m_ErrorMessage; }
306 
307  /// Get error location in case previous command was unsuccessful.
308  /// Numbering starts from 1.
309  /// Horizontal tabs are treated to be of length 1.
310  Uint4 GetErrorLine() const { return (m_Status)? 0 : m_Line; }
311  Uint4 GetErrorColumn() const { return (m_Status)? 0 : m_Column; }
312 
313  /// Gets the most recently executed macro statistics
314  const CMacroStat& GetStatistics() const { return m_MacroStat; }
315 
316  /// get one of the "where" sub-clauses which are indentified by an index in the
317  /// main parsed tree
318  virtual CQueryParseTree* GetAssignmentWhereClause(int index) const;
319 
320  /// Gets or creates run-time vars (used in assignment in Do clause)
321  virtual CRef<CMQueryNodeValue> GetOrCreateRTVar(const string& name);
322 
323  /// Returns true if the run-time variable, identified by its name, was defined
324  virtual bool ExistRTVar(const string& name);
325 
326  /// Temporary run-time variables used in evaluating subqueries in the DO section,
327  /// used together with the Assignment Operator (CMQueryFunctionAssignment)
328  virtual void AddTmpRTVarObject(const string& name, CObjectInfo& oi);
329  virtual bool GetTmpRTVarObject(const string& name, CObjectInfo& oi);
330  virtual void ResetTmpRTVarObjects();
331 
332  void SetRemoteUpdater(const shared_ptr<objects::edit::CRemoteUpdater>& updater) { m_RemoteUpdater = updater; }
333  void SetDOIUpdater(const shared_ptr<CDoiLookupWithCache>& updater) { m_DOIUpdater = updater; }
334  void SetISOJTAUpdater(const shared_ptr<CISOJTALookupWithCache>& updater) { m_ISOJTAUpdater = updater; }
335 
336  void SetHugeContext(const shared_ptr<CHugeMacroContext>& context) { m_MacroContext = context; }
337 
338 private:
339  bool x_AppendToLibrary(const string& filename, CMacroLib::TMacroLibrary& lib);
340 
341  /// Gets the value of RT variable
342  bool x_ResolveRTVar(const string& identifier, CMQueryNodeValue& v, const CQueryParseTree::TNode* parent);
343  /// Gets the pointer to RT variable
344  CRef<CMQueryNodeValue> x_LocateRTVar(const string& identifier);
345 
346  /// Function resolves identifiers
347  virtual bool ResolveIdentifier(const string& identifier, CMQueryNodeValue& v, const CQueryParseTree::TNode* parent) ;
348 
349  /// Function call the function specified by name, passing args as parameters and
350  /// it can update nv as a result.
351  virtual void CallFunction(const string& name, CQueryParseTree::TNode &qnode);
352 
353  /// Reset status and error related private data
354  void x_ResetStatus(void);
355 
356  /// Define the list of functions that is recognized by the engine
357  void x_InitSetOfBuiltInFunctions(bool lookup_synfile);
358 
359  /// Define the list of functions used in string constraints
360  void x_InitStringConstraints(bool lookup_synfile);
361 
362  /// Locates function by name
363  IEditMacroFunction* x_ResolveFunctionName(const string& name) const;
364 
365  /// Sets m_Status, error_message and its location.
366  void x_SetError(const string& message, Uint4 line = 0, Uint4 column = 0);
367 
368  /// Initializes do/where lists of functions in parser.
369  void x_SetFunctions(CMacroParser& parser);
370 
371 private:
372  bool m_Status; ///< execution status of most recent command
373  bool m_HugeFileMode; ///< execution will take place in 'huge file mode'
374  string m_ErrorMessage; ///< error message
375  Uint4 m_Line, m_Column; ///< location of problem in macro text
376  CMacroStat m_MacroStat; ///< Most recent executed macro statistics
377  shared_ptr<objects::edit::CRemoteUpdater> m_RemoteUpdater;
378  shared_ptr<CDoiLookupWithCache> m_DOIUpdater;
379  shared_ptr<CISOJTALookupWithCache> m_ISOJTAUpdater;
380 
381  shared_ptr<CHugeMacroContext> m_MacroContext;
382 
383  struct SKey_Less
384  {
385  bool operator()(const string& str1, const string& str2) const
386  {
387  return NStr::CompareNocase(str1, str2) < 0;
388  }
389  };
390 
392  /// Do section functions
394 
395  /// temporary data available while Exec is working. It is used by resolver.
399 
402 
405 
406  bool m_EvalDo; ///< Flag indicates if the work should be performed on the Do clause.
407 };
408 
409 END_SCOPE(macro)
411 
412 /* @} */
413 
414 #endif // GUI_OBJUTILS___MACRO_ENGINE__HPP
Subclass of the IQueryParseUserObject which is held as the user-defined object in each CQueryParseNod...
Definition: macro_exec.hpp:71
The following asn-selectors are defined to be used in the FOR EACH statement:
CMacroEngine.
Class provides macro language interface for bulk bio-editing.
Class for parsed macro representation.
Definition: macro_rep.hpp:254
CMacroStat - collecting statistics about a single macro.
CObjectInfo –.
Definition: objectinfo.hpp:597
CObject –.
Definition: ncbiobj.hpp:180
Query tree and associated utility methods.
definition of a Culling tree
Definition: ncbi_tree.hpp:100
Base class for any user function that performs editing operations on ASN.1 data.
IMacroEngine.
Interface class for resolving entities during function execution.
Definition: macro_exec.hpp:291
#define false
Definition: bool.h:36
static const char * column
Definition: stats.c:23
char data[12]
Definition: iconv.c:80
vector< CRef< CMacroRep > > TMacroLibrary
Definition: macro_lib.hpp:58
Uint4 GetErrorLine() const
Get error location in case previous command was unsuccessful.
CMacroStat & operator=(const CMacroStat &)
void SetName(const string &name)
shared_ptr< CISOJTALookupWithCache > m_ISOJTAUpdater
virtual bool Exec(const CMacroRep &macro_rep, const CMacroBioData &data, CRef< CMacroCmdComposite > CmdComposite, bool throw_on_error=false, CNcbiOstream *ostream=nullptr)=0
Executes a macro.
CIRef< IMacroBioDataIter > m_DataIter
const set< string > & GetDOFunctionNames() const
const string & GetErrorLog() const
bool m_Status
execution status of most recent command
TRTVarsMap m_RTVars
bool GetStatus() const
Get status of most recent command.
void SetRemoteUpdater(const shared_ptr< objects::edit::CRemoteUpdater > &updater)
long GetExecutionTime() const
bool HasDataUpdated() const
virtual bool ReadAndParseMacros(const string &filename, vector< CRef< CMacroRep >> &macro_list)=0
Parses a file containing macros and stores them in a list.
void AppendError(const string &errmsg)
shared_ptr< objects::edit::CRemoteUpdater > m_RemoteUpdater
void SetISOJTAUpdater(const shared_ptr< CISOJTALookupWithCache > &updater)
string m_ErrorLog
Logging errors encountered during macro execution.
string m_ErrorMessage
error message
TBuiltInFunctionsMap m_BuiltInFunctions
Do section functions.
const string & GetName() const
shared_ptr< CHugeMacroContext > m_MacroContext
CRef< CMacroCmdComposite > m_CmdComposite
const CMacroRep * m_MacroRep
temporary data available while Exec is working. It is used by resolver.
virtual bool AppendToLibrary(const string &filename, CMacroLib::TMacroLibrary &lib)=0
Parses a file containing macros and appends the results to a map.
vector< CRef< IFunctionLog > > m_FunctionsLog
string GetLog() const
void BuildReport(const string &rep)
time_t m_StopTime
CMacroLog m_MacroReport
map< string, IEditMacroFunction *, SKey_Less > TBuiltInFunctionsMap
map< string, CRef< CMQueryNodeValue > > TRTVarsMap
Uint4 GetErrorColumn() const
CMacroStat(const CMacroStat &)
virtual const CMacroStat & GetStatistics() const =0
Gets the most recently executed macro statistics.
shared_ptr< CDoiLookupWithCache > m_DOIUpdater
map< string, CObjectInfo > TTempRTVarsMap
const vector< CRef< IFunctionLog > > & GetFunctionsLog() const
CMacroEngine(bool huge_file_mode, bool lookup_synfile=false)
bool operator()(const string &str1, const string &str2) const
void SetHugeContext(const shared_ptr< CHugeMacroContext > &context)
const CMacroStat & GetStatistics() const
Gets the most recently executed macro statistics.
virtual ~IMacroEngine()
string m_Log
Logging all activities during this macro.
TTempRTVarsMap m_TempRTVars
const vector< pair< string, int > > & GetUnmatchedTableEntries() const
size_t GetCounter() const
CMacroEngine(bool lookup_synfile=false)
Constructor.
bool m_EvalDo
Flag indicates if the work should be performed on the Do clause.
const CMacroLog & GetMacroReport() const
vector< pair< string, int > > m_UnmatchedList
void ClearAndBuildReport(const string &rep)
virtual CMacroRep * Parse(const string &macro_text)=0
Parses macro script into its binary representation.
const string & GetErrorMessage() const
Get error message in case previous command was unsuccessful.
time_t m_StartTime
bool m_HugeFileMode
execution will take place in 'huge file mode'
set< string > m_FuncNames
string m_MacroName
name of macro
void SetDOIUpdater(const shared_ptr< CDoiLookupWithCache > &updater)
CMacroStat m_MacroStat
Most recent executed macro statistics.
uint32_t Uint4
4-byte (32-bit) unsigned integer
Definition: ncbitype.h:103
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define END_SCOPE(ns)
End the previously defined scope.
Definition: ncbistl.hpp:75
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define BEGIN_SCOPE(ns)
Define a new scope.
Definition: ncbistl.hpp:72
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
static int CompareNocase(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n, const char *s2)
Case-insensitive compare of a substring with another string.
Definition: ncbistr.cpp:219
#define NCBI_GUIOBJUTILS_EXPORT
Definition: gui_export.h:512
Defines to provide correct exporting from DLLs in Windows.
Macro library for storing parsed macros.
Macro parser components.
#define count
static CS_CONTEXT * context
Definition: will_convert.c:21
Modified on Wed Sep 04 15:01:15 2024 by modify_doxy.py rev. 669887