Algorithm.cpp 57.7 KB
Newer Older
1
2
3
4
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/Algorithm.h"
5
#include "MantidAPI/AlgorithmHistory.h"
6
#include "MantidAPI/AlgorithmProxy.h"
7
#include "MantidAPI/AnalysisDataService.h"
8
#include "MantidAPI/DeprecatedAlgorithm.h"
9
#include "MantidAPI/AlgorithmManager.h"
10
11
#include "MantidAPI/MemoryManager.h"
#include "MantidAPI/IWorkspaceProperty.h"
12
13
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidAPI/MemoryManager.h"
14

15
#include "MantidKernel/EmptyValues.h"
16
17
#include "MantidKernel/DateAndTime.h"
#include "MantidKernel/MultiThreaded.h"
18
#include "MantidKernel/Strings.h"
19
#include "MantidKernel/Timer.h"
20
21
22
23

#include <boost/algorithm/string/trim.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/regex.hpp>
24
#include <boost/weak_ptr.hpp>
25

26
27
28
#include <Poco/ActiveMethod.h>
#include <Poco/ActiveResult.h>
#include <Poco/NotificationCenter.h>
29
#include <Poco/RWLock.h>
Nick Draper's avatar
Nick Draper committed
30
#include <Poco/StringTokenizer.h>
31
#include <Poco/Void.h>
Nick Draper's avatar
Nick Draper committed
32

33
#include <map>
34
35
36
37
38
39

using namespace Mantid::Kernel;


namespace Mantid
{
40
41
  namespace API
  {
42
43
44
45
46
47
    namespace
    {
      /// Separator for workspace types in workspaceMethodOnTypes member
      const std::string WORKSPACE_TYPES_SEPARATOR = ";";
    }

48
49
50
    // Doxygen can't handle member specialization at the moment: https://bugzilla.gnome.org/show_bug.cgi?id=406027
    // so we have to ignore them
    ///@cond
51
52
53
54
55
56
57
    template <typename NumT>
    bool Algorithm::isEmpty(const NumT toCheck)
    {
      return static_cast<int>(toCheck) == EMPTY_INT();
    }

    template <>
58
    MANTID_API_DLL
59
60
61
62
63
64
    bool Algorithm::isEmpty(const double toCheck)
    {
      return std::abs( (toCheck - EMPTY_DBL())/(EMPTY_DBL()) ) < 1e-8  ;
    }

    // concrete instantiations
65
66
67
    template MANTID_API_DLL bool Algorithm::isEmpty<int> (const int);
    template MANTID_API_DLL bool Algorithm::isEmpty<int64_t> (const int64_t);
    template MANTID_API_DLL bool Algorithm::isEmpty<std::size_t> (const std::size_t);
68
    ///@endcond
69

70
    //=============================================================================================
71
    //================================== Constructors/Destructors =================================
72
73
    //=============================================================================================

74
75
    /// Initialize static algorithm counter
    size_t Algorithm::g_execCount=0;
76

77
78
    /// Constructor
    Algorithm::Algorithm() :
79
    PropertyManagerOwner(),
Gigg, Martyn Anthony's avatar
Gigg, Martyn Anthony committed
80
      m_cancel(false),m_parallelException(false), m_log("Algorithm"), g_log(m_log),
81
82
83
      m_executeAsync(NULL),
      m_notificationCenter(NULL),
      m_progressObserver(NULL),
84
      m_isInitialized(false), m_isExecuted(false),m_isChildAlgorithm(false), m_recordHistoryForChild(false),
85
86
      m_alwaysStoreInADS(false),m_runningAsync(false),
      m_running(false),m_rethrow(false),m_algorithmID(this),
87
      m_singleGroup(-1), m_groupSize(0), m_groupsHaveSimilarNames(false)
88
89
    {
    }
90
91
92

    /// Virtual destructor
    Algorithm::~Algorithm()
93
    {
94
95
96
97
      delete m_notificationCenter;
      delete m_executeAsync;
      delete m_progressObserver;

98
99
      // Free up any memory available.
      Mantid::API::MemoryManager::Instance().releaseFreeMemory();
100
101
102
    }


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
    //=============================================================================================
    //================================== Simple Getters/Setters ===================================
    //=============================================================================================



    //---------------------------------------------------------------------------------------------
    /// Has the Algorithm already been initialized
    bool Algorithm::isInitialized() const
    {
      return m_isInitialized;
    }

    /// Has the Algorithm already been executed
    bool Algorithm::isExecuted() const
    {
      return m_isExecuted;
    }

    //---------------------------------------------------------------------------------------------
    /// Set the Algorithm initialized state
    void Algorithm::setInitialized()
    {
      m_isInitialized = true;
    }

    /** Set the executed flag to the specified state
    // Public in Gaudi - don't know why and will leave here unless we find a reason otherwise
    //     Also don't know reason for different return type and argument.
    @param state :: New executed state
    */
    void Algorithm::setExecuted(bool state)
    {
      m_isExecuted = state;
    }

    //---------------------------------------------------------------------------------------------
    /** To query whether algorithm is a child.
    *  @returns true - the algorithm is a child algorithm.  False - this is a full managed algorithm.
    */
    bool Algorithm::isChild() const
    {
      return m_isChildAlgorithm;
    }

