@@ -42,7 +42,7 @@ a piece of cargo.
4242 *Show/hide code *
4343
4444 .. literalinclude :: river-crossing.ql
45- :lines: 15-22
45+ :lines: 15-23
4646
4747Second, any item can be on one of two shores. Let's call these the "left shore" and the "right shore".
4848Define a class ``Shore `` containing ``"Left" `` and ``"Right" ``.
@@ -75,7 +75,7 @@ temporary variables in the body of a class are called `fields <https://help.semm
7575 *Show/hide code *
7676
7777 .. literalinclude :: river-crossing-1.ql
78- :lines: 33-43,90
78+ :lines: 33-40,87
7979
8080We are interested in two particular states, namely the initial state and the goal state,
8181which we have to achieve to solve the puzzle.
@@ -89,7 +89,7 @@ Assuming that all items start on the left shore and end up on the right shore, d
8989 *Show/hide code *
9090
9191 .. literalinclude :: river-crossing-1.ql
92- :lines: 92-100
92+ :lines: 89-97
9393
9494.. pull-quote ::
9595
@@ -107,7 +107,7 @@ Using the above note, the QL code so far looks like this:
107107 *Show/hide code *
108108
109109 .. literalinclude :: river-crossing.ql
110- :lines: 14-55,105-115
110+ :lines: 15-52,103-113
111111
112112Model the action of "ferrying"
113113~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -125,15 +125,14 @@ after ferrying a particular cargo. (Hint: Use the predicate ``other``.)
125125 *Show/hide code *
126126
127127 .. literalinclude :: river-crossing.ql
128- :lines: 57-70
128+ :lines: 54-67
129129
130130Of course, not all ferrying actions are possible. Add some extra conditions to describe when a ferrying
131131action is "safe". That is, it doesn't lead to a state where the goat or the cabbage get eaten.
132132For example, follow these steps:
133133
134- #. Define a predicate ``eating `` that encodes the conditions for when a "predator" is able to eat an
135- unguarded "prey".
136- #. Define a predicate ``isSafe `` that holds when nothing gets eaten.
134+ #. Define a predicate ``isSafe `` that holds when the state itself is safe. Use this to encode the
135+ conditions for when nothing gets eaten.
137136 #. Define a predicate ``safeFerry `` that restricts ``ferry `` to only include safe ferrying actions.
138137
139138.. container :: toggle
@@ -143,13 +142,14 @@ For example, follow these steps:
143142 *Show/hide code *
144143
145144 .. literalinclude :: river-crossing.ql
146- :lines: 72-84
145+ :lines: 69-81
147146
148147Find paths from one state to another
149148~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
150149
151150The main aim of this query is to find a path, that is, a list of successive ferrying actions, to get
152- from the initial state to the goal state.
151+ from the initial state to the goal state. You could write this "list" by separating each item by a
152+ newline (``"\n" ``).
153153
154154When finding the solution, you should be careful to avoid "infinite" paths. For example, the man
155155could ferry the goat back and forth any number of times without ever reaching an unsafe state.
@@ -180,7 +180,7 @@ for example ``steps <= 7``.
180180 *Show/hide code *
181181
182182 .. literalinclude :: river-crossing-1.ql
183- :lines: 73-89
183+ :lines: 70-86
184184
185185However, although this ensures that the solution is finite, it can still contain loops if the upper bound
186186for ``steps `` is large. In other words, you could get an inefficient solution by revisiting the same state
@@ -210,7 +210,7 @@ the given path without revisiting any previously visited states.
210210 *Show/hide code *
211211
212212 .. literalinclude :: river-crossing.ql
213- :lines: 86-105
213+ :lines: 83-102
214214
215215Display the results
216216~~~~~~~~~~~~~~~~~~~
@@ -225,7 +225,7 @@ that returns the resulting path.
225225 *Show/hide code *
226226
227227 .. literalinclude :: river-crossing.ql
228- :lines: 118-120
228+ :lines: 115-117
229229
230230For now, the path defined in the above predicate ``reachesVia `` just lists the order of cargo items to ferry.
231231You could tweak the predicates and the select clause to make the solution clearer. Here are some suggestions:
@@ -245,7 +245,7 @@ Here are some more example QL queries that solve the river crossing puzzle:
245245 #. This query uses a modified ``path `` variable to describe the resulting path in
246246 more detail.
247247
248- ➤ `See solution in the query console <https://lgtm.com/query/374739798559373721 / >`__
248+ ➤ `See solution in the query console <https://lgtm.com/query/659603593702729237 / >`__
249249
250250 #. This query models the man and the cargo items in a different way, using an
251251 `abstract <https://help.semmle.com/QL/ql-handbook/annotations.html#abstract >`__
0 commit comments