TMPath - Revisited



Dmitry Bogachev

2004-01-16

Copyright @ 2004 Dmitry Bogachev



Abstract

TMPath is an experimental XPath-like language for addressing parts of Topic Maps. It can form part of query, transformation and constraint languages for Topic Maps. This document describes revisited version of TMPath. Current version is in better concord with the Topic Map Data Model (TMDM) [1]. This version of the TMPath has also been optimized as a navigational mechanism for the Topic Maps Query. language.

Table of contents





Introduction

The primary purpose of TMPath is to address Topic Map constructs in a context without explicitly naming such constructs. TMPath uses “/”- path metaphor for addressing parts of Topic Maps, which is similar to well-known conventions used in URI addressing, file system element addressing, and XML elements addressing in XPath.

TMPath can be regarded, to some extent, as a derivative from XPath 2.0. TMPath relies on a different data model, but it reuses many of XPath 2.0 ideas, syntax constructs and functions.

TMPath relies on Topic Map Data Model. TMDM dependency is expressed through TMPath State Diagram. TMPath is not dependent on any Topic Maps serialization syntax.

TMPath uses the concept of “predicates”. Predicate can be applied to a set of elements represented by a path to select a subset of these elements based on defined criteria.

TMPath uses the concept of “types” in Topic Maps. Types play important role in filtering topic map items.

TMPath also enables representing such constructs as “every topic such as” or “some topic such as”. This feature makes it possible to use TMPath as the “selector” part of TMQL and TMCL statements.



TMPath Main State Diagram

TMPath State Diagram explains correlations and possible path steps among various nodes.



Nodes:




TMPath steps:

[1] /topic<Predicate>

[2] /association<Predicate>

[3] <Topic>/roleOf::<roleTypeExpr><Predicate>

[4] <Association>/role::<roleTypeExpr><Predicate>

[5] <Topic>/roleOf<Predicate>

[6] <AssociationRole>/association<Predicate>

[7] <Association>/role<Predicate>

[8] <AssociationRole>/topic<Predicate>

[9] <Topic>/baseName<Predicate>

[10] <TopicName>/value<Predicate>

[11] <Topic>/baseName::<topicNameTypeExpr ><Predicate>

[12] <Topic>/occurrence<Predicate>

[13] <Occurrence>/value<Predicate>

[14] <Topic>/occurrence::<occurrenceTypeExpr><Predicate>


Abbreviations:

/association -> /a

/topic -> /t

/baseName -> /bn

/occurrence -> /oc


Topic map node is implicit in the first two expressions. It is provided by a query context or environment.


Examples:

Note: [] - defines predicate; simple form of predicate “filtering by type” is used in the following examples

1. /topic[person]

2. /association[born-In]

3.1 /topic[person]/roleOf[who]/association[bornIn]/role[where]/topic[city]

3.2 /topic[person]/roleOf::who[bornIn]/role::where[city]

4.1 /topic/bn[displayName | shortName]/value

4.2 /topic/bn::(displayName | shortName)

5.1 /topic/oc[webPage]/value

5.2 /topic/oc::webPage


Additional TMPath Steps

TMPath provides access to additional Topic Map constructs, such as reifiers and type-topics, through a predefined set of steps.


TMPath defines the following parent step for any node:

Parent step has a dynamic nature and is relative to TMPath expression execution context. Abbreviation “..” can be used in lieu of a parent step within predicate constructs.


TMPath defines the following additional step for a topic map node:


TMPath defines the following additional steps for topics:

Example:

/topic[*; bn::*=’John Smith’]/type - provides access to the explicitly declared types of the topics where one of the base names is ‘John Smith’



TMPath defines the following additional steps for associations:



Examples:

/association/type - provides access to all association types

$a/type - provides access to the type of the association specified by variable $a

/association[bornIn]/reifier - gives access to the topics which reify associations of the type bornIn

