wuattr.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "wuattr.hpp"
  14. #include "jptree.hpp"
  15. struct WuAttrInfo
  16. {
  17. public:
  18. WuAttr kind; // The attribute enumeration
  19. StatisticMeasure measure; // units for the measure
  20. const char * name; // text version of the attribute
  21. const char * graphPath; // The xpath required to extract a result from a graph node.
  22. const char * overridePath; // Alternative xpath to check 1st for some overloaded attributes
  23. const char * childPath; // The name of the <atr> for setting, or matching when iterating
  24. const char * dft; // default value if not present
  25. };
  26. #define CHILDPATH(x) "att[@name='" x "']/@value"
  27. #define CHILDMPATH(x) "att[@name='" x "'][1]/@value"
  28. #define ATTR(kind, measure, path) { WA ## kind, measure, #kind, path, nullptr, nullptr, nullptr }
  29. #define ALTATTR(kind, measure, path, alt) { WA ## kind, measure, #kind, path, alt, nullptr }
  30. #define CHILD(kind, measure, path) { WA ## kind, measure, #kind, CHILDPATH(path), nullptr, path, nullptr }
  31. #define CHILD_MULTI(kind, measure, path) { WA ## kind, measure, #kind, CHILDMPATH(path), nullptr, path, nullptr }
  32. #define CHILD_D(kind, measure, path, dft) { WA ## kind, measure, #kind, CHILDPATH(path), nullptr, path, dft }
  33. const static WuAttrInfo attrInfo[] = {
  34. { WANone, SMeasureNone, "none", nullptr, nullptr, nullptr },
  35. { WAAll, SMeasureNone, "all", nullptr, nullptr, nullptr },
  36. CHILD(Kind, SMeasureEnum, "_kind"),
  37. ALTATTR(IdSource, SMeasureId, "@source", "att[@name='_sourceActivity']/@value"),
  38. ALTATTR(IdTarget, SMeasureId, "@target", "att[@name='_targetActivity']/@value"),
  39. CHILD_D(SourceIndex, SMeasureText, "_sourceIndex", "0"),
  40. CHILD_D(TargetIndex, SMeasureText, "_targetIndex", "0"),
  41. ATTR(Label, SMeasureText, "@label"),
  42. CHILD(IsDependency, SMeasureBool, "_dependsOn"),
  43. CHILD(IsChildGraph, SMeasureBool, "_childGraph"),
  44. CHILD_MULTI(Definition, SMeasureText, "definition"),
  45. CHILD_MULTI(EclName, SMeasureText, "name"),
  46. CHILD(EclText, SMeasureText, "ecl"),
  47. CHILD(RecordSize, SMeasureText, "recordSize"),
  48. CHILD(PredictedCount, SMeasureText, "predictedCount"),
  49. CHILD(Filename, SMeasureText, "_fileName"),
  50. { WAMax, SMeasureNone, nullptr, nullptr, nullptr, nullptr }
  51. };
  52. MODULE_INIT(INIT_PRIORITY_STANDARD)
  53. {
  54. static_assert(_elements_in(attrInfo) >= (WAMax-WANone)+1, "Elements missing from attrInfo[]");
  55. static_assert(_elements_in(attrInfo) <= (WAMax-WANone)+1, "Extra elements in attrInfo[]");
  56. for (unsigned i=0; i < _elements_in(attrInfo); i++)
  57. {
  58. assertex(attrInfo[i].kind == WANone + i);
  59. }
  60. return true;
  61. }
  62. const char * queryWuAttributeName(WuAttr kind)
  63. {
  64. if ((kind >= WANone) && (kind < WAMax))
  65. return attrInfo[kind-WANone].name;
  66. return nullptr;
  67. }
  68. WuAttr queryWuAttribute(const char * kind, WuAttr dft)
  69. {
  70. //MORE: This needs to use a hash table
  71. for (unsigned i=WANone; i < WAMax; i++)
  72. {
  73. if (strieq(kind, attrInfo[i-WANone].name))
  74. return (WuAttr)i;
  75. }
  76. return dft;
  77. }
  78. extern WORKUNIT_API const char * queryAttributeValue(IPropertyTree & src, WuAttr kind, StringBuffer & scratchpad)
  79. {
  80. if ((kind <= WANone) || (kind >= WAMax))
  81. return nullptr;
  82. const WuAttrInfo & info = attrInfo[kind-WANone];
  83. const char * path = info.graphPath;
  84. const char * altpath = info.overridePath;
  85. const char * value = altpath ? src.queryProp(altpath) : nullptr;
  86. if (!value)
  87. value = src.queryProp(path);
  88. if (!value && info.dft)
  89. value = info.dft;
  90. //The following switch statement allows the value returned to be transformed from the value stored.
  91. switch (kind)
  92. {
  93. case WAIdSource:
  94. case WAIdTarget:
  95. //A bit of a hack - source and target for edges are activity ids. Return a computed string.
  96. value = scratchpad.clear().append(ActivityScopePrefix).append(value).str();
  97. break;
  98. }
  99. return value;
  100. }
  101. extern WORKUNIT_API WuAttr queryGraphAttrToWuAttr(const char * name)
  102. {
  103. //MORE: Create a hash table to implement this mapping efficiently
  104. for(unsigned i=1; i < WAMax-WANone; i++)
  105. {
  106. const WuAttrInfo & info = attrInfo[i];
  107. const char * path = info.graphPath;
  108. if (path[0] == '@' && strieq(name, path+1))
  109. return (WuAttr)(i+WANone);
  110. }
  111. return WANone;
  112. }
  113. extern WORKUNIT_API WuAttr queryGraphChildAttToWuAttr(const char * name)
  114. {
  115. //MORE: Create a hash table to implement this mapping efficiently
  116. for(unsigned i=1; i < WAMax-WANone; i++)
  117. {
  118. const WuAttrInfo & info = attrInfo[i];
  119. const char * childPath = info.childPath;
  120. if (childPath && strieq(name, childPath))
  121. return (WuAttr)(i+WANone);
  122. }
  123. return WANone;
  124. }
  125. static IPropertyTree * addGraphAttribute(IPropertyTree * node, const char * name)
  126. {
  127. IPropertyTree * att = createPTree();
  128. att->setProp("@name", name);
  129. return node->addPropTree("att", att);
  130. }
  131. extern WORKUNIT_API void setAttributeValue(IPropertyTree & tgt, WuAttr kind, const char * value)
  132. {
  133. const WuAttrInfo & info = attrInfo[kind-WANone];
  134. const char * path = info.graphPath;
  135. if (path[0] == '@')
  136. tgt.setProp(path, value);
  137. else
  138. addGraphAttribute(&tgt, info.childPath)->setProp("@value", value);
  139. }