![]() |
||||||||||||||
![]() ![]() Order any two training programs in our online chess shop and get a third training program absolutely free. If your order is more than $120 / €100 Euro, you will get additional present. New CT-ART 5.0 combines three excellent courses guiding you from beginner to advanced level. |
||||||||||||||
ChessOK.com » CQL Queries in Chess Assistant | ||||||||||||||
CQL Queries in Chess Assistant
22 December 2008
This article is an informal introduction to CQL, based on several simple examples that illustrate some of its capabilities. Although using and running CQL is quite simple in Chess Assistant, designing the queries themselves requires at least some basic knowledge of CQL. Fortunately, the manual is available online and contains many example queries that can serve as a starting point for experimenting.
When you are ready, click on OK and Chess Assistant will begin processing the query. In case you have made an error Chess Assistant displays an error message describing what’s wrong and allows you to continue editing the query. While Chess Assistant is searching, a dialog box similar to this one is displayed. The colored bar on the left shows that 31% of the games have been searched, or 33,038 of 106,096 as shown below the bar. The text above the blue and white horizontal bar tells us that Chess Assistant has been searching for 6 seconds and the total search time is expected to be 19 seconds. The “Pause” button allows you to pause the search and continue later.
The “Background” button minimizes the application and clicking on “Cancel” stops the search, but creates a list of the games found so far. This can be useful when testing new queries and allows you to quickly examine the results to see if they match your expectations. Finally the text “Found: 1789” at the bottom of the screen shows that so far 1,789 games (of the 33,038 already searched) match the query. Quite often these numbers will tell you if your query is actually doing what you expected. If you know that only a few games can match your query, but the statistics show that several hundred have been found, something must be wrong and it may be best to hit the “Cancel” button and check the results.
The Structure of CQL Queries A CQL query has the following generic structure:
(position … ) (position … ) (position … ) … ) In this example, the ”..” represent various keywords and commands. Each of the “(position …)” elements is usually referred to as a position list. Here is a simple CQL query to find games where White wins without moving his king (see explanations below):
:result 1-0 ; White wins (position :movefrom K :matchcount 0) ) A keyword in CQL is a string beginning with “:.” There are three keywords in this example: “:result,” “:movefrom” and “:matchcount”: :result – Note that this is the only keyword outside the position list as the result is a game attribute. This keyword has one parameter, 1-0, indicating that we are only interested in games won by White. :movefrom – This keyword is within the position list, meaning that it contributes to the description of a particular position(s) that we are interested in. :movefrom takes a single parameter, a piece designator. “K” is a very simple piece designator. It means that we are looking for any move by the white king (“k” would be the black king). A slightly more complicated (and familiar) example of a piece designator would be “Kg4,” i.e. the piece designator can also specify a square (actually more than one) for the piece. :matchcount – This keyword specifies a limit on how often the position we are describing can occur in the game. In our case there is one parameter specified “0.” It means that we are looking for games where the position described within the “(position …)” never occurs. In other words, we are looking for games where the white king never moves. Finally, the text “; White wins” is a comment. A comment starts with a semicolon and extends to the end of the current line. Now, let’s have a look at a few more CQL examples. Double Check CQL can be helpful in chess training where you want to find games with some particular theme. Here is a simple example finding games where the white king is in double check:
(position :attackcount a K 2) ) Here we see a new keyword, :attackcount. It takes two piece designators (“a” and “K” in our example) followed by a range (here “2”) as parameters. The “a” stands for the attacking piece (s) and is shorthand for “any black piece” (including pawns), so it is equivalent to writing [kqrbnp] (note the square brackets):
The second parameter (“K”) stands for the white king. The range is “2” meaning that there must be two simultaneous attacks. Looking at the position list as a whole, we are looking for a position where two black pieces attack the white king. Instead of a single number, we could have specified a range for the number of attacks:
Here we have replaced “2” with “1 100” meaning “from 1 to 100.” In other words this would match any position where the white king is in check. Switching Colors In the example above we looked for positions where there was a double attack on the white king. Similarly the following query would find positions where the black king is in double check:
(position :attackcount A k 2) ) The only differences is that now the attacking pieces are “A” (meaning any white piece) and the attacked piece is now “k,” i.e. the black king. But there is an easier and more general way to flip the colors automatically in a query:
(position :attackcount a K 2 :flipcolor) ) The :flipcolor keyword automatically tries the colors as they are specified and then checks for the same pattern with colors reversed. As a result this query would find all games where either king is in double check. White Wins Without Castling
:result 1-0 (position :movefrom Ke1 :moveto .[c1,g1] :matchcount 0) ) We are already familiar with three of the keywords in this query: :result, :movefrom and :matchcount.
So looking at :movefrom and :moveto we can see that the position list needs a castling move. But we also have “:matchcount 0” which means that a game will not match the query except if White never castles. Additionally, White must win the game (:result 1-0). As a result we find all games where White wins in spite of not castling. This may include games where White moves his king, as long as he never castles. For the case where White wins, but never moves his king see the first example in this article. Search the Whole Board for a Pattern One of the powerful features of CQL is that you can specify a pattern and then search for it anywhere on the board using the :shift keyword. Here is an example where we search for a white passed pawn.
(position Pd2 :piececount [pP][d3-7] 0 :piececount p[c3-7,e3-7] 0 :shift ) )
Meeting Check with a Checkmate This is a simple CQL query, mainly to introduce the concept of position sequences in CQL queries. We want to find all games where one side responds to a check with a checkmate.
(position :sequence ( (position :check) (position :mate) ) ) ) A :sequence keyword can be specified within a position list. It groups together positions that must occur successively in the game, following whatever position may be specified before the :sequence. In this example we only have two positions specified. First, either side must be in check (“:check”) and the response to the check is a checkmate (“:mate”). There in no need to specify :flipcolor in this query as we didn’t bind the pieces to a specific color. Running this query in Chess Assistant on the three million game HugeBase returned 159 games. Here is a nice little example from the simul game Alekhine, A. – Popovic, A. (Osijek, 1930). Black just played 42…Bg6 giving a check, which Alekhine answered with 43 Rc2, blocking the check and simultaneously checkmating the black king.
:forany pawn P (position :initial $pawn[b2]) (position :movefrom $pawn :promote U) ) The :forany keyword in a match list defines a tag and a piece designator. In our example, we are looking at the white pawns (“P”) and we have chosen the tag “pawn” as an identifier. So, “pawn” can stand for any white pawn on the board. If we want to limit it to a particular pawn, an additional condition is needed as we describe below. We can use the tag we have selected in the position lists in the query by preceding the name with a “$” (“$pawn”). Let’s see how that is done.
The :initial keyword ties this position list to the initial position of the game, and in that position our $pawn must be on the b2-square. So, here we have ensured that $pawn can only refer to the b-pawn. When we have two or more position lists in a CQL query, it matches a game only if there is a match for all the position lists. Note that we did not use the :sequence keyword here, but we made sure that the first position list was tied to the initial position. And it will always match for a normal game of chess as there is always a b2-pawn in the initial position. Now we come to the second position list:
Since $pawn refers to the pawn that started on b2, this position list matches only a move by that pawn which also is a promotion. The promotion part is ensured by the :promote keyword, which takes a single parameter, a piece designator. In this case it is “U”, which is shorthand for any piece at all. Therefore, it matches regardless what piece the pawn is promoted to.
Knight Gives Mate with its First Move Games in which a knight gives mate with its first move are rare. Edward Winter has collected and published several such games, including four specimens in his book Kings, Commoners and Knaves (Russell Enterprises, Inc., Milford, 1999, pages 78-80).
:forany knight N (position :movefrom $knight :matchcount 1) (position :mate :attackcount $knight k 1 ) ) Here we use tagging again, this time to keep track of the white knights. The first position list ensures that we only take into consideration a knight that moves exactly once during the game. The second position list ensures that the knight attacks the black king in the final position where the king is checkmated. Running this query through HugeBase returns nine games. It is easy to modify the query so that it finds similar checkmates by Black. Do You Have a Query? If you have created some interesting CQL queries, I would be interested in seeing them and possibly publishing them in a future column. Also, if you have given up trying to search for an interesting theme using traditional chess database tools. Send me a note describing what you were trying to search for and I might come up with a solution in CQL. Chess Assistant supports the full Chess Query Language and adds several extensions. Some of the queries that were taken as examples in this article are quite simple, but others use more advanced features like tagging. There are features in Chess Assistant that make managing CQL queries much easier that were not described here. In particular I would like to point the interested reader to the Composite Search function. Dadi Jonsson |
Study chess online on Chess King Learn! Time-proven training courses, thousands of examples and exercises. Start playing right now in your web browser on Chess King Playing Zone! Teams, tournaments, training, analysis and much more! Download weekly chess database updates (containing 2000+ games) with latest tournaments and games in PGN and Chess Assistant format.
|
|||||||||||||
About - Contact Us | ||||||||||||||
© 2008 — 2019 ChessOK.com |