mt/src/cl/cromer/mt/Automata.java

79 lines
2.5 KiB
Java

/* 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.
*/
package cl.cromer.mt;
import org.w3c.dom.Document;
import java.util.ArrayList;
class Automata {
private ArrayList<Estado> estados;
private ArrayList<Integer> estadosExistentes;
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));
estados.add(qj, new Estado(qj));
}
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);
}
}
estadosExistentes = new ArrayList<>();
for (int i = 0; i < getEstados().size(); i++) {
if (getEstados().get(i) != null) {
getEstadosExistentes().add(getEstados().get(i).getQ());
}
}
}
ArrayList<Estado> getEstados() {
return estados;
}
private void setEstados(ArrayList<Estado> estados) {
this.estados = estados;
}
ArrayList<Integer> getEstadosExistentes() {
return estadosExistentes;
}
public void setEstadosExistentes(ArrayList<Integer> estadosExistentes) {
this.estadosExistentes = estadosExistentes;
}
}