Negated and Optional Elements

Numeric annotations can also be used to indicate that a database element must not or need not be present to match the query.

To be well formed, a query must remain a connected graph after removing any negated or optional elements.

The database fragment shown in Figure 4.24 includes information on movies and any awards they have won.

Database fragment [Annot_DB04.xml]

Figure 4.24. Database fragment [Annot_DB04.xml]


This fragment contains information on three movies, all directed by Ron Howard. A Beautiful Mind was nominated for and won the Academy Award for Best Picture for 2002; Apollo 13 was nominated for the Academy Award for Best Picture for 1995, but the Oscar was awarded to Forrest Gump that year; and Far and Away was not nominated for any Academy Awards.

Suppose we want to find all movies that have not been nominated for an Academy Award. One way to do so is to only match subgraphs that do not include any Academy Award objects.

Query [Annot_DB04_Q01.qg2.xml]

Figure 4.25. Query [Annot_DB04_Q01.qg2.xml]


The [0] annotation on the award object means that this query matches only those movies that are not linked to any Academy Awards. For now we’ll simply note that the [1..] edge annotation is usually the correct annotation for edges adjacent to negated or optional vertices and proceed accordingly. Additional information on choosing the correct annotation for edges adjacent to negated or optional vertices is included in the section “Annotating edges adjacent to negative and optional vertices” later in this chapter.

The result of executing this query on the database fragment shown in Figure 4.24 is shown below.

Query results

Figure 4.26. Query results


Only a single movie, Far and Away, matches the query. Note that negated query elements do not appear in the query results. Because the query includes a [0] annotation on the award vertex, there can be no corresponding object in order to match the query, and therefore no award objects can appear in the matching subgraphs.

Compare this query to a similar query where we don’t care whether the movie has been nominated for an Academy Award.

Query [Annot_DB04_Q02.qg2.xml]

Figure 4.27. Query [Annot_DB04_Q02.qg2.xml]


In this example, the award vertex is annotated with [0..], making matches to this vertex optional. That is, subgraphs in the database will match this query regardless of whether or not the corresponding movie has been nominated for an Academy Award. The results of executing the new query with the optional award vertex are shown below.

Query results

Figure 4.28. Query results


This time, all three movies appear in the query results. The optional award vertex lets the query match both movies that have received nominations and movies that have not received any nominations. Because Far and Away has no nominations, its subgraph does not include any award objects.

Although not yet supported by Proximity, QGraph queries can include negated and optional edges in addition to negated and optional vertices. Care must be taken to avoid disconnected queries; queries with negated or optional edges must also include required alternate paths to all query elements.

Figure 4.29 illustrates a query that includes a negated edge. This query finds movies that have been nominated for, but not won an Academy Award. The won edge is annotated with [0] indicating that matching subgraphs must not include a matching link.

Query

Figure 4.29. Query


To prevent the query from becoming disconnected, the nominated edge is required. The [1..] edge annotation means that the query only matches subgraphs that contain one or more nominated links connecting the movie to any Academy Award objects.

The expected results, had we been able to execute this query in Proximity, are shown below.

Query results

Figure 4.30. Query results


Only one movie in our database fragment, Apollo 13 has been nominated for but not won an Academy Award.

Annotating edges adjacent to negative and optional vertices

Because edges adjacent to annotated vertices must themselves be annotated, how are we to annotate the edge adjacent to a negated or optional vertex? In general, the usual annotation of [1..] remains appropriate. We require the presence of the query edge to avoid having the query become disconnected. For example, an optional edge connected to an optional vertex could result in a disconnected subgraph when the matching vertex is present but the matching edge is not. Optional or negated edges are usually inappropriate in most queries, although they may be safely used if other elements in the query ensure that the query remains connected after removing any optional or negated elements.

For example, consider the database fragment shown in Figure 4.31.

Stephen King books and movies

Figure 4.31. Stephen King books and movies


If we wanted to identify author-book pairs and also include movie adaptations, if any, we might initially (and incorrectly) create a query that looks like that shown in Figure 4.32.

Improperly annotated query

Figure 4.32. Improperly annotated query


This query uses the improper annotation [0..] for both the movie vertex and adjacent adapted-to edge. Such a query can match disconnected subgraphs. Consider the highlighted subset of elements from this database shown in Figure 4.33.

Disconnected subgraph

Figure 4.33. Disconnected subgraph


The highlighted set of objects and links matches the query shown in Figure 4.32. The [0..] annotation on the movie vertex and [0..] annotation on the adapted-to edge mean that the query is allowed to match database structures that include a matching movie object but that omit the matching adapted-to link. This can produce disconnected subgraphs like the highlighted elements shown above, which clearly isn’t what was intended by the query. To prevent the possibility of matching such disconnected subgraphs, use the [1..] annotation on edges adjacent to a vertex annotated with [0] or [0..].