Seminar on Databases Some Useful Transformations We transform rules of the form W → A into Datalog rulse. Here W is an arbitrary first-order formula and A is an atom. Formula W is represented by F ∧ G ∧ H, where F and H may be missing. The transformation rules are listed in Table 1. Rules are fired until no rule is applicable. Table 1: Transformation rules. Formula [F ∧ ¬(G1 ∧ G2 ) ∧ H] → A [F ∧ (G1 ∨ G2 ) ∧ H] → A [F [F [F [F [F [F [F [F ∧ ¬(G1 ∨ G2 ) ∧ H] → A ∧ (G1 → G2 ) ∧ H] → A ∧ ¬(G1 → G2 ) ∧ H] → A ∧ (G1 ≡ G2 ) ∧ H] → A ∧ ¬(G1 ≡ G2 ) ∧ H] → A ∧ ¬¬G ∧ H] → A ∧ ¬∀x̄(G) ∧ H] → A ∧ ∃x̄(G) ∧ H] → A Replacing formula(s) [F ∧ (¬G1 ∨ ¬G2 ) ∧ H] → A [F ∧ G1 ∧ H] → A, and [F ∧ G2 ∧ H] → A [F ∧ ¬G1 ∧ ¬G2 ∧ H] → A [F ∧ (¬G1 ∨ G2 ) ∧ H] → A [F ∧ G1 ∧ ¬G2 ∧ H] → A [F ∧ (G1 → G2 ) ∧ (G2 → G1 ) ∧ H] → A [F ∧ ¬((G1 → G2 ) ∧ (G2 → G1 )) ∧ H] → A [F ∧ G ∧ H] → A [F ∧ ∃x̄(¬G) ∧ H] → A [F ∧ G0 ∧ H] → A where G0 is obtained from G by replacing variables in x̄ by fresh variables The following equivalence is also useful: [F → (G ∧ H)] ≡ [(F → G) ∧ (F → H)]. 1 Exercise Transform the following rules into Datalog rules: 1. [mother(x, y) ∨ f ather(x, y)] → parent(x, y) 2. mother(x, y) → [parent(x, y) ∧ f emale(x)] 3. [¬(parent(x, y) → ¬male(y))] → son(y) 4. [∃x(parent(y, x) ∧ parent(x, z))] → grandparent(y, z) 5. [¬∀y[¬parent(x, y)] ∧ male(x)] → f ather(x). Exercise 1. Design a Datalog database for storing information about objects on a town street. Objects might be cars, bikes, etc. Each object is characterized by its size (small, medium or large) and velocity (slow, moderate, fast). In addition, the database should contain information about direct precedence between objects.1 2. Express in predicate calculus the constraint “every object has unique (exactly one) size and unique velocity”. 3. Provide an exemplary integrity constraint concerning the precedence relation between objects. 4. Formulate in logic queries selecting: (a) all bikes which are directly between two large cars (b) all medium cars which precede (not necessarily directly2 ) a fast large car. 1 Object a “directly precedes” object b, denoted by a ← b, if a precedes b and there are no objects between a and b. 2 It is assumed that object c precedes object e if c ← e or there is n ≥ 1 and objects d1 , . . . , dn such that c ← d1 ← d2 ← . . . ← dn ← e. 2 Exercise Consider regions on a screen. We say that a region r is contained in region t if all pixels of region r are located within region t and r 6= t. We say that r is directly contained in region t when • r is contained in region t, and • there is no region s such that r is contained in s and s is contained t. For example, in the following figure region a is contained in region b and in region c. Region a is directly contained in b but not directly contained in c. Region b is directly contained in c. Region d is contained (also directly) in c but neither in a nor in b. c $ ' b d a & % 1. Design a Datalog database for storing information about regions on a screen, where each region is characterized by its shape, size and color. In addition, for each pair of regions r, s, the database contains information whether r is directly contained in s. 2. Express in predicate calculus the constraint: “the relation of containment is antisymmetric” 3. Provide another exemplary integrity constraint concerning the relation of region containment. 4. Formulate in logic queries selecting: (a) all small regions directly contained in a large circle; (b) all small objects contained3 in a big green square. 3 Directly or not. 3 Exercise Consider road segments and landmarks. Road segments are chosen in such a way that each landmark is close to exactly one road segment. Road segments are directly connected if they have a common border. In particular, each road segment is directly connected to itself. For example, in Figure 1: • segments S1 and S3 , segments S3 and S4 , segments S4 and S5 , segments S4 and S6 as well as S4 and S2 are directly connected • 1 ≤ i ≤ 6 we have that Si is directly connected to Si • for example, segments S1 and S6 , segments S2 and S5 as well as S3 and S5 are not directly connected • landmark L1 is close to segment S5 and landmark L2 is close to road segment S1 . S2 S1 i L2 S3 S4 L1 i S5 S6 Figure 1: Exemplary road segments S1 , . . . , S6 and landmarks L1 , L2 . A landmark L is accessible by a road trip from landmark L0 if L is close to a road segment S and L0 is close to a road segment S 0 and there is a sequence of road segments R1 , . . . , Rn with n ≥ 1 such that S = R0 , S 0 = Rn and for all i such that 1 ≤ i < n, Ri is directly connected to Ri+1 . 1. Design a database storing information about road segments, their direct connections and landmarks which are close to particular road segments. Road segments are characterized by their quality (low, medium, high) and average speed. The database contains also information about direct connections between road segments. 2. Express in predicate calculus the constraint: “each landmark is close to exactly one road segment”. 3. Provide another exemplary integrity constraint concerning relationships in the database. 4. Formulate in logic queries selecting: (a) all landmarks close to roads which are of high quality with average speed greater than 65 km h ; (b) find all landmarks which are accessible by a road trip from a given landmark. 4