use of de.neemann.digital.analyse.expression.Variable in project Digital by hneemann.
the class TruthTableFormatterLaTeX method format.
@Override
public String format(TruthTable truthTable) throws ExpressionException {
StringBuilder sb = new StringBuilder();
sb.append("\\begin{center}\n\\begin{tabular}{");
for (Variable v : truthTable.getVars()) sb.append("c");
sb.append("|");
for (int i = 0; i < truthTable.getResultCount(); i++) sb.append("c");
sb.append("}\n");
for (Variable v : truthTable.getVars()) sb.append("$").append(formatVar(v.getIdentifier())).append("$&");
for (int i = 0; i < truthTable.getResultCount(); i++) {
sb.append("$").append(formatVar(truthTable.getResultName(i))).append("$");
if (i < truthTable.getResultCount() - 1)
sb.append("&");
}
sb.append("\\\\\n");
sb.append("\\hline\n");
ContextFiller cf = new ContextFiller(truthTable.getVars());
for (int i = 0; i < cf.getRowCount(); i++) {
cf.setContextTo(i);
for (Variable v : cf) sb.append(format(cf.get(v))).append("&");
for (int j = 0; j < truthTable.getResultCount(); j++) {
ThreeStateValue r = truthTable.getResult(j).get(i);
sb.append(format(r));
if (j < truthTable.getResultCount() - 1)
sb.append("&");
}
sb.append("\\\\\n");
}
sb.append("\\end{tabular}\n\\end{center}\n");
return sb.toString();
}
use of de.neemann.digital.analyse.expression.Variable in project Digital by hneemann.
the class ExpressionCreator method create.
/**
* Creates the expressions
*
* @param listener the listener to report the found expressions to
* @throws ExpressionException ExpressionException
* @throws FormatterException FormatterException
* @throws AnalyseException AnalyseException
*/
public void create(ExpressionListener listener) throws ExpressionException, FormatterException, AnalyseException {
final List<Variable> vars = Collections.unmodifiableList(theTable.getVars());
long time = System.currentTimeMillis();
if (theTable.getResultCount() > 100) {
ExecutorService ex = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
ThreadSaveExpressionListener threadListener = new ThreadSaveExpressionListener(listener);
for (int table = 0; table < theTable.getResultCount(); table++) {
final int t = table;
ex.submit(() -> {
try {
simplify(listener, vars, theTable.getResultName(t), theTable.getResult(t));
} catch (ExpressionException | FormatterException | AnalyseException e) {
e.printStackTrace();
}
});
}
ex.shutdown();
try {
ex.awaitTermination(100, TimeUnit.HOURS);
} catch (InterruptedException e) {
e.printStackTrace();
}
threadListener.close();
} else {
for (int table = 0; table < theTable.getResultCount(); table++) simplify(listener, vars, theTable.getResultName(table), theTable.getResult(table));
listener.close();
}
time = System.currentTimeMillis() - time;
LOGGER.debug("time: " + time / 1000.0 + " sec");
}
use of de.neemann.digital.analyse.expression.Variable in project Digital by hneemann.
the class ExpressionCreator method simplify.
private void simplify(ExpressionListener listener, List<Variable> vars, String resultName, BoolTable boolTable) throws AnalyseException, ExpressionException, FormatterException {
List<Variable> localVars = vars;
if (vars.size() > 4) {
TableReducer tr = new TableReducer(vars, boolTable);
if (tr.canReduce()) {
LOGGER.debug(resultName + " reduced from " + vars.size() + " to " + tr.getVars().size() + " variables (" + tr.getVars() + ")");
boolTable = tr.getTable();
localVars = tr.getVars();
}
}
if (!Main.isExperimentalMode() && localVars.size() > MAX_INPUTS_ALLOWED)
throw new AnalyseException(Lang.get("err_toManyInputsIn_N0_max_N1_is_N2", resultName, MAX_INPUTS_ALLOWED, localVars.size()));
listener = new CheckResultListener(listener, localVars, boolTable);
getMinimizer(localVars.size()).minimize(localVars, boolTable, resultName, listener);
}
use of de.neemann.digital.analyse.expression.Variable in project Digital by hneemann.
the class KarnaughMapComponent method paintComponent.
@Override
protected void paintComponent(Graphics graphics) {
gr = (Graphics2D) graphics;
gr.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
gr.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
gr.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
gr.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
int width = getWidth();
int height = getHeight();
gr.setColor(Color.WHITE);
gr.fillRect(0, 0, width, height);
gr.setColor(Color.BLACK);
if (kv != null) {
// store the old transform
AffineTransform trans = gr.getTransform();
int kvWidth = kv.getColumns();
int kvHeight = kv.getRows();
cellSize = (int) Math.min(height / (kvHeight + 2.5f), width / (kvWidth + 2.5f));
Font origFont = gr.getFont();
Font valuesFont = origFont.deriveFont(cellSize * 0.5f);
gr.setFont(valuesFont);
Font headerFont = valuesFont;
try {
int maxHeaderStrWidth = 0;
FontMetrics fontMetrics = gr.getFontMetrics();
for (Variable v : vars) {
int w = fontMetrics.stringWidth(FormatToExpression.defaultFormat(not(v)));
if (w > maxHeaderStrWidth)
maxHeaderStrWidth = w;
}
if (maxHeaderStrWidth > cellSize)
headerFont = origFont.deriveFont(cellSize * 0.5f * cellSize / maxHeaderStrWidth);
} catch (FormatterException e) {
// can not happen
}
xOffs = (width - (kvWidth + 2) * cellSize) / 2;
yOffs = (height - (kvHeight + 2) * cellSize) / 2;
// center the kv map
gr.translate(xOffs, yOffs);
// draw table
gr.setColor(Color.GRAY);
gr.setStroke(new BasicStroke(STROKE_WIDTH / 2));
for (int i = 0; i <= kvWidth; i++) {
int dy1 = isNoHeaderLine(kv.getHeaderTop(), i - 1) ? cellSize : 0;
int dy2 = isNoHeaderLine(kv.getHeaderBottom(), i - 1) ? cellSize : 0;
gr.drawLine((i + 1) * cellSize, dy1, (i + 1) * cellSize, (kvHeight + 2) * cellSize - dy2);
}
for (int i = 0; i <= kvHeight; i++) {
int dx1 = isNoHeaderLine(kv.getHeaderLeft(), i - 1) ? cellSize : 0;
int dx2 = isNoHeaderLine(kv.getHeaderRight(), i - 1) ? cellSize : 0;
gr.drawLine(dx1, (i + 1) * cellSize, (kvWidth + 2) * cellSize - dx2, (i + 1) * cellSize);
}
gr.setStroke(new BasicStroke(STROKE_WIDTH));
// fill in bool table content
for (KarnaughMap.Cell cell : kv.getCells()) {
gr.setColor(Color.BLACK);
gr.setFont(valuesFont);
drawString(boolTable.get(cell.getBoolTableRow()).toString(), cell.getCol() + 1, cell.getRow() + 1);
gr.setColor(Color.GRAY);
gr.setFont(origFont);
gr.drawString(Integer.toString(cell.getBoolTableRow()), (cell.getCol() + 1) * cellSize + 1, (cell.getRow() + 2) * cellSize - 1);
}
// draw the text in the borders
gr.setColor(Color.BLACK);
gr.setFont(headerFont);
drawVerticalHeader(kv.getHeaderLeft(), 0);
drawVerticalHeader(kv.getHeaderRight(), kvWidth + 1);
drawHorizontalHeader(kv.getHeaderTop(), 0);
drawHorizontalHeader(kv.getHeaderBottom(), kvHeight + 1);
// draw the covers
int color = 0;
for (KarnaughMap.Cover c : kv) {
gr.setColor(COVER_COLORS[color++]);
KarnaughMap.Pos p = c.getPos();
int frame = (c.getInset() + 1) * STROKE_WIDTH;
int edgesRadius = cellSize - frame * 2;
if (c.isDisconnected()) {
Rectangle clip = gr.getClipBounds();
gr.setClip(cellSize, cellSize, kvWidth * cellSize, kvHeight * cellSize);
if (c.onlyEdges()) {
gr.drawRoundRect(frame, frame, 2 * cellSize - frame * 2, 2 * cellSize - frame * 2, edgesRadius, edgesRadius);
gr.drawRoundRect(4 * cellSize + frame, frame, 2 * cellSize - frame * 2, 2 * cellSize - frame * 2, edgesRadius, edgesRadius);
gr.drawRoundRect(frame, 4 * cellSize + frame, 2 * cellSize - frame * 2, 2 * cellSize - frame * 2, edgesRadius, edgesRadius);
gr.drawRoundRect(4 * cellSize + frame, 4 * cellSize + frame, 2 * cellSize - frame * 2, 2 * cellSize - frame * 2, edgesRadius, edgesRadius);
} else {
// draw the two parts of the cover
int xofs = 0;
int yOfs = 0;
if (c.isVerticalDivided())
xofs = cellSize * 3;
else
yOfs = cellSize * 3;
gr.drawRoundRect((p.getCol() + 1) * cellSize + frame + xofs, (p.getRow() + 1) * cellSize + frame + yOfs, p.getWidth() * cellSize - frame * 2, p.getHeight() * cellSize - frame * 2, edgesRadius, edgesRadius);
gr.drawRoundRect((p.getCol() + 1) * cellSize + frame - xofs, (p.getRow() + 1) * cellSize + frame - yOfs, p.getWidth() * cellSize - frame * 2, p.getHeight() * cellSize - frame * 2, edgesRadius, edgesRadius);
}
gr.setClip(clip.x, clip.y, clip.width, clip.height);
} else
gr.drawRoundRect((p.getCol() + 1) * cellSize + frame, (p.getRow() + 1) * cellSize + frame, p.getWidth() * cellSize - frame * 2, p.getHeight() * cellSize - frame * 2, edgesRadius, edgesRadius);
}
gr.setTransform(trans);
} else
gr.drawString(message, 10, 20);
}
use of de.neemann.digital.analyse.expression.Variable in project Digital by hneemann.
the class TruthTable method addVariable.
/**
* Adds a new variable
*/
public void addVariable() {
char v = 'A';
Variable var;
do {
var = new Variable("" + v);
v++;
} while (variables.contains(var) && v <= 'Z');
addVariable(var);
}
Aggregations