/topic[person]/roleOf::who[bornIn]/reifier - gives access to the topics which reify the “bornIn” association for topics of the type ‘person’

/topic[person]/roleOf::*[*]/type - provides access to all types of any association where a person plays a role;

$johnSmith/roleOf::*[*]/type - provides access to all types of any association where the topic represented by variable $johnSmith plays a role.



TMPath defines the following additional steps for an association role:



Example:

/topic[person]/roleOf/type - provides access to all role types of the topics of the ‘person’ type.



TMPath defines following additional steps for occurrences:



Examples:

/topic[person]/occurrence/type - provides access to all types of all occurrences for any person;

/topic[person]/occurrence[webPage]/resource - gives access to the topic which represents the resource which is the occurrence of the “webPage” type within any “person”;

/topic[person]/occurrence[webPage]/reifier - gives access to the topic which reifies the fact of being an occurrence of the “webPage” type.



TMPath defines the following additional steps for base names:


Examples:

/topic[person]/baseName/type - provides access to all types of all baseNames of any person;

/topic[person]/baseName[webPage]/reifier - gives access to the topics which reify the fact of being the baseName of type “webPage” for any person.



User Defined Path Steps

TMPath enables user defined path steps. The following syntax should be used to call a user defined path steps:


User defined TMPath steps always have default implicit parameters “current context” and “scope”
and can have additional parameters.



Example:


"hasManager” path step can be defined as following:

define step hasManager()

{

$$self/roleOf::who@$$scope[is-manager-of]/role::manager

}


"hasManager” can be used to construct a path:


/topic[person; bn::*=”John Smith”]/hasManager/bn::fullName



Predicates

Predicates have the following syntax:

<TMPath Step>[predicates]

Predicates enable selection of a subset of nodes extracted by a simple path. In more general case predicates can be applied to any sequence to select subset of nodes/values.



Simple predicates

Simple predicates are constructed as value1 operator value2 where:



Operators:

=

/topic[person; bn::fullName=roleOf::who[hasManager]/role::manager/bn::fullName]

!=

/topic[person; roleOf::who[hasFavoritePerlBook]/role::book/bn::name!=perlInANutshell]

>

/topic[person; oc::age>roleOf::who[hasManager]/role::manager/oc::age]

>=

/topic[person; oc::age>roleOf::who[hasManager]/role::manager/oc::age]

<

/topic[person; oc::age< roleOf::who[hasManager]/role::manager/oc::age]

<=

/topic[person; oc::age<=roleOf::who[hasManager]/role::manager/oc::age]


Table 1.



Note that TMPath expressions in predicate have “existence” quantification by default. For example, expression

[oc::age> roleOf::who[hasManager]/role::manager/oc::age]

is “true”, if in the current context there is “age” value which is greater than the value of the “age of some manager”.

By default, “existence” quantifier has a limited “operator” of “function call” scope. For example, if we have predicate such as

[Expr1 = Expr2 or Expr1 = Expr3], Expr1 values which satisfy first condition (Expr1 = Expr2) can be different from Expr1 values which satisfy second condition (Expr1 = Expr3). The same substitution can be done by using Quantified Expressions.



Compound predicates

Simple predicates can be combined using logical operators (“and”, “or”, not() ) into compound predicates.

Example:

/topic[person; oc::age>20 and roleOf::who[bornIn]/role::place/bn::*='Paris']



Functions in predicates

TMPath has an extensive set of functions for manipulating data of various types. TMPath also includes built-in predicates.

Some useful functions:

contains(string, substring) - tests if the “string” contains the “substring”.





Type Expressions

Type expressions allow selection of a subset of topic map constructs based on the type information.

There are two constructs in which type expressions can be used:

TPath has a special form of a predicate to simplify filtering by a topic type: [<TypeExpression>;<OtherConditions>]

Type expression in predicate can include:



Examples:

/topic[perlHacker, pythonHacker] - "and" operator

/topic[perlHacker | pythonHacker] - "or" operator

