ChainSearch: draft interface and possible implementation approach

The intention is to augment the iterator based (linear) search
used in EventLog to allow for real backtracking, based on a evaluation tree.
This should be rather staight forward to implement, relying on the
exploreChildren() functionality of TreeExplorer. The trick is to package
the chained search step as a monadic flatMap operation
This commit is contained in:
Fischlurch 2018-09-04 02:02:26 +02:00
parent 9d7ce1e6a4
commit ec8d0557e8
4 changed files with 427 additions and 6 deletions

View file

@ -0,0 +1,181 @@
/*
ITER-CHAIN-SEARCH.hpp - chained search with backtracking based on (bidirectional) iterator
Copyright (C) Lumiera.org
2018, Hermann Vosseler <Ichthyostega@web.de>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/** @file iter-cursor.hpp
** An iterator with the ability to switch direction.
** This wrapper relies on the ability of typical STL container iterators
** to work in both directions, similar to std::reverse_iterator.
** Yet it is a single, self-contained element and in compliance to the
** ["Lumiera Forward Iterator"](iter-adapter.hpp) concept. But it has
** the additional ability to [switch the working direction](\ref IterCursor<IT>::switchDir).
**
** @see IterCursor_test
** @see iter-adapter.hpp
** @see [usage example](event-log.hpp)
**
*/
#ifndef SRC_LIB_ITER_CHAIN_SEARCH_H
#define SRC_LIB_ITER_CHAIN_SEARCH_H
#include "lib/error.hpp"
//#include "lib/iter-adapter.hpp"
//#include <type_traits>
//#include <utility>
#include <utility>
#include <string>
namespace lib {
using std::move;
using std::string;
namespace iter {
/**
* @internal implementation for....
*/
template<class IT>
class Cur
{
public:
};
} // namespace iter
/**
* Iterator based linear search mechanism, with the ability to perform consecutive search with backtracking.
* The IterChainSearch can be configured with a sequence of search goals (filter conditions), and will
* apply these in succession on the underlying iterator. It will search for the first hit of the first
* condition, and then continue to search _from there_ matching on the second condition, and so on.
* After the first combination of matches is exhausted, the search will backtrack and try to evaluate
* the next combination, leading to a tree of search solutions.
*/
// template<class IT>
class IterChainSearch
{
// using _Core = iter::CursorGear<IT>;
// using _Parent = IterStateWrapper<typename _Core::value_type, _Core>;
public:
// IterChainSearch() =default;
// IterChainSearch (IterChainSearch&&) =default;
// IterChainSearch (IterChainSearch const&) =default;
// IterChainSearch& operator= (IterChainSearch&&) =default;
// IterChainSearch& operator= (IterChainSearch const&) =default;
// template<class CON>
// explicit
// IterChainSearch (CON& container)
//// : _Parent(_Core(container.begin(), container.end()))
// { }
//
// IterChainSearch (IT&& begin, IT&& end)
//// : _Parent(_Core(std::forward<IT>(begin), std::forward<IT>(end)))
// { }
operator bool() const
{
UNIMPLEMENTED ("solution test");
}
bool
empty() const
{
return not(*this);
}
/** */
IterChainSearch&&
search (string target)
{
UNIMPLEMENTED ("configure additional chained search condition");
return move(*this);
}
IterChainSearch&&
clearFilter()
{
UNIMPLEMENTED ("drop all search condition frames");
return move(*this);
}
/////////////////////////TODO dummy placeholder code. Replace by real iterator access
string&
operator*() const
{
static string boo{"bääääh"};
return boo;
}
IterChainSearch&
operator++()
{
return *this;
}
/////////////////////////TODO dummy placeholder code. Replace by real iterator access
};
/* ==== convenient builder free function ==== */
/** setup a chain search configuration by suitably wrapping the given container.
* @return a TreeEplorer, which is an Iterator to yield all the source elements,
* but may also be used to build an processing pipeline.
* @warning if you capture the result of this call by an auto variable,
* be sure to understand that invoking any further builder operation on
* TreeExplorer will invalidate that variable (by moving it into the
* augmented iterator returned from such builder call).
*/
template<class CON>
inline auto
chainSearch (CON&& srcData)
{
// using SrcIter = typename _DecoratorTraits<IT>::SrcIter;
// using Base = iter_explorer::BaseAdapter<SrcIter>;
UNIMPLEMENTED ("figure out the Iterator type and build a suitable IterChainSearch instance");
return IterChainSearch();
// return TreeExplorer<Base> (std::forward<IT> (srcSeq));
}
} // namespace lib
#endif /*SRC_LIB_ITER_CHAIN_SEARCH_H*/

