|
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 |
|
@@ -103,4 +105,48 @@ You know that the fire starters live in the south *and* that they must have been
|
103 | 105 |
|
104 | 106 | ➤ `See the answer in the query console <https://lgtm.com/query/2551838470440192723/>`__
|
105 | 107 |
|
106 |
| -Continue to the :doc:`next page <fire-2>` to gather more clues and find out which of your suspects started the fire... |
| 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 <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 | +What next? |
| 148 | +---------- |
| 149 | + |
| 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 handbook <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>`. |
0 commit comments