Exemplo n.º 1
0
def __negation_cnf(operands):
    tail = Braces.remove_surrounding(operands[0])
    if __is_symbol(tail):
        return LogicOperator.Negation + tail

    tail_operation = __breakdown_sentence(tail)
    operator = tail_operation[0]
    tail_operands = tail_operation[1:]
    rewritten_formula = ""
    if operator == LogicOperator.Negation:
        rewritten_formula = tail_operands[0]
    elif operator == LogicOperator.Conjuction:
        lhs, rhs = tail_operands
        pattern = "{2}{0} {3} {2}({1})"
        rewritten_formula = pattern.format(lhs, rhs, LogicOperator.Negation,
                                           LogicOperator.Disjunction)
    elif operator == LogicOperator.Disjunction:
        lhs, rhs = tail_operands
        pattern = "{2}({0}) {3} {2}({1})"
        rewritten_formula = pattern.format(lhs, rhs, LogicOperator.Negation,
                                           LogicOperator.Conjuction)
    elif operator == LogicOperator.Every or operator == LogicOperator.Exists:
        negated_operator = LogicOperator.Exists if operator == LogicOperator.Every \
                                                else LogicOperator.Every
        variables, expression = tail_operands
        pattern = "{0} {1}({2}({3}))"
        rewritten_formula = pattern.format(negated_operator, variables,
                                           LogicOperator.Negation, expression)
    else:
        rewritten_formula = "{0}({1})".format(LogicOperator.Negation,
                                              __compute_cnf(tail))

    return __compute_cnf(rewritten_formula)
Exemplo n.º 2
0
def __breakdown_sentence(sentence):
    sentence = Braces.remove_surrounding(sentence.strip())
    replaced = Braces.replace(sentence)
    operator = __get_operator(replaced[0])
    operands = __get_operands(replaced, operator)

    if not operands:
        msg = "Could not parse sentence '{0}'".format(sentence)
        raise utils.ParsingError(msg)

    return (operator, ) + operands
Exemplo n.º 3
0
def __breakdown_sentence(sentence):
    sentence = Braces.remove_surrounding(sentence.strip())
    replaced = Braces.replace(sentence)
    operator = __get_operator(replaced[0])
    operands = __get_operands(replaced, operator)

    if not operands:
        msg = "Could not parse sentence '{0}'".format(sentence)
        raise utils.ParsingError(msg)

    return (operator, ) + operands
Exemplo n.º 4
0
def __negation_cnf(operands):
    tail = Braces.remove_surrounding(operands[0])
    if __is_symbol(tail):
        return LogicOperator.Negation + tail

    tail_operation = __breakdown_sentence(tail)
    operator = tail_operation[0]
    tail_operands = tail_operation[1:]
    rewritten_formula = ""
    if operator == LogicOperator.Negation:
        rewritten_formula = tail_operands[0]
    elif operator == LogicOperator.Conjuction:
        lhs, rhs = tail_operands
        pattern = "{2}{0} {3} {2}({1})"
        rewritten_formula = pattern.format(lhs, rhs,
                                           LogicOperator.Negation,
                                           LogicOperator.Disjunction)
    elif operator == LogicOperator.Disjunction:
        lhs, rhs = tail_operands
        pattern = "{2}({0}) {3} {2}({1})"
        rewritten_formula = pattern.format(lhs, rhs,
                                           LogicOperator.Negation,
                                           LogicOperator.Conjuction)
    elif operator == LogicOperator.Every or operator == LogicOperator.Exists:
        negated_operator = LogicOperator.Exists if operator == LogicOperator.Every \
                                                else LogicOperator.Every
        variables, expression = tail_operands
        pattern = "{0} {1}({2}({3}))"
        rewritten_formula = pattern.format(negated_operator,
                                           variables,
                                           LogicOperator.Negation,
                                           expression)
    else:
        rewritten_formula = "{0}({1})".format(LogicOperator.Negation,
                                              __compute_cnf(tail))

    return __compute_cnf(rewritten_formula)