trie.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { TrieNodeKind } from './trie_node.js';
  2. import { getNode } from './trie_node_factory.js';
  3. export class Trie {
  4. static collectRules_(root) {
  5. const rules = [];
  6. let explore = [root];
  7. while (explore.length) {
  8. const node = explore.shift();
  9. if (node.getKind() === TrieNodeKind.QUERY ||
  10. node.getKind() === TrieNodeKind.BOOLEAN) {
  11. const rule = node.getRule();
  12. if (rule) {
  13. rules.unshift(rule);
  14. }
  15. }
  16. explore = explore.concat(node.getChildren());
  17. }
  18. return rules;
  19. }
  20. static printWithDepth_(node, depth, str) {
  21. const prefix = new Array(depth + 2).join(depth.toString()) + ': ';
  22. str += prefix + node.toString() + '\n';
  23. const children = node.getChildren();
  24. for (let i = 0, child; (child = children[i]); i++) {
  25. str = Trie.printWithDepth_(child, depth + 1, str);
  26. }
  27. return str;
  28. }
  29. static order_(node) {
  30. const children = node.getChildren();
  31. if (!children.length) {
  32. return 0;
  33. }
  34. const max = Math.max.apply(null, children.map(Trie.order_));
  35. return Math.max(children.length, max);
  36. }
  37. constructor() {
  38. this.root = getNode(TrieNodeKind.ROOT, '', null);
  39. }
  40. addRule(rule) {
  41. let node = this.root;
  42. const context = rule.context;
  43. const dynamicCstr = rule.dynamicCstr.getValues();
  44. for (let i = 0, l = dynamicCstr.length; i < l; i++) {
  45. node = this.addNode_(node, dynamicCstr[i], TrieNodeKind.DYNAMIC, context);
  46. }
  47. node = this.addNode_(node, rule.precondition.query, TrieNodeKind.QUERY, context);
  48. const booleans = rule.precondition.constraints;
  49. for (let i = 0, l = booleans.length; i < l; i++) {
  50. node = this.addNode_(node, booleans[i], TrieNodeKind.BOOLEAN, context);
  51. }
  52. node.setRule(rule);
  53. }
  54. lookupRules(xml, dynamic) {
  55. let nodes = [this.root];
  56. const rules = [];
  57. while (dynamic.length) {
  58. const dynamicSet = dynamic.shift();
  59. const newNodes = [];
  60. while (nodes.length) {
  61. const node = nodes.shift();
  62. const children = node.getChildren();
  63. children.forEach((child) => {
  64. if (child.getKind() !== TrieNodeKind.DYNAMIC ||
  65. dynamicSet.indexOf(child.getConstraint()) !== -1) {
  66. newNodes.push(child);
  67. }
  68. });
  69. }
  70. nodes = newNodes.slice();
  71. }
  72. while (nodes.length) {
  73. const node = nodes.shift();
  74. if (node.getRule) {
  75. const rule = node.getRule();
  76. if (rule) {
  77. rules.push(rule);
  78. }
  79. }
  80. const children = node.findChildren(xml);
  81. nodes = nodes.concat(children);
  82. }
  83. return rules;
  84. }
  85. hasSubtrie(cstrs) {
  86. let subtrie = this.root;
  87. for (let i = 0, l = cstrs.length; i < l; i++) {
  88. const cstr = cstrs[i];
  89. subtrie = subtrie.getChild(cstr);
  90. if (!subtrie) {
  91. return false;
  92. }
  93. }
  94. return true;
  95. }
  96. toString() {
  97. return Trie.printWithDepth_(this.root, 0, '');
  98. }
  99. collectRules(root = this.root) {
  100. return Trie.collectRules_(root);
  101. }
  102. order() {
  103. return Trie.order_(this.root);
  104. }
  105. enumerate(opt_info) {
  106. return this.enumerate_(this.root, opt_info);
  107. }
  108. byConstraint(constraint) {
  109. let node = this.root;
  110. while (constraint.length && node) {
  111. const cstr = constraint.shift();
  112. node = node.getChild(cstr);
  113. }
  114. return node || null;
  115. }
  116. enumerate_(node, info) {
  117. info = info || {};
  118. const children = node.getChildren();
  119. for (let i = 0, child; (child = children[i]); i++) {
  120. if (child.kind !== TrieNodeKind.DYNAMIC) {
  121. continue;
  122. }
  123. info[child.getConstraint()] = this.enumerate_(child, info[child.getConstraint()]);
  124. }
  125. return info;
  126. }
  127. addNode_(node, constraint, kind, context) {
  128. let nextNode = node.getChild(constraint);
  129. if (!nextNode) {
  130. nextNode = getNode(kind, constraint, context);
  131. node.addChild(nextNode);
  132. }
  133. return nextNode;
  134. }
  135. }