2017-06-28 23:54:27 -04:00
|
|
|
/* Copyright (c) 2017 Christopher Cromer
|
|
|
|
* Copyright (c) 2017 Carlos Faúndez
|
|
|
|
*
|
|
|
|
* This file is part of mt. It is subject to the license terms in the LICENSE file found in the top-level directory of this distribution.
|
|
|
|
* This file may not be copied, modified, propagated, or distributed except according to the terms contained in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2017-07-03 20:46:11 -04:00
|
|
|
package cl.cromer.mt;
|
2017-06-28 10:09:44 -04:00
|
|
|
|
|
|
|
import org.w3c.dom.Document;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
class Automata {
|
2017-06-28 23:54:27 -04:00
|
|
|
private ArrayList<Estado> estados;
|
2017-07-04 23:34:45 -04:00
|
|
|
|
|
|
|
private ArrayList<Integer> estadosExistentes;
|
2017-06-28 23:54:27 -04:00
|
|
|
|
|
|
|
Automata(Document document) {
|
|
|
|
setEstados(new ArrayList<>());
|
|
|
|
for (int i = 0; i < document.getElementsByTagName("transicion").getLength(); i++) {
|
|
|
|
char move = document.getElementsByTagName("movimiento").item(i).getTextContent().charAt(0);
|
|
|
|
if (move == 'E' || move == 'R' || move == 'L' || move == '*') {
|
|
|
|
int qi = Integer.parseInt(document.getElementsByTagName("qi").item(i).getTextContent());
|
|
|
|
int qj = Integer.parseInt(document.getElementsByTagName("qj").item(i).getTextContent());
|
|
|
|
char si = document.getElementsByTagName("si").item(i).getTextContent().charAt(0);
|
|
|
|
char sj = document.getElementsByTagName("sj").item(i).getTextContent().charAt(0);
|
|
|
|
/*if(estados.isEmpty() && qi != qj){
|
|
|
|
estados.add(qi, new Estado(qi));
|
2017-06-28 10:09:44 -04:00
|
|
|
estados.add(qj, new Estado(qj));
|
|
|
|
}
|
2017-06-28 23:54:27 -04:00
|
|
|
else if(estados.isEmpty() && qi == qj) {
|
|
|
|
estados.add(qi, new Estado(qi));
|
|
|
|
}*/
|
|
|
|
if (estados.size() <= qi) {
|
|
|
|
estados.add(qi, new Estado(qi));
|
|
|
|
}
|
|
|
|
if (estados.size() <= qj) {
|
|
|
|
estados.add(qj, new Estado(qj));
|
|
|
|
}
|
|
|
|
if (!estados.get(qi).createLink(si, estados.get(qj), sj, move)) {
|
|
|
|
if (qi == qj) {
|
|
|
|
System.out.println("Recursivo");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
System.out.println("En" + qi + " a " + qj + "Transicion para " + si + " ya esta creada");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
System.out.println("Movimiento invalido de cinta");
|
|
|
|
System.exit(1);
|
|
|
|
}
|
|
|
|
}
|
2017-07-04 23:34:45 -04:00
|
|
|
estadosExistentes = new ArrayList<>();
|
|
|
|
for (int i = 0; i < getEstados().size(); i++) {
|
|
|
|
if (getEstados().get(i) != null) {
|
|
|
|
getEstadosExistentes().add(getEstados().get(i).getQ());
|
|
|
|
}
|
2017-07-03 13:30:30 -04:00
|
|
|
}
|
2017-06-28 23:54:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ArrayList<Estado> getEstados() {
|
|
|
|
return estados;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setEstados(ArrayList<Estado> estados) {
|
|
|
|
this.estados = estados;
|
|
|
|
}
|
2017-07-04 16:08:29 -04:00
|
|
|
|
2017-07-04 23:34:45 -04:00
|
|
|
ArrayList<Integer> getEstadosExistentes() {
|
|
|
|
return estadosExistentes;
|
2017-07-04 16:08:29 -04:00
|
|
|
}
|
|
|
|
|
2017-07-04 23:34:45 -04:00
|
|
|
public void setEstadosExistentes(ArrayList<Integer> estadosExistentes) {
|
|
|
|
this.estadosExistentes = estadosExistentes;
|
2017-07-04 16:08:29 -04:00
|
|
|
}
|
2017-06-28 10:09:44 -04:00
|
|
|
}
|