TMPath - Introduction



Dmitry Bogachev

Copyright @ 2003 Dmitry Bogachev

Revisited version is available: TMPath Revisited 

Abstract

TMPath is an experimental language for addressing parts of Topic Maps. It can form part of query, transformation and constraint languages for Topic Maps. This document highlights the author's initial concepts of an XPath-like language optimized for manipulating Topic Maps. TMPath development has been inspired by the discussions on TMCL and TMQL working groups, and other language artifacts, such as XPath [1,2], XTMPath [3], Tolog [4] and AsTMa? [5].



Table of contents

·        Introduction

·        TMPath State Diagram

·        Functional TMPath Steps

·        Predefined Functional Steps

·        Type Expressions

·        Predicates

·        TMPath Abbreviations

·        Index Selector

·        Variables

·        Quantified Expressions

·        Conditional Expressions

·        “For” Expressions

·        Reference to a Specific Topic

·        Transitive Associations

·        Summary

·        Links



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 a 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 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 uses an abstract Topic Maps data model based on the mapping expressed in the 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 and select a subset of these elements based on certain criteria.

TMPath uses “axes” as a tool to describe various directions that can be used to construct a path. Axes reflect the Topic Maps data model.

TMPath uses the concept of “types” in Topic Maps. Types play important role in TMPath navigation and provide functionality equivalent to elements in XPath. 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 State Diagram

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





Nodes:

·         Topic Map

·         Topic

·         Base Name

·         Occurrence

·         Association


TMPath steps:

[1]      /topic::<topicTypeExpr>

[2]      /association::<associationTypeExpr >

[3]      <Topic>/association::<associationTypeExpr>

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

[5]      <Topic>/baseName::<baseNameTypeExpr>

[6]      <Topic>/occurrence::<OccurrenceTypeExpr>


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


Examples:

/topic::city

/topic::(city or town)

/association::bornIn

/topic::city/baseName::name

/topic::city/occurrence::webPage



Functional TMPath Steps

TMPath enables functional steps. They use the following syntax:

·         /function(arguments)

Functions used in TMPath steps always have a default implicit parameter “current context” and can have additional parameters. Brackets “( )” are mandatory.


Example:

/topic::person[baseName::*=johnSmith]/hasManager()/baseName::fullName



Predefined Functional Steps

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


Parent TMPath functional step

·         <TMPath Expr>/parent()

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


TMPath defines the following function for a topic map node:

·         /reifier() - provides access to the topic which reifies the topic map in a given context.


TMPath defines following functions for topics:

·         subjectIndicator() – provides access to topic subject indicators;

·         resourceAddress() – provides access to topic addresses (in case the topic represents a resource) ;

·         type() - provides access to the types where the topic is an instance;

·         fact() - if a topic has an association as the parent and plays a role in this association, this function provides access to the topics that reify the fact of playing a role by the original topic;

·         roleType() - if a topic has an association as the parent and plays a role in this association, this function provides access to the topics that represent the type of the role which the original topic plays in the parent association.


Examples:

/topic::*[baseName::*=johnSmith]/type() - provides access to the types of the topics where one of the base names is 'johnSmith'

/association::bornIn/role::who/fact() - provides access to the topics which reify the facts of playing the role of “who” in the association “bornIn”

/association::$a/role::who/fact() - provides access to the topics which reify the fact of playing the role of “who” in the association represented by variable $a

/association::$a/role::*/roleType() - provides access to the topics which represent the role type

for $player in $a/role::*[. != $johnSmith] return a$/$player/roleType()/name + “:” + $player/fullName


TMPath defines the following functions for associations:

·         type() - provides access to the association type;

·         reifier() - provides access to the topic which reifies an association;

·         roleType() - if the parent is a topic, this function provides access to the types of the role which the parent topic plays in the association;

·         fact() - if the parent is a topic which plays certain role(s) in the association, this function provides access to the topic which reifies the assertion of playing the role by the parent topic.


Examples:

/association::*/type() - provides access to all association types

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

/association::bornIn/reifier() - gives access to topics which reify associations of type bornIn

/topic::person/association::bornIn[role::who=..]/reifier() - gives access to the topic which reifies the “bornIn” association

