Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
4.0 KiB

  1. //
  2. // MODULE: APGTSCAC.CPP
  3. //
  4. // PURPOSE: Belief network caching support classes
  5. // Fully implements class CBNCacheItem
  6. // Fully implements class CBNCache
  7. //
  8. // PROJECT: Generic Troubleshooter DLL for Microsoft AnswerPoint
  9. //
  10. // COMPANY: Saltmine Creative, Inc. (206)-284-7511 [email protected]
  11. //
  12. // AUTHOR: Joe Mabel, modeled on earlier work by Roman Mach
  13. //
  14. // ORIGINAL DATE: 10-2-96, completely rewritten 8/98
  15. //
  16. // NOTES:
  17. // 1. The strategy here builds a "Most-recently-used" cache (singly-linked list of
  18. // CBNCacheItem ordered by how recently used)
  19. // 2. Although you are first supposed to call FindCacheItem and only call AddCacheItem
  20. // if that fails, there is no support to do this in a threadsafe manner, so there
  21. // had better be only one thread with access to a given CCache. Mutex protection
  22. // must come at a higher level.
  23. // 3. One cache is associated with each [instance of a] Belief Network
  24. //
  25. // Version Date By Comments
  26. //--------------------------------------------------------------------
  27. // V0.1 - RM Original
  28. // V3.0 8/7/98 JM Original
  29. //
  30. #include "stdafx.h"
  31. #include "event.h"
  32. #include "apgtscac.h"
  33. #include "baseexception.h"
  34. #include "CharConv.h"
  35. #include <algorithm>
  36. // The CCacheItem comparison operators depend on the assumption that if the cache key
  37. // is identical, the cache value will be, too.
  38. bool CCacheItem::operator== (const CCacheItem &item) const
  39. {
  40. return (BasisForInference == item.BasisForInference);
  41. }
  42. bool CCacheItem::operator!= (const CCacheItem &item) const
  43. {
  44. return (BasisForInference != item.BasisForInference);
  45. }
  46. // Note that this is not lexicographical order. We're saying any shorter
  47. // cache key compares as less than any longer.
  48. bool CCacheItem::operator< (const CCacheItem &item) const
  49. {
  50. const CBasisForInference::size_type thisSize = BasisForInference.size();
  51. const CBasisForInference::size_type otherSize = item.BasisForInference.size();
  52. if (thisSize < otherSize)
  53. return true;
  54. if (thisSize > otherSize)
  55. return false;
  56. // same length, use lexicographical order.
  57. return (BasisForInference < item.BasisForInference);
  58. }
  59. // Note that this is not lexicographical order. We're saying any longer
  60. // cache key compares as greater than any shorter.
  61. bool CCacheItem::operator> (const CCacheItem &item) const
  62. {
  63. const CBasisForInference::size_type thisSize = BasisForInference.size();
  64. const CBasisForInference::size_type otherSize = item.BasisForInference.size();
  65. if (thisSize > otherSize)
  66. return true;
  67. if (thisSize < otherSize)
  68. return false;
  69. // same length, use lexicographical order.
  70. return (BasisForInference > item.BasisForInference);
  71. }
  72. // NOTE: Must call FindCacheItem first and not call this
  73. // function to prevent duplicate records from going into cache
  74. bool CCache::AddCacheItem(
  75. const CBasisForInference &BasisForInference,
  76. const CRecommendations &Recommendations)
  77. {
  78. if (GetCount() >= MAXCACHESIZE)
  79. listItems.pop_back();
  80. try
  81. {
  82. CCacheItem item(BasisForInference, Recommendations);
  83. listItems.push_front(item);
  84. if (listItems.size() >= k_CacheSizeMax)
  85. listItems.pop_back();
  86. return true; // always succeeds
  87. }
  88. catch (exception& x)
  89. {
  90. CString str;
  91. // Note STL exception in event log.
  92. CBuildSrcFileLinenoStr SrcLoc( __FILE__, __LINE__ );
  93. CEvent::ReportWFEvent( SrcLoc.GetSrcFileLineStr(),
  94. SrcLoc.GetSrcFileLineStr(),
  95. CCharConversion::ConvertACharToString(x.what(), str),
  96. _T(""),
  97. EV_GTS_STL_EXCEPTION );
  98. return( false );
  99. }
  100. }
  101. bool CCache::FindCacheItem(
  102. const CBasisForInference &BasisForInference,
  103. CRecommendations &Recommendations /* output */) const
  104. {
  105. Recommendations.clear();
  106. CCacheItem item(BasisForInference, Recommendations /* effectively, a dummy */ );
  107. const list<CCacheItem>::const_iterator itBegin = listItems.begin();
  108. const list<CCacheItem>::const_iterator itEnd = listItems.end();
  109. const list<CCacheItem>::const_iterator itMatch = find(itBegin, itEnd, item);
  110. if (itMatch == itEnd)
  111. return false;
  112. Recommendations = itMatch->GetRecommendations();
  113. return true;
  114. }
  115. UINT CCache::GetCount() const
  116. {
  117. return listItems.size();
  118. }