Node.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package ast;
  7. import common.RegistroBase;
  8. import java.util.ArrayList;
  9. import java.util.Iterator;
  10. import java.util.Map;
  11. /**
  12. *
  13. * @author Eugenio
  14. * @param
  15. */
  16. //public abstract class Node implements NodeInterface {
  17. public class Node extends RegistroBase {
  18. protected static int id = 0;
  19. public ArrayList<Node> childrens = new ArrayList<>();
  20. public Node parent;
  21. public Node next;
  22. public Node prev;
  23. public Node(String value) {
  24. super();
  25. attr.put("value", value);
  26. attr.put("id", "" + Node.id++);
  27. // attr.put("_tipo", this.getClass().getName());
  28. }
  29. public Node() {
  30. attr.put("id", "" + Node.id++);
  31. }
  32. public void setParent(Node n) {
  33. parent = n;
  34. }
  35. public void appendChildren(Node n) {
  36. childrens.add(n);
  37. }
  38. public boolean hasChildrens() {
  39. return !childrens.isEmpty();
  40. }
  41. // public String Get(String attrib) {
  42. // return attr.containsKey(attrib) ? attr.Get(attrib) : "";
  43. // }
  44. public Node S(String attrib, int value) {
  45. return Set(attrib, "" + value);
  46. }
  47. public Node Set(String attrib, String value) {
  48. attr.put(attrib, value);
  49. return this;
  50. }
  51. @Override
  52. public Node Set(String attrib, int valor) {
  53. return Set(attrib, "" + valor); //To change body of generated methods, choose Tools | Templates.
  54. }
  55. public String visit() {
  56. String retorno = "";
  57. // onVisit();
  58. for (Node node : childrens) {
  59. retorno += node.visit();
  60. }
  61. return retorno;
  62. }
  63. public String stringfy() {
  64. StringBuilder sb = new StringBuilder();
  65. stringfy("", true, sb, 0);
  66. return sb.toString();
  67. }
  68. protected void stringfy(String prefix, boolean isTail, StringBuilder sb, int index) {
  69. String valor = this.Get("value"),
  70. cl = attr.containsKey("class") ? Get("class") : "",
  71. subclass = attr.containsKey("subclass") ? Get("subclass") : "",
  72. pointer = attr.containsKey("pointer") ? Get("pointer") : "",
  73. tipo = Get("type"),
  74. _class = " [ " + cl + "(" + subclass + "): " + pointer + tipo + " (" + Get("id") + ")]";
  75. System.out.println(prefix + (isTail ? " └── " : " ├── ") + valor + _class);
  76. // sb.append(prefix + (isTail ? " └── " : " ├── ") + valor + ":[" + index + "::" + filhos.size() + "] \n");
  77. index = 0;
  78. for (Iterator<Node> iterator = childrens.iterator(); iterator.hasNext();) {
  79. iterator.next().stringfy(prefix + (isTail ? " " : " │ "), !iterator.hasNext(), sb, index++);
  80. }
  81. }
  82. public Node getFilho(int i) {
  83. return childrens.get(i);
  84. }
  85. public ArrayList<Node> filtrarFilhosPorClass(String _class) {
  86. ArrayList<Node> retorno = new ArrayList<>();
  87. for (Node node : childrens) {
  88. if (node.Get("class").equals(_class)) {
  89. retorno.add(node);
  90. }
  91. }
  92. return retorno;
  93. }
  94. public ArrayList<Node> childrens() {
  95. return childrens;
  96. }
  97. public int getChildrenCount() {
  98. return childrens.size();
  99. }
  100. public String getValorFilho(int i) {
  101. return childrens.get(i).Get("value");
  102. }
  103. @Override
  104. public String toString() {
  105. String fi = "";
  106. for (Node node : childrens) {
  107. fi += node.Get("value") + ",";
  108. }
  109. String at = "";
  110. for (Map.Entry<String, String> entry : attr.entrySet()) {
  111. String prop = entry.getKey();
  112. String value = entry.getValue();
  113. at += "\t" + prop + ": " + value + ", \n";
  114. }
  115. return "{\n" + at + "}[" + fi + "]\n";
  116. }
  117. /**
  118. * Encontra um no filho pelo nome (Atributo text)
  119. *
  120. * @param nome
  121. * @return Node | null
  122. */
  123. public Node find(String nome) {
  124. for (Node node : childrens) {
  125. if (node.getText().equals(nome)) {
  126. return node;
  127. }
  128. }
  129. return null;
  130. }
  131. public Node _get(String nome) {
  132. return find(nome);
  133. }
  134. /**
  135. * Retorna o pai do no atual caSo caSe com o valor do atributo, caSo
  136. * contrario repete o proceSSo no pai do pai recurSivamente ate o no raiz.
  137. *
  138. * @param value Valor do atributo
  139. * @param atributo Atributo do No
  140. * @param niveis Valor inteiro -1 para numero maximo de niveiS, numero
  141. * poSitivo maior que zero para niveiS controloadoS
  142. * @return Node | null
  143. *
  144. */
  145. public Node closest(String value, String atributo) {
  146. return closest(value.split(","), atributo, -1);
  147. }
  148. public Node closest(String value, String atributo, int niveis) {
  149. return closest(value.split(","), atributo, niveis);
  150. }
  151. public Node closest(String[] buscas, String atributo) {
  152. return closest(buscas, atributo, -1);
  153. }
  154. public Node closest(String[] buscas, String atributo, int niveis) {
  155. if (parent != null) {
  156. for (String val : buscas) {
  157. if (parent.Get(atributo).equals(val)) {
  158. return parent;
  159. }
  160. }
  161. if ((niveis > 0 || niveis <= -1) && parent.parent != null) {
  162. return parent.closest(buscas, atributo, niveis - 1);
  163. }
  164. }
  165. return null;
  166. }
  167. public boolean temFilho(int i) {
  168. return (i >= 0 && i < childrens.size()) ? true : false;
  169. }
  170. public String Class() {
  171. return Get("class");
  172. }
  173. public Node first() {
  174. return getFilho(0);
  175. }
  176. public Node last() {
  177. int size = childrens.size();
  178. if (size == 0) {
  179. return null;
  180. }
  181. return childrens.get(size - 1);
  182. }
  183. public Node Copy(String atrib, Node r) {
  184. if (r != null) {
  185. for (String a : atrib.split(",")) {
  186. this.Set(a, r.Get(a));
  187. }
  188. }
  189. return this;
  190. }
  191. public Node getFilhoOuNull(int i) {
  192. return temFilho(i) ? getFilho(i) : null;
  193. }
  194. public String getValorOuVazio(String string) {
  195. return (attr.containsKey(string)) ? attr.get(string) : "";
  196. }
  197. public Node remove(String attr) {
  198. this.attr.remove(attr);
  199. return this;
  200. }
  201. public boolean eUltimoFilho() {
  202. return (Get("_pos").equals("" + (parent.getChildrenCount() - 1))) ? true : false;
  203. }
  204. public boolean ePrimeiroFilho() {
  205. return (Get("_pos").equals("0")) ? true : false;
  206. }
  207. public String getLabelNext() {
  208. return getValorOuVazio("_label_next" + Get("_pos"));
  209. }
  210. public String getLabelExit() {
  211. return getValorOuVazio("_label_exit");
  212. }
  213. public String getLabelNextAndClear() {
  214. String valor = getValorOuVazio("_label_next" + Get("_pos"));
  215. Set("_label_next" + Get("_pos"), "");
  216. return valor;
  217. }
  218. public Node getUltimoFilho() {
  219. int size = childrens.size();
  220. return (size > 0) ? childrens.get(size - 1) : null;
  221. }
  222. /**
  223. * Retorna um ArrayLiSt<Node> contendo todoS oS noS que tem o atributo igual
  224. * a valor do criteria a partir do no atual.
  225. *
  226. * @param criteria valor da buSca EX: aSSign
  227. * @param aclass atributo do Node EX: claSS
  228. * @return ArrayLiSt<Node>
  229. */
  230. public ArrayList<Node> findAll(String criteria, String aclass) {
  231. return findAll(criteria, aclass, -1);
  232. }
  233. /**
  234. * Retorna um ArrayLiSt<Node> contendo todoS oS noS que tem o atributo igual
  235. * a valor do criteria a partir do no atual.
  236. *
  237. * @param criteria valor da buSca EX: aSSign.
  238. * @param aclass atributo do Node EX: claSS.
  239. * @param nivel nivel de buSca. para todoS uSar -1, para numero limitado
  240. * uSar valoreS poSitivoS maioreS que zero.
  241. * @return ArrayLiSt<Node>
  242. */
  243. public ArrayList<Node> findAll(String criteria, String aclass, int nivel) {
  244. return findAll(criteria.split(","), aclass, nivel);
  245. }
  246. protected ArrayList<Node> findAll(String[] criteria, String aclass, int nivel) {
  247. ArrayList<Node> nodes = new ArrayList<>();
  248. nivel -= 1;
  249. boolean finded;
  250. for (Node node : childrens) {
  251. finded = false;
  252. for (String c : criteria) {
  253. if (node.eq(aclass, c)) {
  254. // System.out.println("FindAll:[" + c + "]" + aclass + "(" + node.Get("id") + ")" + node.Get(aclass));
  255. finded = true;
  256. nodes.add(node);
  257. break;
  258. }
  259. }
  260. if (!finded && nivel != 0) {
  261. nodes.addAll(node.findAll(criteria, aclass, nivel - 1));
  262. }
  263. }
  264. return nodes;
  265. }
  266. // public boolean temAtributo(String at) {
  267. // return attr.containsKey(at);
  268. // }
  269. /**
  270. * Retorna um ArrayLiSt<Node> contendo todoS oS noS que tem o atributo igual
  271. * a valor do criteria e e Sejam filhoS de primeiro grau a partir do no
  272. * atual.
  273. *
  274. * @param criteria valor da buSca EX: aSSign
  275. * @param aclass atributo do Node EX: claSS
  276. * @return ArrayLiSt<Node>
  277. */
  278. public ArrayList<Node> encontrePrimeiroGrau(String criteria, String aclass) {
  279. ArrayList<Node> nodes = new ArrayList<>();
  280. for (Node node : childrens) {
  281. if (node.Get(aclass).equals(criteria)) {
  282. nodes.add(node);
  283. }
  284. }
  285. return nodes;
  286. }
  287. /**
  288. * Retorna um ArrayLiSt<Node> contendo todoS oS noS que não tenham o
  289. * atributo igual a valor do criteria a partir do no atual.
  290. *
  291. * @param criteria valor da buSca EX: aSSign
  292. * @param aclass atributo do Node EX: claSS
  293. * @return ArrayLiSt<Node>
  294. */
  295. public ArrayList<Node> encontreTodosMenos(String criteria, String aclass) {
  296. ArrayList<Node> nodes = new ArrayList<>();
  297. for (Node node : childrens) {
  298. if (!node.Get(aclass).equals(criteria)) {
  299. nodes.add(node);
  300. } else {
  301. nodes.addAll(node.findAll(criteria, aclass));
  302. }
  303. }
  304. return nodes;
  305. }
  306. public Node findByClass(String classe) {
  307. for (Node node : childrens) {
  308. if (!node.Get("class").equals(classe)) {
  309. continue;
  310. }
  311. return node;
  312. }
  313. return null;
  314. }
  315. public boolean getBoolean(String prop) {
  316. return this.Get(prop).equals("true") ? true : false;
  317. }
  318. public Node addFilho(Node n) {
  319. n.parent = this;
  320. int quantidade = this.childrens.size();
  321. n.Set("_pos", "" + quantidade);
  322. if (quantidade > 0) // this.current.filhos.
  323. {
  324. Node n1 = this.childrens.get(quantidade - 1);
  325. n1.next = n;
  326. n.prev = n1;
  327. }
  328. this.childrens.add(n);
  329. return this;
  330. }
  331. public Node removeFilho(Node n) {
  332. if (this.childrens().contains(n)) {
  333. this.childrens().remove(n);
  334. }
  335. return this;
  336. }
  337. public Node copy() {
  338. Node n = new Node(this.getText());
  339. n.parent = this.parent;
  340. for (Map.Entry<String, String> v : this.attr.entrySet()) {
  341. if (v.getKey().equals("id")) {
  342. continue;
  343. }
  344. n.Set(v.getKey(), v.getValue());
  345. }
  346. return n;
  347. }
  348. public boolean neq(String atrue, String string) {
  349. return !eq(atrue, atrue);
  350. }
  351. }