Comparing Items in a Query

Conditions let you specify restrictions on individual items in a query. To place restrictions across different items we use constraints. Constraints compare one vertex or edge in the query to another vertex or edge.

QGraph permits two types of constraints:

Identity constraints are typically used to ensure that the same database entity does not match two different query elements. Exercise 5.5 illustrates a typical use of an identity constraint.

Attribute constraints allow you to compare different attributes as long as their data types are comparable. In addition to comparing attribute values with the same data type, Proximity also lets you to compare DBL with FLT and INT, and FLT with INT. This exercise uses an attribute constraint to compare values for the school attribute across objects.

The following exercise creates a query that finds linked web pages from different schools. The example query uses an attribute constraint to compare the value of the school attribute for the two web page objects.

Exercise 5.4. Adding constraints to a query:

The query created in this exercise is also available in the Proximity 4.3 distribution in $PROX_HOME/doc/user/tutorial/examples/different-schools.qg2.xml.

Before beginning, make sure that you are serving the ProxWebKB database using Mserver. Start the Proximity Database Browser if it is not already running.

  1. From the Query menu, choose New Query. Proximity starts the Query Editor.

  2. Create a vertex labeled start_page.

  3. Create a second vertex labeled linked_page.

  4. Add the numeric annotation [1..] to the linked_page vertex.

  5. Create a directed edge from start_page to linked_page. Label the edge linked_to.

  6. If you do not have automatic edge annotation enabled, add the numeric annotation [1..] to the linked_to edge.

  7. In the Constraints area at the bottom of the query properties pane, click Add to add a new constraint to the query.

    The Query Editor adds a temporary constraint, item1 > item2, as an example to be edited with the correct values. (Because this temporary constraint uses vertex labels not present in the query, the status list shows that the query is invalid.)

  8. Replace the example constraint with

    start_page.school <> linked_page.school

    and press Tab.

    The query should now be valid.

  9. Make sure the query is valid by checking the status list at the bottom of the Query Editor window. If the query is not valid, examine the errors in the drop-down list and fix any problems before continuing.

  10. [Optional] Add a name and description and save the query.

    [Caution]

    The Proximity distribution includes the new query in the file $PROX_HOME/doc/user/tutorial/examples/different-schools.qg2.xml. Be careful not to overwrite this file.

  11. From the File menu, choose Run or press the Ctrl-R to execute your query. Proximity prompts you for a name for the results container. Enter different-schools and click OK.

    Proximity opens a window to show you a trace of the query execution. The last lines should be similar to the following excerpt (leading information showing elapsed time and execution thread has been omitted from the trace for brevity):

    INFO kdl.prox.qgraph2.QueryGraph2CompOp - -> found 0 subgraphs
    INFO kdl.prox.qgraph2.QueryGraph2CompOp - -> query results saved in
       container: different-schools
    INFO kdl.prox.qgraph2.QueryGraph2CompOp - * query: done
    Status: finished running query
    

    Close this window after the query finishes.

    The resulting container has no subgraphs—there are no web pages in this database that link to a page at another school. Proximity creates a container even when there are no matches to the query.