Previous | Next

A CTM tutorial

Posted in Technology on 2008-08-31 15:02

Happy CTM users, Frognerparken, Oslo

The new compact textual syntax for Topic Maps being standardized by ISO, CTM, is now approaching stability, and so I thought it would be good to provide a little tutorial on it to show people what the syntax looks like in the current draft. With a little luck we might even get some feedback, which would be nice.

The basics

Let's start with a trivial example:

ctm .

This creates a topic with the ID ctm (same as an ID in XTM). The period just completes the "topic block", which is where we can put more information about the topic. So let's do that:

ctm - "CTM";
homepage: .

Now I added a name, signalled by the hyphen (-) and terminated by the semicolon. The next line has an occurrence of type homepage. The topic name is typed with the default name type, but I can always specify another type if I want:

ctm - "Compact Topic Maps Notation";
- abbreviation : "CTM" .

Associations are almost like in tolog and LTM, but the positions for role players and types are swapped, as follows:

defined-in(definition : ctm-spec, defined : ctm)

You can also attach subject locators and subject identifiers, if you want:

ctm .
ctm-spec = .

This is another two topic blocks. The first one gives the ctm topic a subject identifier, because it contains a URI with no extra symbols. The second gives the ctm-spec topic a subject locator (because of the equals sign).

Specifying topic types can be done with isa and supertypes with ako as follows:

ctm isa syntax .
syntax ako technology .

If you put all the examples given so far into a single file that would make a correct CTM topic map. To show more clearly what we've done and how this works I've collected it all into one big example below:

ctm isa syntax; - "Compact Topic Maps Notation";
- abbreviation : "CTM"; ;
homepage: .

ctm-spec - "CTM specification" ;
= .

defined-in(definition : ctm-spec, defined : ctm)

syntax ako technology .

You'll note that subject identifiers and locators appear in topic blocks just like names and occurrences, and that associations stand outside blocks.

Some more features

Scope is indicated with an @ after the scoped statement as follows:

ctm - "CTM";
  - "CTM" @ norwegian, swedish, danish .

Other CTM files can be merged in in two ways, using include or mergemap:

%include foo.ctm
%mergemap bar.xtm XTM

The include directive includes the referenced CTM file in the same ID namespace. That is, topics with the same ID in the master file and the referenced file will merge. This is not the case with mergemap, however, and it also allows files in other syntaxes than CTM to be merged in.

So far we've only used IDs to refer to topics, but the syntaxes shown above for adding subject identifiers and locators can also be used generally for identifying topics anywhere a topic reference is allowed in CTM. So our scope example could also be written:

ctm - "CTM";
  - "CTM" @ , , .

Referring to topics this way gets very verbose, of course, and so CTM supports qnames (foo:bar) by allowing prefixes to be defined. So the above can be shortened to:

%prefix lang
ctm - "CTM";
  - "CTM" @ lang:nno, lang:swe, lang:dan .

This is substantially easier on the eyes.

An feature that is perhaps a little unexpected of CTM is that it allows typed data to be given directly in occurrence values. So numbers, for example, do not need to be wrapped in strings. This means that one can write:

wadlow - "Robert Wadlow";
date-of-birth: 1918-02-22;
height: 2.72 .

Here, as you can see, a date and a floating point number are given directly in the CTM, and the correct datatype URIs will be attached to these values in the topic map. The supported datatypes are integer, decimal, date, datetime, string, and URI.

Frognerparken, Oslo

Fancy stuff

TMCL uses CTM as its syntax, and to make this more convenient CTM allows templates to be defined. Templates are a kind of structured macro. Below is an example which defines a template for an association type, and uses it to simplify the topic map example above:

def birthplace($person, $place)
  born-in(person : $person, place : $place)

wadlow - "Robert Wadlow";
date-of-birth: 1918-02-22;
height: 2.72;

The last line invokes the template, and since the template is invoked in a topic block the current topic is the first parameter, and alton is the second. The example could also have been written this way:

def birthplace($person, $place)
  born-in(person : $person, place : $place)

wadlow - "Robert Wadlow";
date-of-birth: 1918-02-22;
height: 2.72 .

birthplace(wadlow, alton)

If the template needs to create a new topic every type it's called, that's also possible. Let's imagine we want to create a topic for the birth of every person in the topic map. To save typing, we can use a template for this, as follows:

def birth($person, $place, $date)
  ?the-birth isa birth;
    took-place-on: $date .
  took-place-in(event: ?the-birth, place: $place)

wadlow - "Robert Wadlow";
  birth(alton, 1918-02-22);
  height: 2.72 .

?the-birth is a wildcard, which creates a new topic every time the template is instantiated. Note that it occurs twice in the template, and every time the template is invoked those two references will point to the same topic (for that invocation, in the next invocation they'll both point to the same new topic). There is also a nameless wildcard, which is just ?, but this creates a new topic every time.


The ISO meeting in Leipzig in October (just before TMRA) will probably finalize CTM. So since the syntax is probably pretty stable now, this is probably your last chance to influence it. If you have feedback, send it to the editors, to the sc34wg3 list, to the topicmapmail list, or leave a comment below.

Similar posts

A TMCL tutorial

The TMCL standard now seems more or less stable, and so now it is finally possible to explain to outsiders what the language looks like and how it works

Read | 2008-10-03 17:33

ISO meeting in Montréal

The second ISO meeting of 2007 was held over three days in Montréal, Canada, in conjunction with the Extreme Markup conference

Read | 2007-08-07 16:58

The Prague meeting

The ISO SC34 meeting in Prague was a big affair with five different working groups and many attendees

Read | 2009-04-02 10:50


Lars Heuer - 2010-03-30 12:18:15

Note that the %mergemap directive was modified from:

%mergemap xtm-source XTM


%mergemap xtm-source


%mergemap ctm-source

to merge a CTM source

Add a comment

Name required
Email optional, not published
URL optional, published
Spam don't check this if you want to be posted
Not spam do check this if you want to be posted
> Home
> Technology
> Beer
> Personal

> The author .
> On Twitter


follow us in feedly

Subscribe by email:

My new book

det norske ølet

My other book

Guidebook to Lithuanian beer
Rough guide to
Lithuanian beer

Technology blogs

Robert Barta
Sveins blogg
Stephen Fry
Messages in a bottle
Alex Brown
Planet Topic Maps

Last comments

Ekta on Bayesian identity re...

Lars Marius Garshol on A sudoku solver in P...

Heinz-GŁnter on A sudoku solver in P...

alex bloom on Active learning, alm...

alex bloom on Experiments in genet...

kenneth mwelwa on 10 tips on presentin...

fadirra on 7 tips on writing cl...

Tim on 7 tips on writing cl...

elmarie on What is an informati...

p2r on 7 tips on writing cl...