TopicMap Query Language
TopicMap Query Language (TMQL) can be used to make queries on topicmaps. It provides a way of accessing parts of a topicmap based on various conditions. For example, you can query a topicmap to find all of the topics that are in an association.
Syntax
Between SELECT and WHERE
A TMQL query can be split into two parts. The first part identifies the things that will be returned as the results of the query. The second part describes the data that will be returned. This is most simply explained by looking at an example: SELECT topic x WHERE x in assoc
TMQL queries always start with the keyword SELECT. Between the keyword SELECT and the keyword WHERE, the return identities are defined. In the above example we are saying that a topic will be returned. We call it x so we can identify that specific topic in the second part of the query. More than one return identity can be defined between the SELECT and the WHERE keywords but each identity must have a unique name and only one type, e.g. SELECT topic x assoc y WHERE x in y The types that TMQL supports are topics, associations, association templates, roles, association ends, scopes, names, strings, occurrences, resources, locators and identities. The typing keywords used to represent these are (respectively) topic, assoc, assoctemp, role, end, scope, name, string, occurrence, resource, locator and id.
It is not necessary to have a WHERE keyword in a TMQL query. For example: SELECT topic x is a valid query.
When the WHERE keyword is omitted the query returns all items of the specified type or types in the topicmap. In the above example all topics are returned.
Note that the keywords in TMQL are not case sensitive. However, identifiers that you define, are.
To the right of WHERE
The data that will be returned from the query is described from the keyword WHERE to the end of the query. In the first example above, we define that we want x to be all of the topics that are in associations. We know that x is of type topic from the information between SELECT and WHERE. The typing keyword 'assoc' refers to all associations, we do not bother to give it an identifier as we are not interested in returning the values that it contains. The keyword 'in' when used between something of type 'topic' something of type 'assoc' means that the topic or topics play roles in the association or associations.
The right hand side of the query can consist of many conditions chained together. For example, if we wanted all of the topics that played a role in an association and that association's template is called "myTemplate" we could make a query something like this:
SELECT topic x WHERE x in assoc template_is assoctemp named
"myTemplate"
Notice that we have used the value "myTemplate" in the last query. This is called a literal. Its actual type is 'string' but as we know that the keyword named can only take a literal of type 'string' on its right hand side, the type is inferred. There are three literal types: 'string', 'locator' and 'id'. Whereever possible TMQL will infer their type but in ambiguous situations you may need to type the literal yourself. For example:
SELECT topic x WHERE topic has occurrence has locator 'http://www.mysite.co.uk'
In this case 'occurrence has' can take an id or a locator so we have to specify which it is.
Brackets - part 1
Non-typing keywords (such as 'in' and 'named' in the above example) always act on the things immediately to their left and right in the query. They cannot act on anything that is outside their grammar semantics. We can use brackets to group together parts to make more complex queries possible. If, for example, we wanted to get all of the topics called "Spielberg" that are in a role called "Director" we CANNOT do the following query:
SELECT
topic x WHERE x named "Spielberg" in role named
"Director"
The above query is invalid because the keyword 'in' cannot take a string on its left hand side. By using brackets we can produce a valid query that will return what we want:
SELECT topic x WHERE (x named
"Spielberg") in role named "Director"
The keyword to the right of the brackets considers everything within the brackets to be the first type contained. In the example the first type in the brackets is a topic so the keyword 'in' after the brackets uses this for evaluation purposes.
AND and OR keywords
The AND and OR keywords make it possible to do more complex queries. For example, say we wanted to get all of the topics that are called "Bill" and all the topics that are called "Ben". Using only the TMQL described thus far, it is impossible to do in a single query. However, using OR we can make the following query:
SELECT topic plantPot WHERE plantPot named "Ben" OR plantPot
named "Bill"
This means that the results of the query will contain all of the topics called "Ben" as well as all of the topics called "Bill".
If we wanted to get all of the associations that "Bill" and "Ben" play roles in, we could use the AND keyword. For example:
SELECT assoc a WHERE a has topic named "Ben" AND a has topic named
"Bill"
When evaluating queries, any AND keywords are evaluated before any OR keywords. So a query like
SELECT topic x WHERE x named "Bill" OR x named "Ben" AND x in
assoc
will find all the topics that are named "Ben" that are in associations plus all the topics that are just named "Bill".
Brackets - part 2
Brackets can also be used to group AND and OR statements. Things in brackets get evaluated before anything else. If, in the previous example, we wanted to find all of the topics that were named either "Bill" or "Ben" and that were in associations, the query would look like this:
SELECT
topic x WHERE (x named "Ben" OR x named "Bill") AND x in
assoc
NOT keyword
The NOT keyword can be used to return things that are not true. For example, if we want all of the topics in a map that are not in an association, we could say:
SELECT topic x WHERE NOT x in
assoc
It is possible to use NOT in complex statements as well. For example:
SELECT topic x WHERE NOT (x named "Bill" OR x named "Ben")
AND x in assoc
will find all of the topics that aren't called either "Bill" or "Ben" and that are in an association.
Defining more identifiers
It is perfectly valid to define more identifiers to the right hand side of the WHERE keyword. This allows more complex queries and checking within queries. For example, if we want all of the topics that are in associations that the topic named "Ben" is in, we can do the query:
SELECT topic x
WHERE x in assoc has topic y named "Ben" AND NOT x = y
If we do not specify that x should not equal y then the topic called "Ben" will be returned as "Ben" in an association that has a topic named "Ben".
Regular Expressions
TMQL provides support for matching strings with regular expressions. This means that you can search for string names, locators or ids that match a particular pattern rather than having to match them exactly. The use of regular expressions in TMQL is indicated by the 'like' keyword directly preceding the literal itself. For example, to match all topics whose unconstrained names begin with A:
SELECT topic x WHERE x named like "^A"
The "^A" is the regular expression. If the type of the literal has to be specified, then the query would look like this:
SELECT topic x WHERE x named string like "^A"
The expressions available in regular expressions in TMQL are outlined in the documentation for theJakarta-RegExp package.
TMQL Grammar
This section describes the entire grammar of TMQL. For convenience, the descriptions in the table assume that there is one value in the left hand type and therefore describe what will be put into the right hand type. During actual TMQL evaluations there may be one or many values in both, either or neither type. The descriptions of these scenarios have been omitted to try and keep this table manageable.
Type on leftKeywordType on right Description
topicnamednameAll of the names of the topic.
topicnamedstringThe string form of the topic's name in the unconstrained scope.
topicinassoctempAll of the associations where the topic plays a role in one or more of the association template's child associations.
topicinassocAll of the associations that the topic plays a role in.
topicinendAll of the association ends that the topic plays a role in.
topicinroleAll of the roles that the topic plays.
topicinscopeAll of the scope sets that the topic is in.
topichasoccurrenceAll occurrences of the topic.
topichasidAll identities of the topic.
topic=topic
topic=role
topicoccursresourceAll of the resource topics that are in occurrences of the topic.
topicoccurslocatorAll of the URIs that define the occurrence locations for the topic.
topicinstance_oftopicAll of the topics that are instances of the topic.
topicinstance_ofassoctempAll of the association templates that are instances of the topic.
topicinstance_ofassocAll of the associations that are instances of the topic.
topicinstance_ofroleAll of the roles that are instances of the topic.
topicinstance_ofoccurrenceAll of the occurrences that are instances of the topic.
topicinstance_istopicAll of the topics of which the topic is an instance.
topicinstance_isassoctempAll of the association templates of which the topic is an instance.
topicinstance_isassocAll of the associations of which the topic is an instance.
topicinstance_isroleAll of the roles of which the topic is an instance.
topicinstance_isoccurrenceAll of the occurrences of which the topic is an instance.
topicsubclass_oftopicAll of the topics that are direct subclasses of the topic.
topicsubclass_ofassoctempAll of the association templates that are direct subclasses of the topic.
topicsubclass_ofassocAll of the associations that are direct subclasses of the topic.
topicsubclass_ofroleAll of the roles that are direct subclasses of the topic.
topicsubclass_ofoccurrenceAll of the occurrences that are direct subclasses of the topic.
topicsuperclass_oftopicAll of the topics that are direct superclasses of the topic.
topicsuperclass_ofassoctempAll of the association templates that are direct superclasses of the topic.
topicsuperclass_ofassocAll of the associations that are direct superclasses of the topic.
topicsuperclass_ofroleAll of the roles that are direct superclasses of the topic.
topicsuperclass_ofoccurrenceAll of the occurrences that are direct superclasses of the topic.
assoctempnamednameAll of the names of the association template.
assoctempnamedstringThe string form of the association template's name in the unconstrained scope.
assoctempinassocAll of the associations that the association template plays a role in.
assoctempinendAll of the association ends that the association template plays a role in.
assoctempinroleAll of the roles that the association template plays.
assoctempinscopeAll of the scope sets that the association template is in.
assoctemphastopicAll of the topics that plays roles in the associations that are templated by the association template.
assoctemphasroleThe roles that are defined in the association template.
assoctemphasendThe ends of the association template.
assoctemphasoccurrenceAll occurrences of the association template.
assoctemphasidAll identities of the association template.
assoctemp=assoctemp
assoctempoccursresourceAll of the resource topics that are in occurrences of the association template.
assoctempoccurslocatorAll of the URI's that define the occurrence locations for the association template.
assoctemptemplate_ofassocThe associations that the association template is the template for.
assoctempinstance_oftopicAll of the topics that are instances of the association template.
assoctempinstance_ofassoctempAll of the association templates that are instances of the association template.
assoctempinstance_ofassocAll of the associations that are instances of the association template.
assoctempinstance_ofroleAll of the roles that are instances of the association template.
assoctempinstance_ofoccurrenceAll of the occurrences that are instances of the association template.
assoctempinstance_istopicAll of the topics of which the association template is an instance.
assoctempinstance_isassoctempAll of the association templates of which the association template is an instance.
assoctempinstance_isassocAll of the associations of which the association template is an instance.
assoctempinstance_isroleAll of the roles of which the association template is an instance.
assoctempinstance_isoccurrenceAll of the occurrences of which the association template is an instance.
assoctempsubclass_oftopicAll of the topics that are direct subclasses of the association template.
assoctempsubclass_ofassoctempAll of the association templates that are direct subclasses of the association template.
assoctempsubclass_ofassocAll of the associations that are direct subclasses of the association template.
assoctempsubclass_ofroleAll of the roles that are direct subclasses of the association template.
assoctempsubclass_ofoccurrenceAll of the occurrences that are direct subclasses of the association template.
assoctempsuperclass_oftopicAll of the topics that are direct superclasses of the association template.
assoctempsuperclass_ofassoctempAll of the association templates that are direct superclasses of the association template.
assoctempsuperclass_ofassocAll of the associations that are direct superclasses of the association template.
assoctempsuperclass_ofroleAll of the roles that are direct superclasses of the association template.
assoctempsuperclass_ofoccurrenceAll of the occurrences that are direct superclasses of the association template.
assoctempscoped_byscopeThe scope set that scopes the association template.
assocnamednameAll of the names of the association.
assocnamedstringThe string form of the association's name in the unconstrained scope.
associnassocAll of the associations that the association plays a role in.
associnendAll of the association ends that the association plays a role in.
associnroleAll of the roles that the association plays.
associnscopeAll of the scope sets that the association template is in.
assochastopicAll of the topics playing roles in the association.
assochasassoctempAll of the association templates playing roles in the associations.
assochasassocAll of the associations playing roles in the association.
assochasroleAll of the roles in the association.
assochasendAll of the ends in the association.
assochasoccurrenceAll occurrences of the association.
assochasidAll identities of the association.
assoc=assoc
assococcursresourceAll of the resource topics that are in occurrences of the association.
assococcurslocatorAll of the URI's that define the occurrence locations for the association.
assoctemplate_isassoctempThe template for the association.
associnstance_oftopicAll of the topics that are instances of the association.
associnstance_ofassoctempAll of the association templates that are instances of the association.
associnstance_ofassocAll of the associations that are instances of the association.
associnstance_ofroleAll of the roles that are instances of the association.
associnstance_ofoccurrenceAll of the occurrences that are instances of the association.
associnstance_istopicAll of the topics of which the association is an instance.
associnstance_isassoctempAll of the association templates of which the association is an instance.
associnstance_isassocAll of the associations of which the association is an instance.
associnstance_isroleAll of the roles of which the association is an instance.
associnstance_isoccurrenceAll of the occurrences of which the association is an instance.
assocsubclass_oftopicAll of the topics that are direct subclasses of the association.
assocsubclass_ofassoctempAll of the association templates that are direct subclasses of the association.
assocsubclass_ofassocAll of the associations that are direct subclasses of the association.
assocsubclass_ofroleAll of the roles that are direct subclasses of the association.
assocsubclass_ofoccurrenceAll of the occurrences that are direct subclasses of the association.
assocsuperclass_oftopicAll of the topics that are direct superclasses of the association.
assocsuperclass_ofassoctempAll of the association templates that are direct superclasses of the association.
assocsuperclass_ofassocAll of the associations that are direct superclasses of the association.
assocsuperclass_ofroleAll of the roles that are direct superclasses of the association.
assocsuperclass_ofoccurrenceAll of the occurrences that are direct superclasses of the association.
assocscoped_byscopeThe scope set that scopes the association.
rolenamednameAll of the names of the role.
rolenamedstringThe string form of the role's name in the unconstrained scope.
roleinassoctempThe association templates for which the role defines a role.
roleinassocThe associations for which the role defines a role.
roleinendThe ends in which the role defines a role.
roleinroleThe roles the role plays.
roleinscopeThe scope sets that the role is in.
rolehastopicThe topics that play the role.
rolehasassoctempThe association templates that play the role.
rolehasassocThe associations that play the role.
rolehasroleThe roles that play the role.
rolehasoccurrenceThe occurrences of the role.
rolehasidThe identities of the role.
role=role
role=topic
roleoccursresourceAll of the resource topics that are in occurrences of the role.
roleoccurslocatorAll of the URI's that define the occurrence locations for the role.
roleinstance_oftopicAll of the topics that are instances of the role.
roleinstance_ofassoctempAll of the association templates that are instances of the role.
roleinstance_ofassocAll of the associations that are instances of the role.
roleinstance_ofroleAll of the roles that are instances of the role.
roleinstance_ofoccurrenceAll of the occurrences that are instances of the role.
roleinstance_istopicAll of the topics of which the role is an instance.
roleinstance_isassoctempAll of the association templates of which the role is an instance.
roleinstance_isassocAll of the associations of which the role is an instance.
roleinstance_isroleAll of the roles of which the role is an instance.
roleinstance_isoccurrenceAll of the occurrences of which the role is an instance.
rolesubclass_oftopicAll of the topics that are direct subclasses of the role.
rolesubclass_ofassoctempAll of the association templates that are direct subclasses of the role.
rolesubclass_ofassocAll of the associations that are direct subclasses of the role.
rolesubclass_ofroleAll of the roles that are direct subclasses of the role.
rolesubclass_ofoccurrenceAll of the occurrences that are direct subclasses of the role.
rolesuperclass_oftopicAll of the topics that are direct superclasses of the role.
rolesuperclass_ofassoctempAll of the association templates that are direct superclasses of the role.
rolesuperclass_ofassocAll of the associations that are direct superclasses of the role.
rolesuperclass_ofroleAll of the roles that are direct superclasses of the role.
rolesuperclass_ofoccurrenceAll of the occurrences that are direct superclasses of the role.
endinassoctempThe association template that the end is in.
endinassocThe associations that the end is in.
endhastopicThe topic that play the role in the end.
endhasassoctempThe association template that plays the role in the end.
endhasassocThe association that plays the role in the end.
endhasroleThe role that plays the role in the end.
endhasoccurrenceThe occurrence that plays the role in the end.
end=end
scopehastopicThe topics that make up the scope.
scopehasassoctempThe association templates that make up the scope.
scopehasassocThe associations that make up the scope.
scopehasroleThe roles that make up the scope.
scopehasoccurrenceThe occurrences that make up the scope.
scope=scope
scopescopesassoctempThe association templates that the scope scopes.
scopescopesassocThe associations that the scope scopes.
scopescopesnameThe names that the scope scopes.
scopescopesoccurrenceThe occurrences that the scope scopes.
name=name
nameisstringThe string value of the name.
namescoped_byscopeThe scope set that scopes the name.
namenamestopicThe topics which the name is a name of.
namenamesassoctempThe association templates which the name is a name of.
namenamesassocThe associations which the name is a name of.
namenamesroleThe roles which the name is a name of.
namenamesoccurrenceThe occurrences which the name is a name of.
string=string
stringisnameThe names that the string is the value of.
stringnamestopicThe topics which the string is the value of a name of (in the unconstrained scope).
stringnamesassoctempThe association templates which the string is the value of a name of (in the unconstrained scope).
stringnamesassocThe associations which the string is the value of a name of (in the unconstrained scope).
stringnamesroleThe roles which the string is the value of a name of (in the unconstrained scope).
stringnamesoccurrenceThe occurrences which the string is the value of a name of (in the unconstrained scope).
occurrencenamednameAll of the names of the occurrence.
occurrencenamedstringThe string form of the occurrence's name in the unconstrained scope.
occurrenceinassocAll of the associations that the occurrence plays a role in.
occurrenceinendAll of the association ends that the occurrence plays a role in.
occurrenceinroleAll of the roles that the occurrence plays.
occurrenceinscopeAll of the scopes that the occurrence is in.
occurrencehasresourceThe resource topics that are in the occurrence.
occurrencehaslocatorThe URI locators of the resource topics that are in the occurrence.
occurrencehasidThe identities of the occurrence.
occurrence=occurrence
occurrenceoccursresourceAll of the resource topics that are in occurrences of the occurrence.
occurrenceoccurslocatorAll of the URI's that define the occurrence locations for the occurrence.
occurrenceinstance_oftopicAll of the topics that are instances of the occurrence.
occurrenceinstance_ofassoctempAll of the association templates that are instances of the occurrence.
occurrenceinstance_ofassocAll of the associations that are instances of the occurrence.
occurrenceinstance_ofroleAll of the roles that are instances of the occurrence.
occurrenceinstance_ofoccurrenceAll of the occurrences that are instances of the occurrence.
occurrenceinstance_istopicAll of the topics of which the occurrence is an instance.
occurrenceinstance_isassoctempAll of the association templates of which the occurrence is an instance.
occurrenceinstance_isassocAll of the associations of which the occurrence is an instance.
occurrenceinstance_isroleAll of the roles of which the occurrence is an instance.
occurrenceinstance_isoccurrenceAll of the occurrences of which the occurrence is an instance.
occurrencesubclass_oftopicAll of the topics that are direct subclasses of the occurrence.
occurrencesubclass_ofassoctempAll of the association templates that are direct subclasses of the occurrence.
occurrencesubclass_ofassocAll of the associations that are direct subclasses of the occurrence.
occurrencesubclass_ofroleAll of the roles that are direct subclasses of the occurrence.
occurrencesubclass_ofoccurrenceAll of the occurrences that are direct subclasses of the occurrence.
occurrencesuperclass_oftopicAll of the topics that are direct superclasses of the occurrence.
occurrencesuperclass_ofassoctempAll of the association templates that are direct superclasses of the occurrence.
occurrencesuperclass_ofassocAll of the associations that are direct superclasses of the occurrence.
occurrencesuperclass_ofroleAll of the roles that are direct superclasses of the occurrence.
occurrencesuperclass_ofoccurrenceAll of the occurrences that are direct superclasses of the occurrence.
occurrenceoftopicThe topic that the occurrence is of.
occurrenceofassoctempThe association template that the occurrence is of.
occurrenceofassocThe association that the occurrence is of.
occurrenceofroleThe role that the occurrence is of.
occurrenceofoccurrenceThe occurrence that the occurrence is of.
occurrencescoped_byscopeThe scope set that scopes the occurrence.
resourcehaslocatorThe URI locator of the resource.
resource=resource
resourceoftopicThe topic in the same occurrence as the resource.
resourceofassoctempThe association template in the same occurrence as the resource.
resourceofassocThe association in the same occurrence as the resource.
resourceofroleThe role in the same occurrence as the resource.
resourceofoccurrenceThe occurrence in the same occurrence as the resource.
locator=locator
locatoroftopicThe topics that the locator is the URI for.
locatorofassoctempThe association templates that the locator is the URI for.
locatorofassocThe associations that the locator is the URI for.
locatorofroleThe roles that the locator is the URI for.
locatorofoccurrenceThe occurrences that the locator is the URI for.
locatorofresourceThe resources that contain the locator.
id=id
idoftopicThe topics that the id identifies.
idofassoctempThe association templates that the id identifies.
idofassocThe associations that the id identifies.
idofroleThe roles that the id identifies.
idofoccurrenceThe occurrences that the id identifies.