    /** To set whether algorithm is a child.
    *  @param isChild :: True - the algorithm is a child algorithm.  False - this is a full managed algorithm.
    */
    void Algorithm::setChild(const bool isChild)
    {
      m_isChildAlgorithm = isChild;
    }

156
157
158
159
160
161
162
163
164
165
    /**
     * Change the state of the history recording flag. Only applicable for
     * child algorithms. 
     * @param on :: The new state of the flag
     */
     void Algorithm::enableHistoryRecordingForChild(const bool on)
     {
       m_recordHistoryForChild = on;
     }

166
167
168
169
170
171
172
173
174
175
176
    /** Do we ALWAYS store in the AnalysisDataService? This is set to true
     * for python algorithms' child algorithms
     *
     * @param doStore :: always store in ADS
     */
    void Algorithm::setAlwaysStoreInADS(const bool doStore)
    {
      m_alwaysStoreInADS = doStore;
    }


177
178
179
180
181
182
183
184
    /** Set whether the algorithm will rethrow exceptions
     * @param rethrow :: true if you want to rethrow exception.
     */
    void Algorithm::setRethrows(const bool rethrow)
    {
      this->m_rethrow = rethrow;
    }

185
186
187
188
189
190
    /// True if the algorithm is running.
    bool Algorithm::isRunning() const
    {
      Poco::FastMutex::ScopedLock _lock(m_mutex);
      return m_running;
    }
191
192
193
194
195
196
197

    //---------------------------------------------------------------------------------------------
    /**  Add an observer to a notification
    @param observer :: Reference to the observer to add
    */
    void Algorithm::addObserver(const Poco::AbstractObserver& observer)const
    {
198
      notificationCenter().addObserver(observer);
199
200
201
202
203
204
205
    }

    /**  Remove an observer
    @param observer :: Reference to the observer to remove
    */
    void Algorithm::removeObserver(const Poco::AbstractObserver& observer)const
    {
206
      notificationCenter().removeObserver(observer);
207
208
    }

209
210
211
212
213
214
215
216
217
    //---------------------------------------------------------------------------------------------
    /** Sends ProgressNotification.
     * @param p :: Reported progress,  must be between 0 (just started) and 1 (finished)
     * @param msg :: Optional message string
     * @param estimatedTime :: Optional estimated time to completion
     * @param progressPrecision :: optional, int number of digits after the decimal point to show.
    */
    void Algorithm::progress(double p, const std::string& msg, double estimatedTime, int progressPrecision)
    {
218
      notificationCenter().postNotification(new ProgressNotification(this,p,msg, estimatedTime, progressPrecision));
219
220
    }

221
222
223
224
225
    //---------------------------------------------------------------------------------------------
    /// Function to return all of the categories that contain this algorithm
    const std::vector<std::string> Algorithm::categories() const
    {
      std::vector < std::string > res;
226
      Poco::StringTokenizer tokenizer(category(), categorySeparator(),
227
228
          Poco::StringTokenizer::TOK_TRIM | Poco::StringTokenizer::TOK_IGNORE_EMPTY);
      Poco::StringTokenizer::Iterator h = tokenizer.begin();
229
      
230
231
232
233
234
235
236
237
238
239
240
241
242
      for (; h != tokenizer.end(); ++h)
      {
        res.push_back(*h);
      }

      const DeprecatedAlgorithm * depo = dynamic_cast<const DeprecatedAlgorithm *>(this);
      if (depo != NULL)
      {
        res.push_back("Deprecated");
      }
      return res;
    }

243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
    /**
     * @return A string giving the method name that should be attached to a workspace
     */
    const std::string Algorithm::workspaceMethodName() const
    {
      return "";
    }

    /**
     *
     * @return A list of workspace class names that should have the workspaceMethodName attached
     */
    const std::vector<std::string> Algorithm::workspaceMethodOn() const
    {
      Poco::StringTokenizer tokenizer(this->workspaceMethodOnTypes(), WORKSPACE_TYPES_SEPARATOR,
                                      Poco::StringTokenizer::TOK_TRIM | Poco::StringTokenizer::TOK_IGNORE_EMPTY);
      std::vector<std::string> res;
      res.reserve(tokenizer.count());
      for( auto iter = tokenizer.begin(); iter != tokenizer.end(); ++iter )
      {
        res.push_back(*iter);
      }      

      return res;
    }

    /**
     * @return The name of the property that the calling object will be passed to.
     */
    const std::string Algorithm::workspaceMethodInputProperty() const
    {
      return "";
    }

277
278
279
280
281

