abstract_trie_node.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.StaticTrieNode = exports.AbstractTrieNode = void 0;
  4. const debugger_js_1 = require("../common/debugger.js");
  5. const trie_node_js_1 = require("./trie_node.js");
  6. class AbstractTrieNode {
  7. constructor(constraint, test) {
  8. this.constraint = constraint;
  9. this.test = test;
  10. this.children_ = {};
  11. this.kind = trie_node_js_1.TrieNodeKind.ROOT;
  12. }
  13. getConstraint() {
  14. return this.constraint;
  15. }
  16. getKind() {
  17. return this.kind;
  18. }
  19. applyTest(object) {
  20. return this.test(object);
  21. }
  22. addChild(node) {
  23. const constraint = node.getConstraint();
  24. const child = this.children_[constraint];
  25. this.children_[constraint] = node;
  26. return child;
  27. }
  28. getChild(constraint) {
  29. return this.children_[constraint];
  30. }
  31. getChildren() {
  32. const children = [];
  33. for (const val of Object.values(this.children_)) {
  34. children.push(val);
  35. }
  36. return children;
  37. }
  38. findChildren(object) {
  39. const children = [];
  40. for (const val of Object.values(this.children_)) {
  41. if (val.applyTest(object)) {
  42. children.push(val);
  43. }
  44. }
  45. return children;
  46. }
  47. removeChild(constraint) {
  48. delete this.children_[constraint];
  49. }
  50. toString() {
  51. return this.constraint;
  52. }
  53. }
  54. exports.AbstractTrieNode = AbstractTrieNode;
  55. class StaticTrieNode extends AbstractTrieNode {
  56. constructor(constraint, test) {
  57. super(constraint, test);
  58. this.rule_ = null;
  59. this.kind = trie_node_js_1.TrieNodeKind.STATIC;
  60. }
  61. getRule() {
  62. return this.rule_;
  63. }
  64. setRule(rule) {
  65. if (this.rule_) {
  66. debugger_js_1.Debugger.getInstance().output('Replacing rule ' + this.rule_ + ' with ' + rule);
  67. }
  68. this.rule_ = rule;
  69. }
  70. toString() {
  71. const rule = this.getRule();
  72. return rule
  73. ? this.constraint + '\n' + '==> ' + this.getRule().action
  74. : this.constraint;
  75. }
  76. }
  77. exports.StaticTrieNode = StaticTrieNode;