example1.cpp 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * @file
  3. * @author Caleb Fangmeier <caleb@fangmeier.tech>
  4. * @version 0.1
  5. *
  6. * @section LICENSE
  7. *
  8. *
  9. * MIT License
  10. *
  11. * Copyright (c) 2017 Caleb Fangmeier
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a copy
  14. * of this software and associated documentation files (the "Software"), to deal
  15. * in the Software without restriction, including without limitation the rights
  16. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. * copies of the Software, and to permit persons to whom the Software is
  18. * furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in all
  21. * copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  29. * SOFTWARE.
  30. *
  31. * @section DESCRIPTION
  32. * A short example demonstrating some of the basic functionality of the FilVal
  33. * Value system.
  34. */
  35. #include <iostream>
  36. #include <utility>
  37. #include "filval/filval.hpp"
  38. int main(int argc, const char* argv[]){
  39. // Initialize logging. Let's not worry about what is being logged right now
  40. // and just redirect it to /dev/null so it doesn't appear on the screen.
  41. fv::util::Log::init_logger("/dev/null", fv::util::LogPriority::kLogDebug);
  42. // declare a helper function to print out a std::pair object
  43. auto print_pair = [](fv::Value<std::pair<double, double>>* dp){
  44. std::pair<double, double> p = dp->get_value();
  45. std::cout << "(" << p.first << ", " << p.second << ")\n";
  46. };
  47. // These variables are the "Values" that will be observed. Think of these
  48. // as sources of data that will be updated as new observations are loaded.
  49. double x = 12;
  50. double y = 13;
  51. // This is where the fun begins. Here we declare a couple ObservedValue
  52. // objects. these are basically just fancy wrappers around the x, and y
  53. // variables declared above. They have the job of supplying the value
  54. // stored by x and y when requested.
  55. fv::ObservedValue<double> x_val("x", &x);
  56. fv::ObservedValue<double> y_val("y", &y);
  57. // Now that we have a few source values, let's compose them together into a
  58. // pair. The fv api defines a function to do this: fv::pair. This function
  59. // takes pointers to two Value objects and creates a new value that is a
  60. // std::pair of these objects.
  61. fv::Value<std::pair<double, double>>* dp = fv::pair(&x_val, &y_val);
  62. // If we call the print_pair function that we declared earlier with the new
  63. // value object, we can see that, indeed, we see the correct output
  64. // (12, 13)
  65. print_pair(dp);
  66. // Now let's update the values of x and y to both be 2. Normally this job
  67. // will be handled by a DataSet object which manages these variables, but
  68. // we can do it here for now.
  69. x = 2;
  70. y = 2;
  71. // Before we can access these new values through our value chain, we need
  72. // to tell the values to "reset". One of the main features of FV is that
  73. // each value is only calculated at most once per observation, regardless
  74. // of how many times it is accessed. However, this means that we have to
  75. // tell the objects when a new object has been loaded so it will actually
  76. // do a re-calculation. In the case of dp, this means that it will
  77. // re-access the values of x and y and create a new pair with the updated
  78. // values.
  79. fv::GenValue::reset();
  80. // Call print_pair again just to verify that it gives the updated values,
  81. // and indeed it does.
  82. // (2, 2)
  83. print_pair(dp);
  84. return 0;
  85. }