/topic::person/association::bornIn/roleType() - provides access to the role types which “person” plays in the “bornIn” association.

/topic::person/association::bornIn[role::who=..]/fact() - gives access to the topics which reify the assertion of playing the “who” role by the parent topic

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

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


TMPath defines following functions for occurrences:

·         type() - provides access to the occurrence type;

·         resource() - provides access to the topic which represents the resource referred to by the occurrence;

·         fact() - provides access to the topics which reify the fact of being an occurrence.


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/fact() - gives access to the topic which reifies the fact of being an occurrence of the “webPage” type.


TMPath defines the following functions for the baseName:

·         type() - provides access to the baseName type;

·         fact() - provides access to the topic which reifies the fact of being the baseName.


Examples:

/topic::person/baseName::*:type() - provides access to all types of all baseNames which at least one person has as a baseName

/topic::person/baseName::webPage/fact() - gives access to the topic which reifies the fact of being the baseName of type “webPage”.


Type Expressions

Type expressions are used in axes to select a subset of topic map constructs based on the type information.

Type expression can include:

·         type name;

·         variable bound to the topic which represents the type of a topic, an association, an occurrence or a role;

·         type names and variables combined by logical operators “and”, “or”, “not”;

·         variables bound to specific topics or associations;

·         ”*” element which represents “any” type.



TMPath engine calculates automatically 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.



Predicates

Predicates have the following syntax: <TMPath Step>[predicates]

Predicates enable selection of a subset of nodes extracted by application of a simple path.


Simple predicates

Simple predicates are constructed as value1 operator value2 where:

·         value1 and value2 are TMPath expressions

·         operator is one of the operators listed in Table 1 (this list is not final, can be changed)


Operators

Examples

=

/person[fullName=hasManager[who=..]/manager/fullName]

!=

/person[hasFavoritePerlBook[who=..]/book/name!=perlInANutshell]

>

/person[age>hasManager[who=..]/manager/age]

>=

/person[age>=hasManager[who=..]/manager/age]

<

/person[age<hasManager[who=..]/manager/age]

<=

/person[age<=hasManager[who=..]/manager/age]

Table 1.


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

[occurrence::age>association::hasManager[role::who=..]/role::manager/occurrence::age]

is 'true', if in the current context there is $x, which is “age”, and there is $y, which is “age of some manager”, and $x>$y


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). If there is a need for same substitution in various parts of a compound predicate, Quantified Expressions should be used.


Compound predicates

Simple predicates can be combined using logical operators into compound predicates.

Example:

/topic::person[occurrence::age>20 and association::bornIn[role::who=..]/role::place/baseName::*='Paris']


Functions in predicates

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

Some useful functions:

hasType(type) – tests the current node for the type “type”;

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


TMPath Abbreviations


In many cases topic, association, role, baseName and occurrence axes can be omitted

Example

/topic::person[basename::*=johnSmith]/association::bornIn[role::who=..]/role::place/baseName::name

can be presented as

/person[basename::*=johnSmith]/bornIn[who=..]/place/name


[<name>] abbreviation

Predicate [baseName::*=name] can be represented using the following syntax: [name]

Example

/person[johnSmith]/bornIn[who=..]/place/name


[<type:name>] abbreviation

Predicate [hasType(type) and baseName::*=name] can be presented as [type:name]

Example

/person[johnSmith]/bornIn[who=..]/place[city:paris] or

