3.2. Supporting a Polymorphic Query Language

In the remainder of this chapter we implement a query language class hierarchy to introduce the C# language constructs and programming idioms that support object-oriented programming. What is a query language? It is part of a general text query application that allows users to search a text file for the occurrence of one or more words. For example, here is the text file against which our queries are directed:

Alice Emma has long flowing red hair. Her Daddy says

when the wind blows through her hair, it looks almost alive,

like a fiery bird in flight. A beautiful fiery bird, he tells her,

magical but untamed. "Daddy, shush, there is no such creature,"

she tells him, at the same time wanting him to tell her more.

Shyly, she asks, "I mean, Daddy, is there?"

Here is a sample execution of the text query system:

TextQuery: begin()

Reading c:QueryManageralice_emma.txt -- please wait!
Reading c:QueryManageralice_emma.txt -- OK, done!

The text file alice_emma.txt
         contains 6 lines in 357 bytes.

entering text into dictionary -- please wait!
entering text into dictionary -- OK, done!

         Time to read text file: 0.004 seconds
         Time to enter text into dictionary: 0.009 seconds

Please enter a query or 'q' to Quit: alice

alice

         1 line(s) match: [ 1 ]

[1]: Alice Emma has long flowing red hair. Her Daddy says
Please enter a query or 'q' to Quit: alice || daddy

alice
         1 line(s) match: [ 1 ]

daddy
         3 line(s) match: [ 1 4 6 ]

alice  || daddy
         3 line(s) match: [ 1 4 6 ]

[1]: Alice Emma has long flowing red hair. Her Daddy says
[4]: magical but untamed. "Daddy, shush, there is no such
creature,"
[6]: Shyly, she asks, "I mean, Daddy, is there?"

Please enter a query or 'q' to Quit: q

OK, bye!
TextQuery: end()

The query facility we have chosen to support consists of the following elements:

  • A name query, represented as a single word, such as Alice or untamed. All lines in which the word appears are displayed with the line number in square brackets. (The lines are displayed in ascending order.)

  • A NOT query, represented as the ! operator. For example, in the query !daddy, all the lines in which the word daddy does not appear are displayed:

Please enter a query or 'q' to Quit: ! daddy

daddy
    3 line(s) match: [ 1 4 6 ]

! daddy
    3 line(s) match: [ 2 3 5 ]

[2]: when the wind blows through her hair, it looks almost alive,
[3]: like a fiery bird in flight. A beautiful fiery bird, he tells
her,
[5]: she tells him, at the same time wanting him to tell her more.

  • An OR query, represented as the || operator. For example, in the query alice||daddy, all the lines in which either of the two names appear are displayed.

  • An AND query, represented as the && operator. For example, in the query alice&&daddy, all the lines in which both words are present are displayed.

Please enter a query or 'q' to Quit: alice && daddy
alice
    1 line(s) match: [ 1 ]

daddy
    3 line(s) match: [ 1 4 6 ]

alice  && daddy
    1 line(s) match: [ 1 ]

[1]: Alice Emma has long flowing red hair. Her Daddy says

These elements can be combined, as in

fiery && bird || shyly

However, the order of evaluation is from left to right, with each element maintaining the same precedence level. The evaluation of the query here finds all lines in which the words fiery and bird appear on the same line, or a line in which shyly appears:

Please enter a query or 'q' to Quit: fiery && bird || shyly
fiery
      1 line(s) match: [ 3 ]
bird
      1 line(s) match: [ 3 ]
fiery  && bird
      1 line(s) match: [ 3 ]
shyly
      1 line(s) match: [ 6 ]
fiery  && bird  || shyly
      2 line(s) match: [ 3 6 ]

[3]: like a fiery bird in flight. A beautiful fiery bird, he tells
her,
[6]: Shyly, she asks, "I mean, Daddy, is there?"

To allow subgrouping of a query, our query facility must also support parentheses. For example, the query

fiery && ( bird || shyly )

finds all references in which either fiery and bird, or fiery and shyly, appear on the same lineā€”for example,

Please enter a query or 'q' to Quit: fiery && ( bird || shyly )
fiery
      1 line(s) match: [ 3 ]

( bird
      1 line(s) match: [ 3 ]

shyly
      1 line(s) match: [ 6 ]

( bird  || shyly
      2 line(s) match: [ 3 6 ]

fiery  && ( bird  || shyly  )
      1 line(s) match: [ 3 ]

[3]: like a fiery bird in flight. A beautiful fiery bird, he tells
her,

So this is what we need to represent in our class hierarchy. How do we do that?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.56.12