Example #1
0
 def __init__(self, atom, q):
     _, x, _ = q.decompose_atom(atom)
     head_atom = Atom("Garbage_" + atom.name, x)
     relevant_atom = Atom("Rlvant_" + atom.name, atom.content)
     DatalogQuery.__init__(self, head_atom)
     self.add_atom(atom)
     self.add_atom(relevant_atom, True)
Example #2
0
 def __init__(self, cycle, q, rewriting_index, renamings):
     k = len(cycle)
     head_content = []
     _, x_0, _ = q.decompose_atom(cycle[0])
     x_0_0 = apply_renaming_to_atom_values(x_0, renamings[0])
     x_0_2k = apply_renaming_to_atom_values(x_0, renamings[2 * k])
     head_content += x_0_0
     head_content += x_0_2k
     common = []
     for i in range(1, k):
         _, x, _ = q.decompose_atom(cycle[i])
         common += apply_renaming_to_atom_values(x, renamings[i])
     head_atom = Atom("DCon_" + str(rewriting_index), head_content + common)
     DatalogQuery.__init__(self, head_atom)
     x_0_2k_1 = apply_renaming_to_atom_values(x_0, renamings[2 * k + 1])
     self.add_atom(
         Atom("DCon_" + str(rewriting_index), x_0_0 + x_0_2k_1 + common))
     pk_content = x_0_2k_1
     for i in range(1, k):
         _, x, _ = q.decompose_atom(cycle[i])
         pk_content += apply_renaming_to_atom_values(x, renamings[k + i])
     pk_content += x_0_2k
     self.add_atom(Atom("Pk_" + str(rewriting_index), pk_content))
     for i in range(1, k):
         _, x, _ = q.decompose_atom(cycle[i])
         key1 = apply_renaming_to_atom_values(x, renamings[k + i])
         key2 = apply_renaming_to_atom_values(x, renamings[i])
         self.add_atom(Atom("Neq_" + cycle[i].name, key1 + key2))
def read_datalog_file(file: str) -> DatalogProgram:
    f = open(file, "r")
    names_regex = "[A-Za-z][A-Za-z0-9_]*"
    atom_regex = names_regex + "(\(" + "(" + names_regex + ",)*" + names_regex + "\))?"
    other_regex = names_regex + "(=|!=|>|<)" + names_regex
    query_element_regex = "(" + other_regex + "|" + "(not )?" + atom_regex + ")"
    query_regex = "^" + atom_regex + " :- " + "(" + query_element_regex + ", )*" + query_element_regex + "\.$"
    queries = []
    line_index = 0
    try:
        for line in f:
            if regex.match(query_regex, line):
                elements = regex.findall(query_element_regex, line)
                head = parse_datalog_atom(elements[0][0])
                q = DatalogQuery(head)
                for element in elements[1:]:
                    atom_element = element[0]
                    if "not " in atom_element:
                        q.add_atom(parse_datalog_atom(atom_element[4:]), True)
                    elif "=" in atom_element:
                        q.add_atom(parse_datalog_equality_atom(atom_element))
                    elif ">" in atom_element or "<" in atom_element:
                        q.add_atom(parse_datalog_compare_atom(atom_element))
                    else:
                        q.add_atom(parse_datalog_atom(atom_element))
                queries.append(q)
                line_index += 1
            else:
                f.close()
                raise MalformedQuery(line, "DatalogQuery")
    except MalformedQuery:
        f.close()
        raise
    f.close()
    return DatalogProgram(queries)
Example #4
0
 def __init__(self, cycle, q, rewriting_index, renamings):
     _, x_0, _ = q.decompose_atom(cycle[0])
     x_0_0 = apply_renaming_to_atom_values(x_0, renamings[0])
     x_0_1 = apply_renaming_to_atom_values(x_0, renamings[1])
     DatalogQuery.__init__(
         self, Atom("Trans_" + str(rewriting_index), x_0 + x_0_0))
     self.add_atom(Atom("Trans_" + str(rewriting_index), x_0 + x_0_1))
     self.add_atom(Atom("Link_" + str(rewriting_index), x_0_1 + x_0_0))
Example #5
0
 def __init__(self, atom1, atom2, q):
     _, x1, _ = q.decompose_atom(atom1)
     _, x2, _ = q.decompose_atom(atom2)
     head_atom = Atom("Garbage_" + atom1.name, x1)
     DatalogQuery.__init__(self, head_atom)
     for atom in q.get_atoms():
         self.add_atom(atom)
     self.add_atom(Atom("Garbage_" + atom2.name, x2))
