예제 #1
0
def new_atoms(cycle: List[structures.Atom], q: structures.ConjunctiveQuery, rewriting_index: int,
              renaming: Dict[structures.AtomValue, structures.AtomValue]) -> \
        Tuple[Tuple[structures.Atom, structures.FunctionalDependencySet, List[bool], bool],
              List[Tuple[structures.Atom, structures.FunctionalDependencySet, List[bool], bool]]]:
    """
    Generates the atoms needed for the reduction
    :param cycle:				Cycle being reduced
    :param q:					A ConjunctiveQuery
    :param rewriting_index:		Index used to enumerate the Datalog rules
    :param renaming:			Necessary renaming
    :return:					Atoms needed for the reduction
    """
    _, x_0, _ = q.decompose_atom(cycle[0])
    x_0_ren = algorithms.apply_renaming_to_atom_values(x_0, renaming)
    n_atoms = []
    t_content = []
    t_released = set()
    for atom in cycle:
        _, x, y = q.decompose_atom(atom)
        n = structures.Atom("N_" + atom.name, x + x_0_ren, atom.released)
        t_released = t_released.union(atom.released)
        fd = structures.FunctionalDependencySet()
        for var in x_0_ren:
            fd.add(structures.FunctionalDependency(x, var))
        is_key = [True] * len(x) + [False] * len(x_0_ren)
        n_atoms.append((n, fd, is_key, True))
        t_content += x
        t_content += y
    t = structures.Atom("T_" + str(rewriting_index), x_0_ren + t_content, t_released)
    fd = structures.FunctionalDependencySet()
    for var in t_content:
        fd.add(structures.FunctionalDependency(x_0_ren, var))
    is_key = [True] * len(x_0_ren) + [False] * len(t_content)
    return (t, fd, is_key, False), n_atoms
예제 #2
0
 def __init__(self, q: ConjunctiveQuery, atom: Atom, index: int,
              is_last: bool, done: Set[Atom]):
     self.q = q
     self.atom = atom
     self.index = index
     self.v, self.x, self.y = q.decompose_atom(atom)
     _, self.vars_x, self.vars_y = q.decompose_atom(atom, True)
     self.vars_z, self.c = generate_z_and_c(self.x, self.y, self.vars_y,
                                            index)
     self.has_c = len(self.c) > 0
     self.is_last = is_last
     self.frozen = q.free_vars
     self.new_frozen = []
     for var in q.free_vars:
         self.new_frozen.append(var)
     for var in self.v:
         if var not in self.new_frozen:
             self.new_frozen.append(var)
     self.done = done
예제 #3
0
def new_atoms_rules(cycle: List[structures.Atom], q: structures.ConjunctiveQuery, rewriting_index : int,
                    renamings: List[Dict[structures.AtomValue, structures.AtomValue]]) -> List[structures.DatalogQuery]:
    """
    Generated the rules defining the new atoms added by the reduction
    :param cycle: 				Cycle being reduced
    :param q: 					A ConjunctiveQuery
    :param rewriting_index: 	Index used to enumerate the Datalog rules
    :param renamings: 			Necessary renamings
    :return: 					Rules defining the new atoms
    """
    _, x_0, _ = q.decompose_atom(cycle[0])
    rules = []
    rules += [templates.KeepQuery(atom, q) for atom in cycle]
    rules += [templates.LinkQuery(atom, cycle, q, rewriting_index, renamings[0]) for atom in cycle]
    rules += [templates.TransBaseQuery(cycle, q, rewriting_index, renamings[0])]
    rules += [templates.TransRecQuery(cycle, q, rewriting_index, renamings)]
    if len(x_0) > 1:
        rules += [templates.LowerCompositeQuery(cycle, q, i, rewriting_index, renamings) for i in range(len(x_0))]
    else:
        rules += [templates.LowerSingleQuery(cycle, q, rewriting_index, renamings)]
    rules += [templates.IdentifiedByQuery(cycle, q, rewriting_index, renamings[0])]
    rules += [templates.NQuery(atom, cycle, q, rewriting_index, renamings[0]) for atom in cycle]
    rules += [templates.TQuery(cycle, q, rewriting_index, renamings[0])]
    return rules