943,604 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 734
  • C++ RSS
Aug 28th, 2008
0

Design Problem: Dealing with multiple time frames

Expand Post »
I am trying to come up with a good class design to deal with asynchronous data to be stored and analyzed over multiple time frames.

I obtain data elements from an asynchronous data source (i.e. the data arrives at irregular time intervals) and wish to place them in a container of fixed length in time, which I call a "DataChart". This will be implemented using a deque. As a new data element arrives from the source, I remove all elements that are, say, 10mins older than the new data element from the back of the deque before pushing the new element on the front. This "10min DataChart" is my shortest time frame.

When the deque fills and the oldest DataElement is 10mins old and is about to be discarded as a new element arrives, I take a snapshot of the deque by averaging over its contents, creating a 10min_AverageElement and storing this average in another container of fixed width in time, a 1day_AveragesChart which is implemented as a deque of 10min_AverageElements. Every 10mins the entire contents of the shortest timeframe, 10min_DataChart, are renewed and thus a new 10min_AverageElement is generated to be stored in the 1day_AveragesChart.

Likewise, I have a 1month_AveragesChart containing 1day_AverageElements made by averaging over the 1day_AveragesChart of 10min_AverageElements when its contents are renewed once a day, and so on.

In this way, I can have any number of time frames spanning arbitrary lengths in time, the shortest will always be a DataChart comprised of DataElements, and subsequent time frames will be AveragesCharts comprised of AverageElements, every chart overflows into a chart of a longer time frame when it fills.

So far, I have a base class Chart which is the interface, and two derived classes DataChart and AveragesChart, each of which contain a deque to store the data or averages elements respectively, and methods to add new data, etc.

So here are my questions.

(1) I have a pure virtual method addelement() in the base class, Chart, to update a given chart with new data. The overloaded addelement() in the derived DataChart takes DataElements, does some validation and adds the elements to the DataChart.

By contrast, addelement() in the derived AveragesChart takes AveragesCharts. It constructs an AverageElement from the supplied (short-timeframe) AverageChart and adds it to the longer timeframe AveragesChart to which it belongs. This is my first problem. addelement should be part of the Chart base class interface, I can't see a way to do that. It could be that I should delegate certain responsibilities of addelement to other methods in the chart, or I could use a visitor, I'm not sure what is the best solution. I don't really want to make a base class "Element" to shoehorn derived DataElement and AverageElement objects into the base class addelement() method for reasons explained in this thread. Your thoughts on this?

(2) Later, I'd like to compute other derived quantities from the charts, like a discrete Fourier transform, for example. I can add methods begin() and end() to the chart interface which return iterators to the Deques they contain and use those iterators in computations. I can also use the iterators to calculate the averages of one time frame needed to make AverageElements of a longer time frame chart. Do you think this is a good idea? Does it expose my implementation of the Charts?
I suppose one could again use a visitor here, or pass function objects to the charts containing the algorithm to be computed on the elements. I'm not sure what the design of that should be yet. Is that a good, extensible design?

(3) In the end, a data element comes in and then the charts will have to be updated, sometimes this will entail overflowing to a longer time frame chart as a shorter time frame fills as mentioned above. I was thinking of writing a class ChartSet which will be contain all the charts, to which I delegate the responsibility of managing the charts. Which class do you think should have the responsibility of generating an AverageElement for the longer time frame Chart when a shorter time frame Chart fills, the ChartSet object which encapsulates the Charts, or the Charts themselves?


Any other comments would be greatly appreciated. My apologies for such a long post. I'm not sure how to abridge it without making things too abstract.
Last edited by Gentile; Aug 28th, 2008 at 11:24 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Gentile is offline Offline
5 posts
since Aug 2008
Aug 28th, 2008
0

Re: Design Problem: Dealing with multiple time frames

if you absolutely need a base class Chart (with virtual functions), a hack would be to overload the AddElement function.

