/* MAttia.cpp Created on: Jan 13, 2009 Author: m.attia */ #include "MAttia.h" #include "Algebras/FText/FTextAlgebra.h" #include "SecondoInterface.h" #include "SecondoInterfaceTTY.h" #include "fstream" using namespace temporalalgebra; using namespace std; using namespace datetime; void NDefUnit(MBool* arg, CcBool* nval, MBool* res) { bool debugme=false; res->Clear(); res->StartBulkLoad(); UBool uarg; UBool uBool(nval->GetBoolval()), uBool2(nval->GetBoolval()); uBool.constValue= *nval; if(!arg->IsDefined()||arg->GetNoComponents() == 0) { res->SetDefined(false); return; } arg->Get(0, uarg); //uarg.Print(cout); uBool2.CopyFrom(&uarg); res->MergeAdd(uBool2); //res->Print(cout); uBool.timeInterval.lc = !uarg.timeInterval.rc; uBool.timeInterval.start = uarg.timeInterval.end; for( int i = 1; i < arg->GetNoComponents(); i++) { arg->Get(i, uarg); uBool.timeInterval.rc = !uarg.timeInterval.lc; uBool.timeInterval.end = uarg.timeInterval.start; if(uBool.timeInterval.start < uBool.timeInterval.end || (uBool.timeInterval.start == uBool.timeInterval.end && uBool.timeInterval.lc && uBool.timeInterval.rc)) { res->MergeAdd(uBool); //res->Print(cout); } uBool2.CopyFrom(&uarg); res->MergeAdd(uBool2); //res->Print(cout); uBool.timeInterval.lc = !uarg.timeInterval.rc; uBool.timeInterval.start = uarg.timeInterval.end; } res->EndBulkLoad(false); if(debugme) { cout<<"NDefUnit is called"; cout<<"\nInput1:"; arg->Print(cout); cout<<"\nInput2:"; nval->Print(cout); cout<<"\nOutput:"; res->Print(cout); cout.flush(); } } int Randint(int u)//Computes a random integer in the range 0..u-1, //for u >= 2 { if ( u < 2 ) {u=2; srand ( time(NULL) );} // For u < 2 also initialize the random number generator // rand creates a value between [0,RAND_MAX]. The calculation procedure // below is recommended in the manpage of the rand() function. // Using rand() % u will yield poor results. return (int) ( (float)u * rand()/(RAND_MAX+1.0) ); } string ToString( int number ) { ostringstream o; o << number ; //<< char(0) return o.str(); } string GenerateConnectedRandomConstraints(int numPreds, int numConstraints) { string aliases[]={"a","b","c","d","e","f","g","h","i","j","k","l","m","n", "o","p","q","r","s","t","u","v","w","x","y","z"}; string connectors[]= {"vec(\"aabb\")" , "meanwhile", "vec(\"bbaa\")" , "vec(\"aa.bb\")" , "vec(\"bb.aa\")" , "vec(\"abab\")" , "vec(\"baba\")" , "vec(\"baab\")" , "vec(\"abba\")" , "vec(\"a.bab\")" , "vec(\"a.bba\")" , "vec(\"baa.b\")" , "vec(\"aba.b\")" , "immediately" , "vec(\"a.ba.b\")" , "vec(\"a.abb\")" , "vec(\"a.a.bb\")" , "vec(\"ba.ab\")" , "then", "vec(\"bb.a.a\")" , "vec(\"bba.a\")" , "vec(\"b.baa\")" , "vec(\"b.b.aa\")" , "vec(\"ab.ba\")" , "vec(\"aa.b.b\")" , "vec(\"aab.b\")" , "follows", "vec(\"a.ab.b\")" , "vec(\"a.a.b.b\")" ,"vec(\"b.ba.a\")", "later"}; string result= ""; string str=""; int* ConnectedSet= new int[numPreds]; int c=0; int op1, op2, op3; int seed; int numConnected=0; while(true) { result= ""; str=""; numConnected=0; c=0; for(int i=0; iTheEmptyList(); SecErrInfo err; SecondoInterfaceTTY sec; string sres; StopWatch qqueryTime; ofstream out("STPQ.txt"); ofstream stat("STPQ_Stat.txt"); stat<<"#preds\t#cons\tquery#\tTTime\tCPUTime"; double sumTime=0, sumCPU=0; double queryReal; double queryCPU; for(int c=0; cHasLength(args,1)){ return listutils::typeError("one argument expected"); } if(!Instant::checkType(nl->First(args))){ return listutils::typeError("instant expected"); } return nl->SymbolAtom("mbool"); } ListExpr PassMBoolTypeMap(ListExpr args) { //cout<ToString(args); if(!nl->HasLength(args,1)){ return listutils::typeError("one argument expected"); } if(!MBool::checkType(nl->First(args))){ return listutils::typeError("mbool expected"); } return nl->SymbolAtom("mbool"); } ListExpr RunSTPQExperiment1QueriesTM(ListExpr args) { //cout<ToString(args); if(!nl->HasLength(args,1)){ return listutils::typeError("one argument expected"); } if(!CcString::checkType(nl->First(args))){ return listutils::typeError("string expected"); } return nl->SymbolAtom("bool"); } ListExpr RunSTPQExperiment2QueriesTM(ListExpr args) { //cout<ToString(args); if(!nl->HasLength(args,1)){ return listutils::typeError("one argument expected"); } if(!CcString::checkType(nl->First(args))){ return listutils::typeError("string expected"); } return nl->SymbolAtom("bool"); } /* ~NDefUnitTypeMap~ signatures: mbool x bool -> mbool */ ListExpr NDefUnitTM(ListExpr args){ if(nl->ListLength(args)!=2){ ErrorReporter::ReportError("Two argument expected"); return nl->SymbolAtom( "typeerror" ); } if(nl->IsEqual(nl->First(args),"mbool") && nl->IsEqual(nl->Second(args),"bool")){ return nl->SymbolAtom("mbool"); } ErrorReporter::ReportError("mbool x bool expected"); return nl->SymbolAtom( "typeerror" ); } ListExpr RandomShiftDelayTM( ListExpr args ) { string err = " operator expects (mpoint duration real real)"; if(!nl->HasLength(args,4)){ return listutils::typeError("wrong number of arguments"); } if( !MPoint::checkType(nl->First(args)) || !Duration::checkType(nl->Second(args)) || !CcReal::checkType(nl->Third(args)) || !CcReal::checkType(nl->Fourth(args))){ return listutils::typeError(err); } return (nl->SymbolAtom("mpoint")); } ListExpr TestTM(ListExpr args){ return nl->SymbolAtom( "bool" ); } void CreateRandomMBool(Instant starttime, MBool& result) { bool debugme=false,bval=false; result.Clear(); int rnd,i=0,n; UBool unit(true); Interval intr(starttime, starttime, true, false); Instant tstart(starttime),tendtstart(starttime); rnd=rand()%20; n=++rnd; while(i++1200); unit.constValue.Set(true, bval); unit.timeInterval= intr; result.Add(unit); intr.start= intr.end; bval=!bval; } if(debugme) result.Print(cout); } int RandomMBool(Word* args, Word& result, int message, Word& local, Supplier s) { result = qp->ResultStorage(s); MBool* res = (MBool*) result.addr; DateTime* tstart = (DateTime*) args[0].addr; CreateRandomMBool(*tstart,*res); return 0; } int PassMBool(Word* args, Word& result, int message, Word& local, Supplier s) { result = qp->ResultStorage(s); MBool* res = (MBool*) result.addr; MBool* inp = (MBool*) args[0].addr; res->CopyFrom(inp); return 0; } void RandomShiftDelay( const MPoint* actual, const Instant* threshold, double dx, double dy, MPoint& res) { bool debugme= false; MPoint delayed(actual->GetNoComponents()); UPoint first(0), next(0); UPoint *shifted,*temp=0, *cur; int xshift=0, yshift=0; int rmillisec=0, rday=0; actual->Get( 0, first ); cur=new UPoint(first); for( int i = 1; i < actual->GetNoComponents(); i++ ) { actual->Get( i, next ); rmillisec= rand()% threshold->GetAllMilliSeconds(); rday=0; if(threshold->GetDay()> 0) rday = rand()% threshold->GetDay(); DateTime delta(rday,rmillisec,durationtype) ; xshift= rand() % ((int)dx +1); yshift= rand() % ((int)dy +1); shifted= new UPoint(*cur); delete cur; temp= new UPoint(next); if(rmillisec > rand()%24000 ) { if((shifted->timeInterval.end + delta) < next.timeInterval.end ) { shifted->timeInterval.end += delta ; temp->timeInterval.start= shifted->timeInterval.end; shifted->p1.Set(shifted->p1.GetX()+xshift, shifted->p1.GetY()+yshift); temp->p0.Set(shifted->p1.GetX(), shifted->p1.GetY()); } } else { if((shifted->timeInterval.end - delta) >shifted->timeInterval.start) { shifted->timeInterval.end -= delta ; temp->timeInterval.start= shifted->timeInterval.end; shifted->p1.Set(shifted->p1.GetX()-xshift, shifted->p1.GetY()-yshift); temp->p0.Set(shifted->p1.GetX(), shifted->p1.GetY()); } } cur=temp; if(debugme) { cout.flush(); cout<<"\n original "; cur->Print(cout); cout<<"\n shifted " ; shifted->Print(cout); cout.flush(); } delayed.Add(*shifted); delete shifted; } if(temp){ delayed.Add(*temp); delete temp; } res.CopyFrom(&delayed); if(debugme) { res.Print(cout); cout.flush(); } return; } /* Value mapping function for the operator ~ndefunit~ */ int NDefUnitVM( ArgVector args, Word& result, int msg, Word& local, Supplier s ) { bool debugme=false; MBool *arg = static_cast( args[0].addr ); CcBool *ndefval = static_cast( args[1].addr ); MBool* tmp=new MBool(0); result= qp->ResultStorage(s); MBool* res= (MBool*) result.addr; NDefUnit(arg, ndefval, res); //res->CopyFrom(tmp); tmp->Destroy(); delete tmp; if(debugme) { cout.flush(); res->Print(cout); cout.flush(); } return 0; } int RunSTPQExperiment1QueriesVM(Word* args, Word& result, int message, Word& local, Supplier s) { result = qp->ResultStorage(s); CcBool* res = (CcBool*) result.addr; CcString* selectfrom = (CcString*) args[0].addr; int numpreds[]={2,2,2,2, 3,3,3,3,3, 4,4,4,4,4,4, 5,5,5,5,5,5,5, 6,6,6,6,6,6,6,6, 7,7,7,7,7,7,7,7,7, 8,8,8,8,8,8,8,8,8,8 }; int numconss[]={1,2,3,4, 2,4,3,5,6, 3,4,5,6,7,8, 4,5,6,7,8,9,10, 5,6,7,8,9,10,11,12, 6,7,8,9,10,11,12,13,14, 7,8,9,10,11,12,13,14,15,16 }; int munexmpl[]={100,100,100,100,100,100,100,100,100,100,100,100,100, 100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100, 100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100, 100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100, 100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100, 100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100}; res->Set(true, RunSTPQExperiment1Queries(selectfrom->GetValue(), 49, numpreds, numconss, munexmpl)); return 0; } int RunSTPQExperiment2QueriesVM(Word* args, Word& result, int message, Word& local, Supplier s) { result = qp->ResultStorage(s); CcBool* res = (CcBool*) result.addr; //CcString* filename = (CcString*) args[0].addr; int numpreds[]={2,2,2,2, 3,3,3,3,3, 4,4,4,4,4,4, 5,5,5,5,5,5,5, 6,6,6,6,6,6,6,6, 7,7,7,7,7,7,7,7,7, 8,8,8,8,8,8,8,8,8,8 }; int numconss[]={1,2,3,4, 2,4,3,5,6, 3,4,5,6,7,8, 4,5,6,7,8,9,10, 5,6,7,8,9,10,11,12, 6,7,8,9,10,11,12,13,14, 7,8,9,10,11,12,13,14,15,16 }; int munexmpl[]={10,10,10,10,10,10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10}; string file = "/home/mattia/Desktop/SQLExpr3.txt"; res->Set(true, GenerateSTPQExperiment2Queries("select count(*) from trains ", 49, numpreds, numconss, munexmpl, file.c_str())); return 0; } int RandomShiftDelayVM(ArgVector args, Word& result, int msg, Word& local, Supplier s ) { MPoint *pActual = static_cast( args[0].addr ); Instant *threshold = static_cast(args[1].addr ); double dx = static_cast(args[2].addr )->GetRealval(); double dy = static_cast(args[3].addr )->GetRealval(); MPoint* shifted = (MPoint*) qp->ResultStorage(s).addr; if(pActual->GetNoComponents()<2 || !pActual->IsDefined()) shifted->CopyFrom(pActual); else { RandomShiftDelay(pActual, threshold, dx, dy, *shifted); } result= SetWord(shifted); //This looks redundant but it is really necessary. After 2 hours of //debugging, it seems that the "result" word is not correctly set //by the query processor to point to the results. return 0; } int StretchVM( ArgVector args, Word& result, int msg, Word& local, Supplier s ) { //bool debugme=false; //MPoint *mpoint = static_cast( args[0].addr ); //MPoint *mpref = static_cast( args[1].addr ); //int numAnchors = static_cast( args[2].addr )->GetIntval(); //double distThreshold = static_cast( args[3].addr )->GetRealval(); return 0; } int TestVM( ArgVector args, Word& result, int msg, Word& local, Supplier s ) { //bool debugme=false; result = qp->ResultStorage(s); CcBool* res= static_cast(result.addr); // // list lst; // // for(int i=0; i<10; ++i) // lst.push_back(i); // list::iterator it= lst.begin(); // cerr<<*it< dbarr(1); Page val1; for(int i=0; i<4000; ++i) val1.ar[i]= 'C'; dbarr.Put(0,val1); Page val2('a'); dbarr.Get(0,val2); cerr<Set(true, true); return 0; } const string PatternSpec = "( ( \"Signature\" \"Syntax\" \"Meaning\" " "\"Example\" ) " "( ((stream x) ((map1 x mbool1)...(mapn x mbooln))) -> " "(stream x)" "_ pattern [ fun1,...,funn ]" "Only tuples, fulfilling the specified pattern " "are passed on to the output " "stream." "under construction " ") )"; const string RandomShiftDelaySpec = "( ( \"Signature\" \"Syntax\" \"Meaning\" " "\"Example\" ) " "( mpoint x duration x real x real-> mpoint" "randomdelay(schedule, delay_threshold, dx, dy)" "Given an mpoint and a duration value, the operator randomly shift the" "start and end intstants of every unit in the mpoint. This gives the " "effect of having positive and negative delays and spatial shifts in the " "movement. The random shift values are bound by the given threshold." " query randomdelay(train7)" ") )"; const string TestSpec = "( ( \"Signature\" \"Syntax\" \"Meaning\" " "\"Example\" ) " "( -> bool" "query test()" "A dummy opertor to test code blocks. The idea is to have a fast way to" " code blocks. This operator has a type map, value map, spec, ..., and all " "other necessary functions for a SECONDO operator. One just needs to place " "the code block in the value mapping function and let it yield true if the " "code block works fine, false otherwise. test " "query test()" ") )"; /* 4.1.4 Definition of operator ~pattern~ */ Operator opdefrandmbool ( "randommbool", // name PatternSpec, // specification RandomMBool, // value mapping Operator::SimpleSelect, // trivial selection function RandomMBoolTypeMap // type mapping ); Operator opdefpassmbool ( "passmbool", // name PatternSpec, // specification PassMBool, // value mapping Operator::SimpleSelect, // trivial selection function PassMBoolTypeMap // type mapping ); Operator opRunSTPQExperiment1Queries( "runstpqexpr1", // name PatternSpec, // specification RunSTPQExperiment1QueriesVM, // value mapping Operator::SimpleSelect, // trivial selection function RunSTPQExperiment1QueriesTM // type mapping ); Operator opRunSTPQExperiment2Queries( "runstpqexpr2", // name PatternSpec, // specification RunSTPQExperiment2QueriesVM, // value mapping Operator::SimpleSelect, // trivial selection function RunSTPQExperiment2QueriesTM // type mapping ); OperatorInfo NDefUnitOperatorInfo( "ndefunit", "mbool x bool -> mbool", "ndefunit(mdirection(train7) > 90.0, TRUE)", "Replaces the undefined periods within a moving constant with a constant " "unit having the given value", ""); Operator ndefunit( NDefUnitOperatorInfo, NDefUnitVM, NDefUnitTM); Operator randomshiftdelay ( "randomshiftdelay", // name RandomShiftDelaySpec, // specification RandomShiftDelayVM, // value mapping Operator::SimpleSelect, // trivial selection function RandomShiftDelayTM // type mapping ); Operator test ( "mytest", // name TestSpec, // specification TestVM, // value mapping Operator::SimpleSelect, // trivial selection function TestTM // type mapping ); MAttiaAlgebra::MAttiaAlgebra():Algebra() { // TODO Auto-generated constructor stub /* 5.2 Registration of Types */ /* 5.3 Registration of Operators */ //AddOperator(&opdefrandmbool); //AddOperator(&opdefpassmbool); AddOperator(&opRunSTPQExperiment1Queries); AddOperator(&opRunSTPQExperiment2Queries); AddOperator(&ndefunit); AddOperator(&randomshiftdelay); AddOperator(&test); } MAttiaAlgebra::~MAttiaAlgebra() { // TODO Auto-generated destructor stub } extern "C" Algebra* InitializeMAttiaAlgebra( NestedList* nlRef, QueryProcessor* qpRef ) { // The C++ scope-operator :: must be used to qualify the full name return new MAttiaAlgebra; }