Example #6
0
 def __init__(self, cycle, q, rewriting_index, renaming):
     _, x_0, _ = q.decompose_atom(cycle[0])
     x_0_ren = apply_renaming_to_atom_values(x_0, renaming)
     DatalogQuery.__init__(
         self, Atom("IdentifiedBy_" + str(rewriting_index), x_0 + x_0_ren))
     self.add_atom(Atom("Lower_" + str(rewriting_index), x_0 + x_0_ren),
                   True)
     self.add_atom(Atom("Trans_" + str(rewriting_index), x_0 + x_0_ren))
Example #7
0
def execute_query(dlv_executable: str,q, db):    
    query = DatalogQuery(Atom("CERTAINTY", []))
    for atom in q.get_atoms():
        query.add_atom(atom)
    program = DatalogProgram([query])
    return execute_program(dlv_executable, program, db)

#execute_program()
Example #8
0
 def __init__(self, atom, cycle, q, rewriting_index):
     _, x, y = q.decompose_atom(atom)
     head_atom = Atom("Garbage_" + atom.name, x)
     DatalogQuery.__init__(self, head_atom)
     cycle_content = []
     for other in cycle:
         _, x, y = q.decompose_atom(other)
         cycle_content += x
     self.add_atom(
         Atom("InLongDCycle_" + str(rewriting_index), cycle_content))
Example #9
0
 def __init__(self, cycle, q, rewriting_index, renamings):
     _, x_0, _ = q.decompose_atom(cycle[0])
     x_0_0 = apply_renaming_to_atom_values(x_0, renamings[0])[0]
     x_0_1 = apply_renaming_to_atom_values(x_0, renamings[1])[0]
     x_0 = x_0[0]
     DatalogQuery.__init__(
         self, Atom("Lower_" + str(rewriting_index), [x_0, x_0_0]))
     self.add_atom(Atom("Trans_" + str(rewriting_index), [x_0, x_0_0]))
     self.add_atom(Atom("Trans_" + str(rewriting_index), [x_0, x_0_1]))
     self.add_atom(CompareAtom(x_0_0, x_0_1, True))
Example #10
0
 def __init__(self, cycle, q, rewriting_index):
     head_content = []
     for atom in cycle:
         _, x, y = q.decompose_atom(atom)
         head_content += x
         head_content += y
     head_atom = Atom("Rel1Emb_" + str(rewriting_index), head_content)
     DatalogQuery.__init__(self, head_atom)
     for atom in cycle:
         self.add_atom(atom)
Example #11
0
 def __init__(self, atom, q, renaming):
     v, x, _ = q.decompose_atom(atom)
     renamed_x = apply_renaming_to_atom_values(x, renaming)
     head_atom = Atom("Neq_" + atom.name, x + renamed_x)
     renamed_atom = apply_renaming_to_atom(atom, renaming)
     eq_atom = Atom("Eq_" + atom.name, x + renamed_x)
     DatalogQuery.__init__(self, head_atom)
     self.add_atom(atom)
     self.add_atom(renamed_atom)
     self.add_atom(eq_atom, True)
Example #12
0
 def __init__(self, cycle, q, index, rewriting_index, renamings):
     _, x_0, _ = q.decompose_atom(cycle[0])
     x_0_0 = apply_renaming_to_atom_values(x_0, renamings[0])
     x_0_1 = apply_renaming_to_atom_values(x_0, renamings[1])
     DatalogQuery.__init__(
         self, Atom("Lower_" + str(rewriting_index), x_0 + x_0_0))
     self.add_atom(Atom("Trans_" + str(rewriting_index), x_0 + x_0_0))
     self.add_atom(Atom("Trans_" + str(rewriting_index), x_0 + x_0_1))
     for i in range(0, index - 1):
         self.add_atom(EqualityAtom(x_0_0[i], x_0_1[i]))
     self.add_atom(CompareAtom(x_0_0[index], x_0_1[index], True))
