Changeset b64313


Ignore:
Timestamp:
Mar 19, 2010, 4:13:28 PM (15 years ago)
Author:
Tillmann Crueger <crueger@…>
Branches:
Action_Thermostats, Add_AtomRandomPerturbation, Add_FitFragmentPartialChargesAction, Add_RotateAroundBondAction, Add_SelectAtomByNameAction, Added_ParseSaveFragmentResults, AddingActions_SaveParseParticleParameters, Adding_Graph_to_ChangeBondActions, Adding_MD_integration_tests, Adding_ParticleName_to_Atom, Adding_StructOpt_integration_tests, AtomFragments, Automaking_mpqc_open, AutomationFragmentation_failures, Candidate_v1.5.4, Candidate_v1.6.0, Candidate_v1.6.1, ChangeBugEmailaddress, ChangingTestPorts, ChemicalSpaceEvaluator, CombiningParticlePotentialParsing, Combining_Subpackages, Debian_Package_split, Debian_package_split_molecuildergui_only, Disabling_MemDebug, Docu_Python_wait, EmpiricalPotential_contain_HomologyGraph, EmpiricalPotential_contain_HomologyGraph_documentation, Enable_parallel_make_install, Enhance_userguide, Enhanced_StructuralOptimization, Enhanced_StructuralOptimization_continued, Example_ManyWaysToTranslateAtom, Exclude_Hydrogens_annealWithBondGraph, FitPartialCharges_GlobalError, Fix_BoundInBox_CenterInBox_MoleculeActions, Fix_ChargeSampling_PBC, Fix_ChronosMutex, Fix_FitPartialCharges, Fix_FitPotential_needs_atomicnumbers, Fix_ForceAnnealing, Fix_IndependentFragmentGrids, Fix_ParseParticles, Fix_ParseParticles_split_forward_backward_Actions, Fix_PopActions, Fix_QtFragmentList_sorted_selection, Fix_Restrictedkeyset_FragmentMolecule, Fix_StatusMsg, Fix_StepWorldTime_single_argument, Fix_Verbose_Codepatterns, Fix_fitting_potentials, Fixes, ForceAnnealing_goodresults, ForceAnnealing_oldresults, ForceAnnealing_tocheck, ForceAnnealing_with_BondGraph, ForceAnnealing_with_BondGraph_continued, ForceAnnealing_with_BondGraph_continued_betteresults, ForceAnnealing_with_BondGraph_contraction-expansion, FragmentAction_writes_AtomFragments, FragmentMolecule_checks_bonddegrees, GeometryObjects, Gui_Fixes, Gui_displays_atomic_force_velocity, ImplicitCharges, IndependentFragmentGrids, IndependentFragmentGrids_IndividualZeroInstances, IndependentFragmentGrids_IntegrationTest, IndependentFragmentGrids_Sole_NN_Calculation, JobMarket_RobustOnKillsSegFaults, JobMarket_StableWorkerPool, JobMarket_unresolvable_hostname_fix, MoreRobust_FragmentAutomation, ODR_violation_mpqc_open, PartialCharges_OrthogonalSummation, PdbParser_setsAtomName, PythonUI_with_named_parameters, QtGui_reactivate_TimeChanged_changes, Recreated_GuiChecks, Rewrite_FitPartialCharges, RotateToPrincipalAxisSystem_UndoRedo, SaturateAtoms_findBestMatching, SaturateAtoms_singleDegree, StoppableMakroAction, Subpackage_CodePatterns, Subpackage_JobMarket, Subpackage_LinearAlgebra, Subpackage_levmar, Subpackage_mpqc_open, Subpackage_vmg, Switchable_LogView, ThirdParty_MPQC_rebuilt_buildsystem, TrajectoryDependenant_MaxOrder, TremoloParser_IncreasedPrecision, TremoloParser_MultipleTimesteps, TremoloParser_setsAtomName, Ubuntu_1604_changes, stable
Children:
760ef4
Parents:
e0b6fd
Message:

Improved Observer Framework to include methods to report the state

  • bool Observable::isBlocked() will tell if the Observer is currently updating itself
  • The Circle detection test now uses ASSERT to report when a circle was detected (can be caught by CPPUNIT)
