better error checking while optimizing expressions

This commit is contained in:
hneemann 2017-04-07 08:47:25 +02:00
parent 1aeb54b513
commit 8464e72cf7
6 changed files with 43 additions and 9 deletions

View File

@ -186,7 +186,13 @@ public class TruthTable {
* Adds a new variable * Adds a new variable
*/ */
public void addVariable() { public void addVariable() {
addVariable("A"); char v = 'A';
Variable var;
do {
var = new Variable("" + v);
v++;
} while (variables.contains(var) && v <= 'Z');
addVariable(var);
} }
/** /**
@ -195,7 +201,16 @@ public class TruthTable {
* @param name name of the variable * @param name name of the variable
*/ */
public void addVariable(String name) { public void addVariable(String name) {
variables.add(new Variable(name)); addVariable(new Variable(name));
}
/**
* Adds a variable
*
* @param var the variable to add
*/
public void addVariable(Variable var) {
variables.add(var);
for (Result r : results) for (Result r : results)
r.setValues(BoolTableByteArray.createDoubledValues(r.getValues())); r.setValues(BoolTableByteArray.createDoubledValues(r.getValues()));

View File

@ -28,8 +28,8 @@ import java.util.*;
import static de.neemann.digital.draw.shapes.GenericShape.SIZE; import static de.neemann.digital.draw.shapes.GenericShape.SIZE;
/** /**
* Builder to create a circuit from a set off expression * Builder to create a circuit from a set of expressions.
* Is also able to create a state machine. * Is also able to create a finite state machine.
* *
* @author hneemann * @author hneemann
*/ */
@ -43,6 +43,7 @@ public class CircuitBuilder implements BuilderInterface<CircuitBuilder> {
private final ArrayList<Variable> sequentialVars; private final ArrayList<Variable> sequentialVars;
private final ArrayList<FragmentVisualElement> flipflops; private final ArrayList<FragmentVisualElement> flipflops;
private final boolean useJKff; private final boolean useJKff;
private final ArrayList<Variable> desiredVarOrdering;
private int pos; private int pos;
/** /**
@ -62,8 +63,20 @@ public class CircuitBuilder implements BuilderInterface<CircuitBuilder> {
* @param useJKff true if JK flip-flops should be used to create state machines instead of D flip-flops. * @param useJKff true if JK flip-flops should be used to create state machines instead of D flip-flops.
*/ */
public CircuitBuilder(ShapeFactory shapeFactory, boolean useJKff) { public CircuitBuilder(ShapeFactory shapeFactory, boolean useJKff) {
this(shapeFactory, useJKff, null);
}
/**
* Creates a new builder.
*
* @param shapeFactory ShapeFactory which is set to the created VisualElements
* @param useJKff true if JK flip-flops should be used to create state machines instead of D flip-flops.
* @param varOrdering the desired ordering of the variables, There may be more variables than required. Maybe null.
*/
public CircuitBuilder(ShapeFactory shapeFactory, boolean useJKff, ArrayList<Variable> varOrdering) {
this.shapeFactory = shapeFactory; this.shapeFactory = shapeFactory;
this.useJKff = useJKff; this.useJKff = useJKff;
desiredVarOrdering = varOrdering;
variableVisitor = new VariableVisitor(); variableVisitor = new VariableVisitor();
fragmentVariables = new ArrayList<>(); fragmentVariables = new ArrayList<>();
fragments = new ArrayList<>(); fragments = new ArrayList<>();
@ -285,6 +298,8 @@ public class CircuitBuilder implements BuilderInterface<CircuitBuilder> {
// order bus variables // order bus variables
Collection<Variable> variables = variableVisitor.getVariables(); Collection<Variable> variables = variableVisitor.getVariables();
if (desiredVarOrdering != null)
variables = order(variables, desiredVarOrdering);
if (!sequentialVars.isEmpty()) if (!sequentialVars.isEmpty())
variables = order(variables, sequentialVars); variables = order(variables, sequentialVars);

View File

@ -56,11 +56,11 @@ public class CheckResultListener implements ExpressionListener {
return; return;
case one: case one:
if (!calculate) if (!calculate)
throw new ExpressionException("internal minimization error: found false, expected true"); throw new ExpressionException(Lang.get("err_minimizationFailed"));
break; break;
case zero: case zero:
if (calculate) if (calculate)
throw new ExpressionException("internal minimization error: found true, expected false"); throw new ExpressionException(Lang.get("err_minimizationFailed"));
break; break;
} }
} }

View File

@ -507,7 +507,7 @@ public class TableDialog extends JDialog {
private void createCircuit(boolean useJKff, ExpressionModifier... modifier) { private void createCircuit(boolean useJKff, ExpressionModifier... modifier) {
try { try {
CircuitBuilder circuitBuilder = new CircuitBuilder(shapeFactory, useJKff); CircuitBuilder circuitBuilder = new CircuitBuilder(shapeFactory, useJKff, model.getTable().getVars());
new BuilderExpressionCreator(circuitBuilder, modifier).setUseJKOptimizer(useJKff).create(lastGeneratedExpressions); new BuilderExpressionCreator(circuitBuilder, modifier).setUseJKOptimizer(useJKff).create(lastGeneratedExpressions);
Circuit circuit = circuitBuilder.createCircuit(); Circuit circuit = circuitBuilder.createCircuit();
SwingUtilities.invokeLater(() -> new Main(null, library, circuit).setVisible(true)); SwingUtilities.invokeLater(() -> new Main(null, library, circuit).setVisible(true));

View File

@ -462,6 +462,8 @@ Es sind nur {1} Variablen erlaubt, es wurden jedoch {2} gefunden.</string>
<string name="err_varName_N_UsedTwice">Die Variable {0} wird mehrfach verwendet!</string> <string name="err_varName_N_UsedTwice">Die Variable {0} wird mehrfach verwendet!</string>
<string name="err_fileNeedsToBeSaved">Die Datei muss zunächst gespeichert werden!</string> <string name="err_fileNeedsToBeSaved">Die Datei muss zunächst gespeichert werden!</string>
<string name="err_recursiveNestingAt_N0">Die Schaltung {0} bindet sich selbst ein!</string> <string name="err_recursiveNestingAt_N0">Die Schaltung {0} bindet sich selbst ein!</string>
<string name="err_minimizationFailed">Das Ergebnis der Minimierung ist nicht korrekt!
Sind evtl. die Namen der Variablen nicht eindeutig?</string>
<string name="key_AddrBits">Adress-Bits</string> <string name="key_AddrBits">Adress-Bits</string>
<string name="key_AddrBits_tt">Anzahl der Adress-Bits die verwendet werden.</string> <string name="key_AddrBits_tt">Anzahl der Adress-Bits die verwendet werden.</string>
@ -744,7 +746,7 @@ Die Icons stammen aus dem Tango Desktop Project.</string>
<string name="msg_fileNotAccessible">Dieser Dateiename ist nicht aus dem aktuellen Projekt importierbar!</string> <string name="msg_fileNotAccessible">Dieser Dateiename ist nicht aus dem aktuellen Projekt importierbar!</string>
<string name="msg_fileIsNotUnique">Der Dateiname ist nicht eindeutig!</string> <string name="msg_fileIsNotUnique">Der Dateiname ist nicht eindeutig!</string>
<string name="msg_fileNotImportedYet">Die Datei wurde noch nicht importiert.</string> <string name="msg_fileNotImportedYet">Die Datei wurde noch nicht importiert.</string>
<string name="msg_fileExists">Die Datei {0} existiert schon!</string> <string name="msg_fileExists">Die Datei {0} existiert schon! Soll die Datei überschrieben werden?</string>
<string name="ok">Ok</string> <string name="ok">Ok</string>
<string name="rot_0"></string> <string name="rot_0"></string>

View File

@ -449,6 +449,8 @@ allowed are {1} variables but {2} are found.</string>
<string name="err_varName_N_UsedTwice">The variable {0} is used twice!</string> <string name="err_varName_N_UsedTwice">The variable {0} is used twice!</string>
<string name="err_fileNeedsToBeSaved">The file needs to be saved!</string> <string name="err_fileNeedsToBeSaved">The file needs to be saved!</string>
<string name="err_recursiveNestingAt_N0">The circuit {0} imports itself!</string> <string name="err_recursiveNestingAt_N0">The circuit {0} imports itself!</string>
<string name="err_minimizationFailed">The result of the minimization is not correct!
The names of the variables may not be unique.</string>
<string name="key_AddrBits">Address Bits</string> <string name="key_AddrBits">Address Bits</string>
<string name="key_AddrBits_tt">Number of address bits used.</string> <string name="key_AddrBits_tt">Number of address bits used.</string>
@ -731,7 +733,7 @@ The icons are taken from the Tango Desktop Project.</string>
<string name="msg_fileNotAccessible">The selected file name is not importable from the actual project!</string> <string name="msg_fileNotAccessible">The selected file name is not importable from the actual project!</string>
<string name="msg_fileIsNotUnique">The file name is not unique!</string> <string name="msg_fileIsNotUnique">The file name is not unique!</string>
<string name="msg_fileNotImportedYet">The file has not yet been imported.</string> <string name="msg_fileNotImportedYet">The file has not yet been imported.</string>
<string name="msg_fileExists">The file {0} already exists!</string> <string name="msg_fileExists">The file {0} already exists! Do you want to overwrite the file?</string>
<string name="ok">Ok</string> <string name="ok">Ok</string>
<string name="rot_0"></string> <string name="rot_0"></string>