From 375dc5e986c93bce6f9bec0f2d68aefec64910e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Vald=C3=A9s?= Date: Fri, 9 Feb 2024 17:00:34 +0100 Subject: [PATCH] N-tree is now created and queried with stretched arguments (duration 1 hour) --- Algebras/MainMemory2/MainMemory2Algebra.cpp | 187 +++++++++++--------- Algebras/Temporal/Temporal.examples | 6 +- Algebras/Temporal/TemporalAlgebra.cpp | 43 ++++- Algebras/Temporal/TemporalAlgebra.h | 45 ++++- 4 files changed, 176 insertions(+), 105 deletions(-) diff --git a/Algebras/MainMemory2/MainMemory2Algebra.cpp b/Algebras/MainMemory2/MainMemory2Algebra.cpp index b0dbbc33f..0d79e6f17 100644 --- a/Algebras/MainMemory2/MainMemory2Algebra.cpp +++ b/Algebras/MainMemory2/MainMemory2Algebra.cpp @@ -4136,6 +4136,99 @@ Operator matchbelowSOp( matchbelowSTM ); +/* +Auxiliary functions applied for M-tree2 and N-tree creation and querying. + +*/ +void StretchOrCompress(const Point& src, Point& res) { + res = src; +} + +void StretchOrCompress(const CcString& src, CcString& res) { + res = src; +} + +void StretchOrCompress(const CcInt& src, CcInt& res) { + res = src; +} + +void StretchOrCompress(const CcReal& src, CcReal& res) { + res = src; +} + +void StretchOrCompress(const Rectangle<1>& src, Rectangle<1>& res) { + res = src; +} + +void StretchOrCompress(const Rectangle<2>& src, Rectangle<2>& res) { + res = src; +} + +void StretchOrCompress(const Rectangle<3>& src, Rectangle<3>& res) { + res = src; +} + +void StretchOrCompress(const Rectangle<4>& src, Rectangle<4>& res) { + res = src; +} + +void StretchOrCompress(const Rectangle<8>& src, Rectangle<8>& res) { + res = src; +} + +void StretchOrCompress(const hist_hsv<64, false>& src, + hist_hsv<64, false>& res) { + res.CopyFrom(&src); +} + +void StretchOrCompress(const hist_hsv<128, false>& src, + hist_hsv<128, false>& res) { + res.CopyFrom(&src); +} + +void StretchOrCompress(const hist_hsv<256, false>& src, + hist_hsv<256, false>& res) { + res.CopyFrom(&src); +} + +void StretchOrCompress(const hist_hsv<256, true>& src, + hist_hsv<256, true>& res) { + res.CopyFrom(&src); +} + +void StretchOrCompress(const temporalalgebra::MPoint& src, + temporalalgebra::MPoint& res) { + datetime::DateTime duration(0, 3600000, datetime::durationtype); + StretchOrCompressToDuration(src, duration, true, true, res); +} + +void StretchOrCompress(const temporalalgebra::CUPoint& src, + temporalalgebra::CUPoint& res) { + datetime::DateTime duration(0, 3600000, datetime::durationtype); + StretchOrCompressUnitToDuration(src, duration, true, res); +} + +void StretchOrCompress(const temporalalgebra::CMPoint& src, + temporalalgebra::CMPoint& res) { + datetime::DateTime duration(0, 3600000, datetime::durationtype); + StretchOrCompressToDuration(src, duration, true, true, res); +} + +void StretchOrCompress(const stj::MLabel& src, stj::MLabel& res) { + res = src; +} + +void StretchOrCompress(const stj::MLabels& src, stj::MLabels& res) { + res = src; +} + +void StretchOrCompress(const stj::MPlace& src, stj::MPlace& res) { + res = src; +} + +void StretchOrCompress(const stj::MPlaces& src, stj::MPlaces& res) { + res = src; +} /* 6 M-tree support @@ -4694,11 +4787,13 @@ int mcreatemtree2StreamVM(Word* args, Word& result, int message, Word& local, sym = (Sym*)(tuple->GetAttribute(indexSym)); TupleIdentifier* tid = (TupleIdentifier*)(tuple->GetAttribute(indexTID)); if (tid->IsDefined()) { - Spa copySpa = *spa; - Sym copySym = *sym; - flobused = flobused || copySpa.NumOfFLOBs() > 0 || - copySym.NumOfFLOBs() > 0; - pair spasym(copySpa, copySym); + Spa spaStretched(true); + StretchOrCompress(*spa, spaStretched); + Sym symStretched(true); + StretchOrCompress(*sym, symStretched); + flobused = flobused || spaStretched.NumOfFLOBs() > 0 || + symStretched.NumOfFLOBs() > 0; + pair spasym(spaStretched, symStretched); MTreeEntry > entry(spasym, tid->GetTid()); // pair, TupleId> p(spasym, tid->GetTid()); tree->insert(entry); @@ -5985,84 +6080,6 @@ ListExpr mclosestCenterNTM(ListExpr args) { // nl->Second(a2)); } -/* -Auxiliary functions applied for N-tree creation and querying. - -*/ -void StretchOrCompress(const Point& src, Point& res) { - res = src; -} - -void StretchOrCompress(const CcString& src, CcString& res) { - res = src; -} - -void StretchOrCompress(const CcInt& src, CcInt& res) { - res = src; -} - -void StretchOrCompress(const CcReal& src, CcReal& res) { - res = src; -} - -void StretchOrCompress(const Rectangle<1>& src, Rectangle<1>& res) { - res = src; -} - -void StretchOrCompress(const Rectangle<2>& src, Rectangle<2>& res) { - res = src; -} - -void StretchOrCompress(const Rectangle<3>& src, Rectangle<3>& res) { - res = src; -} - -void StretchOrCompress(const Rectangle<4>& src, Rectangle<4>& res) { - res = src; -} - -void StretchOrCompress(const Rectangle<8>& src, Rectangle<8>& res) { - res = src; -} - -void StretchOrCompress(const hist_hsv<64, false>& src, - hist_hsv<64, false>& res) { - res.CopyFrom(&src); -} - -void StretchOrCompress(const hist_hsv<128, false>& src, - hist_hsv<128, false>& res) { - res.CopyFrom(&src); -} - -void StretchOrCompress(const hist_hsv<256, false>& src, - hist_hsv<256, false>& res) { - res.CopyFrom(&src); -} - -void StretchOrCompress(const hist_hsv<256, true>& src, - hist_hsv<256, true>& res) { - res.CopyFrom(&src); -} - -void StretchOrCompress(const temporalalgebra::MPoint& src, - temporalalgebra::MPoint& res) { - datetime::DateTime duration(0, 3600000, datetime::durationtype); - StretchOrCompressToDuration(src, duration, true, true, res); -} - -void StretchOrCompress(const temporalalgebra::CUPoint& src, - temporalalgebra::CUPoint& res) { - datetime::DateTime duration(0, 3600000, datetime::durationtype); - StretchOrCompressUnitToDuration(src, duration, true, res); -} - -void StretchOrCompress(const temporalalgebra::CMPoint& src, - temporalalgebra::CMPoint& res) { - datetime::DateTime duration(0, 3600000, datetime::durationtype); - StretchOrCompressToDuration(src, duration, true, true, res); -} - template class closestCenterNInfo { public: @@ -6399,7 +6416,7 @@ class distRangeNInfo { rel = mrel->getmmrel(); T* stretched = new T(true); StretchOrCompress(*ref, *stretched); - MTreeEntry p(*ref, 0); + MTreeEntry p(*stretched, 0); stretched->DeleteIfAllowed(); it = ntreeX->getNtreeX()->rangeSearch(p, range); } @@ -6814,7 +6831,7 @@ class mnearestNeighborNInfo { k = min((int)_k, (int)(rel->size())); T* stretched = new T(true); StretchOrCompress(*q, *stretched); - MTreeEntry p(*q, 0); + MTreeEntry p(*stretched, 0); stretched->DeleteIfAllowed(); it = ntreeX->getNtreeX()->nnSearch(p, k); sc = SecondoSystem::GetCatalog(); diff --git a/Algebras/Temporal/Temporal.examples b/Algebras/Temporal/Temporal.examples index 3e2dc6d9b..162c42fba 100644 --- a/Algebras/Temporal/Temporal.examples +++ b/Algebras/Temporal/Temporal.examples @@ -801,19 +801,19 @@ Operator : distanceAvg Number : 1 Signature : mpoint x mpoint (x duration) (x geoid) -> real Example : query distanceAvg(train5, train6) -Result : 17030.615965 +Result : 12368.294642 Operator : distanceAvgLB Number : 1 Signature : cXpoint x cXpoint (x duration) (x geoid) -> real (X = m|u) Example : query distanceAvgLB(cbbox(train5), cbbox(train6)) -Result : 13561.344094 +Result : 6433.35614 Operator : distanceAvgUB Number : 1 Signature : cXpoint x cXpoint (x duration) (x geoid) -> real (X = m|u) Example : query distanceAvgUB(cbbox(train5), cbbox(train6)) -Result : 23050.401494 +Result : 21866.491402 Operator : cbbox Number : 1 diff --git a/Algebras/Temporal/TemporalAlgebra.cpp b/Algebras/Temporal/TemporalAlgebra.cpp index e170aa9a7..f3d4031be 100644 --- a/Algebras/Temporal/TemporalAlgebra.cpp +++ b/Algebras/Temporal/TemporalAlgebra.cpp @@ -3346,6 +3346,8 @@ double CUPoint::DistanceAvg(const CUPoint& cup, const DateTime& duration, CMPoint cm1(true), cm2(true); cm1.Add(*this); cm2.Add(cup); + cout << *this << endl << cup; + cout << endl << endl << cm1 << endl << cm2 << endl; return DistanceComputation::DistanceAvg(cm1, cm2, duration, upperBound, geoid); } @@ -12978,7 +12980,7 @@ ListExpr StretchTypeMap(ListExpr args) { return nl->TypeError(); } if (MPoint::checkType(nl->First(args)) || - CMPoint::checkType(nl->Second(args))) { + CMPoint::checkType(nl->First(args))) { if (Duration::checkType(nl->Second(args))) { return nl->First(args); } @@ -14638,8 +14640,16 @@ int DistanceAvgMap(Word* args, Word& result, int message, Word& local, else if (qp->GetNoSons(s) == 3) { geoid = (Geoid*)args[2].addr; } - DistanceComputation::DistanceAvg(*((MPoint*)args[0].addr), - *((MPoint*)args[1].addr), *duration, true, *((CcReal*)result.addr), geoid); + MPoint *stretched1 = new MPoint(true); + StretchOrCompressToDuration(*((MPoint*)args[0].addr), + *duration, true, true, *stretched1); + MPoint *stretched2 = new MPoint(true); + StretchOrCompressToDuration(*((MPoint*)args[1].addr), + *duration, true, true, *stretched2); + DistanceComputation::DistanceAvg(*stretched1, *stretched2, + *duration, true, *((CcReal*)result.addr), geoid); + stretched1->DeleteIfAllowed(); + stretched2->DeleteIfAllowed(); return 0; } @@ -14660,9 +14670,16 @@ int DistanceAvgCMMap(Word* args, Word& result, int message, Word& local, else if (qp->GetNoSons(s) == 3) { geoid = (Geoid*)args[2].addr; } - DistanceComputation::DistanceAvg(*((CMPoint*)args[0].addr), - *((CMPoint*)args[1].addr), *duration, - upperBound, *((CcReal*)result.addr), geoid); + CMPoint *stretched1 = new CMPoint(true); + StretchOrCompressToDuration(*((CMPoint*)args[0].addr), + *duration, true, true, *stretched1); + CMPoint *stretched2 = new CMPoint(true); + StretchOrCompressToDuration(*((CMPoint*)args[1].addr), + *duration, true, true, *stretched2); + DistanceComputation::DistanceAvg(*stretched1, *stretched2, + *duration, upperBound, *((CcReal*)result.addr), geoid); + stretched1->DeleteIfAllowed(); + stretched2->DeleteIfAllowed(); return 0; } @@ -14682,9 +14699,17 @@ int DistanceAvgCUMap(Word* args, Word& result, int message, Word& local, } else if (qp->GetNoSons(s) == 3) { geoid = (Geoid*)args[2].addr; - } - ((CUPoint*)args[0].addr)->DistanceAvg(*((CUPoint*)args[1].addr), *duration, - upperBound, *((CcReal*)result.addr), geoid); + } + CUPoint *stretched1 = new CUPoint(true); + StretchOrCompressUnitToDuration(*((CUPoint*)args[0].addr), *duration, + true, *stretched1); + CUPoint *stretched2 = new CUPoint(true); + StretchOrCompressUnitToDuration(*((CUPoint*)args[1].addr), *duration, + true, *stretched2); + stretched1->DistanceAvg(*stretched2, *duration, upperBound, + *((CcReal*)result.addr), geoid); + stretched1->DeleteIfAllowed(); + stretched2->DeleteIfAllowed(); return 0; } diff --git a/Algebras/Temporal/TemporalAlgebra.h b/Algebras/Temporal/TemporalAlgebra.h index a38beb1aa..e478279df 100644 --- a/Algebras/Temporal/TemporalAlgebra.h +++ b/Algebras/Temporal/TemporalAlgebra.h @@ -4417,7 +4417,7 @@ MappingNoFlob MappingNoFlob::operator=(const M& src) { if (isdefined) { units.clear(); units.reserve(src.GetNoComponents()); - UPoint unit(true); + U unit(true); for (int i = 0; i < src.GetNoComponents(); i++) { src.Get(i, unit); Add(unit); @@ -4623,6 +4623,14 @@ void StretchOrCompressToFactor(const M& src, const double factor, constantUnit.SetToConstantUnit(lastUnit.p1, 0.0); constantUnit.timeInterval = Interval(lastNewIv.end,nextStart, !lastNewIv.rc, !iv.lc); + oldDuration += (iv.start - lastOldIv.end); + newDuration += (nextStart - lastNewIv.end); + durationDiff = oldDuration * factor - newDuration; + if (!durationDiff.IsZero()) { + constantUnit.timeInterval.end += durationDiff; + nextStart += durationDiff; + newDuration += durationDiff; + } result.Add(constantUnit); } newUnit.timeInterval = Interval(nextStart, @@ -4654,6 +4662,7 @@ void StretchOrCompressToFactor(const M& src, const double factor, result.Add(newUnit); i++; } + result.GetDuration(oldDuration); } template @@ -4695,8 +4704,11 @@ void StretchOrCompressUnitToDuration(const U& src, result.SetDefined(false); return; } - result.SetDefined(true); result = src; + if (startAtBeginOfTime) { + Instant beginOfTime(0.0); + result.timeInterval.start = beginOfTime; + } result.timeInterval.end = result.timeInterval.start + duration; } @@ -10018,12 +10030,29 @@ static double DistanceAvg(const M& mp1, const M& mp2, return DBL_MAX; } // cout << "original: " << mp1 << endl << mp2 << endl << endl; - MappingNoFlob m1(mp1.GetNoComponents()), m2(mp2.GetNoComponents()); - ForceToDuration(mp1, duration, true, m1, geoid); - ForceToDuration(mp2, duration, true, m2, geoid); - if (mp1.Compare(&mp2) == -1) { - std::swap(m1, m2); - } + MappingNoFlob m1(mp1), m2(mp2); + datetime::DateTime durationTest(0, 0, datetime::durationtype); + mp1.GetDuration(durationTest); + if (!(durationTest == duration)) { + cout << duration << " != " << durationTest << endl; + } + assert(durationTest == duration); + // if (!(durationTest == duration)) { + // cout << "EXECUTE ForceToDuration for mp1 " << durationTest << endl; + // ForceToDuration(mp1, duration, true, m1, geoid); + // } + mp2.GetDuration(durationTest); + if (!(durationTest == duration)) { + cout << duration << " # " << durationTest << endl; + } + assert(durationTest == duration); + // if (!(durationTest == duration)) { + // cout << "EXECUTE ForceToDuration for mp2 " << durationTest << endl; + // ForceToDuration(mp2, duration, true, m2, geoid); + // } + // if (mp1.Compare(&mp2) == -1) { + // std::swap(m1, m2); + // } double durTemp(0.0), sum(0.0); U u1(true), u2(true), u1cut(true), u2cut(true), up1(true), up2(true); RefinementPartition, MappingNoFlob, U, U> rp(m1,m2);