mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-13 23:06:22 -04:00
simplified table modification
This commit is contained in:
parent
83126adb1b
commit
94d42a6088
@ -18,6 +18,7 @@ import de.neemann.digital.analyse.quinemc.ThreeStateValue;
|
||||
import de.neemann.digital.lang.Lang;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
@ -52,7 +53,7 @@ public class TruthTable {
|
||||
*/
|
||||
public void save(File filename) throws IOException {
|
||||
XStream xStream = getxStream();
|
||||
try (Writer out = new OutputStreamWriter(new FileOutputStream(filename), "utf-8")) {
|
||||
try (Writer out = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_8)) {
|
||||
out.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
|
||||
xStream.marshal(this, new PrettyPrintWriter(out));
|
||||
}
|
||||
@ -68,7 +69,7 @@ public class TruthTable {
|
||||
if (results.size() > 63)
|
||||
throw new IOException(Lang.get("err_tableHasToManyResultColumns"));
|
||||
|
||||
try (Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), "utf-8"))) {
|
||||
try (Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_8))) {
|
||||
saveHex(out);
|
||||
}
|
||||
}
|
||||
@ -456,30 +457,18 @@ public class TruthTable {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the don't cares to a given value
|
||||
* Modifies all column in the table
|
||||
*
|
||||
* @param b the value to set
|
||||
* @param m the modifier to use
|
||||
* @return this for chained calls
|
||||
*/
|
||||
public void setXto(boolean b) {
|
||||
public TruthTable modifyValues(BoolTableByteArray.TableModifier m) {
|
||||
for (Result r : results) {
|
||||
BoolTable bt = r.getValues();
|
||||
if (bt instanceof BoolTableByteArray)
|
||||
((BoolTableByteArray) bt).setXTo(b ? 1 : 0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set all table entries to the given value.
|
||||
* Zero and one behave as expected. All other values represent "don't care"
|
||||
*
|
||||
* @param value the value to set
|
||||
*/
|
||||
public void setAllTo(int value) {
|
||||
for (Result r : results) {
|
||||
BoolTable bt = r.getValues();
|
||||
if (bt instanceof BoolTableByteArray)
|
||||
((BoolTableByteArray) bt).setAllTo(value);
|
||||
((BoolTableByteArray) bt).modify(m);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -79,23 +79,25 @@ public class BoolTableByteArray implements BoolTable {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the don't cares to the given value
|
||||
* Modifies all the table elements using the given modifier.
|
||||
*
|
||||
* @param value the value
|
||||
* @param m the modifier
|
||||
*/
|
||||
public void setXTo(int value) {
|
||||
public void modify(TableModifier m) {
|
||||
for (int i = 0; i < table.length; i++)
|
||||
if (table[i] > 1)
|
||||
table[i] = (byte) value;
|
||||
table[i] = m.modify(table[i]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets all entries to the given value
|
||||
*
|
||||
* @param value the value
|
||||
* Modifier to modify the table
|
||||
*/
|
||||
public void setAllTo(int value) {
|
||||
for (int i = 0; i < table.length; i++)
|
||||
table[i] = (byte) value;
|
||||
public interface TableModifier {
|
||||
/**
|
||||
* Creates the modified value
|
||||
*
|
||||
* @param b the original value
|
||||
* @return the modified value
|
||||
*/
|
||||
byte modify(byte b);
|
||||
}
|
||||
}
|
||||
|
@ -83,7 +83,7 @@ public class TransitionTableCreator {
|
||||
// create state variables
|
||||
ArrayList<Variable> vars = new ArrayList<>();
|
||||
for (int i = stateBits - 1; i >= 0; i--) {
|
||||
final Variable var = new Variable(STATE_VAR+"_" + i + "^n");
|
||||
final Variable var = new Variable(STATE_VAR + "_" + i + "^n");
|
||||
vars.add(var);
|
||||
boolean initVal = (initState & (1 << i)) != 0;
|
||||
modelAnalyserInfo.setSequentialInitValue(var.getIdentifier(), initVal ? 1 : 0);
|
||||
@ -93,7 +93,7 @@ public class TransitionTableCreator {
|
||||
|
||||
// create the next state result variables
|
||||
for (int i = stateBits - 1; i >= 0; i--)
|
||||
truthTable.addResult(STATE_VAR+"_" + i + "^{n+1}");
|
||||
truthTable.addResult(STATE_VAR + "_" + i + "^{n+1}");
|
||||
|
||||
// add the output variables
|
||||
TreeSet<String> results = new TreeSet<>();
|
||||
@ -108,7 +108,7 @@ public class TransitionTableCreator {
|
||||
}
|
||||
|
||||
// set all to dc
|
||||
truthTable.setAllTo(2);
|
||||
truthTable.modifyValues(v -> (byte) 2);
|
||||
|
||||
// set state output variables
|
||||
for (State s : states) {
|
||||
|
@ -366,44 +366,44 @@ public class TableDialog extends JDialog {
|
||||
setMenu.add(new ToolTipAction(Lang.get("menu_table_setXTo0")) {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
TruthTable t = model.getTable();
|
||||
t.setXto(false);
|
||||
setModel(new TruthTableTableModel(t));
|
||||
modifyTable(v -> v > 1 ? 0 : v);
|
||||
}
|
||||
}.setToolTip(Lang.get("menu_table_setXTo0_tt")).createJMenuItem());
|
||||
setMenu.add(new ToolTipAction(Lang.get("menu_table_setXTo1")) {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
TruthTable t = model.getTable();
|
||||
t.setXto(true);
|
||||
setModel(new TruthTableTableModel(t));
|
||||
modifyTable(v -> v > 1 ? 1 : v);
|
||||
}
|
||||
}.setToolTip(Lang.get("menu_table_setXTo1_tt")).createJMenuItem());
|
||||
setMenu.add(new ToolTipAction(Lang.get("menu_table_setAllToX")) {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
setAllValuesTo(2);
|
||||
modifyTable(v -> (byte) 2);
|
||||
}
|
||||
}.setToolTip(Lang.get("menu_table_setAllToX_tt")).createJMenuItem());
|
||||
setMenu.add(new ToolTipAction(Lang.get("menu_table_setAllTo0")) {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
setAllValuesTo(0);
|
||||
modifyTable(v -> (byte) 0);
|
||||
}
|
||||
}.setToolTip(Lang.get("menu_table_setAllTo0_tt")).createJMenuItem());
|
||||
setMenu.add(new ToolTipAction(Lang.get("menu_table_setAllTo1")) {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
setAllValuesTo(1);
|
||||
modifyTable(v -> (byte) 1);
|
||||
}
|
||||
}.setToolTip(Lang.get("menu_table_setAllTo1_tt")).createJMenuItem());
|
||||
setMenu.add(new ToolTipAction(Lang.get("menu_table_invert")) {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
modifyTable(v -> v > 1 ? v : (byte) (1 - v));
|
||||
}
|
||||
}.setToolTip(Lang.get("menu_table_invert_tt")).createJMenuItem());
|
||||
return setMenu;
|
||||
}
|
||||
|
||||
private void setAllValuesTo(int value) {
|
||||
TruthTable t = model.getTable();
|
||||
t.setAllTo(value);
|
||||
setModel(new TruthTableTableModel(t));
|
||||
private void modifyTable(BoolTableByteArray.TableModifier m) {
|
||||
setModel(new TruthTableTableModel(model.getTable().modifyValues(m)));
|
||||
}
|
||||
|
||||
private JMenu createCreateMenu() {
|
||||
|
@ -1565,6 +1565,9 @@ Sind evtl. die Namen der Variablen nicht eindeutig?</string>
|
||||
<string name="menu_table_setAllTo0_tt">Setzt alle Werte auf Null.</string>
|
||||
<string name="menu_table_setAllTo1">Alles auf 1 setzen</string>
|
||||
<string name="menu_table_setAllTo1_tt">Setzt alle Werte auf Eins.</string>
|
||||
<string name="menu_table_invert">Alle Bits invertieren</string>
|
||||
<string name="menu_table_invert_tt">Aus einer "1" wird eine "0" und umgekehrt. Don't Cares bleiben unverändert.
|
||||
</string>
|
||||
<string name="menu_table_showAllSolutions">Lösungsdialog anzeigen</string>
|
||||
<string name="menu_table_showAllSolutions_tt">Zeigt den Lösungsdialog wieder an, wenn er manuell geschlossen wurde.</string>
|
||||
<string name="menu_terminalDelete">Löschen</string>
|
||||
|
@ -1548,6 +1548,8 @@
|
||||
<string name="menu_table_setAllTo0_tt">Set all values to zero.</string>
|
||||
<string name="menu_table_setAllTo1">Set all to 1</string>
|
||||
<string name="menu_table_setAllTo1_tt">Set all values to one.</string>
|
||||
<string name="menu_table_invert">Invert all bits</string>
|
||||
<string name="menu_table_invert_tt">A "1" becomes a "0" and vice versa. Don't cares remain unchanged.</string>
|
||||
<string name="menu_table_showAllSolutions">Show results dialog</string>
|
||||
<string name="menu_table_showAllSolutions_tt">Shows the results dialog again if it was closed manually.</string>
|
||||
<string name="menu_terminalDelete">Delete</string>
|
||||
|
Loading…
x
Reference in New Issue
Block a user