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)
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 * *****************************************************************/
13 
14 
31 
34 #include "steam/asset/procpatt.hpp"
35 #include "steam/asset/pipe.hpp"
36 #include "lib/query-util.hpp"
37 #include "common/query.hpp"
38 
39 #include "include/logging.h"
40 #include "lib/symbol.hpp"
41 #include "lib/util.hpp"
42 
43 using lib::Literal;
44 using util::isnil;
45 
47 
48 namespace steam {
49 namespace mobject {
50 namespace session {
51 
52  namespace query {
53 
54  using asset::Struct;
55  using asset::Pipe;
56  using asset::PPipe;
57 
58  using asset::ProcPatt;
59  using asset::PProcPatt;
60 
61 // using lib::query::extractID;
62  using lib::query::extractID;
64 
65 
66  namespace {
67 
68  typedef std::pair<const QueryKey, any> AnyPair;
69 
71  template<class TY>
72  AnyPair entry (Query<TY> const& query, typename WrapReturn<TY>::Wrapper& obj)
73  {
74  return AnyPair(query, any(obj));
75  }
76 
78  template<class STRU>
79  AnyPair entry_Struct(Literal caps)
80  {
81  typedef typename WrapReturn<STRU>::Wrapper Ptr;
82 
83  string capabilities (caps);
84  Query<STRU> query (capabilities);
85  Ptr obj = Struct::retrieve (query);
86  return AnyPair(query, obj);
87  }
88  }
89 
90 
96  void
98  {
99  INFO (config, "creating mock answers for some hard wired config queries...");
100  isInit_ = true; // allow re-entrance
101 
102  typedef const ProcPatt cPP;
103 
104 
105  // for baiscpipetest.cpp ---------
106  answer_.insert (entry_Struct<cPP> ("stream(video)"));
107  answer_.insert (entry_Struct<cPP> ("stream(teststream)"));
108  item<cPP> ("stream(default)") = item<cPP> ("stream(video)"); // set up a default stream
109 
110  answer_.insert (entry_Struct<Pipe> ("pipe(master), stream(video)"));
111  item<Pipe> ("") = item<Pipe>("pipe(master), stream(video)");// use as default
112 
113  answer_.insert (entry_Struct<Pipe> ("pipe(ambiance)"));
114  }
115 
116 
120  void
122  {
123  answer_.clear();
124  isInit_ = false;
125  INFO (config, "discarded all config query mock answers.");
126  }
127 
128 
129 
130 
131 
132  /* under some circumstances we need to emulate the behaviour *
133  * of a real resolution engine in a more detailed manner. *
134  * The code below implements these cases hard wired. */
135 
137  bool
138  MockTable::fabricate_matching_new_Pipe (Query<Pipe> const& q, string const& pipeID, string const& streamID)
139  {
140  typedef WrapReturn<Pipe>::Wrapper Ptr;
141 
142  Ptr newPipe (Struct::retrieve.newPipe (pipeID, streamID));
143  answer_.insert (entry<Pipe> (q, newPipe));
144  return true; // indicates that the query will now succeed...
145  }
146 
148  bool
150  {
151  typedef WrapReturn<Pipe>::Wrapper Ptr;
152 
153  Ptr newPipe (Struct::retrieve.made4fake (q));
154  answer_.insert (entry<Pipe> (q, newPipe));
155  return true;
156  }
157 
159  bool
161  {
162  typedef const ProcPatt cPP;
163  typedef WrapReturn<cPP>::Wrapper Ptr;
164 
165  Ptr newPP (Struct::retrieve.made4fake (q));
166  answer_.insert (entry<cPP> (q, newPP));
167  return true;
168  }
169 
174  bool
176  {
177  typedef asset::Timeline aTL;
178  typedef WrapReturn<aTL>::Wrapper Ptr;
179 
180  string nameID = query.extractID("id");
181  if (isnil (nameID))
182  nameID = query.extractID("timeline");
183  if (isnil (nameID))
184  nameID = "prime";
185 
186  Query<aTL> normalisedQuery =
187  query.rebuild()
188  .removeTerm("id")
189  .removeTerm("timeline")
190  .prependConditions("id("+nameID+")");
191 
192  // try to find an existing one with the desired id
193  Ptr newTimeline;
194  size_t i=0, cnt=Session::current->timelines.size();
195  for ( ; i < cnt; ++i)
196  if (nameID == Session::current->timelines[i]->ident.name)
197  {
198  newTimeline = Session::current->timelines[i];
199  break;
200  }
201 
202  if (!newTimeline)
203  newTimeline = Struct::retrieve.made4fake (normalisedQuery); // no suitable Timeline found: create and attach new one
204  answer_.insert (entry<aTL> (normalisedQuery, newTimeline)); // "learn" the found/created Timeline as new solution
205  answer_.insert (entry<aTL> (query, newTimeline));
206  return true;
207  }
208 
210  bool
212  {
213  typedef asset::Sequence aSeq;
214  typedef WrapReturn<aSeq>::Wrapper Ptr;
215 
216  string nameID = query.extractID("id");
217  if (isnil (nameID))
218  nameID = query.extractID("sequence");
219  if (isnil (nameID))
220  nameID = "first";
221 
222  Query<aSeq> normalisedQuery =
223  query.rebuild()
224  .removeTerm("id")
225  .removeTerm("sequence")
226  .prependConditions("id("+nameID+")");
227 
228  // try to find an existing sequence with the desired id
229  Ptr newSequence;
230  size_t i=0, cnt=Session::current->sequences.size();
231  for ( ; i < cnt; ++i)
232  if (nameID == Session::current->sequences[i]->ident.name)
233  {
234  newSequence = Session::current->sequences[i];
235  break;
236  }
237 
238  if (!newSequence)
239  newSequence = Struct::retrieve.made4fake (normalisedQuery); // no suitable found: create and attach new Sequence
240  answer_.insert (entry<aSeq> (normalisedQuery, newSequence)); // "learn" the found/created new solution
241  answer_.insert (entry<aSeq> (query, newSequence));
242  return true;
243  }
244 
245 
247  template<class TY>
248  bool
249  MockTable::set_new_mock_solution (Query<TY> const& q, typename WrapReturn<TY>::Wrapper& obj)
250  {
251  answer_.erase (q);
252  answer_.insert (entry<TY> (q, obj));
253  return true;
254  }
255  // generate the necessary specialisations-----------------------------------
256  template bool MockTable::set_new_mock_solution (Query<Pipe> const&, PPipe&);
257 
258 
259 
260 
261 
263  {
264  WARN (config, "using a mock implementation of the ConfigQuery interface");
265  }
266 
267 
268  MockTable::MockTable ()
269  : answer_()
270  , isInit_(false)
271  { }
272 
273 
274 
275 
286  any const&
288  {
289  static const any NOTFOUND;
290  if (!isInit_) fill_mock_table();
291 
292  Tab::iterator i = answer_.find (query);
293  if (i == answer_.end())
294  return NOTFOUND;
295  else
296  return i->second;
297  }
298 
299 
300 
301  } // namespace query
302 }}} // 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:52
TODO type comment.
Definition: sequence.hpp:83
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:76
This header is for including and configuring NoBug.
static session::SessManager & current
access point to the current Session
Definition: session.hpp:120
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:65
structural asset corresponding to the part of the model forming a processing pipe for generating medi...
Definition: pipe.hpp:70
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:107
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:92
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:109
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:388
SequenceAccess sequences
collection of sequences
Definition: session.hpp:125
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:140
Customised refcounting smart pointer template, built upon std::shared_ptr, but forwarding type relati...
Definition: trait.hpp:71
key abstraction: structural asset Created automatically as a sideeffect of building the structure of ...
Definition: struct.hpp:104
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:270
string extractID(Symbol predicate) const
convenience shortcut to extract a desired name-ID.
Definition: query.hpp:586
TimelineAccess timelines
collection of timelines (top level)
Definition: session.hpp:124