DetectorGraph  2.0
lag.hpp
Go to the documentation of this file.
1 // Copyright 2017 Nest Labs, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef DETECTORGRAPH_INCLUDE_LAG_HPP_
16 #define DETECTORGRAPH_INCLUDE_LAG_HPP_
17 
18 #include "graph.hpp"
19 #include "topicstate.hpp"
20 #include "detector.hpp"
21 #include "futurepublisher.hpp"
22 
23 namespace DetectorGraph
24 {
25 
26 /* This is a utility detector and topic template that performs a 1-lag on a
27  * TopicState. It's meant to enable closing feedback loops in a graph in a
28  * expressive and transparent instead of an explicit ad-hoc way.
29  *
30  *
31  * - Lagged< Topic< T > >
32  * ^
33  * |
34  *
35  * | (FuturePublish)
36  *
37  * |
38  * O Lag< T >
39  * ^
40  * |
41  * - Topic< T >
42  *
43  *
44  */
45 
46 
47 /// @brief A templated TopicState used as the output of Lag
48 template <class T>
49 struct Lagged : public TopicState
50 {
51  Lagged() : data()
52  {
53  }
54 
55  Lagged(const T& a) : data(a)
56  {
57  }
58 
59  T data;
60 };
61 
62 /**
63  * @brief Produces a [Lagged<T>](@ref Lagged) for T
64  *
65  * This is a utility detector (and topic template) that performs a 1-lag on a
66  * TopicState. It's meant to enable closing feedback loops in a graph in an
67  * expressive and transparent way. It creates a Detector with the following
68  * relationships:
69  *
70  * @dot "Lag"
71 digraph GraphAnalyzer {
72  rankdir = "LR";
73  node[fontname=Helvetica];
74  size="12,5";
75  "T" [label="0:T",style=filled, shape=box, color=lightblue];
76  "T" -> "Lag";
77  "Lag" -> "Lagged" [style=dotted, color=red];
78  "Lag" [label="1:Lag<T>", color=blue];
79  "Lagged" [label="2:Lagged<T>",style=filled, shape=box, color=limegreen];
80 }
81  * @enddot
82  *
83  * Adding a Lag<T> Detector to a graph is done simply by adding it to a
84  * graph as shown ([from this example](@ref robotlocalization.cpp)) below:
85  @snippet robotlocalization.cpp RobotBrain Static
86  *
87  * Lagged<T> can then be used in any detector. In the same [example](
88  * @ref robotlocalization.cpp):
89  @snippet robotlocalization.cpp KalmanPoseCorrector Feedback Loop
90  *
91  */
92 template<class T>
93 class Lag : public Detector,
94  public SubscriberInterface< T >,
95  public FuturePublisher< Lagged< T > >
96 {
97 public:
98  Lag(Graph* graph) : Detector(graph)
99  {
100  Subscribe< T >(this);
101  SetupFuturePublishing< Lagged< T > >(this);
102  }
103 
104  virtual void Evaluate(const T& currentTopicState)
105  {
106  FuturePublisher< Lagged<T> >::PublishOnFutureEvaluation(Lagged<T>(currentTopicState));
107  }
108 };
109 
110 } // namespace DetectorGraph
111 
112 #endif // DETECTORGRAPH_INCLUDE_LAG_HPP_
Implements a graph of Topics & Detectors with Input/Output APIs.
Definition: graph.hpp:127
Publish data to the graph for future evaluation.
Lag(Graph *graph)
Definition: lag.hpp:98
A templated TopicState used as the output of Lag.
Definition: lag.hpp:49
Lagged(const T &a)
Definition: lag.hpp:55
Base struct for topic data types.
Definition: topicstate.hpp:52
virtual void Evaluate(const T &currentTopicState)
Pure-virtual method that should Evaluate a piece of input data.
Definition: lag.hpp:104
Produces a Lagged<T> for T.
Definition: lag.hpp:93
A unit of logic in a DetectorGraph.
Definition: detector.hpp:68
A Pure interface that declares the Subscriber behavior.