From ea0416881e9c891eff4fe3142fd62cbc64c2bee3 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Mon, 18 Feb 2008 04:16:53 +0100 Subject: [PATCH] WIP towards a asset::Struct naming scheme --- src/common/query.cpp | 37 +++++++++++ src/common/query.hpp | 9 ++- src/common/query/mockconfigrules.cpp | 18 +++++- src/proc/asset/pipe.cpp | 35 +++++------ src/proc/asset/pipe.hpp | 4 +- src/proc/asset/struct.cpp | 37 +++++------ src/proc/asset/struct.hpp | 1 + src/proc/asset/structfactoryimpl.hpp | 61 ++++++++++++------- tests/components/proc/asset/basicpipetest.cpp | 11 ++-- 9 files changed, 143 insertions(+), 70 deletions(-) diff --git a/src/common/query.cpp b/src/common/query.cpp index bb3e6ae86..152086bc3 100644 --- a/src/common/query.cpp +++ b/src/common/query.cpp @@ -26,10 +26,18 @@ #include "nobugcfg.h" #include +#include +#include +using std::map; +using boost::regex; +using boost::smatch; +using boost::regex_search; using boost::algorithm::is_upper; using boost::algorithm::is_alpha; +using util::contains; + namespace cinelerra { @@ -48,6 +56,35 @@ namespace cinelerra if (is_upper() (first)) id[0] = std::tolower (first); } + + + + + namespace // Implementation details + { + map regexTable; + } + + /** (preliminary) helper: instead of really parsing and evaluating the terms, + * just do a regular expression match to extract the literal argument + * behind the given predicate symbol. e.g calling + * queryID ("stream", "id(abc), stream(mpeg)") + * yields "mpeg" + */ + const string + extractID (Symbol sym, const string& termString) + { + + if (!contains (regexTable, sym)) + regexTable[sym] = regex (string(sym)+="\\(\\s*([\\w_\\.\\-]+)\\s*\\)"); + + smatch match; + if (regex_search (termString, match, regexTable[sym])) + return string (match[1]); + else + return ""; + + } } // namespace query diff --git a/src/common/query.hpp b/src/common/query.hpp index b5575f0da..9bdb3a389 100644 --- a/src/common/query.hpp +++ b/src/common/query.hpp @@ -28,11 +28,13 @@ #include #include +#include namespace cinelerra { using std::string; + using boost::format; /* ==== comon definitions for rule based queries ==== */ @@ -47,7 +49,9 @@ namespace cinelerra class Query : public std::string { public: - Query (const string& predicate="") : string(predicate) {} + explicit Query (const string& predicate="") : string(predicate) {} + explicit Query (format& pattern) : string(str(pattern)) {} + const string asKey() const { @@ -65,6 +69,9 @@ namespace cinelerra */ void normalizeID (string& id); + const string extractID (Symbol, const string& termString); + + } // namespace query diff --git a/src/common/query/mockconfigrules.cpp b/src/common/query/mockconfigrules.cpp index 52f7f93fd..54ba5aaf1 100644 --- a/src/common/query/mockconfigrules.cpp +++ b/src/common/query/mockconfigrules.cpp @@ -67,6 +67,14 @@ namespace cinelerra Ptr obj = Struct::create (query); return AnyPair(query.asKey(), obj); } + + /** shortcut for simply accessing a table entry */ + template + any& + item (PTAB& table, const string& query) + { + return (*table)[Query(query).asKey()]; + } } @@ -82,8 +90,16 @@ namespace cinelerra INFO (config, "creating mock answers for some config queries..."); isInit_ = true; // allow re-entrance + typedef const ProcPatt cPP; + // for baiscpipetest.cpp --------- - answer_->insert (entry_Struct ("stream(teststream)")); + answer_->insert (entry_Struct ("stream(video)")); + answer_->insert (entry_Struct ("stream(teststream)")); + item (answer_, "stream(default)") = item (answer_,"stream(video)"); //TODO killme + + answer_->insert (entry_Struct ("pipe(master), stream(video)")); + item (answer_, "pipe(default)") = item(answer_,"pipe(master), stream(video)"); //TODO killme + TODO ("remove the default entries!!! DefaultsManager should find them automatically"); } diff --git a/src/proc/asset/pipe.cpp b/src/proc/asset/pipe.cpp index e15a09254..78899e844 100644 --- a/src/proc/asset/pipe.cpp +++ b/src/proc/asset/pipe.cpp @@ -22,35 +22,30 @@ #include "proc/asset/pipe.hpp" +#include "common/util.hpp" + +using util::isnil; namespace asset { - namespace // Pipe Asset implementation details - { - /** @internal derive a sensible asset ident tuple when creating - * a pipe asset based on a query - * @todo define the actual naming scheme of struct assets - */ - const Asset::Ident - createPipeIdent (PProcPatt& wiring, string& id, wstring& shortD, wstring& longD) - { - string name ("pipe-" + id); // TODO something sensible here; append number, sanitize etc. - TODO ("Implement pipe name scheme!!"); - Category category (STRUCT,"pipes"); - return Asset::Ident (name, category ); - } - } /** */ - Pipe::Pipe (PProcPatt& wiring, string pipeID, wstring shortDesc, wstring longDesc) - : Struct (createPipeIdent (wiring,pipeID,shortDesc,longDesc)), + Pipe::Pipe ( const Asset::Ident& idi + , PProcPatt& wiring + , const string& pipeID + , wstring shortName + , wstring longName + ) + : Struct (idi), pipeID_ (pipeID), wiringTemplate(wiring), - shortDesc (shortDesc), - longDesc (longDesc) + shortDesc (shortName), + longDesc (longName) { - + REQUIRE (!isnil (pipeID)); + if (isnil (shortDesc)) + shortDesc = wstring (pipeID.begin(), pipeID.end()); } diff --git a/src/proc/asset/pipe.hpp b/src/proc/asset/pipe.hpp index 0761efe65..ea354361d 100644 --- a/src/proc/asset/pipe.hpp +++ b/src/proc/asset/pipe.hpp @@ -52,7 +52,7 @@ namespace asset */ class Pipe : public Struct { - string pipeID_; + const string pipeID_; PProcPatt wiringTemplate; public: @@ -66,7 +66,7 @@ namespace asset protected: - Pipe (PProcPatt& wiring, string pipeID="", wstring shortDesc =wstring(), wstring longDesc =wstring()) ; + Pipe (const Asset::Ident&, PProcPatt& wiring, const string& pipeID, wstring shortName =wstring(), wstring longName =wstring()) ; friend class StructFactory; friend class StructFactoryImpl; diff --git a/src/proc/asset/struct.cpp b/src/proc/asset/struct.cpp index 47586202a..bcaff3118 100644 --- a/src/proc/asset/struct.cpp +++ b/src/proc/asset/struct.cpp @@ -26,7 +26,6 @@ #include "proc/asset/procpatt.hpp" #include "proc/asset/track.hpp" #include "proc/asset/pipe.hpp" -#include "proc/mobject/session.hpp" #include "common/configrules.hpp" #include "proc/asset/structfactoryimpl.hpp" @@ -34,18 +33,16 @@ #include "common/util.hpp" #include "nobugcfg.h" -#include +#include -using boost::regex; -using boost::smatch; -using boost::regex_search; +using boost::format; -using mobject::Session; +using cinelerra::Symbol; using cinelerra::query::normalizeID; - -using cinelerra::ConfigRules; using cinelerra::query::QueryHandler; +using cinelerra::ConfigRules; +using util::contains; namespace asset @@ -53,11 +50,6 @@ namespace asset /****** NOTE: not really implemented yet. What follows is partially a hack to build simple tests *******/ - namespace // Implementation details - { - regex streamID_pattern("stream\\(\\s*(\\w+)\\s*\\)"); - } - /** query the currently defined properties of this @@ -65,12 +57,15 @@ namespace asset const string Struct::queryStreamID() const { - smatch match; - - if (regex_search (this->ident.name, match, streamID_pattern)) - return string (match[1]); - else - return ""; + return cinelerra::query::extractID ("stream", this->ident.name); + } + + /** query the currently defined properties of this + structural asset for a stream-ID predicate */ + const string + Struct::queryPipeID() const + { + return cinelerra::query::extractID ("pipe", this->ident.name); } @@ -126,8 +121,8 @@ namespace asset { normalizeID (pipeID); normalizeID (streamID); - PProcPatt processingPattern = Session::current->defaults (Query("stream("+streamID+")")); - Pipe* pP = new Pipe (processingPattern, pipeID); + static format descriptor("pipe(%s), stream(%s)."); + Pipe* pP = impl_->fabricate (Query (descriptor % pipeID % streamID)); return AssetManager::instance().wrap (*pP); } diff --git a/src/proc/asset/struct.hpp b/src/proc/asset/struct.hpp index 4beb5479e..ed6642690 100644 --- a/src/proc/asset/struct.hpp +++ b/src/proc/asset/struct.hpp @@ -85,6 +85,7 @@ namespace asset } const string queryStreamID() const; + const string queryPipeID() const; protected: diff --git a/src/proc/asset/structfactoryimpl.hpp b/src/proc/asset/structfactoryimpl.hpp index a1086a9fa..3d78920d3 100644 --- a/src/proc/asset/structfactoryimpl.hpp +++ b/src/proc/asset/structfactoryimpl.hpp @@ -34,16 +34,22 @@ #define ASSET_STRUCTFACTORYIMPL_H +#include "proc/mobject/session.hpp" #include "common/configrules.hpp" #include "common/error.hpp" +#include "common/util.hpp" #include using boost::format; +using mobject::Session; + +using util::isnil; +using util::contains; using asset::Query; using cinelerra::query::CINELERRA_ERROR_CAPABILITY_QUERY; - +using cinelerra::query::extractID; namespace asset { @@ -54,16 +60,20 @@ namespace asset { static Symbol namePrefix; static Symbol catFolder; + static Symbol idSymbol; }; - template<> Symbol Traits::namePrefix = "track-"; + template<> Symbol Traits::namePrefix = "track"; template<> Symbol Traits::catFolder = "tracks"; + template<> Symbol Traits::idSymbol = "track"; - template<> Symbol Traits::namePrefix = "pipe-"; + template<> Symbol Traits::namePrefix = "pipe"; template<> Symbol Traits::catFolder = "pipes"; + template<> Symbol Traits::idSymbol = "pipe"; - template<> Symbol Traits::namePrefix = "patt-"; + template<> Symbol Traits::namePrefix = "patt"; template<> Symbol Traits::catFolder = "build-templates"; + template<> Symbol Traits::idSymbol = "procPatt"; @@ -78,30 +88,32 @@ namespace asset /** @internal derive a sensible asset ident tuple when creating * structural asset instances based on a capability query - * @todo define the actual naming scheme of struct assets */ template const Asset::Ident createIdent (const Query& query) { - static int i=0; - static format namePattern ("%s%d-%s"); // TODO finally just use the capability string as name?? - string name = str(namePattern % Traits::namePrefix % (++i) % query); - TODO ("struct asset naming scheme??"); + string name (query); + string nameID = extractID (Traits::idSymbol, query); + if (isnil (nameID)) + { + // no name-ID contained in the query... + // so we'll create a new one + static int i=0; + static format namePattern ("%s.%d"); + static format predPattern ("%s(%s), "); + nameID = str(namePattern % Traits::namePrefix % (++i) ); + name.insert(0, + str(predPattern % Traits::idSymbol % nameID )); + } + ENSURE (!isnil (name)); + ENSURE (!isnil (nameID)); + ENSURE (contains (name, nameID)); + Category cat (STRUCT, Traits::catFolder); return Asset::Ident (name, cat ); } - typedef std::pair PipeIDs; - - PipeIDs - createPipeIdent (const Query& query) - { - string name (Traits::namePrefix + query); // TODO get some more sensible dummy values - TODO ("pipe naming scheme??"); - TODO ("actually extract the pipe stream type from the query..."); - return PipeIDs (name, "data"); // dummy stream type - } @@ -153,8 +165,15 @@ namespace asset Pipe* StructFactoryImpl::fabricate (const Query& caps) { - PipeIDs ids (createPipeIdent (caps)); - return recursive_create_ (ids.first, ids.second).get(); + const Asset::Ident idi (createIdent (caps)); + string pipeID = extractID ("pipe", idi.name); + string streamID = extractID ("stream", caps); + if (isnil (streamID)) streamID = "default"; + PProcPatt processingPattern = Session::current->defaults (Query("stream("+streamID+")")); + return new Pipe( idi + , processingPattern + , pipeID + ); } diff --git a/tests/components/proc/asset/basicpipetest.cpp b/tests/components/proc/asset/basicpipetest.cpp index 315ba83a7..62644b95a 100644 --- a/tests/components/proc/asset/basicpipetest.cpp +++ b/tests/components/proc/asset/basicpipetest.cpp @@ -65,11 +65,12 @@ namespace asset { virtual void run(Arg arg) { - string pipeID = isnil(arg)? "blackHole" : arg[1]; + string pipeID = isnil(arg)? "Black Hole" : arg[1]; string streamID = 2>arg.size()? "teststream" : arg[2] ; createExplicit (pipeID,streamID); create_or_ref (pipeID); + create_using_default (); dependProcPatt (pipeID); } @@ -80,6 +81,7 @@ namespace asset { string pID_sane (pID); normalizeID (pID_sane); + ASSERT (pID_sane != pID); PPipe thePipe = asset::Struct::create (pID,sID); @@ -118,6 +120,7 @@ namespace asset ASSERT (c1 == c2); PPipe pipe3 = Pipe::query ("pipe("+pID2+")"); +//////////////////////////////////////////////////////////////TODO: er macht eine Neue, anstatt die Bestehende zu finden ASSERT (pipe3 == pipe2); } @@ -130,7 +133,7 @@ namespace asset ASSERT (pipe1 == Session::current->defaults (Query())); ASSERT (pipe1->ident.category.hasKind(VIDEO)); ASSERT (pipe1->getProcPatt()); - PProcPatt popa = Session::current->defaults (Query("pipe()")); + PProcPatt popa = Session::current->defaults (Query("pipe(default)")); ASSERT (popa == pipe1->getProcPatt()); // several variants to query for "the default pipe" @@ -138,14 +141,14 @@ namespace asset ASSERT (pipe2 == pipe1); pipe2 = asset::Struct::create (Query ()); ASSERT (pipe2 == pipe1); - pipe2 = asset::Struct::create (Query ("pipe()")); + pipe2 = asset::Struct::create (Query ("pipe(default)")); ASSERT (pipe2 == pipe1); string sID = popa->queryStreamID(); // sort of a "default stream type" PPipe pipe3 = Pipe::query ("stream("+sID+")"); ASSERT (pipe3); ASSERT (pipe3->getProcPatt()->queryStreamID() == sID); - ASSERT (pipe3->getProcPatt() == Session::current->defaults (Query("stream("+sID+")"))); + ASSERT (pipe3->getProcPatt() == Session::current->defaults (Query("stream("+sID+")"))); }