Esempio n. 1
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
Esempio 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
Esempio n. 3
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)
Esempio n. 4
0
def __get_operands(replaced_sentence, operator):
    text, replacements = replaced_sentence

    if operator is None:
        return False

    if operator == LogicOperator.Negation:
        sentence = Braces.restore(text, replacements)
        if sentence[0] == operator:
            return (sentence[1:], )
        return False

    if operator == LogicOperator.Every or operator == LogicOperator.Exists:
        op_index = text.index(operator)
        left_brace = text[op_index:].index(Braces.Left)
        variables = text[op_index + 1:left_brace].strip()
        expression = replacements[0][1:-1]
        return (variables, expression)

    lhs, rhs = text.split(" {0} ".format(operator), maxsplit=1)
    separator = "!SEPARATOR!"
    separated = "{0} {1} {2}".format(lhs, separator, rhs)
    lhs, rhs = Braces.restore(separated, replacements).split(separator)
    return (lhs.strip(), rhs.strip())
Esempio n. 5
0
def __get_operands(replaced_sentence, operator):
    text, replacements = replaced_sentence

    if operator is None:
        return False

    if operator == LogicOperator.Negation:
        sentence = Braces.restore(text, replacements)
        if sentence[0] == operator:
            return (sentence[1:], )
        return False

    if operator == LogicOperator.Every or operator == LogicOperator.Exists:
        op_index = text.index(operator)
        left_brace = text[op_index:].index(Braces.Left)
        variables = text[op_index + 1:left_brace].strip()
        expression = replacements[0][1:-1]
        return (variables, expression)

    lhs, rhs = text.split(" {0} ".format(operator), maxsplit=1)
    separator = "!SEPARATOR!"
    separated = "{0} {1} {2}".format(lhs, separator, rhs)
    lhs, rhs = Braces.restore(separated, replacements).split(separator)
    return (lhs.strip(), rhs.strip())
Esempio n. 6
0
def __clean_cnf(cnf, is_first_order):
    cnf = Braces.flatten(cnf, is_first_order)

    clauses = set()
    for disjunct_text in cnf.split(LogicOperator.Conjuction):
        disjunct_text = disjunct_text.replace(Braces.Left, "")\
                                     .replace(Braces.Right, "")
        if len(disjunct_text) == 0:
            continue

        disjunct_text = disjunct_text.replace(Braces.FO_Left, Braces.Left)\
                                     .replace(Braces.FO_Right, Braces.Right)
        disjunct = frozenset(symbol.strip() for symbol in
                             disjunct_text.split(LogicOperator.Disjunction))

        if not is_disjunction_tautology(disjunct):
            clauses.add(disjunct)

    return __clean_clauses(clauses)
Esempio n. 7
0
def __clean_cnf(cnf, is_first_order):
    cnf = Braces.flatten(cnf, is_first_order)

    clauses = set()
    for disjunct_text in cnf.split(LogicOperator.Conjuction):
        disjunct_text = disjunct_text.replace(Braces.Left, "")\
                                     .replace(Braces.Right, "")
        if len(disjunct_text) == 0:
            continue

        disjunct_text = disjunct_text.replace(Braces.FO_Left, Braces.Left)\
                                     .replace(Braces.FO_Right, Braces.Right)
        disjunct = frozenset(
            symbol.strip()
            for symbol in disjunct_text.split(LogicOperator.Disjunction))

        if not is_disjunction_tautology(disjunct):
            clauses.add(disjunct)

    return __clean_clauses(clauses)
Esempio n. 8
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)