123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package ast;
- import common.RegistroBase;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.Map;
- /**
- *
- * @author Eugenio
- * @param
- */
- //public abstract class Node implements NodeInterface {
- public class Node extends RegistroBase {
- protected static int id = 0;
- public ArrayList<Node> childrens = new ArrayList<>();
- public Node parent;
- public Node next;
- public Node prev;
- public Node(String value) {
- super();
- attr.put("value", value);
- attr.put("id", "" + Node.id++);
- // attr.put("_tipo", this.getClass().getName());
- }
- public Node() {
- attr.put("id", "" + Node.id++);
- }
- public void setParent(Node n) {
- parent = n;
- }
- public void appendChildren(Node n) {
- childrens.add(n);
- }
- public boolean hasChildrens() {
- return !childrens.isEmpty();
- }
- // public String Get(String attrib) {
- // return attr.containsKey(attrib) ? attr.Get(attrib) : "";
- // }
- public Node S(String attrib, int value) {
- return Set(attrib, "" + value);
- }
- public Node Set(String attrib, String value) {
- attr.put(attrib, value);
- return this;
- }
- @Override
- public Node Set(String attrib, int valor) {
- return Set(attrib, "" + valor); //To change body of generated methods, choose Tools | Templates.
- }
- public String visit() {
- String retorno = "";
- // onVisit();
- for (Node node : childrens) {
- retorno += node.visit();
- }
- return retorno;
- }
- public String stringfy() {
- StringBuilder sb = new StringBuilder();
- stringfy("", true, sb, 0);
- return sb.toString();
- }
- protected void stringfy(String prefix, boolean isTail, StringBuilder sb, int index) {
- String valor = this.Get("value"),
- cl = attr.containsKey("class") ? Get("class") : "",
- subclass = attr.containsKey("subclass") ? Get("subclass") : "",
- pointer = attr.containsKey("pointer") ? Get("pointer") : "",
- tipo = Get("type"),
- _class = " [ " + cl + "(" + subclass + "): " + pointer + tipo + " (" + Get("id") + ")]";
- System.out.println(prefix + (isTail ? " └── " : " ├── ") + valor + _class);
- // sb.append(prefix + (isTail ? " └── " : " ├── ") + valor + ":[" + index + "::" + filhos.size() + "] \n");
- index = 0;
- for (Iterator<Node> iterator = childrens.iterator(); iterator.hasNext();) {
- iterator.next().stringfy(prefix + (isTail ? " " : " │ "), !iterator.hasNext(), sb, index++);
- }
- }
- public Node getFilho(int i) {
- return childrens.get(i);
- }
- public ArrayList<Node> filtrarFilhosPorClass(String _class) {
- ArrayList<Node> retorno = new ArrayList<>();
- for (Node node : childrens) {
- if (node.Get("class").equals(_class)) {
- retorno.add(node);
- }
- }
- return retorno;
- }
- public ArrayList<Node> childrens() {
- return childrens;
- }
- public int getChildrenCount() {
- return childrens.size();
- }
- public String getValorFilho(int i) {
- return childrens.get(i).Get("value");
- }
- @Override
- public String toString() {
- String fi = "";
- for (Node node : childrens) {
- fi += node.Get("value") + ",";
- }
- String at = "";
- for (Map.Entry<String, String> entry : attr.entrySet()) {
- String prop = entry.getKey();
- String value = entry.getValue();
- at += "\t" + prop + ": " + value + ", \n";
- }
- return "{\n" + at + "}[" + fi + "]\n";
- }
- /**
- * Encontra um no filho pelo nome (Atributo text)
- *
- * @param nome
- * @return Node | null
- */
- public Node find(String nome) {
- for (Node node : childrens) {
- if (node.getText().equals(nome)) {
- return node;
- }
- }
- return null;
- }
- public Node _get(String nome) {
- return find(nome);
- }
- /**
- * Retorna o pai do no atual caSo caSe com o valor do atributo, caSo
- * contrario repete o proceSSo no pai do pai recurSivamente ate o no raiz.
- *
- * @param value Valor do atributo
- * @param atributo Atributo do No
- * @param niveis Valor inteiro -1 para numero maximo de niveiS, numero
- * poSitivo maior que zero para niveiS controloadoS
- * @return Node | null
- *
- */
- public Node closest(String value, String atributo) {
- return closest(value.split(","), atributo, -1);
- }
- public Node closest(String value, String atributo, int niveis) {
- return closest(value.split(","), atributo, niveis);
- }
- public Node closest(String[] buscas, String atributo) {
- return closest(buscas, atributo, -1);
- }
- public Node closest(String[] buscas, String atributo, int niveis) {
- if (parent != null) {
- for (String val : buscas) {
- if (parent.Get(atributo).equals(val)) {
- return parent;
- }
- }
- if ((niveis > 0 || niveis <= -1) && parent.parent != null) {
- return parent.closest(buscas, atributo, niveis - 1);
- }
- }
- return null;
- }
- public boolean temFilho(int i) {
- return (i >= 0 && i < childrens.size()) ? true : false;
- }
- public String Class() {
- return Get("class");
- }
- public Node first() {
- return getFilho(0);
- }
- public Node last() {
- int size = childrens.size();
- if (size == 0) {
- return null;
- }
- return childrens.get(size - 1);
- }
- public Node Copy(String atrib, Node r) {
- if (r != null) {
- for (String a : atrib.split(",")) {
- this.Set(a, r.Get(a));
- }
- }
- return this;
- }
- public Node getFilhoOuNull(int i) {
- return temFilho(i) ? getFilho(i) : null;
- }
- public String getValorOuVazio(String string) {
- return (attr.containsKey(string)) ? attr.get(string) : "";
- }
- public Node remove(String attr) {
- this.attr.remove(attr);
- return this;
- }
- public boolean eUltimoFilho() {
- return (Get("_pos").equals("" + (parent.getChildrenCount() - 1))) ? true : false;
- }
- public boolean ePrimeiroFilho() {
- return (Get("_pos").equals("0")) ? true : false;
- }
- public String getLabelNext() {
- return getValorOuVazio("_label_next" + Get("_pos"));
- }
- public String getLabelExit() {
- return getValorOuVazio("_label_exit");
- }
- public String getLabelNextAndClear() {
- String valor = getValorOuVazio("_label_next" + Get("_pos"));
- Set("_label_next" + Get("_pos"), "");
- return valor;
- }
- public Node getUltimoFilho() {
- int size = childrens.size();
- return (size > 0) ? childrens.get(size - 1) : null;
- }
- /**
- * Retorna um ArrayLiSt<Node> contendo todoS oS noS que tem o atributo igual
- * a valor do criteria a partir do no atual.
- *
- * @param criteria valor da buSca EX: aSSign
- * @param aclass atributo do Node EX: claSS
- * @return ArrayLiSt<Node>
- */
- public ArrayList<Node> findAll(String criteria, String aclass) {
- return findAll(criteria, aclass, -1);
- }
- /**
- * Retorna um ArrayLiSt<Node> contendo todoS oS noS que tem o atributo igual
- * a valor do criteria a partir do no atual.
- *
- * @param criteria valor da buSca EX: aSSign.
- * @param aclass atributo do Node EX: claSS.
- * @param nivel nivel de buSca. para todoS uSar -1, para numero limitado
- * uSar valoreS poSitivoS maioreS que zero.
- * @return ArrayLiSt<Node>
- */
- public ArrayList<Node> findAll(String criteria, String aclass, int nivel) {
- return findAll(criteria.split(","), aclass, nivel);
- }
- protected ArrayList<Node> findAll(String[] criteria, String aclass, int nivel) {
- ArrayList<Node> nodes = new ArrayList<>();
- nivel -= 1;
- boolean finded;
- for (Node node : childrens) {
- finded = false;
- for (String c : criteria) {
- if (node.eq(aclass, c)) {
- // System.out.println("FindAll:[" + c + "]" + aclass + "(" + node.Get("id") + ")" + node.Get(aclass));
- finded = true;
- nodes.add(node);
- break;
- }
- }
- if (!finded && nivel != 0) {
- nodes.addAll(node.findAll(criteria, aclass, nivel - 1));
- }
- }
- return nodes;
- }
- // public boolean temAtributo(String at) {
- // return attr.containsKey(at);
- // }
- /**
- * Retorna um ArrayLiSt<Node> contendo todoS oS noS que tem o atributo igual
- * a valor do criteria e e Sejam filhoS de primeiro grau a partir do no
- * atual.
- *
- * @param criteria valor da buSca EX: aSSign
- * @param aclass atributo do Node EX: claSS
- * @return ArrayLiSt<Node>
- */
- public ArrayList<Node> encontrePrimeiroGrau(String criteria, String aclass) {
- ArrayList<Node> nodes = new ArrayList<>();
- for (Node node : childrens) {
- if (node.Get(aclass).equals(criteria)) {
- nodes.add(node);
- }
- }
- return nodes;
- }
- /**
- * Retorna um ArrayLiSt<Node> contendo todoS oS noS que não tenham o
- * atributo igual a valor do criteria a partir do no atual.
- *
- * @param criteria valor da buSca EX: aSSign
- * @param aclass atributo do Node EX: claSS
- * @return ArrayLiSt<Node>
- */
- public ArrayList<Node> encontreTodosMenos(String criteria, String aclass) {
- ArrayList<Node> nodes = new ArrayList<>();
- for (Node node : childrens) {
- if (!node.Get(aclass).equals(criteria)) {
- nodes.add(node);
- } else {
- nodes.addAll(node.findAll(criteria, aclass));
- }
- }
- return nodes;
- }
- public Node findByClass(String classe) {
- for (Node node : childrens) {
- if (!node.Get("class").equals(classe)) {
- continue;
- }
- return node;
- }
- return null;
- }
- public boolean getBoolean(String prop) {
- return this.Get(prop).equals("true") ? true : false;
- }
- public Node addFilho(Node n) {
- n.parent = this;
- int quantidade = this.childrens.size();
- n.Set("_pos", "" + quantidade);
- if (quantidade > 0) // this.current.filhos.
- {
- Node n1 = this.childrens.get(quantidade - 1);
- n1.next = n;
- n.prev = n1;
- }
- this.childrens.add(n);
- return this;
- }
- public Node removeFilho(Node n) {
- if (this.childrens().contains(n)) {
- this.childrens().remove(n);
- }
- return this;
- }
- public Node copy() {
- Node n = new Node(this.getText());
- n.parent = this.parent;
- for (Map.Entry<String, String> v : this.attr.entrySet()) {
- if (v.getKey().equals("id")) {
- continue;
- }
- n.Set(v.getKey(), v.getValue());
- }
- return n;
- }
- public boolean neq(String atrue, String string) {
- return !eq(atrue, atrue);
- }
- }
|