i would prefer using templates for your containers as suggested by Narue in the other thread. and as for this issue:
> ... generic template<class T> container and have a method addelement(T& elt)
> but the implemebtation of addelement() depends on the element being added
template specialization would address the problem.
c++ Syntax (Toggle Plain Text)
  1. #include <stdexcept>
  2.  
  3. struct DataElement { /* ... */ };
  4. struct AverageElement { /* ... */ };
  5.  
  6. #ifdef INHERIT
  7.  
  8. struct Chart // base class
  9. {
  10. virtual void AddElement( const DataElement& ) = 0 ;
  11. virtual void AddElement( const AverageElement& ) = 0 ;
  12. // other (pure) virtual functions
  13. };
  14.  
  15. struct DataChart : Chart
  16. {
  17. virtual void AddElement( const DataElement& )
  18. { /* add DataElement */ }
  19.  
  20. virtual void AddElement( const AverageElement& )
  21. { throw std::logic_error("incorrect element type") ; }
  22.  
  23. // other overrides etc.
  24. };
  25.  
  26. struct AveragesChart : Chart
  27. {
  28. virtual void AddElement( const DataElement& )
  29. { throw std::logic_error("incorrect element type") ; }
  30.  
  31. virtual void AddElement( const AverageElement& )
  32. { /* add AverageElement */ }
  33.  
  34. // other overrides etc.
  35. };
  36.  
  37. #else // GENERIC
  38.  
  39. template< typename T > struct chart_base
  40. {
  41. // common stuff goes here
  42. };
  43.  
  44. template< typename T > struct chart ; // generalization
  45.  
  46. // specialization
  47. template<> struct chart<DataElement> : chart_base<DataElement>
  48. {
  49. void addelement( const DataElement& ) ;
  50. // other DataChart specific stuff
  51. };
  52.  
  53. // specialization
  54. template<> struct chart<AverageElement> : chart_base<AverageElement>
  55. {
  56. void AddElement( const AverageElement& ) ;
  57. // other AverageChart specific stuff
  58. };
  59.  
  60. typedef chart<DataElement> DataChart ;
  61. typedef chart<AverageElement> AverageChart ;
  62.  
  63. #endif // INHERIT

> I suppose one could again use a visitor here, or pass function objects
> to the charts containing the algorithm to be computed on the elements.
i guess, vistor if your chart is object-oriented, function object if it is generic.

> Which class do you think should have the responsibility of generating
> an AverageElement for the longer time frame Chart
> the ChartSet object or the Charts themselves?
perhaps an element of personal taste here. i think i would let the Charts themselves generate this if all the required information is available within a single Chart; the ChartSet otherwise.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Aug 28th, 2008
0

Re: Design Problem: Dealing with multiple time frames

I was not familiar with this particular way of using templates. Thank you very much.
Does it make sense to write a virtual method in the templated base class in order to force derived classes to have an appropriate addelement() method? Something like this:
cpp Syntax (Toggle Plain Text)
  1. template< typename T > class chart_base
  2. {
  3. virtual void addelement( const T& ) = 0;
  4. };

Obviously the first method would lead to a maintenance nightmare; as more chart types are added, more overloaded addelement() placeholders need to be added to the base and every subclass.
Last edited by Gentile; Aug 28th, 2008 at 1:05 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Gentile is offline Offline
5 posts
since Aug 2008
Aug 28th, 2008
0

Re: Design Problem: Dealing with multiple time frames

> Does it make sense to write a virtual method in the templated base class
> in order to force derived classes to have an appropriate addelement() method?
no. the polymorphism provided by templates is compile-time polymorphism; if you need an addelement() in a class and the class does not provide it, it will be caught at compile-time.

> first method would lead to a maintenance nightmare;
> as more chart types are added, more overloaded addelement() placeholders need to be added
true.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Inheritence/substitutability dilemma
Next Thread in C++ Forum Timeline: error LNK2001





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC