User Manual, Developers Guide and API Documentation

SimpleProbe.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002  * This file is part of openWNS (open Wireless Network Simulator)
00003  * _____________________________________________________________________________
00004  *
00005  * Copyright (C) 2004-2007
00006  * Chair of Communication Networks (ComNets)
00007  * Kopernikusstr. 5, D-52074 Aachen, Germany
00008  * phone: ++49-241-80-27910,
00009  * fax: ++49-241-80-22242
00010  * email: info@openwns.org
00011  * www: http://www.openwns.org
00012  * _____________________________________________________________________________
00013  *
00014  * openWNS is free software; you can redistribute it and/or modify it under the
00015  * terms of the GNU Lesser General Public License version 2 as published by the
00016  * Free Software Foundation;
00017  *
00018  * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY
00019  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
00020  * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00021  * details.
00022  *
00023  * You should have received a copy of the GNU Lesser General Public License
00024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00025  *
00026  ******************************************************************************/
00027 
00028 #include <WNS/CppUnit.hpp>
00029 #include <WNS/simulator/ISimulator.hpp>
00030 
00031 #include <WNS/probe/bus/ProbeBusRegistry.hpp>
00032 #include <WNS/probe/bus/ProbeBus.hpp>
00033 #include <WNS/probe/bus/ContextCollector.hpp>
00034 #include <WNS/probe/bus/tests/ProbeBusStub.hpp>
00035 
00036 #include <WNS/pyconfig/Parser.hpp>
00037 #include <WNS/testing/TestTool.hpp>
00038 
00039 namespace wns { namespace probe { namespace bus { namespace examples {
00040 
00041     class SimpleProbe :
00042         public wns::TestFixture
00043     {
00044         CPPUNIT_TEST_SUITE( SimpleProbe );
00045         CPPUNIT_TEST( underTheHood );
00046         CPPUNIT_TEST( theWNSWay );
00047         CPPUNIT_TEST_SUITE_END();
00048     public:
00049         void prepare();
00050         void cleanup();
00051 
00052         void underTheHood();
00053         void theWNSWay();
00054     private:
00055         // Create Empty global context
00056         ContextProviderCollection globalContext_;
00057     };
00058 
00059     // Registered in the Spike() testsuite, so it is not run
00060     // automatically with the other unittests
00061     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( SimpleProbe, wns::testsuite::Spike() );
00062 
00063 }
00064 }
00065 }
00066 }
00067 
00068 using namespace wns::probe::bus::examples;
00069 
00070 void
00071 SimpleProbe::prepare()
00072 {
00073 }
00074 
00075 void
00076 SimpleProbe::cleanup()
00077 {
00078 }
00079 
00080 void
00081 SimpleProbe::underTheHood()
00082 {
00083     // begin example "wns::probe::bus::example::SimpleProbe::underTheHood.example"
00084     // Source and sink are only coupled via a Name
00085     std::string probeBusName = "ThisNameLinksSourceAndSink";
00086 
00087     // Prepare Sink
00088     ProbeBusRegistry* pbr = wns::simulator::getProbeBusRegistry();
00089     // Ask registry for probeBus, implicitly creating a default probeBus (as link) under
00090     // the given name if none is known already
00091     ProbeBus* link = pbr->getMeasurementSource(probeBusName);
00092     // Create the actual sink object
00093     wns::probe::bus::tests::ProbeBusStub sink;
00094     // observe the root of the tree for the given Name
00095     sink.startObserving(link);
00096 
00097     // Prepare Source, it will approach the global ProbeBusRegistry and ask for probeBusName
00098     ContextCollector source(globalContext_, probeBusName);
00099     // end example
00100 
00101     // If everything has worked out as planned, the source will now put values
00102     // into the probebus under name "ThisNameLinksSourceAndSink", the link_
00103     // will then propagate the value to all its children, and consequently into
00104     // our sink_
00105 
00106     // The sink starts up in "vanilla" state
00107     CPPUNIT_ASSERT_EQUAL( 0, sink.receivedCounter );
00108 
00109     // After having connected source and sink, it is child's play. Just trigger
00110     // the forwarding of values and they will be delivered to the source
00111     source.put(42.0);
00112     source.put(43.0);
00113 
00114     // Two values received by sink
00115     CPPUNIT_ASSERT_EQUAL( 2, sink.receivedCounter );
00116     CPPUNIT_ASSERT_EQUAL( 42.0, sink.receivedValues.at(0) );
00117     CPPUNIT_ASSERT_EQUAL( 43.0, sink.receivedValues.at(1) );
00118 
00119     // Tear down
00120     sink.stopObserving(link);
00121 }
00122 
00123 void
00124 SimpleProbe::theWNSWay()
00125 {
00126     // begin example "wns::probe::bus::example::SimpleProbe::theWNSWay.example"
00127     // Config specifies the (joint) name for source and sink and configures
00128     // the sink, in this case a simple statistical evaluation Object (MomentsEval)
00129     wns::pyconfig::View config = wns::pyconfig::Parser::fromString(
00130         "import wns.ProbeBus\n"
00131         "import wns.probe.StatEval\n"
00132         "import wns.WNS\n"
00133         "sim = wns.WNS.WNS()\n"
00134         "probeBusName = 'ThisNameLinksSourceAndSink'\n"
00135         "pbr = sim.environment.probeBusRegistry\n"
00136         "bus = wns.ProbeBus.StatEvalProbeBus('outFile_Mom.dat', wns.probe.StatEval.MomentsEval())\n"
00137         "pbr.getMeasurementSource(probeBusName).addObserver(bus)\n"
00138         );
00139 
00140     // Prepare Source, it will approach the global ProbeBusRegistry and ask for probeBusName
00141     ContextCollector source(globalContext_, config.get<std::string>("probeBusName"));
00142 
00143     // mimic some measurements
00144     source.put(10.0);
00145     source.put(20.0);
00146     source.put(30.0);
00147     // end example
00148 
00149 
00150 
00151     // trigger the output just for testing purposes, normally WNS does this for you at appropriate
00152     // points in time
00153     wns::simulator::getProbeBusRegistry()->forwardOutput();
00154 
00155     // Check output for consistency
00156     std::stringstream fileName;
00157     fileName << wns::simulator::getConfiguration().get<std::string>("outputDir")
00158          << "/outFile_Mom.dat";
00159 
00160     std::vector<std::string> expectations;
00161     expectations.push_back("#.*Trials: 3.*\n");
00162     expectations.push_back("#.*Mean: 20.000*\n");
00163 
00164     CPPUNIT_ASSERT( wns::testing::matchInFile(fileName.str(), expectations) );
00165 }
00166 
00167 

Generated on Fri May 25 03:31:49 2012 for openWNS by  doxygen 1.5.5