Automaton build(int lo, int hi) { int firstClose = -1; int level = 0; for (int i = lo; i <= hi; ++i) { if (isOpen(i)) ++level; if (isClose(i)) --level; if (isClose(i) && firstClose == -1 && level == 0) firstClose = i; if (isOr(i) && level == 0) return autoOr(build(lo, i - 1), build(i + 1, hi)); } int mid = lo+1; if (isOpen(lo)) mid = firstClose + 1; bool star = false; if (mid <= hi && isStar(mid)) { ++mid; star = true; } Automaton A = isOpen(lo) ? build(lo + 1, firstClose - 1) : autoChar(regex[lo]); if (star) A = autoStar(A); if (mid > hi) return A; return autoSeq(A, build(mid, hi)); }
bool TableFieldBinaryTreeEvalNode::testEvaluator(const QHash<QString, bool>& vals) const { //qDebug(qPrintable(QString("Type(%1) Value(%2)").arg(nodeType()).arg(nodeValue()))); if (m_node == nullptr) { qDebug("NULL Node in the evaluator"); } else if (isValue()) { //qDebug(qPrintable(QString("Returning %1").arg(vals.value(nodeValue())))); return vals.value(nodeValue()); } else if (m_children.size() > 0) { bool rc = m_children.at(0)->testEvaluator(vals); if (isAnd()) { for (int i=1; rc && i<m_children.size(); ++i) { rc = m_children.at(i)->testEvaluator(vals); } } else if (isOr()) { for (int i=1; !rc && i<m_children.size(); ++i) { rc = m_children.at(i)->testEvaluator(vals); } } else if (isNot()) { return !rc; } else { return false; qDebug("Wrong number of arguments in the evaluator."); } return rc; } else { qDebug("No children for the evaluator to evaluate"); } return false; }
const Predicate& Predicate::orRight() const { assert(isOr()); return *right_; }
const Predicate& Predicate::orLeft() const { assert(isOr()); return *left_; }
// // Sort a term if it commutes. // The following simplifications implemented // (should be refactored to separate methods?): // - `and': // - drop constant true terms // - convert an empty `and' to the constant term `true' // - convert an `and' containing `false' to a replicate `false' // - `or': // - drop constant false terms // - convert an empty `or' to a replicate `false' term // - convert an `or' containing `true' to a replicate `true' // // void Logic::simplify(SymRef& s, vec<PTRef>& args) { // First sort it #ifdef SORT_BOOLEANS if (sym_store[s].commutes()) #else if (sym_store[s].commutes() && !isAnd(s) && !isOr(s)) #endif sort(args, LessThan_PTRef()); if (!isBooleanOperator(s) && !isEquality(s)) return; int dropped_args = 0; bool replace = false; if (s == getSym_and()) { int i, j; PTRef p = PTRef_Undef; for (i = j = 0; i < args.size(); i++) { if (args[i] == getTerm_false()) { args.clear(); s = getSym_false(); #ifdef SIMPLIFY_DEBUG cerr << "and -> false" << endl; #endif return; } else if (args[i] != getTerm_true() && args[i] != p) { args[j++] = p = args[i]; } else { #ifdef SIMPLIFY_DEBUG cerr << "and -> drop" << endl; #endif } } dropped_args = i-j; if (dropped_args == args.size()) { s = getSym_true(); args.clear(); #ifdef SIMPLIFY_DEBUG cerr << "and -> true" << endl; #endif return; } else if (dropped_args == args.size() - 1) replace = true; else if (dropped_args > 0) args.shrink(dropped_args); } if (s == getSym_or()) { int i, j; PTRef p = PTRef_Undef; for (i = j = 0; i < args.size(); i++) { if (args[i] == getTerm_true()) { args.clear(); s = getSym_true(); #ifdef SIMPLIFY_DEBUG cerr << "or -> true" << endl; #endif return; } else if (args[i] != getTerm_false() && args[i] != p) { args[j++] = p = args[i]; } else { #ifdef SIMPLIFY_DEBUG cerr << "or -> drop" << endl; #endif } } dropped_args = i-j; if (dropped_args == args.size()) { s = getSym_false(); args.clear(); #ifdef SIMPLIFY_DEBUG cerr << "or -> false" << endl; #endif return; } else if (dropped_args == args.size() - 1) replace = true; else if (dropped_args > 0) args.shrink(dropped_args); } if (isEquality(s)) { assert(args.size() == 2); if (isBooleanOperator(s) && (args[0] == getTerm_true())) { Pterm& t = getPterm(args[1]); s = t.symb(); args.clear(); for (int i = 0; i < t.size(); i++) args.push(t[i]); #ifdef SIMPLIFY_DEBUG cerr << "eq -> second" << endl; #endif return; } else if (isBooleanOperator(s) && (args[0] == getTerm_false())) { PTRef old = args[1]; PTRef tr = mkNot(args[1]); Pterm& t = getPterm(tr); s = t.symb(); args.clear(); args.push(old); #ifdef SIMPLIFY_DEBUG cerr << "eq -> not second" << endl; #endif return; } else if (isBooleanOperator(s) && (args[1] == getTerm_true())) { args.clear(); Pterm& t = getPterm(args[0]); s = t.symb(); args.clear(); for (int i = 0; i < t.size(); i++) args.push(t[i]); #ifdef SIMPLIFY_DEBUG cerr << "eq -> first" << endl; #endif return; } else if (isBooleanOperator(s) && (args[1] == getTerm_false())) { PTRef old = args[0]; PTRef tr = mkNot(args[0]); Pterm& t = getPterm(tr); s = t.symb(); args.clear(); args.push(old); #ifdef SIMPLIFY_DEBUG cerr << "eq -> not first"<< endl; #endif return; } else if (args[0] == args[1]) { args.clear(); s = getSym_true(); #ifdef SIMPLIFY_DEBUG cerr << "eq -> true" << endl; #endif return; } else if (isBooleanOperator(s) && (args[0] == mkNot(args[1]))) { args.clear(); s = getSym_false(); #ifdef SIMPLIFY_DEBUG cerr << "eq -> false" << endl; #endif return; } } if (isNot(s)) { if (isTrue(args[0])) { args.clear(); s = getSym_false(); #ifdef SIMPLIFY_DEBUG cerr << "not -> false" << endl; #endif return; } if (isFalse(args[0])) { args.clear(); s = getSym_true(); #ifdef SIMPLIFY_DEBUG cerr << "not -> true" << endl; #endif return; } } // Others, to be implemented: // - distinct // - implies // - xor // - ite if (replace) { // Return whatever is the sole argument Pterm& t = getPterm(args[0]); s = t.symb(); args.clear(); for (int i = 0; i < t.size(); i++) args.push(t[i]); #ifdef SIMPLIFY_DEBUG cerr << " replace" << endl; #endif } }