You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/language/learn-ql/beginner/catch-the-fire-starter.rst
+50-4Lines changed: 50 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,7 @@
1
-
Catch the fire starter: Classes and predicates
2
-
==============================================
1
+
Catch the fire starter
2
+
======================
3
+
4
+
Learn about QL predicates and classes to solve your second mystery as a QL detective.
3
5
4
6
Just as you've successfully found the thief and returned the golden crown to the castle, another terrible crime is committed. Early in the morning, a few people start a fire in a field in the north of the village and destroy all the crops!
5
7
@@ -101,6 +103,50 @@ Now try applying ``isAllowedIn(string region)`` to a person ``p``. If ``p`` is n
101
103
102
104
You know that the fire starters live in the south *and* that they must have been able to travel to the north. Write a query to find the possible suspects. You could also extend the ``select`` clause to list the age of the suspects. That way you can clearly see that all the children have been excluded from the list.
103
105
104
-
➤ `See the answer in the query console <https://lgtm.com/query/2551838470440192723/>`__
106
+
➤ `See the answer in the query console on LGTM.com <https://lgtm.com/query/2551838470440192723/>`__
107
+
108
+
You can now continue to gather more clues and find out which of your suspects started the fire...
109
+
110
+
Identify the bald bandits
111
+
-------------------------
112
+
113
+
You ask the northerners if they have any more information about the fire starters. Luckily, you have a witness! The farmer living next to the field saw two people run away just after the fire started. He only saw the tops of their heads, and noticed that they were both bald.
114
+
115
+
This is a very helpful clue. Remember that you wrote a QL query to select all bald people:
116
+
117
+
.. code-block:: ql
118
+
119
+
from Person p
120
+
where not exists (string c | p.getHairColor() = c)
121
+
select p
122
+
123
+
To avoid having to type ``not exists (string c | p.getHairColor() = c)`` every time you want to select a bald person, you can instead define another new predicate ``isBald``.
124
+
125
+
.. code-block:: ql
126
+
127
+
predicate isBald(Person p) {
128
+
not exists (string c | p.getHairColor() = c)
129
+
}
130
+
131
+
The property ``isBald(p)`` holds whenever ``p`` is bald, so you can replace the previous query with:
132
+
133
+
.. code-block:: ql
134
+
135
+
from Person p
136
+
where isBald(p)
137
+
select p
138
+
139
+
The predicate ``isBald`` is defined to take a ``Person``, so it can also take a ``Southerner``, as ``Southerner`` is a subtype of ``Person``. It can't take an ``int`` for example—that would cause an error.
140
+
141
+
You can now write a query to select the bald southerners who are allowed into the north.
142
+
143
+
➤ `See the answer in the query console on LGTM.com <https://lgtm.com/query/2572701606358725253/>`__
144
+
145
+
You have found the two fire starters! They are arrested and the villagers are once again impressed with your work.
146
+
147
+
Further reading
148
+
---------------
105
149
106
-
Continue to the :doc:`next page <fire-2>` to gather more clues and find out which of your suspects started the fire...
150
+
- Find out who will be the new ruler of the village in the :doc:`next tutorial <crown-the-rightful-heir>`.
151
+
- Learn more about predicates and classes in the `QL language reference <https://help.semmle.com/QL/ql-handbook/index.html>`__.
152
+
- Explore the libraries that help you get data about code in :doc:`Learning CodeQL <../../index>`.
Copy file name to clipboardExpand all lines: docs/language/learn-ql/beginner/crown-the-rightful-heir.rst
+12-10Lines changed: 12 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,11 @@
1
1
Crown the rightful heir
2
2
=======================
3
3
4
+
This is a QL detective puzzle that shows you how to use recursion in QL to write more complex queries.
5
+
6
+
King Basil's heir
7
+
-----------------
8
+
4
9
Phew! No more crimes in the village—you can finally leave the village and go home.
5
10
6
11
But then... During your last night in the village, the old king—the great King Basil—dies in his sleep and there is chaos everywhere!
@@ -9,9 +14,6 @@ The king never married and he had no children, so nobody knows who should inheri
9
14
10
15
Eventually you decide to stay in the village to resolve the argument and find the true heir to the throne.
11
16
12
-
King Basil's heir
13
-
-----------------
14
-
15
17
You want to find out if anyone in the village is actually related to the king. This seems like a difficult task at first, but you start work confidently. You know the villagers quite well by now, and you have a list of all the parents in the village and their children.
16
18
17
19
To find out more about the king and his family, you get access to the castle and find some old family trees. You also include these relations in your database to see if anyone in the king's family is still alive.
@@ -125,7 +127,7 @@ Here is one way to define ``relativeOf()``:
125
127
126
128
Don't forget to use the predicate ``isDeceased()`` to find relatives that are still alive.
127
129
128
-
➤ `See the answer in the query console <https://lgtm.com/query/6710025057257064639/>`__
130
+
➤ `See the answer in the query console on LGTM.com <https://lgtm.com/query/6710025057257064639/>`__
129
131
130
132
Select the true heir
131
133
--------------------
@@ -136,9 +138,9 @@ To decide who should inherit the king's fortune, the villagers carefully read th
136
138
137
139
*"The heir to the throne is the closest living relative of the king. Any person with a criminal record will not be considered. If there are multiple candidates, the oldest person is the heir."*
138
140
139
-
As your final challenge, define a predicate ``hasCriminalRecord`` so that ``hasCriminalRecord(p)`` holds if ``p`` is any of the criminals you unmasked earlier (in the :doc:`Find the thief <find-thief-1>` and :doc:`Catch the fire starter <fire-1>` tutorials).
141
+
As your final challenge, define a predicate ``hasCriminalRecord`` so that ``hasCriminalRecord(p)`` holds if ``p`` is any of the criminals you unmasked earlier (in the :doc:`Find the thief <find-the-thief>` and :doc:`Catch the fire starter <catch-the-fire-starter>` tutorials).
140
142
141
-
➤ `See the answer in the query console <https://lgtm.com/query/1820692755164273290/>`__
143
+
➤ `See the answer in the query console on LGTM.com <https://lgtm.com/query/1820692755164273290/>`__
142
144
143
145
Experimental explorations
144
146
-------------------------
@@ -156,9 +158,9 @@ You could also try writing more of your own QL queries to find interesting facts
156
158
- Do all villagers live in the same region of the village as their parents?
157
159
- Find out whether there are any time travelers in the village! (Hint: Look for "impossible" family relations.)
158
160
159
-
What next?
160
-
----------
161
+
Further reading
162
+
---------------
161
163
162
-
- Learn more about recursion in the `QL language handbook<https://help.semmle.com/QL/ql-handbook/index.html>`__.
163
-
- Put your QL skills to the test and solve the :doc:`River crossing puzzle <../ql-etudes/river-crossing>`.
164
+
- Learn more about recursion in the `QL language reference<https://help.semmle.com/QL/ql-handbook/index.html>`__.
165
+
- Put your QL skills to the test and solve the :doc:`River crossing puzzle <cross-the-river>`.
164
166
- Start using QL to analyze projects. See :doc:`Learning CodeQL <../../index>` for a summary of the available languages and resources.
0 commit comments