Example #13
0
 def __init__(self, atom, cycle, q, rewriting_index, renaming):
     _, x, _ = q.decompose_atom(atom)
     _, x_0, _ = q.decompose_atom(cycle[0])
     x_0_ren = apply_renaming_to_atom_values(x_0, renaming)
     DatalogQuery.__init__(self, Atom("N_" + atom.name, x + x_0_ren))
     t_atom_content = x_0_ren
     for atom in cycle:
         _, x, y = q.decompose_atom(atom)
         t_atom_content += x
         t_atom_content += y
     self.add_atom(Atom("T_" + str(rewriting_index), t_atom_content))
Example #14
0
 def __init__(self, atom, cycle, q, rewriting_index, renamings):
     index = cycle.index(atom)
     k = len(cycle)
     _, x, _ = q.decompose_atom(atom)
     head_atom = Atom("Garbage_" + atom.name,
                      apply_renaming_to_atom_values(x, renamings[index]))
     DatalogQuery.__init__(self, head_atom)
     irr_content = []
     for i in range(k):
         _, x, _ = q.decompose_atom(cycle[i])
         irr_content += apply_renaming_to_atom_values(x, renamings[i])
     self.add_atom(Atom("Irr1Emb_" + str(rewriting_index), irr_content))
Example #15
0
 def __init__(self, cycle, q, rewriting_index, renamings):
     k = len(cycle)
     head_content = []
     for i in range(k):
         _, x, _ = q.decompose_atom(cycle[i])
         head_content += apply_renaming_to_atom_values(x, renamings[i])
     head_atom = Atom("InLongDCycle_" + str(rewriting_index), head_content)
     DatalogQuery.__init__(self, head_atom)
     _, x_0, _ = q.decompose_atom(cycle[0])
     x_0_k = apply_renaming_to_atom_values(x_0, renamings[k])
     self.add_atom(Atom("Pk_" + str(rewriting_index), head_content + x_0_k))
     self.add_atom(
         Atom("DCon_" + str(rewriting_index), x_0_k + head_content))
Example #16
0
 def __init__(self, cycle, q, rewriting_index, renamings):
     k = len(cycle)
     head_content = []
     any_content = []
     for i in range(k):
         _, x, y = q.decompose_atom(cycle[i])
         head_content += apply_renaming_to_atom_values(x, renamings[i])
         any_content += apply_renaming_to_atom_values(x, renamings[i])
         any_content += apply_renaming_to_atom_values(y, renamings[i])
     head_atom = Atom("Irr1Emb_" + str(rewriting_index), head_content)
     DatalogQuery.__init__(self, head_atom)
     self.add_atom(Atom("Any1Emb_" + str(rewriting_index), any_content))
     self.add_atom(Atom("Rel1Emb_" + str(rewriting_index), any_content),
                   True)
Example #17
0
 def __init__(self, atom, cycle, q, rewriting_index, renaming):
     k = len(cycle)
     _, x_0, _ = q.decompose_atom(cycle[0])
     x_0_ren = apply_renaming_to_atom_values(x_0, renaming)
     DatalogQuery.__init__(
         self, Atom("Link_" + str(rewriting_index), x_0 + x_0_ren))
     for i in range(k):
         _, x, y = q.decompose_atom(cycle[i])
         self.add_atom(Atom("Keep_" + cycle[i].name, x + y))
         x = apply_renaming_to_atom_values(x, renaming)
         y = apply_renaming_to_atom_values(y, renaming)
         self.add_atom(Atom("Keep_" + cycle[i].name, x + y))
     index = cycle.index(atom)
     _, x_i, _ = q.decompose_atom(cycle[index])
     x_i_ren = apply_renaming_to_atom_values(x_i, renaming)
     self.add_atom(Atom("Eq_" + atom.name, x_i + x_i_ren))
Example #18
0
 def __init__(self, cycle, q, rewriting_index, renaming):
     _, x_0, _ = q.decompose_atom(cycle[0])
     x_0_ren = apply_renaming_to_atom_values(x_0, renaming)
     head_content = []
     body_atoms = []
     head_content += x_0_ren
     for atom in cycle:
         _, x, y = q.decompose_atom(atom)
         head_content += x + y
         body_atoms.append(Atom("Keep_" + atom.name, x + y))
     DatalogQuery.__init__(self,
                           Atom("T_" + str(rewriting_index), head_content))
     for atom in body_atoms:
         self.add_atom(atom)
     self.add_atom(
         Atom("IdentifiedBy_" + str(rewriting_index), x_0 + x_0_ren))
