Channels ▼


The Data Distribution Service for Real-Time Systems: Part 2

Content-Filtered Topics

Domains and Partitions are a very good way of organizing data. Yet, they operate at structural level. What if you need to control the data received based on its actual content? DDS Content-Filtered Topics allow you to create topics that constrain the values their instances might take. When subscribing to a content-filtered topic an application will only receive, among all published values, only those that match the topic filter. The filter expression can operate on the full topic content, as opposed to being able to operate only on headers as it happens in many other pub/sub technologies, such as JMS. The filter expression is structurally similar to a SQL WHERE clause. The operators supported are listed in Table 3.

Table 4: Legal operators for DDS Filters and Query Conditions

Content-Filtered topics are very useful from several different perspective. First of all they limit the amount of memory used by the middleware to the instances and samples that match the filter. Furthermore, filtering can be used to simplify your application by delegating to DDS the logic that checks certain data properties. For instance, if we consider our temperature control application we might be interested in being notified only when the temperature or the humidity are outside a given range.

// Create the TempSensor topic
dds::Topic<TempSensorType> tsTopic("TempSensor");
// Define the filter expression
std::string filter_expression =
     "(temp NOT BETWEEN (%0 AND %1)) \
    OR \
    (hum NOT BETWEEN (%2 and %3))";
// Define the filter parameters (taking some C++0x liberty)
std::vector<std::string> params =
     {"20.5", "21.5", "30", "50"};
// Create the ContentFilteredTopic
// Create the DataReader
dds::DataReader<TempSensorType> dr(cfTsTopic);
dds::SampleInfoSeq info;
TempSensorSeq data;
while (true) {
// The only data received will satisfy the filter
// condition of the ContentFilteredTopic, info);
for (int i =0; i < data.length(); ++i)
std::cout << data[i] << std::endl;

Listing 2: Creating a content-filtered topic.

Thus assuming we wanted to maintain the temperature between 20.5 degrees C and 21.5 degrees C and the humidity between 30% and 50%, we could create a Content-Filtered topic that would alert the application when the sensor is producing value outside our desired set point. This can be done by using the filter expression below:

((temp NOT BETWEEN (20.5 AND 21.5))
(hum NOT BETWEEN (30 AND 50)))

Listing 2 shows the code that creates a content-filtered topic for the TempSensor topic with the expression above. Notice that the content-filtered topic is created starting from a regular topic. Furthermore it is worth noticing that the filter expression is relying on positional arguments %0, %2, etc., whose actual values are passed via a vector of strings.

Query Conditions

For completeness I need to mention that DDS also supports Query Conditions . The main difference between a Query Condition and a Content-Filtered Topics is that the former does not filter incoming data, it simply queries data received and available on an existing reader cache. As a result the execution of the Query Condition is completely under the user control and executed in the context of a read as shown in Listing 3. The syntax supported by Query Expression is identical to that used to define filter expression and listed in Table 4.

// Create the TempSensor topic
dds::Topic<TempSensorType> tsTopic("TempSensor");
// Define the filter expression
std::string filter_expression =
    "(temp NOT BETWEEN (%0 AND %1)) \
    OR \
    (hum NOT BETWEEN (%2 and %3))";
// Define the filter parameters (taking some C++0x liberty)
std::vector<std::string> params =
   {"20.5", "21.5", "30", "50"};
// Create Query Condition
dds::QueryCondition cond =
   dr.create_querycondition(filter_expression, params);
TempSensorTypeSeq data;
SampleInfoSeq info;
// Read with Condition, info, cond);

Listing 3: Using Query Conditions to read data

Concluding Remarks

In Part 2 of this DDS Series I have covered the most important aspects of data management in DDS. By now you've learned all you needed to know about topics-types and topic instances and you've been exposed to the the various mechanism provided by DDS for scoping information. In summary, you can organize structurally your information by means of domains and partitions. Then you can create special views using content-filtered topics and query conditions.

At this point my usual suggestion is that you try to compile and run the examples available with SimD under demo/ddj-series/02 and then experiment a bit on your own.


The Data Distribution Service for Real-Time Systems: Part-1

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.