Location:
src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/Patterns/Observer.cpp

    re0b6fd rb64313  
    1010
    1111#include <iostream>
    12 #include <cassert>
     12
     13#include "Helpers/Assert.hpp"
    1314
    1415using namespace std;
     
    133134    // observers, but still we are called by one of our sub-Observables
    134135    // we cannot be sure observation will still work at this point
    135     cerr << "Circle detected in observation-graph." << endl;
    136     cerr << "Observation-graph always needs to be a DAG to work correctly!" << endl;
    137     cerr << "Please check your observation code and fix this!" << endl;
     136    ASSERT(0,"Circle detected in observation-graph.\n"
     137             "Observation-graph always needs to be a DAG to work correctly!\n"
     138             "Please check your observation code and fix this!\n");
    138139    return;
    139140  }
     
    154155 */
    155156void Observable::signOn(Observer *target,int priority) {
    156   assert(priority>=-20 && priority<=+20 && "Priority out of range [-20:+20]");
     157  ASSERT(priority>=-20 && priority<=+20, "Priority out of range [-20:+20] when signing on Observer");
    157158  bool res = false;
    158159  callees_t *callees = 0;
     
    178179 */
    179180void Observable::signOff(Observer *target) {
    180   assert(callTable.count(this) && "SignOff called for an Observable without Observers.");
     181  ASSERT(callTable.count(this),"SignOff called for an Observable without Observers.");
    181182  callees_t *callees = callTable[this];
    182183  callees_t::iterator iter;
     
    196197}
    197198
     199bool Observable::isBlocked(){
     200  return depth.count(this) > 0;
     201}
     202
    198203/** Handles sub-observables that just got killed
    199204 *  when an sub-observerable dies we usually don't need to do anything
  • src/Patterns/Observer.hpp

    re0b6fd rb64313  
    9898  virtual void signOff(Observer *target);
    9999
     100  /**
     101   * Ask an Observer if it is currently in a blocked state, i.e. if
     102   * Changes are in Progress, that are not yet published.
     103   */
     104  virtual bool isBlocked();
     105
    100106protected:
    101107  virtual void update(Observable *publisher);
     
    122128  static std::set<Observable*> busyObservables;
    123129
     130  //! @cond
    124131  // Structure for RAII-Style notification
    125132protected:
     
    137144    Observable *protege;
    138145  };
     146  //! @endcond
    139147};
    140148
  • src/unittests/ObserverTest.cpp

    re0b6fd rb64313  
    1515#include "Patterns/Observer.hpp"
    1616#include "Patterns/ObservedIterator.hpp"
     17#include "Helpers/Assert.hpp"
    1718
    1819#include <iostream>
     
    6465    i++;
    6566    changeMethod1();
     67  }
     68};
     69
     70class BlockObservable : public Observable {
     71public:
     72  void changeMethod1(){
     73    OBSERVE;
     74    // test if we report correctly as blocked
     75    CPPUNIT_ASSERT(isBlocked());
     76  }
     77
     78  void changeMethod2(){
     79    OBSERVE;
     80    internalMethod1();
     81    internalMethod2();
     82  }
     83
     84  void internalMethod1(){
     85    // we did not block, but our caller did...
     86    // see if this is found
     87    CPPUNIT_ASSERT(isBlocked());
     88  }
     89
     90  void internalMethod2(){
     91    OBSERVE;
     92    // Both this method and the caller do block
     93    // Does the reporting still work as expected?
     94    CPPUNIT_ASSERT(isBlocked());
     95  }
     96
     97  void noChangeMethod(){
     98    // No Block introduced here
     99    // reported correctely?
     100    CPPUNIT_ASSERT(!isBlocked());
    66101  }
    67102};
     
    120155
    121156void ObserverTest::setUp() {
     157  ASSERT_DO(Assert::Throw);
    122158  simpleObservable1 = new SimpleObservable();
    123159  simpleObservable2 = new SimpleObservable();
    124160  callObservable = new CallObservable();
    125161  superObservable = new SuperObservable();
     162  blockObservable = new BlockObservable();
    126163
    127164  observer1 = new UpdateCountObserver();
     
    199236  CPPUNIT_ASSERT_EQUAL( 2, observer1->updates );
    200237  CPPUNIT_ASSERT_EQUAL( 2, observer2->updates );
     238}
     239
     240void ObserverTest::doesReportTest(){
     241  // Actual checks are in the Stub-methods for this
     242  blockObservable->changeMethod1();
     243  blockObservable->changeMethod2();
     244  blockObservable->noChangeMethod();
    201245}
    202246
     
    227271  // make this Observable its own subject. NEVER DO THIS IN ACTUAL CODE
    228272  simpleObservable1->signOn(simpleObservable1);
    229   simpleObservable1->changeMethod();
     273  CPPUNIT_ASSERT_THROW(simpleObservable1->changeMethod(),Assert::AssertionFailure);
    230274
    231275  // more complex test
     
    233277  simpleObservable1->signOn(simpleObservable2);
    234278  simpleObservable2->signOn(simpleObservable1);
    235   simpleObservable1->changeMethod();
     279  CPPUNIT_ASSERT_THROW(simpleObservable1->changeMethod(),Assert::AssertionFailure);
    236280  simpleObservable1->signOff(simpleObservable2);
    237281  simpleObservable2->signOff(simpleObservable1);
  • src/unittests/ObserverTest.hpp

    re0b6fd rb64313  
    1717class SuperObservable;
    1818class ObservableCollection;
     19class BlockObservable;
    1920
    2021
     
    2526  CPPUNIT_TEST ( doesBlockUpdateTest );
    2627  CPPUNIT_TEST ( doesSubObservableTest );
     28  CPPUNIT_TEST ( doesReportTest );
    2729  CPPUNIT_TEST ( iteratorTest );
    2830  CPPUNIT_TEST ( CircleDetectionTest );
     
    3638  void doesBlockUpdateTest();
    3739  void doesSubObservableTest();
     40  void doesReportTest();
    3841  void iteratorTest();
    3942  void CircleDetectionTest();
     
    4851  SimpleObservable *simpleObservable2;
    4952  CallObservable *callObservable;
     53  BlockObservable *blockObservable;
    5054  SuperObservable *superObservable;
    5155
Note: See TracChangeset for help on using the changeset viewer.