/** * Construct a truth table from the given collection of Boolean expressions. * * @param expressionCollection the collection of Boolean expressions for which to construct this * truth table * @throws ParseException if any of the Boolean expressions is not properly formatted */ public TruthTable(final Collection<BooleanExpression> expressionCollection) throws ParseException { this.expressionCollection = expressionCollection; results = new HashMap<Map<String, Boolean>, Map<BooleanExpression, Boolean>>(); variablesList = new ArrayList<String>(); for (final BooleanExpression expression : expressionCollection) { for (final String str : expression.getVariableSet()) { if (!variablesList.contains(str)) { variablesList.add(str); } } } Collections.sort(variablesList); resultsKeyList = buildDictionary(variablesList); for (final Map<String, Boolean> dict : resultsKeyList) { final Map<BooleanExpression, Boolean> map = new HashMap<BooleanExpression, Boolean>(); for (final BooleanExpression expression : expressionCollection) { map.put(expression, expression.evaluate(dict)); } results.put(dict, map); } }
@Override public Token parse() { Token next = first.getNext(); if (!next.isOperator(Operator.BRACKET_LEFT)) throw new RuntimeException("Expected '(' after WHILE, found: " + next); next = next.getNext(); testExp = new BooleanExpression(next, scope); next = testExp.parse(); if (!next.isOperator(Operator.BRACKET_RIGHT)) throw new RuntimeException("Expected ')' after WHILE condition, found: " + next); next = next.getNext(); if (!next.isNewline()) throw new RuntimeException("Expected newline, found: " + next); next = next.getNext(); Keyword[] target = {Keyword.END}; statements = new StatementGroup(next, target, scope); next = statements.parse(); return next.getNext(); }
/** * Return a string representation of this truth table. * * @return a string representation of this truth table */ @Override public String toString() { String str = ""; // Build the header by listing all variables... for (final String var : variablesList) { str += var + " "; } // and then the expressions themselves for (final BooleanExpression expression : expressionCollection) { str += " " + expression + " "; } str += "\n"; // Build each row by listing the values of the variables and the value // of the expression for (final Map<String, Boolean> dict : resultsKeyList) { for (final String var : variablesList) { str += (dict.get(var) ? "1" : "0") + " "; } for (final BooleanExpression expression : expressionCollection) { for (int i = 0; i < (expression.toString().length() - 1) / 2; i++) { str += " "; } str += " " + (results.get(dict).get(expression) ? "1" : "0"); for (int i = 0; i < expression.toString().length() / 2; i++) { str += " "; } str += " "; } str += "\n"; } str = str.substring(0, str.length() - 1); return str; }
@Override public void print(int level) { printLevel(level); printLn("WHILE"); testExp.print(level + 1); printLevel(level); printLn("DO"); statements.print(level + 1); }
public boolean matches(Filterable message) { boolean match = _matcher.matches(message); if (_logger.isDebugEnabled()) { _logger.debug( message + " match(" + match + ") selector(" + System.identityHashCode(_selector) + "):" + _selector); } return match; }
/** * Avalia a condição <code>boolean</code> para processar ou não a ação. * * @throws <code>IllegalArgumentException</code> caso não tenha ação e/ou condição <code>boolean * </code> vinculada. */ @Override protected void action() { if (action == null) { throw new IllegalArgumentException( "Indique a Ação que deve ser executada, utilize o método addAction."); } if (expression == null) { throw new IllegalArgumentException( "Indique a expressão condicional da Ação, utilize o método addConditional."); } if (expression.conditional()) { action.actionPerformed(); } }
/*@Nullable*/ protected Expression promoteWhereClause(/*@Nullable*/ Binding positionBinding) { if (Choose.isSingleBranchChoice(action)) { Expression condition = ((Choose) action).getConditions()[0]; Binding[] bindingList; if (positionBinding == null) { bindingList = new Binding[] {this}; } else { bindingList = new Binding[] {this, positionBinding}; } List list = new ArrayList(5); Expression promotedCondition = null; BooleanExpression.listAndComponents(condition, list); for (int i = list.size() - 1; i >= 0; i--) { Expression term = (Expression) list.get(i); if (!ExpressionTool.dependsOnVariable(term, bindingList)) { if (promotedCondition == null) { promotedCondition = term; } else { promotedCondition = new AndExpression(term, promotedCondition); } list.remove(i); } } if (promotedCondition != null) { if (list.isEmpty()) { // the whole if() condition has been promoted Expression oldThen = ((Choose) action).getActions()[0]; setAction(oldThen); return Choose.makeConditional(condition, this); } else { // one or more terms of the if() condition have been promoted Expression retainedCondition = (Expression) list.get(0); for (int i = 1; i < list.size(); i++) { retainedCondition = new AndExpression(retainedCondition, (Expression) list.get(i)); } ((Choose) action).getConditions()[0] = retainedCondition; Expression newIf = Choose.makeConditional(promotedCondition, this, Literal.makeEmptySequence()); ExpressionTool.copyLocationInfo(this, newIf); return newIf; } } } return null; }
@Override public void emitCode(GluonOutput code) { // Creae labels String labelStart = GluonLabels.createLabel(first, "start"); String labelEnd = GluonLabels.createLabel(first, "end"); GluonLabels.addEndLabel(labelEnd); code.comment("While Statement"); code.label(labelStart); testExp.emitCode(code); code.code("TEST AL, AL ;EAX, EAX"); code.code("JZ " + labelEnd); statements.emitCode(code); code.code("JMP " + labelStart); code.label(labelEnd); code.comment("End While"); GluonLabels.removeEndLabel(labelEnd); }
public void visitBooleanExpression(BooleanExpression expression) { expression.getExpression().visit(this); }