Example #19
0
 def __init__(self, cycle, q, rewriting_index, renamings):
     k = len(cycle)
     head_content = []
     for i in range(k):
         _, x, y = q.decompose_atom(cycle[i])
         head_content += apply_renaming_to_atom_values(x, renamings[i])
         head_content += apply_renaming_to_atom_values(y, renamings[i])
     head_atom = Atom("Any1Emb_" + str(rewriting_index), head_content)
     DatalogQuery.__init__(self, head_atom)
     for i in range(k):
         for atom in q.get_atoms():
             self.add_atom(apply_renaming_to_atom(atom, renamings[i]))
     for i in range(k):
         _, x, y = q.decompose_atom(cycle[i])
         key1 = apply_renaming_to_atom_values(x, renamings[i])
         key2 = apply_renaming_to_atom_values(x, renamings[(k - 1 + i) % k])
         self.add_atom(Atom("Eq_" + cycle[i].name, key1 + key2))
Example #20
0
 def __init__(self, cycle, q, rewriting_index, renamings):
     k = len(cycle)
     head_content = []
     _, x_0, _ = q.decompose_atom(cycle[0])
     x_0_2k = apply_renaming_to_atom_values(x_0, renamings[2 * k])
     head_content += x_0_2k * 2
     common = []
     for i in range(1, k):
         _, x, _ = q.decompose_atom(cycle[i])
         common += apply_renaming_to_atom_values(x, renamings[i])
     head_atom = Atom("DCon_" + str(rewriting_index), head_content + common)
     DatalogQuery.__init__(self, head_atom)
     for atom in cycle:
         self.add_atom(apply_renaming_to_atom(atom, renamings[2 * k]))
     x_0_0 = apply_renaming_to_atom_values(x_0, renamings[0])
     x_0_k = apply_renaming_to_atom_values(x_0, renamings[k])
     self.add_atom(
         Atom("Pk_" + str(rewriting_index), x_0_0 + common + x_0_k))
Example #21
0
 def __init__(self, cycle, q, rewriting_index, renamings):
     k = len(cycle)
     head_content = []
     for i in range(k):
         _, x, _ = q.decompose_atom(cycle[i])
         head_content += apply_renaming_to_atom_values(x, renamings[i])
     _, x_0, _ = q.decompose_atom(cycle[0])
     x_0_k = apply_renaming_to_atom_values(x_0, renamings[k])
     head_content += x_0_k
     head_atom = Atom("Pk_" + str(rewriting_index), head_content)
     DatalogQuery.__init__(self, head_atom)
     for i in range(k + 1):
         for atom in q.get_atoms():
             self.add_atom(apply_renaming_to_atom(atom, renamings[i]))
     for i in range(1, k):
         _, x, y = q.decompose_atom(cycle[i])
         key1 = apply_renaming_to_atom_values(x, renamings[i])
         key2 = apply_renaming_to_atom_values(x, renamings[i - 1])
         self.add_atom(Atom("Eq_" + cycle[i].name, key1 + key2))
     x_0_k_1 = apply_renaming_to_atom_values(x_0, renamings[k - 1])
     x_0_0 = apply_renaming_to_atom_values(x_0, renamings[0])
     self.add_atom(Atom("Eq_" + cycle[0].name, x_0_k + x_0_k_1))
     self.add_atom(Atom("Neq_" + cycle[0].name, x_0_0 + x_0_k))
Example #22
0
 def __init__(self, head: Atom, done: Set[Atom]):
     DatalogQuery.__init__(self, head)
     for a_d in done:
         self.add_atom(a_d)
Example #23
0
 def __init__(self, atom, q):
     _, x, _ = q.decompose_atom(atom)
     head_atom = Atom("Eq_" + atom.name, x * 2)
     DatalogQuery.__init__(self, head_atom)
     self.add_atom(atom)
Example #24
0
 def __init__(self, atom, q):
     head_atom = Atom("Rlvant_" + atom.name, atom.content)
     DatalogQuery.__init__(self, head_atom)
     for atom2 in q.get_atoms():
         self.add_atom(atom2)
Example #25
0
 def __init__(self, atom, q):
     _, x, y = q.decompose_atom(atom)
     head_atom = Atom("Keep_" + atom.name, x + y)
     DatalogQuery.__init__(self, head_atom)
     self.add_atom(atom)
     self.add_atom(Atom("Garbage_" + atom.name, x), True)