Lumiera  0.pre.03
»edit your freedom«
fake-configrules.cpp
Go to the documentation of this file.
1 /*
2  FakeConfigRules - dummy implementation of the config rules system
3 
4  Copyright (C) Lumiera.org
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
23 
40 
43 #include "steam/asset/procpatt.hpp"
44 #include "steam/asset/pipe.hpp"
45 #include "lib/query-util.hpp"
46 #include "common/query.hpp"
47 
48 #include "include/logging.h"
49 #include "lib/symbol.hpp"
50 #include "lib/util.hpp"
51 
52 using lib::Literal;
53 using util::isnil;
54 
56 
57 namespace steam {
58 namespace mobject {
59 namespace session {
60 
61  namespace query {
62 
63  using asset::Struct;
64  using asset::Pipe;
65  using asset::PPipe;
66 
67  using asset::ProcPatt;
68  using asset::PProcPatt;
69 
70 // using lib::query::extractID;
71  using lib::query::extractID;
73 
74 
75  namespace {
76 
77  typedef std::pair<const QueryKey, any> AnyPair;
78 
80  template<class TY>
81  AnyPair entry (Query<TY> const& query, typename WrapReturn<TY>::Wrapper& obj)
82  {
83  return AnyPair(query, any(obj));
84  }
85 
87  template<class STRU>
88  AnyPair entry_Struct(Literal caps)
89  {
90  typedef typename WrapReturn<STRU>::Wrapper Ptr;
91 
92  string capabilities (caps);
93  Query<STRU> query (capabilities);
94  Ptr obj = Struct::retrieve (query);
95  return AnyPair(query, obj);
96  }
97  }
98 
99 
105  void
107  {
108  INFO (config, "creating mock answers for some hard wired config queries...");
109  isInit_ = true; // allow re-entrance
110 
111  typedef const ProcPatt cPP;
112 
113 
114  // for baiscpipetest.cpp ---------
115  answer_.insert (entry_Struct<cPP> ("stream(video)"));
116  answer_.insert (entry_Struct<cPP> ("stream(teststream)"));
117  item<cPP> ("stream(default)") = item<cPP> ("stream(video)"); // set up a default stream
118 
119  answer_.insert (entry_Struct<Pipe> ("pipe(master), stream(video)"));
120  item<Pipe> ("") = item<Pipe>("pipe(master), stream(video)");// use as default
121 
122  answer_.insert (entry_Struct<Pipe> ("pipe(ambiance)"));
123  }
124 
125 
129  void
131  {
132  answer_.clear();
133  isInit_ = false;
134  INFO (config, "discarded all config query mock answers.");
135  }
136 
137 
138 
139 
140 
141  /* under some circumstances we need to emulate the behaviour *
142  * of a real resolution engine in a more detailed manner. *
143  * The code below implements these cases hard wired. */
144 
146  bool
147  MockTable::fabricate_matching_new_Pipe (Query<Pipe> const& q, string const& pipeID, string const& streamID)
148  {
149  typedef WrapReturn<Pipe>::Wrapper Ptr;
150 
151  Ptr newPipe (Struct::retrieve.newPipe (pipeID, streamID));
152  answer_.insert (entry<Pipe> (q, newPipe));
153  return true; // indicates that the query will now succeed...
154  }
155 
157  bool
159  {
160  typedef WrapReturn<Pipe>::Wrapper Ptr;
161 
162  Ptr newPipe (Struct::retrieve.made4fake (q));
163  answer_.insert (entry<Pipe> (q, newPipe));
164  return true;
165  }
166 
168  bool
170  {
171  typedef const ProcPatt cPP;
172  typedef WrapReturn<cPP>::Wrapper Ptr;
173 
174  Ptr newPP (Struct::retrieve.made4fake (q));
175  answer_.insert (entry<cPP> (q, newPP));
176  return true;
177  }
178 
183  bool
185  {
186  typedef asset::Timeline aTL;
187  typedef WrapReturn<aTL>::Wrapper Ptr;
188 
189  string nameID = query.extractID("id");
190  if (isnil (nameID))
191  nameID = query.extractID("timeline");
192  if (isnil (nameID))
193  nameID = "prime";
194 
195  Query<aTL> normalisedQuery =
196  query.rebuild()
197  .removeTerm("id")
198  .removeTerm("timeline")
199  .prependConditions("id("+nameID+")");
200 
201  // try to find an existing one with the desired id
202  Ptr newTimeline;
203  size_t i=0, cnt=Session::current->timelines.size();
204  for ( ; i < cnt; ++i)
205  if (nameID == Session::current->timelines[i]->ident.name)
206  {
207  newTimeline = Session::current->timelines[i];
208  break;
209  }
210 
211  if (!newTimeline)
212  newTimeline = Struct::retrieve.made4fake (normalisedQuery); // no suitable Timeline found: create and attach new one
213  answer_.insert (entry<aTL> (normalisedQuery, newTimeline)); // "learn" the found/created Timeline as new solution
214  answer_.insert (entry<aTL> (query, newTimeline));
215  return true;
216  }
217 
219  bool
221  {
222  typedef asset::Sequence aSeq;
223  typedef WrapReturn<aSeq>::Wrapper Ptr;
224 
225  string nameID = query.extractID("id");
226  if (isnil (nameID))
227  nameID = query.extractID("sequence");
228  if (isnil (nameID))
229  nameID = "first";
230 
231  Query<aSeq> normalisedQuery =
232  query.rebuild()
233  .removeTerm("id")
234  .removeTerm("sequence")
235  .prependConditions("id("+nameID+")");
236 
237  // try to find an existing sequence with the desired id
238  Ptr newSequence;
239  size_t i=0, cnt=Session::current->sequences.size();
240  for ( ; i < cnt; ++i)
241  if (nameID == Session::current->sequences[i]->ident.name)
242  {
243  newSequence = Session::current->sequences[i];
244  break;
245  }
246 
247  if (!newSequence)
248  newSequence = Struct::retrieve.made4fake (normalisedQuery); // no suitable found: create and attach new Sequence
249  answer_.insert (entry<aSeq> (normalisedQuery, newSequence)); // "learn" the found/created new solution
250  answer_.insert (entry<aSeq> (query, newSequence));
251  return true;
252  }
253 
254 
256  template<class TY>
257  bool
258  MockTable::set_new_mock_solution (Query<TY> const& q, typename WrapReturn<TY>::Wrapper& obj)
259  {
260  answer_.erase (q);
261  answer_.insert (entry<TY> (q, obj));
262  return true;
263  }
264  // generate the necessary specialisations-----------------------------------
265  template bool MockTable::set_new_mock_solution (Query<Pipe> const&, PPipe&);
266 
267 
268 
269 
270 
272  {
273  WARN (config, "using a mock implementation of the ConfigQuery interface");
274  }
275 
276 
277  MockTable::MockTable ()
278  : answer_()
279  , isInit_(false)
280  { }
281 
282 
283 
284 
295  any const&
297  {
298  static const any NOTFOUND;
299  if (!isInit_) fill_mock_table();
300 
301  Tab::iterator i = answer_.find (query);
302  if (i == answer_.end())
303  return NOTFOUND;
304  else
305  return i->second;
306  }
307 
308 
309 
310  } // namespace query
311 }}} // namespace steam::mobject::session
AnyPair entry_Struct(Literal caps)
helper especially for creating structural assets from a capability query
Utilities to support working with predicate queries.
Definition of a structural asset to express patterns of wiring or processing Processing patterns can ...
A "processing pipe" represented as Asset.
bool fabricate_just_new_Pipe(Query< Pipe > const &q)
special case: create a new pipe for a specific stream ID
"Processing Pattern" is a structural Asset representing information how to build some part of the ren...
Definition: procpatt.hpp:61
TODO type comment.
Definition: sequence.hpp:92
void fill_mock_table()
hard coded answers to configuration queries.
Basic and generic representation of an internal query.
bool set_new_mock_solution(Query< TY > const &q, typename WrapReturn< TY >::Wrapper &candidate)
for entering "valid" solutions on-the-fly from tests
AnyPair entry(Query< TY > const &query, typename WrapReturn< TY >::Wrapper &obj)
helper to simplify creating mock table entries, wrapped correctly
Mock/Test/Debugging Implementation of the config rules system.
inline string literal This is a marker type to indicate that
Definition: symbol.hpp:75
This header is for including and configuring NoBug.
static session::SessManager & current
access point to the current Session
Definition: session.hpp:129
Organisational grouping device within the Session model ("Track" / "Media Bin").
Steam-Layer implementation namespace root.
Namespace of Session and user visible high-level objects.
Definition: sequence.hpp:74
structural asset corresponding to the part of the model forming a processing pipe for generating medi...
Definition: pipe.hpp:79
bool fabricate_Timeline_on_demand(Query< asset::Timeline > const &q)
special case: fabricate new Timeline, maybe using specific sub-objects as hinted by the IDs given wit...
static StructFactory retrieve
storage for the static StructFactory instance
Definition: struct.hpp:116
string extractID(Symbol sym, const string &termString)
(preliminary) helper: instead of really parsing and evaluating the terms, just do a regular expressio...
Definition: query-util.cpp:101
bool fabricate_matching_new_Pipe(Query< Pipe > const &q, string const &pipeID, string const &streamID)
special case: create a new pipe with matching pipe and stream IDs on the fly when referred...
Marker types to indicate a literal string and a Symbol.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
bool fabricate_Sequence_on_demand(Query< asset::Sequence > const &q)
special case: fabricate new Timeline, maybe using ID specs from the query...
any const & fetch_from_table_for(QueryKey const &query)
this is the (preliminary/mock) implementation handling queries for objects of a specific type and wit...
string removeTerm(Symbol sym, string &queryString)
(preliminary) helper: cut a term with the given symbol.
Definition: query-util.cpp:118
bool fabricate_ProcPatt_on_demand(Query< const ProcPatt > const &q)
special case: create/retrieve new processing pattern for given stream ID...
Wrapper for indexing and ordering.
Definition: query.hpp:397
SequenceAccess sequences
collection of sequences
Definition: session.hpp:134
virtual void reset()
clear the contents of the mock solution table.
lib::P< STRU > made4fake(Query< STRU > const &query)
special backdoor for fake-configrules.hpp This allows to simulate creation of objects triggered by ru...
Definition: struct.cpp:149
Customised refcounting smart pointer template, built upon std::shared_ptr, but forwarding type relati...
Definition: trait.hpp:78
key abstraction: structural asset Created automatically as a sideeffect of building the structure of ...
Definition: struct.hpp:113
MockConfigRules()
to be used only by the singleton factory
Primary Interface to the current Session.
Generic interface to express a query for specifically typed result elements exposing some capabilitie...
Definition: query.hpp:279
string extractID(Symbol predicate) const
convenience shortcut to extract a desired name-ID.
Definition: query.hpp:595
TimelineAccess timelines
collection of timelines (top level)
Definition: session.hpp:133