    //=============================================================================================
    //================================== Initialization ===========================================
    //=============================================================================================

282
    //---------------------------------------------------------------------------------------------
283
    /** Initialization method invoked by the framework. This method is responsible
Nick Draper's avatar
Nick Draper committed
284
285
    *  for any bookkeeping of initialization required by the framework itself.
    *  It will in turn invoke the init() method of the derived algorithm,
286
287
    *  and of any Child Algorithms which it creates.
    *  @throw runtime_error Thrown if algorithm or Child Algorithm cannot be initialised
Nick Draper's avatar
Nick Draper committed
288
289
    *
    */
290
    void Algorithm::initialize()
291
    {
292
293
294
295
296
      // Bypass the initialization if the algorithm has already been initialized.
      if (m_isInitialized) return;

      g_log.setName(this->name());
      try
297
      {
Nick Draper's avatar
Nick Draper committed
298
299
300
301
        try
        {
          this->init();
        }
302
        catch(std::runtime_error&)
Nick Draper's avatar
Nick Draper committed
303
304
305
306
307
308
        {
          throw;
        }

        // Indicate that this Algorithm has been initialized to prevent duplicate attempts.
        setInitialized();
309
      }
310
      catch(std::runtime_error& )
311
312
313
      {
        throw;
      }
314
315
316
      // Unpleasant catch-all! Along with this, Gaudi version catches GaudiException & std::exception
      // but doesn't really do anything except (print fatal) messages.
      catch (...)
317
      {
Nick Draper's avatar
Nick Draper committed
318
319
        // Gaudi: A call to the auditor service is here
        // (1) perform the printout
Gigg, Martyn Anthony's avatar
Gigg, Martyn Anthony committed
320
        getLogger().fatal("UNKNOWN Exception is caught in initialize()");
Nick Draper's avatar
Nick Draper committed
321
        throw;
322
      }
323
324
325

      // Set the documentation. This virtual method is overridden by (nearly) all algorithms and gives documentation summary.
      initDocs();
326
    }
327

328
329
330
331
332
333
334
335
336
337
338
339
340
341
    //---------------------------------------------------------------------------------------------
    /** Perform validation of ALL the input properties of the algorithm.
     * This is to be overridden by specific algorithms.
     * It will be called in dialogs after parsing all inputs and setting the
     * properties, but BEFORE executing.
     *
     * @return a map where: Key = string name of the the property;
                Value = string describing the problem with the property.
     */
    std::map<std::string, std::string> Algorithm::validateInputs()
    {
      return std::map<std::string, std::string>();
    }

342
343
344
345
346
347
348
349
350
    //---------------------------------------------------------------------------------------------
    /** Go through the properties and cache the input/output
     * workspace properties for later use.
     */
    void Algorithm::cacheWorkspaceProperties()
    {
      // Cache the list of the in/out workspace properties
      m_inputWorkspaceProps.clear();
      m_outputWorkspaceProps.clear();
351
      m_pureOutputWorkspaceProps.clear();
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
      const std::vector<Property*>& props = this->getProperties();
      for (size_t i=0; i<props.size(); i++)
      {
        Property* prop = props[i];
        IWorkspaceProperty * wsProp = dynamic_cast<IWorkspaceProperty*>(prop);
        if (wsProp)
        {
          switch(prop->direction())
          {
          case Kernel::Direction::Input:
            m_inputWorkspaceProps.push_back(wsProp);
            break;
          case Kernel::Direction::InOut:
            m_inputWorkspaceProps.push_back(wsProp);
            m_outputWorkspaceProps.push_back(wsProp);
            break;
          case Kernel::Direction::Output:
            m_outputWorkspaceProps.push_back(wsProp);
370
            m_pureOutputWorkspaceProps.push_back(wsProp);
371
372
373
374
375
376
377
378
            break;
          default:
            throw std::logic_error("Unexpected property direction found for property " + prop->name() + " of algorithm " + this->name());
          }
        } // is a ws property
      } // each property
    }

379
380
381
    //=============================================================================================
    //================================== Execution ================================================
    //=============================================================================================
382

383
    //---------------------------------------------------------------------------------------------
384
385
386
387
388
389
    /** Go through the workspace properties of this algorithm
     * and lock the workspaces for reading or writing.
     *
     */
    void Algorithm::lockWorkspaces()
    {
390
391
392
393
      // Do not lock workspace for child algos
      if (this->isChild())
        return;

394
395
396
397
      if (!m_readLockedWorkspaces.empty() || !m_writeLockedWorkspaces.empty())
        throw std::logic_error("Algorithm::lockWorkspaces(): The workspaces have already been locked!");

      // First, Write-lock the output workspaces
Gigg, Martyn Anthony's avatar
Gigg, Martyn Anthony committed
398
      auto & debugLog = g_log.debug();
399
400
401
402
403
      for (size_t i=0; i<m_outputWorkspaceProps.size(); i++)
      {
        Workspace_sptr ws = m_outputWorkspaceProps[i]->getWorkspace();
        if (ws)
        {
404
405
406
407
          // The workspace property says to do locking,
          // AND it has NOT already been write-locked
          if (m_outputWorkspaceProps[i]->isLocking()
              && std::find(m_writeLockedWorkspaces.begin(), m_writeLockedWorkspaces.end(), ws) == m_writeLockedWorkspaces.end())
408
409
          {
            // Write-lock it if not already
Gigg, Martyn Anthony's avatar
Gigg, Martyn Anthony committed
410
            debugLog << "Write-locking " << ws->getName() << std::endl;
411
412
413
414
415
416
417
418
419
420
421
422
            ws->getLock()->writeLock();
            m_writeLockedWorkspaces.push_back(ws);
          }
        }
      }

      // Next read-lock the input workspaces
      for (size_t i=0; i<m_inputWorkspaceProps.size(); i++)
      {
        Workspace_sptr ws = m_inputWorkspaceProps[i]->getWorkspace();
        if (ws)
        {
423
424
425
426
          // The workspace property says to do locking,
          // AND it has NOT already been write-locked
          if (m_inputWorkspaceProps[i]->isLocking()
              && std::find(m_writeLockedWorkspaces.begin(), m_writeLockedWorkspaces.end(), ws) == m_writeLockedWorkspaces.end())
427
428
          {
            // Read-lock it if not already write-locked
Gigg, Martyn Anthony's avatar
Gigg, Martyn Anthony committed
429
            debugLog << "Read-locking " << ws->getName() << std::endl;
430
431
432
433
434
            ws->getLock()->readLock();
            m_readLockedWorkspaces.push_back(ws);
          }
        }
      }
435
    }
436

437
438
439
440
441
442
    //---------------------------------------------------------------------------------------------
    /** Unlock any previously locked workspaces
     *
     */
    void Algorithm::unlockWorkspaces()
    {
443
444
445
      // Do not lock workspace for child algos
      if (this->isChild())
        return;
Gigg, Martyn Anthony's avatar
Gigg, Martyn Anthony committed
446
      auto & debugLog = g_log.debug();
447
448
449
450
      for (size_t i=0; i<m_writeLockedWorkspaces.size(); i++)
      {
        Workspace_sptr ws = m_writeLockedWorkspaces[i];
        if (ws)
451
        {
Gigg, Martyn Anthony's avatar
Gigg, Martyn Anthony committed
452
          debugLog << "Unlocking " << ws->getName() << std::endl;
453
          ws->getLock()->unlock();
454
        }
455
456
457
458
459
      }
      for (size_t i=0; i<m_readLockedWorkspaces.size(); i++)
      {
        Workspace_sptr ws = m_readLockedWorkspaces[i];
        if (ws)
460
        {
Gigg, Martyn Anthony's avatar
Gigg, Martyn Anthony committed
461
          debugLog << "Unlocking " << ws->getName() << std::endl;
462
          ws->getLock()->unlock();
463
        }
464
465
466
467
468
469
470
471
      }

      // Don't double-unlock workspaces
      m_readLockedWorkspaces.clear();
      m_writeLockedWorkspaces.clear();
    }

