wuattr.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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;
  19. StatisticMeasure measure;
  20. const char * name;
  21. const char * graphPath;
  22. const char * childPath;
  23. const char * dft;
  24. };
  25. #define CHILDPATH(x) "att[@name='" x "']/@value"
  26. #define ATTR(kind, measure, path) { WA ## kind, measure, #kind, path, nullptr, nullptr }
  27. #define CHILD(kind, measure, path) { WA ## kind, measure, #kind, CHILDPATH(path), path, nullptr }
  28. #define CHILD_D(kind, measure, path, dft) { WA ## kind, measure, #kind, CHILDPATH(path), path, dft }
  29. const static WuAttrInfo attrInfo[] = {
  30. { WANone, SMeasureNone, "none", nullptr, nullptr, nullptr },
  31. { WAAll, SMeasureNone, "all", nullptr, nullptr, nullptr },
  32. CHILD(Kind, SMeasureEnum, "_kind"),
  33. ATTR(Source, SMeasureText, "@source"),
  34. ATTR(Target, SMeasureText, "@target"),
  35. CHILD_D(SourceIndex, SMeasureText, "_sourceIndex", "0"),
  36. CHILD_D(TargetIndex, SMeasureText, "_targetIndex", "0"),
  37. ATTR(Label, SMeasureText, "@label"),
  38. CHILD(IsDependency, SMeasureBool, "_dependsOn"),
  39. CHILD(IsChildGraph, SMeasureBool, "_childGraph"),
  40. CHILD(Definition, SMeasureText, "definition"),
  41. CHILD(EclName, SMeasureText, "name"),
  42. { WAMax, SMeasureNone, nullptr, nullptr, nullptr, nullptr }
  43. };
  44. MODULE_INIT(INIT_PRIORITY_STANDARD)
  45. {
  46. static_assert(_elements_in(attrInfo) >= (WAMax-WANone)+1, "Elements missing from attrInfo[]");
  47. static_assert(_elements_in(attrInfo) <= (WAMax-WANone)+1, "Extra elements in attrInfo[]");
  48. for (unsigned i=0; i < _elements_in(attrInfo); i++)
  49. {
  50. assertex(attrInfo[i].kind == WANone + i);
  51. }
  52. return true;
  53. }
  54. const char * queryWuAttributeName(WuAttr kind)
  55. {
  56. if ((kind >= WANone) && (kind < WAMax))
  57. return attrInfo[kind-WANone].name;
  58. return nullptr;
  59. }
  60. WuAttr queryWuAttribute(const char * kind)
  61. {
  62. //MORE: This needs to use a hash table
  63. for (unsigned i=WANone; i < WAMax; i++)
  64. {
  65. if (strieq(kind, attrInfo[i-WANone].name))
  66. return (WuAttr)i;
  67. }
  68. return WANone;
  69. }
  70. extern WORKUNIT_API const char * queryAttributeValue(IPropertyTree & src, WuAttr kind)
  71. {
  72. if ((kind <= WANone) || (kind >= WAMax))
  73. return nullptr;
  74. const WuAttrInfo & info = attrInfo[kind-WANone];
  75. const char * path = info.graphPath;
  76. const char * value = src.queryProp(path);
  77. if (!value && info.dft)
  78. value = info.dft;
  79. return value;
  80. }
  81. extern WORKUNIT_API WuAttr queryGraphAttrToWuAttr(const char * name)
  82. {
  83. //MORE: Create a hash table to implement this mapping efficiently
  84. for(unsigned i=1; i < WAMax-WANone; i++)
  85. {
  86. const WuAttrInfo & info = attrInfo[i];
  87. const char * path = info.graphPath;
  88. if (path[0] == '@' && strieq(name, path+1))
  89. return (WuAttr)(i+WANone);
  90. }
  91. return WANone;
  92. }
  93. extern WORKUNIT_API WuAttr queryGraphChildAttToWuAttr(const char * name)
  94. {
  95. //MORE: Create a hash table to implement this mapping efficiently
  96. for(unsigned i=1; i < WAMax-WANone; i++)
  97. {
  98. const WuAttrInfo & info = attrInfo[i];
  99. const char * childPath = info.childPath;
  100. if (childPath && strieq(name, childPath))
  101. return (WuAttr)(i+WANone);
  102. }
  103. return WANone;
  104. }
  105. static IPropertyTree * addGraphAttribute(IPropertyTree * node, const char * name)
  106. {
  107. IPropertyTree * att = createPTree();
  108. att->setProp("@name", name);
  109. return node->addPropTree("att", att);
  110. }
  111. extern WORKUNIT_API void setAttributeValue(IPropertyTree & tgt, WuAttr kind, const char * value)
  112. {
  113. const WuAttrInfo & info = attrInfo[kind-WANone];
  114. const char * path = info.graphPath;
  115. if (path[0] == '@')
  116. tgt.setProp(path, value);
  117. else
  118. addGraphAttribute(&tgt, info.childPath)->setProp("@value", value);
  119. }