/** * ProcessQueue is called whenever there is a timer interrupt. * We need to wake up and process the current top item in the timer queue as long * as its scheduled time is after the current time. Then the item is removed or * rescheduled (repetitive events) in the queue. */ void Notifier::ProcessQueue(uint32_t mask, void *params) { Notifier *current; while (true) // keep processing past events until no more { { Synchronized sync(queueSemaphore); double currentTime = GetClock(); current = timerQueueHead; if (current == NULL || current->m_expirationTime > currentTime) { break; // no more timer events to process } // need to process this entry timerQueueHead = current->m_nextEvent; if (current->m_periodic) { // if periodic, requeue the event // compute when to put into queue current->InsertInQueue(true); } else { // not periodic; removed from queue current->m_queued = false; } // Take handler semaphore while holding queue semaphore to make sure // the handler will execute to completion in case we are being deleted. semTake(current->m_handlerSemaphore, WAIT_FOREVER); } current->m_handler(current->m_param); // call the event handler semGive(current->m_handlerSemaphore); } // reschedule the first item in the queue Synchronized sync(queueSemaphore); UpdateAlarm(); }
void MasterAdapter::removePending(Notifier& notifier) { /* * removing all jobs from the queue and returning an error. * error handling is don in the servicebroker thread */ Job* job = mQueue.pop(); while( job ) { job->status = EIO; notifier.jobFinished(*job); job = mQueue.pop(); } }
TEST( Notifier, TestOfNotificationChangesStack ) { Notifier notifier; TestClazz testClazz; notifier.addNotification( getNotificationTest1(), Utils::makeCallback( &testClazz, &TestClazz::onDeliverNotification ) ); notifier.addNotification( getNotificationTest2(), Utils::makeCallback( &testClazz, &TestClazz::onDeliverNotification ) ); EXPECT_EQ( 0, testClazz.deliveredCount ); notifier.notify( getNotificationTest1() ); EXPECT_EQ( 1, testClazz.deliveredCount ); notifier.removeAllForObject( &testClazz ); notifier.removeAllForObject( &testClazz ); notifier.notify( getNotificationTest3(), 0x01 ); }
TEST( Notifier, TestOfNotification ) { Notifier notifier; TestClazz testClazz; notifier.addNotification( getNotificationTest1(), Utils::makeCallback( &testClazz, &TestClazz::onDeliverNotification ) ); EXPECT_EQ( 0, testClazz.deliveredCount ); notifier.notify( getNotificationTest1() ); EXPECT_EQ( 1, testClazz.deliveredCount ); notifier.notify( getNotificationTest1() ); notifier.notify( getNotificationTest1() ); EXPECT_EQ( 3, testClazz.deliveredCount ); notifier.removeAllForObject( &testClazz ); notifier.notify( getNotificationTest1() ); EXPECT_EQ( 3, testClazz.deliveredCount ); notifier.addNotification( getNotificationTest1(), Utils::makeCallback( &testClazz, &TestClazz::onDeliverNotification ) ); EXPECT_EQ( 3, testClazz.deliveredCount ); notifier.notify( getNotificationTest1() ); EXPECT_EQ( 4, testClazz.deliveredCount ); notifier.removeNotification( &testClazz, getNotificationTest1() ); notifier.notify( getNotificationTest1() ); EXPECT_EQ( 4, testClazz.deliveredCount ); ////////////////////////////////////////////////////////////////////////////////////// notifier.addNotification( getNotificationTest1(), Utils::makeCallback( &testClazz, &TestClazz::onDeliverNotification ) ); notifier.addNotification( getNotificationTest2(), Utils::makeCallback( &testClazz, &TestClazz::onDeliverNotification ) ); notifier.notify( getNotificationTest1() ); EXPECT_EQ( 5, testClazz.deliveredCount ); notifier.notify( getNotificationTest2() ); EXPECT_EQ( 6, testClazz.deliveredCount ); notifier.removeNotification( &testClazz, getNotificationTest1() ); notifier.notify( getNotificationTest1() ); EXPECT_EQ( 6, testClazz.deliveredCount ); notifier.notify( getNotificationTest2() ); EXPECT_EQ( 7, testClazz.deliveredCount ); notifier.addNotification( getNotificationTest1(), Utils::makeCallback( &testClazz, &TestClazz::onDeliverNotification ) ); notifier.notify( getNotificationTest1() ); EXPECT_EQ( 8, testClazz.deliveredCount ); notifier.removeAllForObject( &testClazz ); notifier.notify( getNotificationTest1() ); EXPECT_EQ( 8, testClazz.deliveredCount ); }
TestClazzReleased( Notifier& notifier ) : m_pNotifier( notifier ) { notifier.addNotification( getNotificationTestUseAfterRelease(), Utils::makeCallback( this, &TestClazzReleased::onCall ) ); }
TEST( Notifier, DISABLED_TestOfNotificationChangesStackDoubleCommands ) { Notifier notifier; TestClazz testClazz; notifier.addNotification( getNotificationTest1(), Utils::makeCallback( &testClazz, &TestClazz::onDeliverNotification ) ); notifier.addNotification( getNotificationTest2(), Utils::makeCallback( &testClazz, &TestClazz::onDeliverNotification ) ); EXPECT_EQ( 0, testClazz.deliveredCount ); notifier.notify( getNotificationTest1() ); EXPECT_EQ( 1, testClazz.deliveredCount ); notifier.removeAllForObject( &testClazz ); notifier.removeAllForObject( &testClazz ); notifier.addNotification( getNotificationTest1(), Utils::makeCallback( &testClazz, &TestClazz::onDeliverNotification ) ); notifier.addNotification( getNotificationTest2(), Utils::makeCallback( &testClazz, &TestClazz::onDeliverNotification ) ); notifier.notify( getNotificationTest3(), 0x01 ); EXPECT_EQ( 1, testClazz.deliveredCount ); notifier.notify( getNotificationTest1() ); EXPECT_EQ( 2, testClazz.deliveredCount ); notifier.addNotification( getNotificationTest1(), Utils::makeCallback( &testClazz, &TestClazz::onDeliverNotification ) ); //Now we have double notification notifier.notify( getNotificationTest1() ); EXPECT_EQ( 4, testClazz.deliveredCount ); notifier.removeNotification( &testClazz, getNotificationTest1() ); notifier.notify( getNotificationTest1() ); EXPECT_EQ( 4, testClazz.deliveredCount ); }
void SelectPoll::notify() { notifier_.notify(); }
int main(){ // { // const std::string mAlign = "xxx,<><,--x"; // const int mRepeatRow = 1; // // int row = 0; // int ibeg=0, iend=0; // for(int i=0; i<mAlign.size(); ++i){ // if(mAlign[i] == ','){ // ++row; // // } // } // } #define SET4(x, a,b,c,d) x[0]=a; x[1]=b; x[2]=c; x[3]=d #define EQ4(x, a,b,c,d) (x[0]==a && x[1]==b && x[2]==c && x[3]==d) // Multidimensional data array { // basics { const int s1=5, s2=4, s3=3, s4=2; Data d(Data::INT, s1,s2,s3,s4); assert(d.hasData()); assert(d.type() == Data::INT); assert(d.sizeType() == sizeof(int)); assert(d.isNumerical()); assert(d.offset() == 0); assert(d.order() == 4); assert(d.stride() == 1); assert(d.size(0) == s1); assert(d.size(1) == s2); assert(d.size(2) == s3); assert(d.size(3) == s4); assert(d.size(0,1) == s1*s2); assert(d.size(0,1,2) == s1*s2*s3); assert(d.size(0,1,2,3) == d.size()); assert(d.indexFlat(0,0,0,0) == 0); assert(d.indexFlat(1,0,0,0) == 1); assert(d.indexFlat(0,1,0,0) == s1*1); assert(d.indexFlat(0,2,0,0) == s1*2); assert(d.indexFlat(0,0,1,0) == s2*s1*1); assert(d.indexFlat(0,0,2,0) == s2*s1*2); assert(d.indexFlat(0,0,0,1) == s3*s2*s1*1); assert(d.indexFlat(0,0,0,2) == s3*s2*s1*2); for(int i=0; i<d.size(); ++i){ int i1,i2,i3,i4; d.indexDim(i1,i2,i3,i4, i); assert(d.inBounds(i1,i2,i3,i4)); assert(d.indexFlat(i1,i2,i3,i4) == i); } for(int i=0; i<d.size(); ++i) d.assign(i, i); for(int i=0; i<d.size(); ++i) assert(d.elem<int>(i) == i); // resizing { Data e(Data::INT, 8); int N; // resize change, in bytes for(int i=0; i<e.size(); ++i) e.assign(i,i); N=e.resize(16); assert(e.sizeType()*8==N); // old elements should be copied over; extras should be zero for(int i=0; i<8 ; ++i) assert(e.at<int>(i) == i); for(int i=8; i<16; ++i) assert(e.at<int>(i) == 0); N=e.resize(0); assert(e.sizeType()*-16==N); N=e.resize(8); assert(e.sizeType()* 8==N); for(int i=0; i<e.size(); ++i) assert(e.at<int>(i) == 0); } // cloning data { Data e = d; assert(e == d); e.clone(); assert(d.elems<int>() != e.elems<int>()); assert(e == d); } // reversed slice { Data e = d.reversed(); for(int i=0; i<e.size(); ++i) assert(d.elem<int>(d.size()-1-i) == e.elem<int>(i)); e.clone(); for(int i=0; i<e.size(); ++i) assert(d.elem<int>(d.size()-1-i) == e.elem<int>(i)); e += d; for(int i=0; i<e.size(); ++i) assert(e.elem<int>(i) == e.size()-1); } } // reference counting { Data d1(Data::INT, 4,4); assert(Data::references(d1.elems<int>()) == 1); { Data d2 = d1; assert(d1.elems<int>() == d2.elems<int>()); assert(Data::references(d1.elems<int>()) == 2); } assert(Data::references(d1.elems<int>()) == 1); { Data d2 = d1.slice(1); Data d3 = d2.slice(1); assert(Data::references(d1.elems<int>()) == 3); } assert(Data::references(d1.elems<int>()) == 1); } const float cf = 10; float f = 10; std::string s = "hello"; Data d(cf); assert(d.hasData()); assert(d.size() == 1); assert(d.type() == Data::FLOAT); assert(d.at<float>(0) == cf); assert(d.toString() == "10"); assert(d.at<std::string>(0) == "10"); // d.put(std::string("8")); // assert(d.at<float>(0) == 8); // Getting/setting values d.assign(11.f); assert(d.at<float>(0) == 11.f); d.clone(); assert(d.hasData()); assert(d.size() == 1); assert(d.type() == Data::FLOAT); d.assign(11.f); assert(d.at<float>(0) == 11.f); d.assign(100); assert(d.at<float>(0) == 100); d.set(f); assert(d.elems<float>() == &f); assert(d.at<float>(0) == f); d.set(s); assert(d.type() == Data::STRING); assert(d.size() == 1); assert(d.elems<std::string>() == &s); assert(d.at<std::string>(0) == s); d.clone(); assert(d.elems<std::string>() != &s); assert(d.at<std::string>(0) == s); d.set("hello"); assert(d.type() == Data::STRING); assert(d.at<std::string>(0) == "hello"); { std::string s = "d2"; Data d2(s); d.set("d"); d2.assign(d); assert(d2.at<std::string>(0) == d.at<std::string>(0)); } { Data a(Data::FLOAT, 3); a.assign(10,0); a.assign(20,1); a.assign(30,2); float b[3]; a.copyTo(b); for(int i=0;i<3;++i) assert(b[i] == a.at<float>(i)); } // checking elements { Data::Type types[] = {Data::BOOL, Data::INT, Data::FLOAT, Data::DOUBLE}; for(int i=0;i<4;++i){ Data a(types[i], 8); a.assignAll(0); assert(a.isZero()); a.assign(1, 1); assert(!a.isZero()); a.assign(1, 3); a.assign(1, 5); a.assign(1, 7); assert(a.slice(0,4,2).isZero()); } } // searching { Data e(Data::STRING, 3); std::string s1 = "test1", s2 = "test2", s3 = "test3"; e.assign(s1, 0); e.assign(s2, 1); e.assign(s3, 2); assert(e.indexOf(s1) == 0); assert(e.indexOf(s2) == 1); assert(e.indexOf(s3) == 2); assert(e.indexOf(std::string("invalid")) == Data::npos); } // multiple element assignment { #define ASSERT_EQUALS(a,b,c,d,e)\ assert(d1.at<int>(0) == a);\ assert(d1.at<int>(1) == b);\ assert(d1.at<int>(2) == c);\ assert(d1.at<int>(3) == d);\ assert(d1.at<int>(4) == e) Data d1(Data::INT, 5); d1.assignAll(-1); ASSERT_EQUALS(-1,-1,-1,-1,-1); d1.assignAll(0); ASSERT_EQUALS(0,0,0,0,0); { float t[] = {1,2,3,4,5}; d1.assignFromArray(t,2); ASSERT_EQUALS(1,2,0,0,0); d1.assignFromArray(t+2,2,1,2); ASSERT_EQUALS(1,2,3,4,0); d1.assignAll(0); d1.assignFromArray(t,3, 2); ASSERT_EQUALS(1,3,5,0,0); d1.assignFromArray(t+1,2, 2,3); ASSERT_EQUALS(1,3,5,2,4); } #undef ASSERT_EQUALS } } // stringification { //for(int i=0;i<4;++i) printf("%g\n", f4[i]); //printf("%s\n", s1.c_str()); bool b1; float f1; double d1; std::string s1; bool b4[4]; float f4[4]; double d4[4]; std::string s4[4]; int N; // number of elements converted N=fromToken(f1, "1e-2"); assert(1==N && f1 == float(1e-2)); N=fromToken(f1, "1000"); assert(1==N && f1 == float(1000)); N=fromToken(d1, "1e-2"); assert(1==N && d1 == double(1e-2)); N=fromToken(d1, "1000"); assert(1==N && d1 == double(1000)); N=fromToken(b1, "0"); assert(1==N && b1 == 0); N=fromToken(b1, "1"); assert(1==N && b1 == 1); N=fromToken(s1, "\"test\""); assert(1==N && s1 == "test"); N=fromToken(b4,4,1, "{1,0,1,1}"); assert(4==N && EQ4(b4, 1,0,1,1)); N=fromToken(b4,4,1, "{0, 1, 0,0}"); assert(4==N && EQ4(b4, 0,1,0,0)); N=fromToken(f4,4,1, "{1, -1.2, 1e10, +.1}"); assert(4==N && EQ4(f4, 1.f,-1.2f,1e10f,+.1f)); N=fromToken(d4,4,1, "{1, -1.2, 1e10, +.1}"); assert(4==N && EQ4(d4, 1,-1.2,1e10,+.1)); N=fromToken(s4,4,1, "{\"one\", \"two\", \"three\", \"four\"}"); assert(4==N && EQ4(s4, "one","two","three","four")); N=toToken(s1, 1000.f); assert(1==N && s1 == "1000"); N=toToken(s1, 2000.0); assert(1==N && s1 == "2000"); N=toToken(s1, true); assert(1==N && s1 == "1"); N=toToken(s1, false); assert(1==N && s1 == "0"); N=toToken(s1, "test"); assert(1==N && s1 == "\"test\""); N=toToken(s1, std::string("test")); assert(1==N && s1 == "\"test\""); SET4(b4, 1,0,1,1); N=toToken(s1, b4,4,1); assert(4==N && s1 == "{1, 0, 1, 1}"); SET4(f4,-1,0.1,3,1e10); N=toToken(s1, f4,4,1); assert(4==N && s1 == "{-1, 0.1, 3, 1e+10}"); SET4(d4,-1,0.1,3,1e10); N=toToken(s1, d4,4,1); assert(4==N && s1 == "{-1, 0.1, 3, 1e+10}"); SET4(s4,"one","two","three","four"); N=toToken(s1,s4,4,1); assert(4==N && s1 == "{\"one\", \"two\", \"three\", \"four\"}"); } // test View linked list implementation { View n00, n10, n11, n20; n00.add(n10); n00.add(n11); n10.add(n20); // check all of our links assert( n00.child == &n10); assert(!n00.parent); assert(!n00.sibling); assert( n10.parent == &n00); assert( n10.sibling == &n11); assert( n10.child == &n20); assert( n11.parent == &n00); assert(!n11.child); assert(!n11.sibling); assert( n20.parent == &n10); assert(!n20.child); assert(!n20.sibling); n10.makeLastSibling(); assert(!n10.sibling); assert(n11.sibling == &n10); n11.makeLastSibling(); n10.remove(); assert(!n10.parent); assert(!n10.sibling); assert(!n00.child->sibling); n11.remove(); assert(!n00.child); assert(n10.child == &n20); } // test View memory management { View * v0d = new View; View v1s; View * v1d = new View; assert(v0d->dynamicAlloc()); assert(!v1s.dynamicAlloc()); *v0d << v1s << v1d; delete v0d; } // Notifications { bool bv1=false, bv2=false, bf=false; Notifier n; assert(n.numObservers(Update::Value) == 0); n.attach(ntSetBool1, Update::Value, &bv1); n.attach(ntSetBool2, Update::Value, &bv2); n.attach(ntSetBool1, Update::Focus, &bf); assert(n.numObservers(Update::Value) == 2); assert(n.numObservers(Update::Focus) == 1); n.notify(Update::Value); assert(bv1); assert(bv2); assert(!bf); n.notify(Update::Focus); assert(bf); bv1=bv2=false; n.detach(ntSetBool1, Update::Value, &bv1); n.notify(Update::Value); assert(!bv1); assert(bv2); bv1=bv2=false; n.detach(ntSetBool2, Update::Value, &bv2); n.notify(Update::Value); assert(!bv1); assert(!bv2); } { bool b=false; Button w; w.attach(ntValue, Update::Value, &b); w.setValue(true); assert(b); assert(w.getValue() == true); // Setting to current value should NOT send notification to avoid infinite loops b=false; w.setValue(w.getValue()); assert(!b); // Boolean model variable bool v = false; w.attachVariable(v); w.setValue(true); assert(v == true); w.setValue(false); assert(v == false); v = true; w.onDataModelSync(); assert(w.getValue() == true); w.setValue(true); assert(w.data().toToken() == "1"); w.setValue(false); assert(w.data().toToken() == "0"); w.setDataFromString("0"); assert(w.getValue() == false); w.setDataFromString("1"); assert(w.getValue() == true); assert(!w.setDataFromString("invalid")); } { Buttons w; bool b=false; w.attach(ntValue, Update::Value, &b); w.setValue(true, 0); assert(b); assert(w.getValue(0) == true); w.data().resize(2,2); w.data().assignAll(0); bool v1 = false; bool v2 = false; w.attachVariable(v1, 0); w.attachVariable(v2, 2); w.setValue(true, 0); assert(v1 == true); w.setValue(true, 2); assert(v2 == true); w.setValue(false, 0); assert(v1 == false); w.setValue(false, 2); assert(v2 == false); w.setValue(false, 0); w.setValue(false, 1); w.setValue(false, 2); w.setValue(false, 3); assert(w.data().toToken() == "{0, 0, 0, 0}"); v1 = v2 = false; w.setDataFromString("{1,1,1,1}"); assert(w.getValue(0) && w.getValue(1) && w.getValue(2) && w.getValue(3)); assert(v1 && v2); } { bool b=false; Slider w; w.attach(ntValue, Update::Value, &b); w.setValue(0.99f); assert(b); assert(w.getValue() == 0.99f); // Setting to current value should NOT send notification to avoid infinite loops b=false; w.setValue(w.getValue()); assert(!b); float v; w.attachVariable(v); w.setValue(0.99); assert(v == 0.99f); v = 0; w.onDataModelSync(); assert(w.getValue() == 0); std::string s; w.setValue(0.25); assert(w.data().toToken() == "0.25"); w.setDataFromString("0.5"); assert(w.getValue() == 0.5); assert(v == 0.5); assert(!w.setDataFromString("invalid")); assert(w.getValue() == 0.5); } { bool b=false; Sliders w(Rect(1), 2,2); w.attach(ntValue, Update::Value, &b); w.setValue(0.99f, 3); assert(b); assert(w.getValue(3) == 0.99f); float v1, v2; w.attachVariable(v1, 0); w.attachVariable(v2, 1); w.setValue(0.1f, 0); assert(v1 == 0.1f); w.setValue(0.2f, 0); assert(v1 == 0.2f); v1 = 0.8f; v2 = 0.9f; w.onDataModelSync(); assert(w.getValue(0) == 0.8f); assert(w.getValue(1) == 0.9f); w.setValue(0.1f, 0); w.setValue(0.2f, 1); w.setValue(0.3f, 2); w.setValue(0.4f, 3); assert(w.data().toToken() == "{0.1, 0.2, 0.3, 0.4}"); v1=v2=0; w.setDataFromString("{0.4,0.3,0.2,0.1}"); assert(w.getValue(0) == 0.4); assert(w.getValue(1) == 0.3); assert(w.getValue(2) == 0.2); assert(w.getValue(3) == 0.1); assert(v1 == (float)w.getValue(0) && v2 == (float)w.getValue(1)); // double vs[4] = {0.11, 0.22, 0.33, 0.44}; // w.attachVariable(vs,4); // w.onDataModelSync(); // for(int i=0; i<4; ++i) printf("%f\n", w.values()[i]); // // w.getValue(0.44, 0); // w.getValue(0.33, 0); // w.getValue(0.22, 0); // w.getValue(0.11, 0); // for(int i=0; i<4; ++i) printf("%f\n", vs[i]); } { bool b=false; Slider2D w; w.attach(ntSetBool1, Update::Value, &b); w.setValue(0.01f, 0); assert(b); assert(w.getValue(0) == 0.01f); // Setting to current value should NOT send notification to avoid infinite loops b=false; w.setValue(0.01f, 0); assert(!b); b=false; w.setValue(0.00f, 0); w.setValue(0.01f, 1); assert(b); assert(w.getValue(1) == 0.01f); b=false; w.setValueMax(); assert(b); assert(w.getValue(0) == 1.f); assert(w.getValue(1) == 1.f); float v1, v2; w.attachVariable(v1, 0); w.attachVariable(v2, 1); w.setValue(0.5f, 0); assert(v1 == 0.5f); w.setValue(0.6f, 1); assert(v2 == 0.6f); v1 = 0.1; v2 = 0.2; w.onDataModelSync(); assert(w.getValue(0) == v1); assert(w.getValue(1) == v2); w.setValue(0.2, 0); w.setValue(0.3, 1); assert(w.data().toToken() == "{0.2, 0.3}"); v1 = v2 = 0; w.setDataFromString("{0.7, 0.8}"); assert(w.getValue(0)==0.7 && w.getValue(1)==0.8); assert(v1==(float)w.getValue(0) && v2==(float)w.getValue(1)); } { bool b=false; SliderGrid<4> w; w.attach(ntValue, Update::Value, &b); w.setValue(0.01f, 0); assert(b); assert(w.getValue(0) == 0.01f); float v1, v2; w.attachVariable(v1, 0); w.attachVariable(v2, 1); w.setValue(0.1f, 0); assert(v1 == 0.1f); w.setValue(0.2f, 0); assert(v1 == 0.2f); v1 = 0.8f; v2 = 0.9f; w.onDataModelSync(); assert(w.getValue(0) == 0.8f); assert(w.getValue(1) == 0.9f); w.setValue(0.1f, 0); w.setValue(0.2f, 1); w.setValue(0.3f, 2); w.setValue(0.4f, 3); assert(w.data().toToken() == "{0.1, 0.2, 0.3, 0.4}"); v1=v2=0; w.setDataFromString("{0.4,0.3,0.2,0.1}"); assert(w.getValue(0) == 0.4); assert(w.getValue(1) == 0.3); assert(w.getValue(2) == 0.2); assert(w.getValue(3) == 0.1); assert(v1 == (float)w.getValue(0) && v2 == (float)w.getValue(1)); } { bool b=false; SliderRange w; w.attach(ntValue, Update::Value, &b); w.setValue(0.01f, 0); w.setValue(0.02f, 1); assert(b); assert(w.getValue(0) == 0.01f); assert(w.getValue(1) == 0.02f); assert(w.center() == 0.015f); assert(w.range() == 0.01f); w.endpoints(0,1); assert(w.getValue(0) == 0); assert(w.getValue(1) == 1); w.centerRange(0.5, 0.25); assert(w.getValue(0) == (0.5-0.25/2)); assert(w.getValue(1) == (0.5+0.25/2)); float v1,v2; w.attachVariable(v1, 0); w.attachVariable(v2, 1); w.setValue(0.2, 0); w.setValue(0.3, 1); assert(w.data().toToken() == "{0.2, 0.3}"); v1 = v2 = 0; w.setDataFromString("{0.7, 0.8}"); assert(w.getValue(0)==0.7 && w.getValue(1)==0.8); assert(v1==(float)w.getValue(0) && v2==(float)w.getValue(1)); } { bool b=false; Label w; w.attach(ntValue, Update::Value, &b); w.setValue("test"); assert(b); assert(w.getValue() == "test"); assert(w.data().toToken() == "\"test\""); w.setValue(""); w.setDataFromString("\"test\""); assert(w.getValue() == "test"); } { // { // double val = 1e14; // double inc = 1; // printf("%f\n", val + inc*1); // printf("%f\n", val + inc*2); // printf("%f\n", val + inc*3); // printf("%f\n", val + inc*4); // } bool b=false; NumberDialer w(1,2,1,0); w.attach(ntValue, Update::Value, &b); w.setValue(0.75); assert(b); assert(w.getValue() == 0.75); // Setting to current value should NOT send notification to avoid infinite loops b=false; w.setValue(w.getValue()); assert(!b); double v=0; w.attachVariable(v); w.setValue(0.5); assert(v == 0.5); v = 0.25; w.onDataModelSync(); assert(w.getValue() == v); w.setValue(0.2); assert(w.data().toToken() == "0.2"); v = 0; w.setDataFromString("0.8"); assert(w.getValue()==0.8 && w.getValue()==v); } { bool b=false; TextView w; w.attach(ntValue, Update::Value, &b); w.setValue("hello"); assert(b); assert(w.getValue() == "hello"); assert(w.data().toToken() == "\"hello\""); std::string v = "test"; w.attachVariable(v); w.onDataModelSync(); assert(w.getValue() == "test"); w.setDataFromString("\"world\""); assert(w.getValue() == "world"); assert(v == "world"); } // model to string conversion { Label l; TextView tv; Button b; Buttons bs(Rect(), 2, 2); Slider s; Sliders ss(Rect(), 1, 4); Slider2D s2D; NumberDialer nd(1,4,1,-1); std::string strings[] = {"test1", "test2", "test3"}; l.setValue("Hello Label!").name("l"); tv.setValue("Hello TextView!").name("tv"); b.setValue(true).name("b"); bs.setValue(true, 0,1).name("bs"); s.setValue(0.5).name("s"); ss.setValue(0.4, 1).name("ss"); s2D.setValue(0.5, 0).setValue(0.1, 1).name("s2D"); nd.setValue(-0.54941).name("nd"); const View * views[] = {&l, &tv, &b, &bs, &s, &ss, &s2D, &nd}; for(unsigned i=0; i<sizeof(views)/sizeof(View *); ++i){ //const Data& d = views[i]->data(); //printf("%p: %s\n", &d, d.toString().c_str()); //views[i]->model().print(); printf("\n"); } View top; top << l << tv << b << bs << s << ss << s2D << nd; std::string str1, str2; // top.modelToString(str1, "test model"); // printf("%s\n", str1.c_str()); // // l.setValue("changed..."); // top.modelFromString(str1); // top.modelToString(str2); // printf("%s\n", str2.c_str()); // assert(str1 == str2); ModelManager mm; mm.add("l", l); mm.add("tv", tv); mm.add("b", b); mm.add("bs", bs); mm.add("s" , s); mm.add("ss", ss); mm.add("s2D",s2D); mm.add("nd", nd); Data dat(strings, 3); //Data dat(10); //dat.print(); //mm.add("strings", dat); //mm.add("strings", *new Data(strings, 3)); // mm.toToken(str1, "test"); // printf("%s\n", str1.c_str()); // // mm.fromToken(str1); // mm.toToken(str2, "test"); // printf("%s\n", str2.c_str()); std::string snapshotString1 = "[\"test 1\"] = {\n\ l = \"Label 1\",\n\ tv = \"TextView 1\",\n\ b = 0,\n\ bs = {0,0,0,0},\n\ }"; std::string snapshotString2 = "[\"test 2\"] = {\n\ l = \"Label 2\",\n\ tv = \"TextView 2\",\n\ b = 1,\n\ bs = {1,1,1,1},\n\ s = 1,\n\ ss = {1,1,1,1},\n\ s2D = {1,1},\n\ nd = 1,\n\ }"; mm.snapshotsFromString("{" + snapshotString1 + ",\r\n" + snapshotString2 + "}"); // mm.snapshots()["test 1"]["l"].print(); // mm.snapshots()["test 1"]["b"].print(); // mm.snapshots()["test 1"]["bs"].print(); // mm.snapshots()["test 1"]["s"].print(); // mm.snapshots()["test 1"]["ss"].print(); // mm.snapshots()["test 1"]["s2D"].print(); // mm.snapshots()["test 1"]["nd"].print(); // mm.snapshots()["test 2"]["l"].print(); // mm.snapshots()["test 2"]["b"].print(); // mm.snapshots()["test 2"]["bs"].print(); // mm.snapshots()["test 2"]["s"].print(); // mm.snapshots()["test 2"]["ss"].print(); // mm.snapshots()["test 2"]["s2D"].print(); // mm.snapshots()["test 2"]["nd"].print(); // // mm.snapshotsToFile("snapshots.txt"); // // mm.clearSnapshots(); // mm.snapshotsFromFile("snapshots.txt"); // // str1.clear(); // mm.snapshotsToString(str1); // printf("%s\n", str1.c_str()); } // { // std::string str( "a bc ab ca ab" ); // std::string searchString("ab"); // std::string replaceString("hello"); // // std::string::size_type pos = 0; // while((pos = str.find(searchString, pos)) != std::string::npos){ // str.replace(pos++, searchString.size(), replaceString); // } // printf("%s\n", str.c_str()); // } // printf("sizeof(Notifier): %d\n", sizeof(Notifier)); // printf("sizeof(View): %d\n", sizeof(View)); // printf("sizeof(Data): %d\n", sizeof(Data)); // printf("sizeof(Model): %d\n", sizeof(Model)); // printf("sizeof(ModelManager): %d\n", sizeof(ModelManager)); return 0; }
bool MasterAdapter::execute(Job& job, Notifier& notifier) { bool rc = mConnector->execute(job); notifier.jobFinished(job); return rc; }
boost::shared_ptr<Model> BDTLearner::train_given_everything ( const vector<Event>& all_sig_events, const vector<Event>& all_bg_events, const vector<double>& init_sig_weights, const vector<double>& init_bg_weights) const { typedef vector<Event> vecev; typedef vector<double> vecd; typedef vecev::const_iterator vecev_citer; typedef vecd::const_iterator vecd_citer; typedef vecd::iterator vecd_iter; using namespace np; int n_sig (all_sig_events.size ()); int n_bg (all_bg_events.size ()); int N_f (all_sig_events[0].size ()); vector<double> all_sig_weights ( div (init_sig_weights, sum (init_sig_weights))); vector<double> all_bg_weights ( div (init_bg_weights, sum (init_bg_weights))); // Warning: technically, this usage of dtl.min_split is not threadsafe DTLearner& dtl (*m_dtlearner); //TODO: Add to Booster Class RegLearner& rdtl (static_cast<RegLearner&>(*m_dtlearner)); rdtl.separation_type("sum_squared"); std::cout<<rdtl.separation_type()<<std::endl; int save_min_split (m_dtlearner->m_min_split); dtl.min_split (max ( dtl.min_split (), static_cast<int> (1.0 * (n_sig + n_bg) / N_f / N_f / 20))); vector<boost::shared_ptr<DTModel> > dtmodels; vector<double> errs; vector<double> alphas; //TODO: Overload function to take map std::map<const DTNode*,double> alpha_j; Notifier<int> notifier ("training decision trees", m_num_trees); if (not m_quiet) { notifier.update (0); } //Get median of classifier value. TODO:Add to booster class double f_mmone = -9999; double quant_val = 0.7; std::map<const DTNode*,double> f_x; // if(n_sig>n_bg){ // f_mmone = 1; // } // else if(n_bg>n_sig){ // f_mmone = -1; // } // else{ // f_mmone = 0; // } Booster gradBoost(true,n_sig,n_bg); //gradBoost.InitFX(n_sig,n_bg); //Iterate over requested number of trees for (int m = 0; m < m_num_trees; ++m) { const int n_sig_used = static_cast<int>((m_frac_random_events * n_sig)); const int n_bg_used = static_cast<int> ((m_frac_random_events * n_bg)); const int n_sig_unused = (n_sig - n_sig_used); const int n_bg_unused = (n_bg - n_bg_used); // storage for picked events, if not using all events vector<Event> picked_sig_events; vector<double> picked_sig_weights; vector<Event> picked_bg_events; vector<double> picked_bg_weights; const vector<Event>* sig_events; const vector<double>* sig_weights; const vector<Event>* bg_events; const vector<double>* bg_weights; // if desired, pick events if (n_sig_unused > 0 or n_bg_unused > 0) { const vector<int> sig_indices ( dtl.m_random_sampler.sample_range<int> ( n_sig_used, 0, n_sig, true)); picked_sig_events = np::subscript (all_sig_events, sig_indices); picked_sig_weights = np::subscript (all_sig_weights, sig_indices); const vector<int> bg_indices ( dtl.m_random_sampler.sample_range<int> ( n_bg_used, 0, n_bg, true)); picked_bg_events = np::subscript (all_bg_events, bg_indices); picked_bg_weights = np::subscript (all_bg_weights, bg_indices); sig_events = &picked_sig_events; sig_weights = &picked_sig_weights; bg_events = &picked_bg_events; bg_weights = &picked_bg_weights; } else { sig_events = &all_sig_events; sig_weights = &all_sig_weights; bg_events = &all_bg_events; bg_weights = &all_bg_weights; } boost::shared_ptr<Model> model ( dtl.train_given_everything ( *sig_events, *bg_events, *sig_weights, *bg_weights)); boost::shared_ptr<DTModel> dtmodel ( dynamic_pointer_cast<DTModel> (model)); dtmodels.push_back (dtmodel); // TODO: if there are unused events, set purity from unused? std::cout<<" Melanie was here "<<std::endl; //Iterate over nodes and get coefficient for each boost::shared_ptr<DTNode> dtNode = dtmodel->root(); dtnresmap resMap; vecd_iter my_w; vecev_citer my_ev; vecpdd resPairs; //Get residuals for delta TODO: Make this a function for(my_ev = sig_events->begin(), my_w = all_sig_weights.begin(); my_ev != sig_events->end(); ++my_ev, ++my_w) { const DTNode& eLeaf (dtNode->trace(make_scoreable(*my_ev))); const DTNode* pLeaf = &eLeaf; double leaf_res = 1; double f_xi; f_x.find(pLeaf)!=f_x.end() ? f_xi = f_x[pLeaf] : f_xi = gradBoost.f_mmone; std::cout<<"First f_x: "<<f_xi<<" "<<gradBoost.f_mmone<<" "<<pLeaf<<std::endl; resPairs.push_back(make_pair(leaf_res-f_xi,*my_w)); } for(my_ev = bg_events->begin(), my_w = all_bg_weights.begin(); my_ev != bg_events->end(); ++my_ev, ++my_w) { const DTNode& eLeaf (dtNode->trace(make_scoreable(*my_ev))); const DTNode* pLeaf = &eLeaf; double leaf_res = -1; double f_xi; f_x.find(pLeaf)!=f_x.end() ? f_xi = f_x[pLeaf] : f_xi = gradBoost.f_mmone; std::cout<<"First f_x: "<<f_xi<<" "<<gradBoost.f_mmone<<" "<<pLeaf<<std::endl; resPairs.push_back(make_pair(leaf_res-f_xi,*my_w)); } //Update delta double delta = GetQuantile(resPairs,quant_val,true,2); //Get residuals for signal TODO:Make this a function for(my_ev = sig_events->begin(), my_w = all_sig_weights.begin(); my_ev != sig_events->end(); ++my_ev, ++my_w) { const DTNode& eLeaf (dtNode->trace(make_scoreable(*my_ev))); const DTNode* pLeaf = &eLeaf; double leaf_res = 1; double sign_leaf_res = boost::math::sign(leaf_res); resMap[pLeaf].weightSum+=*my_w; resMap[pLeaf].resPair.push_back(make_pair(sign_leaf_res*min(fabs(leaf_res-delta),delta),*my_w)); } //Get residuals for background TODO:Make this a function for(my_ev = bg_events->begin(), my_w = all_bg_weights.begin(); my_ev != bg_events->end(); ++my_ev, ++my_w) { const DTNode& eLeaf (dtNode->trace(make_scoreable(*my_ev))); const DTNode* pLeaf = &eLeaf; double leaf_res = -1; double sign_leaf_res = boost::math::sign(leaf_res); resMap[pLeaf].weightSum+=*my_w; resMap[pLeaf].resPair.push_back(make_pair(sign_leaf_res*min(fabs(leaf_res-delta),delta),*my_w)); } //TODO: Make this a function for(dtnresmap_citer n_map = resMap.begin(); n_map != resMap.end(); ++n_map){ //TODO: Add this to Booster Class //Residual and Weight pair vecpdd nPair = n_map->second.resPair; double r_bar = GetQuantile(nPair,0.5,true,n_map->second.weightSum); double tot_gamma = 0; double gamma_j = 0; //Calculate gammas for each node TODO: Make this a function for(vecpdd_iter pair_i = nPair.begin(); pair_i != nPair.end(); pair_i++){ //Node residual variables double res_i = pair_i->first; double meddiff_i = res_i-r_bar; double sign_meddiff_i = boost::math::sign(meddiff_i); double min_i = std::min(delta,meddiff_i); double gsum_i = sign_meddiff_i*min_i; gamma_j += gsum_i; // std::cout<<(1/(sig_size+bg_size))<<" "<<((sig_size*sig_gamma)+(bg_size*bg_gamma))<<" "<<delta<<" "<<sig_res<<" "<<r_bar<<" "<<sig_meddiff<<" "<<sig_sign_meddiff<<" "<<sig_min<<" "<<sig_gamma<<" "<<bg_gamma<<" "<<tot_gamma<<std::endl; }//End vecpdd loop //Get map of node weights alpha_j[n_map->first] = r_bar + (1/nPair.size())*gamma_j; f_x[n_map->first] += alpha_j[n_map->first]; std::cout<<"Delta: "<<delta<<" F(x): "<<f_x[n_map->first]<<" Point: "<<n_map->first<<std::endl; }//End dtnresmap loop // before pruners get to prune before boosting for (vector<boost::shared_ptr<Pruner> >::const_iterator i_pruner = m_before_pruners.begin (); i_pruner != m_before_pruners.end (); ++i_pruner) { (*i_pruner)->prune (dtmodel); } // get scores for boosting const vector<double> all_sig_result ( dtmodel->score (all_sig_events, false, true)); const vector<double> all_bg_result ( dtmodel->score (all_bg_events, false, true)); // AdaBoost: find sum of weights for misclass'd events vecev_citer i_ev; vecd_iter i_weight; vecd_citer i_result; double sum_sig_weights (0); double sum_wrong_sig_weights (0); for (i_weight = all_sig_weights.begin (), i_result = all_sig_result.begin(); i_weight != all_sig_weights.end (); ++i_weight, ++i_result) { sum_sig_weights += *i_weight; if (*i_result < 0) { sum_wrong_sig_weights += *i_weight; } } double sum_bg_weights (0); double sum_wrong_bg_weights (0); for (i_weight = all_bg_weights.begin (), i_result = all_bg_result.begin(); i_weight != all_bg_weights.end (); ++i_weight, ++i_result) { sum_bg_weights += *i_weight; if (*i_result > 0) { sum_wrong_bg_weights += *i_weight; } } // AdaBoost: boost weights of misclass'd events const double err_m ((sum_wrong_sig_weights + sum_wrong_bg_weights) / (sum_sig_weights + sum_bg_weights)); const double boost_factor = pow ((1-err_m)/err_m, m_beta); const double alpha_m = m_beta ? log (boost_factor) : 1; for (i_weight = all_sig_weights.begin (), i_result = all_sig_result.begin(), i_ev = all_sig_events.begin (); i_weight != all_sig_weights.end (); ++i_weight, ++i_result, ++i_ev) { if (*i_result < 0) { *i_weight *= boost_factor; } } for (i_weight = all_bg_weights.begin (), i_result = all_bg_result.begin(), i_ev = all_bg_events.begin (); i_weight != all_bg_weights.end (); ++i_weight, ++i_result, ++i_ev) { if (*i_result > 0) { *i_weight *= boost_factor; } } errs.push_back (err_m); alphas.push_back (alpha_m); // Renormalize weights const double new_total_weight ( sum (all_sig_weights) + sum (all_bg_weights)); all_sig_weights = div (all_sig_weights, new_total_weight); all_bg_weights = div (all_bg_weights, new_total_weight); // after pruners get to prune after boosting for (vector<boost::shared_ptr<Pruner> >::const_iterator i_pruner = m_after_pruners.begin (); i_pruner != m_after_pruners.end (); ++i_pruner) { (*i_pruner)->prune (dtmodel); } if (not m_quiet) { notifier.update (m + 1); } } if (not m_quiet) { notifier.finish (); } dtl.min_split (save_min_split); return boost::make_shared<BDTModel> (m_feature_names, dtmodels, alphas); }
void EPoll::notify() { notifier_.notify(); }
int main(int argc, char ** argv) { LogForwarder * forwarder; Notifier * notifier; Communicator parent_comm; int rank; // initiate the CF core and MPI environment Core::instance().initiate(argc, argv); Comm::instance().init(argc, argv); parent_comm = Comm::instance().get_parent(); rank = Comm::instance().rank(); if( parent_comm == MPI_COMM_NULL ) { CFerror << "This solver cannot run without a manager. Exiting..." << CFendl; return 1; } setup_tree(); // create the PE manager Handle< Manager > mgr = Core::instance().root().get_child("Tools")-> create_component<Manager>("PEManager"); Core::instance().root().create_component<CWorker>("Worker"); // Make sure the python ScriptEngine gets created, if it exists try { build_component("cf3.python.ScriptEngine", "DummyScriptEngine"); } catch(...) { } mgr->mark_basic(); notifier = new Notifier( mgr ); notifier->listen_to_event("tree_updated", true); // set the forwarder, if needed // if( forward == "all" || (forward == "rank0" && Comm::instance().rank() == 0) ) { forwarder = new LogForwarder(); Logger::instance().getStream(INFO).addStringForwarder(forwarder); } bool rank0 = CFinfo.getFilterRankZero(LogStream::SCREEN); CFinfo.setFilterRankZero(LogStream::SCREEN, false); CFinfo << "Worker[" << rank << "] -> Syncing with the parent..." << CFendl; Comm::instance().barrier(); MPI_Barrier( parent_comm ); CFinfo << "Worker[" << rank << "] -> Synced with the parent!" << CFendl; CFinfo.setFilterRankZero(LogStream::SCREEN, rank0); mgr->listening_thread()->join(); CFinfo << "Worker[" << rank << "] -> " << "C U..." << CFendl; // synchronize with the parent before we finalize the MPI environment MPI_Barrier( parent_comm ); // terminate the CF core and MPI environment Comm::instance().finalize(); Core::instance().terminate(); delete forwarder; delete notifier; return 0; }
void MangoListener::onFrame(const Controller& controller) { const Frame frame = controller.frame(); HandList hands = frame.hands(); int extendedFingers = 0; for (HandList::const_iterator hl = hands.begin(); hl != hands.end(); ++hl) { const Hand hand = *hl; for (int i = 0; i < hand.fingers().count(); i++) { Finger finger = hand.fingers()[i]; if(finger.isExtended()) extendedFingers++; } if (!onGesture && !frame.hands().isEmpty() && frame.hands().count() == 1 && extendedFingers == 0) { preGestureCounter++; std::cout << preGestureCounter << std::endl; if (preGestureCounter > MAX_PREGESTURE && frameCount == 0) { onGesture = true; preGestureCounter = 0; frameCount++; note.show(); } } else if (preGestureCounter > 0) { preGestureCounter--; } } if (onGesture && frameCount < MAX_FRAMECOUNT) { switch (extendedFingers) { case 1: { std::string command = commands.getCommand("FING3"); std::cout << command << std::endl; break; } case 2: { break; } case 3: { break; } default: { break; } } frameCount++; const GestureList gestures = frame.gestures(); for (int g = 0; g < gestures.count(); ++g) { Gesture gesture = gestures[g]; //need to move finger detection in here too switch (gesture.type()) { case Gesture::TYPE_CIRCLE: { CircleGesture circle = gesture; std::string clockwiseness; //probably simplfy to a bool if (circle.pointable().direction().angleTo(circle.normal()) <= PI/4) { clockwiseness = "clockwise"; std::cout << "CIRCLE CLOCKWISE" << std::endl; } else { clockwiseness = "counterclockwise"; std::cout << "CIRCLE COUNTERCLOCKWISE" << std::endl; } break; } case Gesture::TYPE_SWIPE: { SwipeGesture swipe = gesture; std::cout << "SWIPE" << std::endl; break; } case Gesture::TYPE_KEY_TAP: { KeyTapGesture tap = gesture; std::cout << "KEY TAP" << std::endl; break; } case Gesture::TYPE_SCREEN_TAP: { ScreenTapGesture screentap = gesture; std::cout << "SCREEN TAP" << std::endl; break; } default: { std::cout << std::string(2, ' ') << "Unknown gesture type." << std::endl; break; } } float seconds = gesture.durationSeconds(); std::cout << seconds << std::endl; } } else if (onGesture == true && frameCount > MAX_FRAMECOUNT) { note.hide(); onGesture = false; frameCount = 0; } else if (onGesture) { frameCount++; } }