Chapter 3 - Practice
Q1: Parent & Grandparent Rules
- اعمل قاعدة بيانات للعيلة دي:
- Aly is the father of Hany.
- Samy is the father of Aly.
- Saly is the mother of Hany.
-
وبعدين اعمل الـ rules دي.
-
X is a parent of Y if X is the father of Y or X is the mother of Y.
-
X is a grandparent of Y if X is a parent of Z and Z is a parent of Y.
-
واكتب الاستعلامات:
- Who is Hany's parent?
- Who is Aly's father?
- Who is hany's grandparent?
Q2: Family Tree Facts
- اعمل قاعدة بيانات لشجرة العيلة دي :
- Hany is a parent of Samy.
- Saly is a parent of Samy.
- Saly is a parent of Lila.
- Samy is a parent of Aly.
- Samy is a parent of Dena.
- Dena is a parent of Tamer.
- Hany, Samy, Aly, Tamer are male.
- Saly, Dena, Lila are female.
Q3: Family Tree Rules
- من قاعدة البيانات بتاعة شجرة العيلة (سؤال 2)، اعمل الـ rules دي:
- X is the father of Y if X is a parent of Y and X is male.
- X is the mother of Y if X is a parent of Y and X is female.
- X is a sibling of Y if they both have the same parent Z.
- Y is an offspring of X if X is a parent of Y.
- X is a sister of Y if X is a sibling of Y and X is female.
Q4: Family Tree Queries
- من قاعدة البيانات بتاعة شجرة العيلة (سؤال 2)، اكتب الاستعلامات دي:
- Is Samy a parent of Dena?
- Is Aly a parent of Dena?
- Who is Lila's parent?
- Who is a parent of Aly and a parent of Dena?
- List all parent relationships.
- Who is Tamer's parent?
- Who is Dena's child?
- Who are the male offspring of Hany?
- Who has a sister?
- Who is Dena's grandparent?
Q5: Student Age Database
- اعمل قاعدة بيانات للطلاب دي using the predicate ا
student(Name, Sex, Age):
| Name | Sex | Age |
|---|---|---|
| Ahmed | male | 30 |
| Hany | male | 25 |
| Rwan | female | 20 |
| Mona | female | 18 |
-
وبعدين اعمل rule:
-
X is older than Y if age of X is greater than age of Y.
-
واكتب الاستعلامات:
- What is Hany's age?
- Who is male?
- List names with age < 30 and they are female.
- Find students older than Mona.
- Find students with age less than Ahmed. (ممكن تتحل بطريقتين)
- Find students younger than Ahmed and older than Mona. (ممكن تتحل بطريقتين)
Q6: son/child Rule
-
اعمل قاعدة بيانات:
-
Samy is male.
-
Hany is male.
-
Samy is a child of Hany.
-
وبعدين اعمل rule:
-
X is a son of Y if X is male and X is a child of Y.
-
اكتب الاستعلامات:
-
Who is Samy the son of?
-
Who is male? (اعرض كل الإجابات)
-
Is Samy the son of someone? (استخدم anonymous variable
_)
Q7: Recursive above (Blocks)
- اعمل قاعدة بيانات لـ 4 blocks فوق بعضها:
- Block a is on block b.
- Block b is on block c.
- Block c is on block d.
-
وبعدين اعمل recursive rule:
-
X is above Y if X is directly on Y.
-
X is above Y if X is on Z and Z is above Y.
-
واكتب الاستعلامات:
- Is block b on block c?
- Is block a on block d?
- Is block a above block d?
- What blocks are above block c?
Q8: Recursive predecessor
-
اعمل قاعدة بيانات:
-
a is a parent of b.
-
b is a parent of c.
-
c is a parent of d.
-
d is a parent of e.
-
وبعدين اعمل recursive rule:
-
X is a predecessor of Z if X is a parent of Z.
-
X is a predecessor of Z if X is a parent of Y and Y is a predecessor of Z.
-
واكتب الاستعلامات:
- Is a a parent of b?
- Is a a predecessor of b?
- Is a a predecessor of e?
- Who are the predecessors of d?
- Who are the descendants of a?
Q9: Recursive is_bigger
-
اعمل قاعدة بيانات بالحقائق دي:
-
An elephant is bigger than a horse.
-
A horse is bigger than a donkey.
-
A donkey is bigger than a dog.
-
A donkey is bigger than a monkey.
-
وبعدين اعمل recursive rule:
-
X is bigger than Y if X is directly bigger than Y.
-
X is bigger than Y if X is bigger than Z and Z is bigger than Y.
-
واكتب الاستعلامات:
- Is an elephant bigger than a monkey?
- What is bigger than a donkey?
- What is a horse bigger than?
- What is bigger than a dog and that thing is also bigger than a monkey?
Q10: Graph Route with Distances
-
اعمل قاعدة بيانات للـ graph دي using the predicate
link(Node1, Node2, Distance): -
Node a is connected to b with distance 2.
-
Node a is connected to c with distance 3.
-
Node b is connected to d with distance 4.
-
Node b is connected to e with distance 5.
-
Node c is connected to f with distance 6.
-
Node c is connected to g with distance 7.
وبعدين اعمل recursive rule:
- X can reach Y with distance D if there is a direct link between them.
- X can reach Y with distance D if X can reach Z with distance D1 and Z can reach Y with distance D2 and D = D1 + D2.
واكتب الاستعلامات:
- What nodes are directly connected to node b and their distance?
- Which nodes connect to node c and their distance?
- Which other nodes can you reach when starting at node a and their distances?
- Which nodes can be found between node a and node e, and their distances from a and to e?
Q11: Flights (Faster & Travel)
- اعمل قاعدة بيانات للرحلات الجوية دي:
| Flight | From | To | Duration (hr) |
|---|---|---|---|
| BA1 | Cairo | Rome | 3.5 |
| AL2 | Rome | Paris | 2.5 |
| BA6 | Cairo | London | 6.2 |
| AF1 | Paris | Brussels | 0.6 |
-
وبعدين اعمل rules:
-
Flight N is faster than flight M if duration of N < duration of M.
-
You can travel from X to Y with total duration D if there is a direct flight from X to Y with duration D.
-
You can travel from X to Y with total duration D if there is a flight from X to Z with duration D1 and you can travel from Z to Y with duration D2 and D = D1 + D2.
-
واكتب الاستعلامات:
-
Which cities can be reached by a single flight from Cairo?
-
How long does flight AL2 take?
-
From which cities can you fly to London (by a single flight)?
-
Which flight numbers are faster than BA1?
-
Which flight numbers are slower than AL2?
-
Which cities can be reached by some sequence of flights from Cairo?
-
From which cities can one travel to Paris by some sequence of flights?
Q12: Reigns Database
- اعمل قاعدة بيانات لفترات حكم الأمراء دي:
| Prince Name | Reign year | To year |
|---|---|---|
| Aly | 200 | 220 |
| Samy | 221 | 230 |
| Hany | 231 | 245 |
| Lotfy | 246 | 260 |
-
وبعدين اعمل rule:
-
X was a prince during year Y if X reigned between years A and B, and Y is between A and B inclusive.
-
واكتب الاستعلامات:
-
Was Samy a prince in year 225?
-
Was Aly a prince in year 1979?
-
Which prince(s) were on the throne in year 240?