    //---------------------------------------------------------------------------------------------
472
    /** The actions to be performed by the algorithm on a dataset. This method is
Nick Draper's avatar
Nick Draper committed
473
474
    *  invoked for top level algorithms by the application manager.
    *  This method invokes exec() method.
475
    *  For Child Algorithms either the execute() method or exec() method
Nick Draper's avatar
Nick Draper committed
476
477
    *  must be EXPLICITLY invoked by  the parent algorithm.
    *
478
    *  @throw runtime_error Thrown if algorithm or Child Algorithm cannot be executed
479
    *  @return true if executed successfully.
Nick Draper's avatar
Nick Draper committed
480
    */
481
482
    bool Algorithm::execute()
    {
483
      AlgorithmManager::Instance().notifyAlgorithmStarting(this->getAlgorithmID());
484
485
486
      {
        DeprecatedAlgorithm * depo = dynamic_cast<DeprecatedAlgorithm *>(this);
        if (depo != NULL)
Gigg, Martyn Anthony's avatar
Gigg, Martyn Anthony committed
487
          getLogger().error(depo->deprecationMsg(this));
488
      }
489
490
491
      // Start by freeing up any memory available.
      Mantid::API::MemoryManager::Instance().releaseFreeMemory();

492
      notificationCenter().postNotification(new StartedNotification(this));
493
      Mantid::Kernel::DateAndTime start_time;
494

495
496
497
      // Return a failure if the algorithm hasn't been initialized
      if ( !isInitialized() )
      {
Nick Draper's avatar
Nick Draper committed
498
        throw std::runtime_error("Algorithm is not initialised:" + this->name());
499
      }
500

501
502
503
      // Cache the workspace in/out properties for later use
      cacheWorkspaceProperties();

504
505
      // no logging of input if a child algorithm (except for python child algos)
      if (!m_isChildAlgorithm || m_alwaysStoreInADS) logAlgorithmInfo();
506

507
508
509
      // Check all properties for validity
      if ( !validateProperties() )
      {
Nick Draper's avatar
Nick Draper committed
510
511
512
513
514
515
516
        // Reset name on input workspaces to trigger attempt at collection from ADS
        const std::vector< Property*> &props = getProperties();
        for (unsigned int i = 0; i < props.size(); ++i)
        {
          IWorkspaceProperty *wsProp = dynamic_cast<IWorkspaceProperty*>(props[i]);
          if (wsProp && !(wsProp->getWorkspace()))
          {
517
            // Setting it's name to the same one it already had
Nick Draper's avatar
Nick Draper committed
518
519
520
521
            props[i]->setValue(props[i]->value());
          }
        }
        // Try the validation again
522
523
        if ( !validateProperties() )
        {
524
           notificationCenter().postNotification(new ErrorNotification(this,"Some invalid Properties found"));
525
526
           throw std::runtime_error("Some invalid Properties found");
        }
527
      }
Nick Draper's avatar
Nick Draper committed
528

529
530
531
532
      // ----- Perform validation of the whole set of properties -------------
      std::map<std::string, std::string> errors = this->validateInputs();
      if (!errors.empty())
      {
533
        size_t numErrors = errors.size();
534
        // Log each issue
Gigg, Martyn Anthony's avatar
Gigg, Martyn Anthony committed
535
536
        auto & errorLog = getLogger().error();
        auto & warnLog = getLogger().warning();
537
        for (auto it = errors.begin(); it != errors.end(); it++)
538
        {
539
          if (this->existsProperty(it->first))
Gigg, Martyn Anthony's avatar
Gigg, Martyn Anthony committed
540
            errorLog << "Invalid value for " << it->first << ": " << it->second << "\n";
541
542
543
          else
          {
            numErrors -= 1; // don't count it as an error
Gigg, Martyn Anthony's avatar
Gigg, Martyn Anthony committed
544
545
            warnLog << "validateInputs() references non-existant property \""
                    << it->first << "\"\n";
546
547
          }
        }
548
        // Throw because something was invalid
549
550
        if (numErrors > 0)
        {
551
          notificationCenter().postNotification(new ErrorNotification(this,"Some invalid Properties found"));
552
553
          throw std::runtime_error("Some invalid Properties found");
        }
554
      }
555

556
      // ----- Check for processing groups -------------
Russell Taylor's avatar
Russell Taylor committed
557
      // default true so that it has the right value at the check below the catch block should checkGroups throw
558
      bool callProcessGroups = true;
559
      try
560
      {
561
        // Checking the input is a group. Throws if the sizes are wrong
562
563
        callProcessGroups = this->checkGroups();
        if (callProcessGroups)
Nick Draper's avatar
Nick Draper committed
564
        {
565
566
567
568
          // This calls this->execute() again on each member of the group.
          return processGroups();
        }
      }
569
      catch(std::exception& ex)
570
      {
Gigg, Martyn Anthony's avatar
Gigg, Martyn Anthony committed
571
572
        getLogger().error() << "Error in execution of algorithm "<< this->name() << std::endl
                      << ex.what()<<std::endl;
573
        notificationCenter().postNotification(new ErrorNotification(this,ex.what()));
574
575
576
577
578
579
580
        m_running = false;
        if (m_isChildAlgorithm || m_runningAsync || m_rethrow)
        {
          m_runningAsync = false;
          throw;
        }
      }
581
582
      // If checkGroups() threw an exception but there ARE group workspaces
      // (means that the group sizes were incompatible)
583
      if (callProcessGroups)
584
        return false;
585

586
      // Read or write locks every input/output workspace
587
      this->lockWorkspaces();
Janik Zikovsky's avatar
Janik Zikovsky committed
588

589
590
591
      // Invoke exec() method of derived class and catch all uncaught exceptions
      try
      {
Nick Draper's avatar
Nick Draper committed
592
593
        try
        {
594
          if (!isChild()) 
595
          { 
596
            Poco::FastMutex::ScopedLock _lock(m_mutex);
597
            m_running = true;
598
          }
599
600
          
          
601
          if(trackingHistory())
602
603
604
605
          {
            // count used for defining the algorithm execution order
            // If history is being recorded we need to count this as a separate algorithm
            // as the history compares histories by their execution number
606
            ++Algorithm::g_execCount;
607
608
609
610
            
            //populate history record before execution so we can record child algorithms in it
            AlgorithmHistory algHist(this);
            m_history = boost::make_shared<AlgorithmHistory>(algHist);
611
          }
612

613
          start_time = Mantid::Kernel::DateAndTime::getCurrentTime();
Nick Draper's avatar
Nick Draper committed
614
615
616
617
          // Start a timer
          Timer timer;
          // Call the concrete algorithm's exec method
          this->exec();
618
619
          // Check for a cancellation request in case the concrete algorithm doesn't
          interruption_point();			
Nick Draper's avatar
Nick Draper committed
620
621
          // Get how long this algorithm took to run
          const float duration = timer.elapsed();
622

Nick Draper's avatar
Nick Draper committed
623
          // need it to throw before trying to run fillhistory() on an algorithm which has failed
624
625
626
627
          if(trackingHistory() && m_history)
          { 
            m_history->addExecutionInfo(start_time, duration);
            m_history->setExecCount(Algorithm::g_execCount);
628
            fillHistory();
629
          }
630
631
632

          // Put any output workspaces into the AnalysisDataService - if this is not a child algorithm
          if (!isChild() || m_alwaysStoreInADS)
Nick Draper's avatar
Nick Draper committed
633
634
635
636
            this->store();

          // RJT, 19/3/08: Moved this up from below the catch blocks
          setExecuted(true);
637
          if (!m_isChildAlgorithm || m_alwaysStoreInADS)
Nick Draper's avatar
Nick Draper committed
638
          {
Gigg, Martyn Anthony's avatar
Gigg, Martyn Anthony committed
639
            getLogger().notice() << name() << " successful, Duration "
Nick Draper's avatar
Nick Draper committed
640
641
              << std::fixed << std::setprecision(2) << duration << " seconds" << std::endl;
          }
642
          else
Gigg, Martyn Anthony's avatar
Gigg, Martyn Anthony committed
643
            getLogger().debug() << name() << " finished with isChild = " << isChild() << std::endl;
Nick Draper's avatar
Nick Draper committed
644
645
646
647
          m_running = false;
        }
        catch(std::runtime_error& ex)
        {
648
          this->unlockWorkspaces();
Nick Draper's avatar
Nick Draper committed
649
650
651
          if (m_isChildAlgorithm || m_runningAsync || m_rethrow) throw;
          else
          {
Gigg, Martyn Anthony's avatar
Gigg, Martyn Anthony committed
652
653
            getLogger().error() << "Error in execution of algorithm "<< this->name()<<std::endl
                          << ex.what()<<std::endl;
Nick Draper's avatar
Nick Draper committed
654
          }
655
          notificationCenter().postNotification(new ErrorNotification(this,ex.what()));
Nick Draper's avatar
Nick Draper committed
656
657
658
659
          m_running = false;
        }
        catch(std::logic_error& ex)
        {
660
          this->unlockWorkspaces();
Nick Draper's avatar
Nick Draper committed
661
662
663
          if (m_isChildAlgorithm || m_runningAsync || m_rethrow) throw;
          else
          {
Gigg, Martyn Anthony's avatar
Gigg, Martyn Anthony committed
664
665
            getLogger().error() << "Logic Error in execution of algorithm "<< this->name() << std::endl
                          << ex.what()<<std::endl;
Nick Draper's avatar
Nick Draper committed
666
          }
667
          notificationCenter().postNotification(new ErrorNotification(this,ex.what()));
Nick Draper's avatar
Nick Draper committed
668
669
          m_running = false;
        }
670
671
672
      }
      catch(CancelException& ex)
      {
Nick Draper's avatar
Nick Draper committed
673
674
        m_runningAsync = false;
        m_running = false;
Gigg, Martyn Anthony's avatar
Gigg, Martyn Anthony committed
675
        getLogger().error() << this->name() << ": Execution terminated by user.\n";
676
        notificationCenter().postNotification(new ErrorNotification(this,ex.what()));
677
        this->unlockWorkspaces();
Nick Draper's avatar
Nick Draper committed
678
        throw;
679
680
681
682
      }
      // Gaudi also specifically catches GaudiException & std:exception.
      catch (std::exception& ex)
      {
Nick Draper's avatar
Nick Draper committed
683
684
685
686
        setExecuted(false);
        m_runningAsync = false;
        m_running = false;

687
        notificationCenter().postNotification(new ErrorNotification(this,ex.what()));
Gigg, Martyn Anthony's avatar
Gigg, Martyn Anthony committed
688
689
        getLogger().error() << "Error in execution of algorithm " << this->name() << ":\n"
                      << ex.what() << "\n";
690
        this->unlockWorkspaces();
Nick Draper's avatar
Nick Draper committed
691
        throw;
692
      }
693

694
695
      catch (...)
      {
696
        // Execution failed
Nick Draper's avatar
Nick Draper committed
697
698
699
700
        setExecuted(false);
        m_runningAsync = false;
        m_running = false;

701
        notificationCenter().postNotification(new ErrorNotification(this,"UNKNOWN Exception is caught in exec()"));
Gigg, Martyn Anthony's avatar
Gigg, Martyn Anthony committed
702
        getLogger().error() << this->name() << ": UNKNOWN Exception is caught in exec()\n";
703
        this->unlockWorkspaces();
Nick Draper's avatar
Nick Draper committed
704
        throw;
705
      }
706

707
708
709
      // Unlock the locked workspaces
      this->unlockWorkspaces();

710
      notificationCenter().postNotification(new FinishedNotification(this,isExecuted()));
711
      // Only gets to here if algorithm ended normally
712
713
      // Free up any memory available.
      Mantid::API::MemoryManager::Instance().releaseFreeMemory();
714
715
      return isExecuted();
    }
716

717
    //---------------------------------------------------------------------------------------------
718
    /** Execute as a Child Algorithm.
719
     * This runs execute() but catches errors so as to log the name
720
     * of the failed Child Algorithm, if it fails.
721
     */
722
    void Algorithm::executeAsChildAlg()
723
724
725
726
727
728
729
730
731
732
733
734
735
    {
      bool executed = false;
      try
      {
        executed = execute();
      }
      catch (std::runtime_error&)
      {
        throw;
      }

      if ( ! executed )
      {
736
        throw std::runtime_error("Unable to successfully run ChildAlgorithm " + this->name());
737
738
739
      }
    }

740
    //---------------------------------------------------------------------------------------------
741
742
743
744
    /** Stores any output workspaces into the AnalysisDataService
    *  @throw std::runtime_error If unable to successfully store an output workspace
    */
    void Algorithm::store()
745
    {
746
      const std::vector< Property*> &props = getProperties();
747
748
749
			std::vector<int> groupWsIndicies;

      //add any regular/child workspaces first, then add the groups
750
751
752
753
      for (unsigned int i = 0; i < props.size(); ++i)
      {
        IWorkspaceProperty *wsProp = dynamic_cast<IWorkspaceProperty*>(props[i]);
        if (wsProp)
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
        { 
					//check if the workspace is a group, if so remember where it is and add it later
					auto group = boost::dynamic_pointer_cast<WorkspaceGroup>( wsProp->getWorkspace() );
					if ( !group )
					{
						try
						{
							wsProp->store();
						}
						catch (std::runtime_error&)
						{
							throw;
						}
					}
					else
					{
						groupWsIndicies.push_back(i);
					}
772
773
        }
      }
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791

			//now store workspace groups once their members have been added
			std::vector<int>::const_iterator wsIndex;
			for (wsIndex = groupWsIndicies.begin(); wsIndex != groupWsIndicies.end(); ++wsIndex)
			{
				IWorkspaceProperty *wsProp = dynamic_cast<IWorkspaceProperty*>(props[*wsIndex]);
				if(wsProp)
				{
					try
					{
						wsProp->store();
					}
					catch (std::runtime_error&)
					{
						throw;
					}
				}
			}
792
    }
793

794
    //---------------------------------------------------------------------------------------------
795
    /** Create a Child Algorithm.  A call to this method creates a child algorithm object.
Nick Draper's avatar
Nick Draper committed
796
797
798
799
    *  Using this mechanism instead of creating daughter
    *  algorithms directly via the new operator is prefered since then
    *  the framework can take care of all of the necessary book-keeping.
    *
800
    *  @param name ::           The concrete algorithm class of the Child Algorithm
801
802
803
804
    *  @param startProgress ::  The percentage progress value of the overall algorithm where this child algorithm starts
    *  @param endProgress ::    The percentage progress value of the overall algorithm where this child algorithm ends
    *  @param enableLogging ::  Set to false to disable logging from the child algorithm
    *  @param version ::        The version of the child algorithm to create. By default gives the latest version.
805
    *  @param recordHistory ::  Set whether to record history for children. By default this is set to false.
806
    *  @return shared pointer to the newly created algorithm object
Nick Draper's avatar
Nick Draper committed
807
    */
808
    Algorithm_sptr Algorithm::createChildAlgorithm(const std::string& name, const double startProgress, const double endProgress,
809
      const bool enableLogging, const int& version)
810
    {
811
      Algorithm_sptr alg = AlgorithmManager::Instance().createUnmanaged(name,version);
812
813
814
      //set as a child
      alg->setChild(true);
      alg->setLogging(enableLogging);
815

816
      // Initialise the Child Algorithm
817
      try
818
      {
Nick Draper's avatar
Nick Draper committed
819
        alg->initialize();
820
      }
821
      catch (std::runtime_error&)
822
      {
Gigg, Martyn Anthony's avatar
Gigg, Martyn Anthony committed
823
        throw std::runtime_error("Unable to initialise Child Algorithm '" + name + "'");
824
      }
825
826
827
828

      // If output workspaces are nameless, give them a temporary name to satisfy validator
      const std::vector< Property*> &props = alg->getProperties();
      for (unsigned int i = 0; i < props.size(); ++i)
829
      {
Nick Draper's avatar
Nick Draper committed
830
831
832
833
        if (props[i]->direction() == 1 && dynamic_cast<IWorkspaceProperty*>(props[i]) )
        {
          if ( props[i]->value().empty() ) props[i]->setValue("ChildAlgOutput");
        }
834
      }
835
836

      if (startProgress >= 0 && endProgress > startProgress && endProgress <= 1.)
837
      {
838
        alg->addObserver(this->progressObserver());
Nick Draper's avatar
Nick Draper committed
839
840
        m_startChildProgress = startProgress;
        m_endChildProgress = endProgress;
841
842
      }

843
844
      // Before we return the shared pointer, use it to create a weak pointer and keep that in a vector.
      // It will be used this to pass on cancellation requests
845
      // It must be protected by a critical block so that Child Algorithms can run in parallel safely.
846
      boost::weak_ptr<IAlgorithm> weakPtr(alg);
847
848
      PARALLEL_CRITICAL(Algorithm_StoreWeakPtr)
      {
849
        m_ChildAlgorithms.push_back(weakPtr);
850
      }
Nick Draper's avatar
Nick Draper committed
851

852
853
      return alg;
    }
854

855
856
857
    //=============================================================================================
    //================================== Algorithm History ========================================
    //=============================================================================================
858
859


860
861
862
863
864
    /**
     * Serialize this object to a string. The format is
     * AlgorithmName.version(prop1=value1,prop2=value2,...)
     * @returns This object serialized as a string 
     */
865
    std::string Algorithm::toString() const
866
867
868
869
    {
      std::ostringstream writer;
      writer << name() << "." << this->version() 
        << "("
870
        << Kernel::PropertyManagerOwner::asString(false)
871
872
873
874
        << ")";
      return writer.str();
    }

875
876

