def parse_query(string): pattern = re.compile("\[[A-Za-z_,0-9]*\]:[A-Za-z_,\(\)\[\]\*0-9]*$") if pattern.match(string): try: free_var_body, query_body = string.split(":") free_var_body = free_var_body[1:-1] free_vars = parse_atoms_values(free_var_body) q = ConjunctiveQuery() for atom, fd_set, is_key, is_consistent in parse_atoms(query_body): q = q.add_atom(atom, fd_set, is_key, is_consistent) for value in free_vars: if value.var: q = q.release_variable(value) return q except MalformedQuery: raise else: raise MalformedQuery(string, "ConjunctiveQuery")
atom_s = Atom("S", [y, z]) # Initialize functional dependencies # First parameter : Left side of the FD (List of variables) # Second parameter : Right side of the FD (must be a single variable) fd1 = FunctionalDependencySet() fd1.add(FunctionalDependency([x], y)) fd2 = FunctionalDependencySet() fd2.add(FunctionalDependency([y], z)) # Initialize the conjunctive query q = ConjunctiveQuery() # Add atoms to q # First parameter : The atom to be added # Second parameter : The set of FD (must be a frozenset as in the example) # Third parameter : A List of booleans describing the "key positions" of the atom # Fourth Parameter : A boolean that must be True if the atom is consistent, False if not q = q.add_atom(atom_r, fd1, [True, True, False], False) q = q.add_atom(atom_s, fd2, [True, False], False) # Choose free variables (creates a new instance) q = q.release_variable(x) q = q.release_variable(y) # Launch rewriting program = rewrite(q) # You can print the Datalog program print(program)