View file

@ -1445,7 +1445,7 @@ namespace lib {
/** _terminal builder_ to package the processing pipeline as IterSource.
* Invoking this function moves the whole iterator compound, as assembled by the preceding
* builder calls, into heap allocated memory and returns a [iterator front-end](\ref IterExploreSource).
* builder calls, into heap allocated memory and returns an [iterator front-end](\ref IterExploreSource).
* Any iteration and manipulation on that front-end is passed through virtual function calls into
* the back-end, thereby concealing all details of the processing pipeline.
*/

View file

@ -0,0 +1,145 @@
/*
IterChainSearch(Test) - verify chained search operations with backtracking
Copyright (C) Lumiera.org
2018, Hermann Vosseler <Ichthyostega@web.de>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
* *****************************************************/
/** @file iter-chain-search-test.cpp
** unit test \ref IterChainSearch_test
*/
#include "lib/test/run.hpp"
#include "lib/test/test-helper.hpp"
#include "lib/format-util.hpp"
#include "lib/iter-chain-search.hpp"
#include "lib/util.hpp"
#include <vector>
#include <string>
namespace lib {
namespace test{
using ::Test;
using util::join;
using util::isnil;
using util::isSameObject;
using std::vector;
using std::string;
using lumiera::error::LERR_(ITER_EXHAUST);
namespace { // test fixture
using Spam = vector<string>;
const Spam SPAM{"spam"
,"sausage"
,"spam"
,"spam"
,"bacon"
,"spam"
,"tomato"
,"and"
,"spam"
};
} // (END)fixture
/*****************************************************************//**
* @test verify a setup for consecutive searches with backtracking.
*
* @see iter-chain-search.hpp
* @see iter-cursor.hpp
* @see [usage example](event-log.hpp)
*/
class IterChainSearch_test : public Test
{
virtual void
run (Arg)
{
simpleSearch();
chainedIteration();
backtracking();
}
/** @test simple basic use case. */
void
simpleSearch ()
{
auto search = chainSearch(SPAM)
.search("bacon")
.search("tomato");
CHECK (search);
CHECK (not isnil(search));
CHECK ("tomato" == *search);
CHECK (isSameObject (*search, SPAM[6]));
search.clearFilter();
CHECK ("tomato" == *search);
++search;
CHECK ("and" == *search);
search.search("spam");
CHECK ("spam" == *search);
CHECK (isSameObject (*search, SPAM[8]));
++search;
CHECK (not search);
CHECK (isnil (search));
VERIFY_ERROR (ITER_EXHAUST, *search);
}
/** @test verify the
*/
void
chainedIteration ()
{
}
/** @test verify the
*/
void
backtracking ()
{
}
};
LAUNCHER (IterChainSearch_test, "unit common");
}} // namespace lib::test

View file

@ -31173,6 +31173,8 @@
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1535938028929" ID="ID_1910123172" MODIFIED="1535938091638" TEXT="Backtracking Search-Engine einbauen">
<arrowlink COLOR="#23408f" DESTINATION="ID_1801538785" ENDARROW="Default" ENDINCLINATION="20;-55;" ID="Arrow_ID_1048873840" STARTARROW="None" STARTINCLINATION="-120;0;"/>
<icon BUILTIN="flag-yellow"/>
<node CREATED="1535939763441" ID="ID_132867774" MODIFIED="1535939780642" TEXT="mu&#xdf; &quot;nur noch&quot; Cursor gegen neue State-Core austauschen"/>
<node CREATED="1535939781302" ID="ID_285459121" MODIFIED="1535939796824" TEXT="diese h&#xe4;lt den Cursor plus einen Stack mit den Filtern"/>
</node>
</node>
<node CREATED="1535893799799" ID="ID_706866636" MODIFIED="1535938019075" TEXT="Auswertungs-Stack">
@ -31197,15 +31199,98 @@
<node CREATED="1535894029921" ID="ID_152779506" MODIFIED="1535894037411" TEXT="den Stack schrittweise hochsteigen"/>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1535894097279" ID="ID_1801538785" MODIFIED="1535938091638" TEXT="als eigene Lib-Klasse bauen">
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1535894097279" ID="ID_1801538785" MODIFIED="1536018600721" TEXT="als eigene Lib-Klasse bauen">
<linktarget COLOR="#23408f" DESTINATION="ID_1801538785" ENDARROW="Default" ENDINCLINATION="20;-55;" ID="Arrow_ID_1048873840" SOURCE="ID_1910123172" STARTARROW="None" STARTINCLINATION="-120;0;"/>
<icon BUILTIN="flag-yellow"/>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1535894115317" ID="ID_1507592237" MODIFIED="1535938112691" TEXT="Frage: wie konkret?">
<icon BUILTIN="pencil"/>
<node COLOR="#435e98" CREATED="1535894115317" ID="ID_1507592237" MODIFIED="1536018340937" TEXT="Design-Frage: wie konkret?">
<icon BUILTIN="help"/>
<node CREATED="1536018209730" ID="ID_1130583934" MODIFIED="1536018221348" TEXT="nur das generische Auswertungs-Schema"/>
<node CREATED="1536018222528" ID="ID_1509547591" MODIFIED="1536018255176" TEXT="die einzelnen Auswertungs-Schritte als Funktor (Monaden-Stil)"/>
<node CREATED="1536018255860" ID="ID_1267468732" MODIFIED="1536018275981" TEXT="der unterliegende Iterator als Template-Parameter"/>
<node CREATED="1536018293535" ID="ID_361886715" MODIFIED="1536018315757" TEXT="Abk&#xfc;rzung f&#xfc;r direkten Wert-equality-Match"/>
<node CREATED="1536018276593" ID="ID_1222260775" MODIFIED="1536018287299" TEXT="prekonfigurierte Builder-Funktionen f&#xfc;r Standardf&#xe4;lle"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1535894132587" ID="ID_1888561007" MODIFIED="1535938003909" TEXT="eigenst&#xe4;ndiger Unit-Test">
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1536018364845" ID="ID_1351452920" MODIFIED="1536018457506" TEXT="Implementierung">
<icon BUILTIN="flag-yellow"/>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1536018375244" ID="ID_1906913554" MODIFIED="1536018458786" TEXT="Kern: Explore-Mechanismus">
<icon BUILTIN="flag-yellow"/>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1536018644456" ID="ID_477475522" MODIFIED="1536018654007" TEXT="Signatur der Explore-Funktion festlegen">
<icon BUILTIN="flag-yellow"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1536018705511" ID="ID_1131109871" MODIFIED="1536018800891" TEXT="greedy Tiefensuche">
<icon BUILTIN="flag-yellow"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1536018802738" ID="ID_1560348563" MODIFIED="1536018807506" TEXT="Abbruchbedingung">
<icon BUILTIN="flag-yellow"/>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1536018816272" ID="ID_40740226" MODIFIED="1536018828687" TEXT="Ende der Filterkette">
<icon BUILTIN="flag-yellow"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1536018829583" ID="ID_722941976" MODIFIED="1536018849229" TEXT="aktueller Filter ersch&#xf6;pft">
<icon BUILTIN="flag-yellow"/>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1536018862802" ID="ID_1044555993" MODIFIED="1536018873126" TEXT="abgeleiteten Frame konstruieren">
<icon BUILTIN="flag-yellow"/>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1536018406232" ID="ID_1330264339" MODIFIED="1536018459425" TEXT="Schritte als Funktoren">
<icon BUILTIN="flag-yellow"/>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1536018684754" ID="ID_1076606298" MODIFIED="1536018696034" TEXT="geeignete Signatur f&#xfc;r die Schritt-Funktoren">
<icon BUILTIN="flag-yellow"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1536018696977" ID="ID_1710558292" MODIFIED="1536018702049" TEXT="Sequenz solcher Funktoren">
<icon BUILTIN="flag-yellow"/>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1536018923378" ID="ID_1163666446" MODIFIED="1536018929138" TEXT="Pipeline konstruieren">
<icon BUILTIN="flag-yellow"/>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1536018931433" ID="ID_1532889609" MODIFIED="1536018943472" TEXT="treeExplorer-Konfiguration">
<icon BUILTIN="flag-yellow"/>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1536018420398" ID="ID_41628967" MODIFIED="1536018460817" TEXT="Abgreifen des Iterators von STL-Container">
<icon BUILTIN="flag-yellow"/>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1536018880168" ID="ID_1619721301" MODIFIED="1536019004173" TEXT="Wert-Typ finden">
<linktarget COLOR="#a9b4c1" DESTINATION="ID_1619721301" ENDARROW="Default" ENDINCLINATION="129;0;" ID="Arrow_ID_1721152439" SOURCE="ID_1700266521" STARTARROW="None" STARTINCLINATION="76;-5;"/>
<icon BUILTIN="flag-yellow"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1536018887223" ID="ID_832237706" MODIFIED="1536018900966" TEXT="Range-Iter konstruieren">
<icon BUILTIN="flag-yellow"/>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1536018442123" ID="ID_829730232" MODIFIED="1536018463121" TEXT="Abk&#xfc;rzung f&#xfc;r direkten Match">
<icon BUILTIN="flag-yellow"/>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1536018985450" ID="ID_1700266521" MODIFIED="1536019004173" TEXT="Wert-Typ deduzieren">
<arrowlink DESTINATION="ID_1619721301" ENDARROW="Default" ENDINCLINATION="129;0;" ID="Arrow_ID_1721152439" STARTARROW="None" STARTINCLINATION="76;-5;"/>
<icon BUILTIN="flag-yellow"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1536019008151" ID="ID_29813547" MODIFIED="1536019026064" TEXT="equality comparision in Lambda">
<icon BUILTIN="flag-yellow"/>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1536018471975" ID="ID_1115030606" MODIFIED="1536018481902" TEXT="Variante f&#xfc;r bidirektionale Suche">
<icon BUILTIN="flag-yellow"/>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1536018956285" ID="ID_128441804" MODIFIED="1536018975076" TEXT="in CursorGear einwickeln">
<icon BUILTIN="flag-yellow"/>
</node>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1535894132587" ID="ID_1888561007" MODIFIED="1536018524436" TEXT="Unit-Test: IterChainSearch_test">
<icon BUILTIN="flag-yellow"/>
<node COLOR="#338800" CREATED="1536018569210" ID="ID_1083474400" MODIFIED="1536018578102" TEXT="Test-Fixture">
<icon BUILTIN="button_ok"/>
<node CREATED="1536018580388" ID="ID_186304417" MODIFIED="1536018584425" TEXT="lovely spam">
<font ITALIC="true" NAME="SansSerif" SIZE="12"/>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1536018539621" ID="ID_1857591081" MODIFIED="1536018556917" TEXT="simpleSearch()">
<icon BUILTIN="flag-yellow"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1536018544936" ID="ID_1750921303" MODIFIED="1536018557580" TEXT="chainedIteration()">
<icon BUILTIN="flag-yellow"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1536018552912" ID="ID_385004210" MODIFIED="1536018557955" TEXT="backtracking()">
<icon BUILTIN="flag-yellow"/>
<node CREATED="1535894140882" ID="ID_1145179443" MODIFIED="1535894190927" TEXT="IterTreeExplorer_test nicht zu sehr &#xfc;berladen!"/>
</node>
</node>
</node>
@ -31213,6 +31298,16 @@
</node>
</node>
</node>
<node CREATED="1536015468698" ID="ID_1045359957" MODIFIED="1536015470477" TEXT="utils">
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1536015471377" ID="ID_1732583592" MODIFIED="1536015487230" TEXT="regex-Iteratoren">
<icon BUILTIN="hourglass"/>
<node CREATED="1536015489143" ID="ID_979562288" MODIFIED="1536015508744" TEXT="std::sregex_token_itertor"/>
<node CREATED="1536015509308" ID="ID_1011220828" MODIFIED="1536015512488" TEXT="geschickt verpacken"/>
<node CREATED="1536015513076" ID="ID_849722427" MODIFIED="1536015521862" TEXT="den Regex-Include opaque halten"/>
<node CREATED="1536015525154" ID="ID_1039059758" MODIFIED="1536015533853" TEXT="soll mal in util.hpp"/>
</node>
</node>
</node>
</node>
<node CREATED="1482524641484" ID="ID_1651495185" MODIFIED="1518487921096" TEXT="Architektur">
<node CREATED="1531419748046" ID="ID_1791265013" MODIFIED="1531419752937" TEXT="Lebenszyklus">