/*[person:johnSmith]/bornIn[who=..]/place[city:paris]


The full form provides more flexibility, because it enables definition of a complex type expression. The second (abbreviated) form [type:name] has the limitation of one type name only


/association::<associationTypeExpr>[role::roleTypeExpr=..] abbreviation


Construct /association::<associationTypeExpr>[role::roleTypeExpr=..]

has standard abbreviation

/association::roleTypeExpr#<associationTypeExpr> or /roleTypeExpr#<associationTypeExpr>


Example

/person[johnSmith]/bornIn[who=..]/place[city:paris] can be presented as

/person[johnSmith]/who#bornIn/place[city:paris]


Simplified forms with “#” operator can also be used together with functional steps, for example:

/topic::person/association::who#bornIn/fact() - gives access to the topics which reify the facts of playing the “who” role in the “bornIn” association.

/topic::person/association::who#bornIn/reifier() - gives access to the topics which reify the association “bornIn” where the “person” plays the role “who”


/association::<associationTypeExpr>[role::roleTypeExpr=..]/role::roleTypeExpr abbreviation


Construct /association::<associationTypeExpr>[role::roleTypeExpr=..]/role::roleTypeExpr

has standard abbreviation

/topic::roleTypeExpr#<associationTypeExpr>#roleTypeExpr or /roleTypeExpr#<associationTypeExpr>#roleTypeExpr


Example

/topic::person[johnSmith]/bornIn[who=..]/place[city:paris] can be represented as

/topic::person[johnSmith]/topic::who#bornIn#place[city:paris] or

/person[johnSmith]/who#bornIn#place[city:paris]

/*[person:johnSmith]/who#bornIn#place[city:paris]


Index selector

<TMPath Step>[indexExpr]


Example

/topic::person[1]


Variables

Variables can be used in TMPath expressions. The value of a variable can be a topic, an association or a data value. A variable can be a 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:

$t/topic::who#bornIn#place

$a/role::who

/person[johnSmith]/association::$aType[role::$roleType=..]

/person[johnSmith]/association::$roleType#$aType

/person[contains(lastName,$name)]


There is a special case when a specific topic variable and an association variable can be connected by “/” operator.

Expressions $player/$a and $a/$player mean $player plays some role in the association represented by variable $a. Such a construct can be used with the functional steps to get access to the reified facts and types. Also, it is possible to specify the role which topic $player plays in such expressions.


Examples:

topic::$t/association::a$:roleType() - gives the type of the role which topic $t plays in association $a;

$t/$a/roleType()/defaultName - gives the default name of the type of the role(s) which specific topic $t plays in specific association $a;

topic::$t/association::$roleType#$a/fact() – gives access to the topic which reifies the fact of topic $t playing the role of the type $roleType in the particular association $a.


Quantified Expressions

Quantified expressions are used to specify conditions which are 'true' for some or for all nodes from some node set. 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/association::hasManager[role::who=..]/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/association::hasFavoritePerlBook[role::who=..]/role::book= $y


Conditional expressions

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

if(<Expr>) then <Expr> else <Expr>



“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

Reference to a specific topic in TMPath can be done via one of the topic names. The full form construct with “baseName” axis, or the short form constuct [<TopicName>], or the construct [<TypeName>:<TopicName>] can be adequately used to select a specific topic. TMPath engine always returns all topics with the specified name. If the name is not unique it is possible to use additional predicates to select a topic.


Examples:

/topic::person[johnSmith]

/person[johnSmith]

/topic::person[”John Smith”]

/person[”John Smith”][who#worksInDepartment#department[‘IT’]]


Other way to reference a specific topic is by using the “topic” function. This function should have a topic subject indicator as an argument. It returns a topic node.

Examples:

topic(”http://www.somewhere.com/people/JohnSmith”)/who#hasManager#manager

In general, the “topic” function argument is a TMPath expression which after calculations should result in a subject indicator. A variable is often used to reference a topic or an association.


Example:

for $author in /topic::author[topic::who#bornIn#place[city:londonUK]]

return $author/baseName::fullName


Transitive Associations

TMPath has “//” operator which is applicable for all transitive associations. The “//”operator can be used as follows:

·         <Topic>//topic::<roleTypeExpr>#<Association Type Expression>#<roleTypeExpr>



The “//” operator can also be used for non-transitive associations, but in this case it is equivalent to the “/” operator. For transitive associations “//” operator provides access to all nodes that can be produced by applying the association recursively.

Example:


/person["John Smith"]

       [every $place in who#favoritePlaceToVisite#place

        satisfies(

                   $place[hasType(city)] or

                   $place//subLocation#isPartOfLocation#location[hasType(city)]

                 )

       ]

Summary

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

Such topics can include:

·         representing and processing of Topic Map scopes;

·         naming conventions;

·         basic data types and functions;

·         set operations;

·         extension mechanisms;

·         possible integration with TMCL schema / type templates;

·         possible integration with/extension to TMQL;

·         possible integration with/extension to TMTL (Topic Map Template Language).



Links

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

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

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

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

[5] Robert Barta. AsTMa? Language Definition