Repository navigation
Expand file tree
/
Copy pathquery.py
More file actions
85 lines (65 loc) · 2.48 KB
/
Copy pathquery.py
File metadata and controls
85 lines (65 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import csv
class Relation:
"""A relation in a conjunctive query.
Attributes:
name: The relation name (e.g., "R", "Edge").
variables: List of variable names (e.g., ["x", "y"]).
data: List of row dicts, where each dict maps variable name -> value.
"""
def __init__(self, name, variables):
self.name = name
self.variables = list(variables)
self.data = []
def __str__(self):
return f"{self.name}({', '.join(self.variables)})"
def __repr__(self):
return self.__str__()
def __iter__(self):
"""Allow iteration over the relation's data rows."""
return iter(self.data)
def __len__(self):
return len(self.data)
def add_data(self, rows):
"""Add data rows to the relation.
Args:
rows: A list of dicts mapping variable name -> value, e.g.
[{"x": 1, "y": 2}, {"x": 1, "y": 3}]
"""
self.data.extend(rows)
def load_csv(self, filepath, value_type=int):
"""Load a CSV file into a table.
Args:
filepath: Path to the CSV file.
value_type: Type to cast values to (default: int).
"""
rows = []
with open(filepath, 'r') as f:
reader = csv.reader(f)
for row in reader:
rows.append({col: value_type(val) for col, val in zip(self.variables, row)})
self.data = rows
class ConjunctiveQuery:
"""A conjunctive query of the form: head :- body.
Example: Q(x, z) :- R(x, y), S(y, z)
Attributes:
head: List of variables in the query head (output variables).
relations: List of Relation objects forming the query body.
"""
def __init__(self, head, relations):
self.head = list(head)
self.relations = list(relations)
def get_variables(self):
"""Return the set of all variables appearing in the query body."""
variables = set()
for rel in self.relations:
variables.update(rel.variables)
return variables
def get_relations_for_variable(self, variable):
"""Return list of Relations that contain the given variable."""
return [rel for rel in self.relations if variable in rel.variables]
def __str__(self):
head_str = f"Q({', '.join(self.head)})"
relations_str = ', '.join(str(rel) for rel in self.relations)
return f"{head_str} :- {relations_str}"
def __repr__(self):
return self.__str__()