/topic[perlHacker, ~pythonHacker] - "and" combined with "not" operator



TMPath engine automatically calculates super types for each topic based on the subtype-supertype associations included in the topic map. Calculations are based on “what is known now about type instances”.



Type expression in a ‘shortcut’ path step defines the type of relationship between nodes. ‘Shortcut’ path step can have allows only the following simplified constructs:



Examples:

/topic[person; bn::fullName=”John Smith”] - ‘fullName’ is a base name type;

/topic[person; bn::*=”John Smith”] – ‘*’ represents base name of any type;

/topic[person; contains(bn::(fulleName | shortName),"John")] - (fulleName | shortName) is a type expression with “|” operator

/topic[person; oc::webPage=”http://www...._john_smith_....com”] - ‘webPage’ is an occurrence type.



Index Selector

<TMPath Step>[indexExpr]


Example:

/topic[person][*; bn:*=”John Smith”][1]



Variables

TMPath expressions can use variables. The value of a variable can be a topic map, a topic, an association, an occurrence, a base name, an association role or a data value. A variable can be the starting point in a TMPath expression. A variable can be used in place of a Type Expression. Variables can be used in predicate constructs.

Examples:

$tm/topic[person]

$a/role::who

/topic[person; bn::*=”JohnSmith” and roleOf::$RoleType[$AssociationType]]

/topic[person; contains(bn::lastName,$name)]



Quantified Expressions

Quantified expressions are used to specify conditions which are 'true' for some or for all nodes/values from a sequence. Quantified expressions also support “nested quantifiers” such as “for each $x exists $y such that ...”.

Example:

every $x in /topic[person] satisfies some $y in /topic[person] satisfies $x/roleOf::who[hasManager]/role::manager = $y

It is possible to use several variables with the same quantifier.

Example:

some $x in /topic[perlHacker], $y in /topic[perlBook] satisfies $x/roleOf::who[hasFavoritePerlBook]/role::book= $y



Conditional Expressions

TMPath allows use of conditional expressions. Each conditional expression has the following syntax:

if(<Expr1>) then <Expr2> else <Expr3>



“For” Expressions

“For” expression has the following syntax:

for <Variable> in <Expr1> return <Expr2>

The result of this expression is a sequence of the results produced by <Expr2> for each value from <Expr1>



Reference to a Specific Topic

A topic can be referenced by its subject indicator.


Example:

/topic[*; sid=”http://www.somewhere.com/people/#JohnSmith”]


A topic can be referenced by its source locator.

Example:

/topic[*; srcl=”http://www.somewhere.com/people.xtm#JohnSmith”]


A topic can also be found by its address if this topic represents a resource on the Web.


Example:

/topic[*; subl=”http://www.somewhere.com/Resume/JohnSmith.htm”]


However, these basic methods are not “user friendly”. It is practically impossible to use them for constructing even simple queries. TMPath has a special ‘shortcut’ form based on the concept of Topic IDs.


Topic ID is a fragment part appended to the base URI which specifies a set of Subject Indicators or Subject Locators.


TMPath enables declarations of default and additional namespaces which connect IDs with base URIs. Such declarations also introduce prefixes which, combined with a topic ID, can be used for referencing topics.


Namespace can be defined in a query context or hosting language (such as topic map query language).


Examples:


declare default sourceLocator namespace="http://www........com/defaultfile.xtm#"

declare default subjectIndicator namespace="http://psi........com/defaultpsi/#"


Only one default namespace can be declared.


declare sourceLocator namespace p=”http://www.somewhere.com/people.xtm#"


declare subjectIndicator namespace b=http://psi.somewhere.com/people/#"


TMPath engine extends topic IDs to full source locators or subject indicators before evaluating query. Query evaluation relies on “full” URIs.


Examples:

/topic[p:person]

“p:person” is in fact abbreviation of

/topic[*; srcl=”http://www.somewhere.com/people.xtm#person”]


/topic[b:person]


“b:person” is an abbreviation of

/topic[*; sid=”http://www.somewhere.com/people/#person”]


p:JohnSmith/age


p:JohnSmith is an abbreviation of

/topic[*; srcl=”http://www.somewhere.com/people.xtm#JohnSmith”]

 

A variable is often used to reference a topic or an association.


Example:

for $author in /topic[p:author; p:bornIn=c:LondonUK]

return $author/bn::p:fullName



Additional Path Step Simplifications

Since in many topic maps there is no overlapping between types of roles, base names and occurrences, it is possible to omit path prefix like “role::”, “roleOf::”, “oc::” and “bn::” in such cases.



Example:

/topic[p:person; p:fullName="John Smith" and p:author[p:is-author-of]/p:opus and not(p:webpage)]


or with a default namespace :


/topic[person; fullName="John Smith" and author[is-author-of]/opus and not(webpage)]



If TMPath engine cannot resolve “::” path prefix it generates a run time error.

Transitive Associations

TMPath has “//” operator which helps to query transitive associations. The “//”operator can be used as follows:



“//” operator provides access to all nodes that can be produced by applying the path step recursively.

To use “//” operator for transitive associations it is necessary to define a binary projection and specify it as “transitive”.

TMPath supports built-in “superType” and “subType” transitive steps.


Example:


$topic/type//superType – provides access to the types where the topic is a direct or indirect instance by using subtype-supertype associations;


$type//subtype/instance - provides access to instances of a type-topic declared in the topic map directly or indirectly by using subtype-supertype associations;


Example:

[transitive=true]

define step isPartOfLocation ()

{

$$self/roleOf::subloc@$$scope[is-part-of-location]/role::superloc

}


/topic[person]

[every $place in favoritePlaceToVisit

satisfies(

$place[city] or

$place//isPartOfLocation[city]

)

]

Scope

Query context can have a scope attached to it. This scope is shared among all TMPath expressions running in the same context.

TMPath enables use of additional scopes for some topic map constructs as well, specifically:



In this case '@<scope expression>' can be added to a path step which leads to a node. In fact, scope expression works as a special kind of predicate which filters nodes based on the scope.

XTM 1.0 compatible form of <scope expression> can be presented as

Examples:

/topic[person]/bn@en[fullName]

/topic[person]/oc@en[webPage]

/a@humanOntology[bornIn; roleOf::who=JohnSmith]


Scope can also be used with 'shortcut' steps such as:

<Topic>/roleOf::<RoleTypeExpr>@<ScopeExpr><Predicate>

<Topic>/bn::<topicNameTypeExpr>@<ScopeExpr><Predicate>

<Topic>/oc::<occurrenceTypeExpr>@<ScopeExpr><Predicate>


Examples:

/topic[person]/roleOf::who@humanOntology[bornIn]

/topic[person; bn::fullName@en]

/topic[person; oc::webPage@en]



Scope can be used with user defined steps too. Scope is passed to a step definition as the implicit parameter $$scope.


Examples:

/topic[person; hasManager@humanWorkOntology]



Summary

This document is a revisited introduction to the concepts of TMPath - an experimental language derived from the main concepts of XPath and optimized for navigating Topic Maps. There are several important issues which have not been covered in this introduction and can be subject for further experiments and discussions.

Examples of such outstanding issues are:



Links

[1] Topic Maps - Data Model

[2] XML Path Language (XPath) Version 1.0 (W3C Recommendation)

[3] XML Path Language (XPath) Version 2.0 (W3C Working Draft)

[4] Jan Gylta. XTMPath, Manipulating Topic Map Data Structures.

[5] Lars Marius Garshol. Tolog. A topic map query language

[6] Robert Barta. AsTMa? Language Definition

[7] Dmitry Bogachev. TMPath Introduction

[8] Dmitry Bogachev Implementation of TMQL use cases with TMPath-TMQuery