    //--------------------------------------------------------------------------------------------
877
878
879
880
881
882
883
    /** Construct an object from a history entry.
     *
     * This creates the algorithm and sets all of its properties using the history.
     *
     * @param history :: AlgorithmHistory object
     * @return a shared pointer to the created algorithm.
     */
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
    IAlgorithm_sptr Algorithm::fromHistory(const AlgorithmHistory & history)
    {
      // Hand off to the string creator
      std::ostringstream stream;
      stream << history.name() << "." << history.version()
       << "(";
      const std::vector<Kernel::PropertyHistory>& props = history.getProperties();
      const size_t numProps(props.size());
      for( size_t i = 0 ; i < numProps; ++i )
      {
        const Kernel::PropertyHistory & prop = props[i];
        if( !prop.isDefault() )
        {
          stream << prop.name() << "=" << prop.value();
        }
        if( i < numProps - 1 ) stream << ",";
      }
      stream << ")";
      return Algorithm::fromString(stream.str());
    }


906
    //--------------------------------------------------------------------------------------------
907
908
    /** De-serializes the algorithm from a string
     *
909
    * @param input :: An input string in the format. The format is
910
911
912
    *        AlgorithmName.version(prop1=value1,prop2=value2,...). If .version is not found the
    *        highest found is used.
    * @return A pointer to a managed algorithm object
913
914
915
916
917
918
919
920
921
922
    */
    IAlgorithm_sptr Algorithm::fromString(const std::string & input)
    {
      static const boost::regex nameExp("(^[[:alnum:]]*)");
      // Double back slash gets rid of the compiler warning about unrecognized escape sequence
      static const boost::regex versExp("\\.([[:digit:]]+)\\(*");
      // Property regex. Simply match the brackets and split
      static const boost::regex propExp("\\((.*)\\)");
      // Name=Value regex
      static const boost::regex nameValExp("(.*)=(.*)");
923
924
925
      // Property name regex
      static const boost::regex propNameExp(".*,([[:word:]]*)");
      // Empty dividers
Doucet, Mathieu's avatar
Doucet, Mathieu committed
926
       static const boost::regex emptyExp(",[ ,]*,");
Doucet, Mathieu's avatar
Doucet, Mathieu committed
927
928
      // Trailing commas
      static const boost::regex trailingCommaExp(",$");
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947

      boost::match_results<std::string::const_iterator> what;
      if( boost::regex_search(input, what, nameExp, boost::match_not_null) )
      {
        const std::string algName = what[1];
        int version = -1;  // Highest version
        if( boost::regex_search(input, what, versExp, boost::match_not_null) )
        {
          try
          {
            version = boost::lexical_cast<int, std::string>(what.str(1));
          }
          catch(boost::bad_lexical_cast&)
          {
          }
        }
        IAlgorithm_sptr alg = AlgorithmManager::Instance().create(algName, version);
        if(  boost::regex_search(input, what, propExp, boost::match_not_null) )
        {
948
949
          std::string _propStr = what[1];

Doucet, Mathieu's avatar
Doucet, Mathieu committed
950
          // Cleanup: Remove empty dividers (multiple commas)
Doucet, Mathieu's avatar
Doucet, Mathieu committed
951
          std::string __propStr = regex_replace(_propStr, emptyExp, "," );
Doucet, Mathieu's avatar
Doucet, Mathieu committed
952
953
          // Cleanup: We might still be left with a trailing comma, remove it
          std::string propStr = regex_replace(__propStr, trailingCommaExp, "" );
954
955
956
957
958

          boost::match_flag_type flags = boost::match_not_null;
          std::string::const_iterator start, end;
          start = propStr.begin();
          end = propStr.end();
959
960
          // Accumulate them first so that we can set some out of order
          std::map<std::string, std::string> propNameValues;
961
          while(boost::regex_search(start, end, what, nameValExp, flags))
962
          {
963
964
965
966
967
            std::string nameValue = what.str(1);
            std::string value = what.str(2);

            if(  boost::regex_search(what[1].first, what[1].second,
                 what, propNameExp, boost::match_not_null) )
968
            {
969
              const std::string name = what.str(1);
970
              propNameValues[name] = value;
971
972
973
              end = what[1].first-1;
            } else {
              // The last property-value pair
974
              propNameValues[nameValue] = value;
975
              break;
976
            }
977
978
979
            // update flags:
            flags |= boost::match_prev_avail;
            flags |= boost::match_not_bob;
980
          }
981
982
983
984
985
986
987
988
989
990
991
992

          // Some algorithms require Filename to be set first do that here
          auto it = propNameValues.find("Filename");
          if(it != propNameValues.end())
          {
            alg->setPropertyValue(it->first, it->second);
            propNameValues.erase(it);
          }
          for(auto cit = propNameValues.begin(); cit != propNameValues.end(); ++cit)
          {
            alg->setPropertyValue(cit->first, cit->second);
          }
993
994
995
996
997
998
999
1000
        }
        return alg;
      }
      else
      {
        throw std::runtime_error("Cannot create algorithm, invalid string format.");
      }
    }
For faster browsing, not all history is shown. View entire blame