mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-22 20:01:09 -04:00
truth table file format is more readable, see #644
This commit is contained in:
parent
6bae3b034e
commit
bb3ea8aebe
@ -108,6 +108,7 @@ public class TruthTable implements Copyable<TruthTable> {
|
||||
xStream.alias("result", Result.class);
|
||||
xStream.alias("BoolTable", BoolTableByteArray.class);
|
||||
xStream.alias("BoolTableEx", BoolTableExpanded.class);
|
||||
xStream.registerConverter(new TruthTableConverter());
|
||||
return xStream;
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Helmut Neemann.
|
||||
* Use of this source code is governed by the GPL v3 license
|
||||
* that can be found in the LICENSE file.
|
||||
*/
|
||||
package de.neemann.digital.analyse;
|
||||
|
||||
import com.thoughtworks.xstream.converters.Converter;
|
||||
import com.thoughtworks.xstream.converters.MarshallingContext;
|
||||
import com.thoughtworks.xstream.converters.UnmarshallingContext;
|
||||
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
|
||||
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
|
||||
import de.neemann.digital.analyse.quinemc.BoolTableByteArray;
|
||||
|
||||
/**
|
||||
* Converter for truth tables.
|
||||
* The created output is much more readable.
|
||||
*/
|
||||
public class TruthTableConverter implements Converter {
|
||||
|
||||
@Override
|
||||
public boolean canConvert(Class aClass) {
|
||||
return aClass.equals(BoolTableByteArray.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void marshal(Object o, HierarchicalStreamWriter writer, MarshallingContext marshallingContext) {
|
||||
BoolTableByteArray bt = (BoolTableByteArray) o;
|
||||
writer.setValue(bt.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext unmarshallingContext) {
|
||||
if (reader.hasMoreChildren()) {
|
||||
// is old format, read base64 encoded byte array
|
||||
reader.moveDown();
|
||||
Object o = unmarshallingContext.convertAnother(new byte[]{}, byte[].class);
|
||||
reader.moveUp();
|
||||
return new BoolTableByteArray((byte[]) o);
|
||||
} else {
|
||||
String values = reader.getValue();
|
||||
return new BoolTableByteArray(values);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -5,7 +5,6 @@
|
||||
*/
|
||||
package de.neemann.digital.analyse.quinemc;
|
||||
|
||||
|
||||
/**
|
||||
* A byte array.
|
||||
* Zero and one behave as expected, any other value represents "don't care"
|
||||
@ -32,6 +31,32 @@ public class BoolTableByteArray implements BoolTable {
|
||||
this.table = table;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance
|
||||
*
|
||||
* @param values the int values
|
||||
*/
|
||||
public BoolTableByteArray(String values) {
|
||||
this(parseString(values));
|
||||
}
|
||||
|
||||
private static byte[] parseString(String values) {
|
||||
byte[] table = new byte[values.length()];
|
||||
for (int i = 0; i < values.length(); i++) {
|
||||
switch (values.charAt(i)) {
|
||||
case '0':
|
||||
table[i] = 0;
|
||||
break;
|
||||
case '1':
|
||||
table[i] = 1;
|
||||
break;
|
||||
default:
|
||||
table[i] = 2;
|
||||
}
|
||||
}
|
||||
return table;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance
|
||||
*
|
||||
@ -89,6 +114,24 @@ public class BoolTableByteArray implements BoolTable {
|
||||
return t;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder(table.length);
|
||||
for (byte b : table) {
|
||||
switch (b) {
|
||||
case 0:
|
||||
sb.append('0');
|
||||
break;
|
||||
case 1:
|
||||
sb.append('1');
|
||||
break;
|
||||
default:
|
||||
sb.append('X');
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Modifies all the table elements using the given modifier.
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user