Help:Template

From semanticweb.org

Jump to: navigation, search

This page considers using template inclusion to allow queries similar to those of SMW.

This is done:

  • for comparison
  • for use also on sites without SMW
  • for features not available with SMW

The wikitext of a page P can be put inside noinclude tags, while an include-only part independently contains any other desired content. If, for example, we use unnamed parameters, using "#if:{{{2|}}}:" etc. we can check the number of parameters, and, for each number of parameters, define what the template should produce.

Contents

[edit] One parameter: distance-1 queries

With a switch, the page can contain content depending on the parameter value; thus that value is comparable with a relation or attribute name, while the produced content is comparable with the object of the relation (the produced content is a link to a page), or the attribute value (the produced content is plain text).

Examples:

In the first example content is produced which is a mix of links and plain text. There is no distinction between one large item, and a set of three smaller ones.

An inverse query such as "which lines have stop Schiphol?" is not possible because we would need to check whether a string is a substring of another string.

An inverse query such as "which lines are located in the Netherlands?" is only possible by checking a list of candidates:

{{#ifeq:{{:NS line 14|located in}}|[[Netherlands]]|[[NS line 14]]}} {{#ifeq:{{:DB line 38|located in}}|[[Netherlands]]|[[DB line 38]]}} {{#ifeq:{{:NS line 22 shl|located in}}|[[Netherlands]]|[[NS line 22 shl]]}} gives

If the content produced by a page using one parameter is (as opposed to the examples above) a single unlinked pagename, we can make a chain of queries in which the latter page is called using another parameter:

{{:{{:NS line 22 shl|stop 2}}|stop of}} gives {{:NS line 22 shl|stop of}}.

[edit] Two parameters

By defining for a page P the produced content S of {{:P|Q|R}} we define a 4-tuple (P, Q, R, S), for example, for shops near public transport stops, (stop, shop type, weekday, closing time). This way we can avoid attributes like Attribute:Monday closing time supermarket, of which there are currently 21, because of having 3 shop types.

Alternatively, we can use it for a distance-2 query.

[edit] Distance-2 query

As we have seen that backward queries are cumbersome, we consider forward distance-2 queries only.

We can define a page P such that the produced content of {{:P|R1|R2}} (where R1 and R2 are relations) is the set of pages Q for which P R1 Q, with for each Q, the set of pages T such that Q R2 T.

The set of pages Q for which P R1 Q is already in the wikitext for the distance-1 query {{:P|R1}}. Either we reuse it (with branching at each item) or, like in the example below, duplicate the list. For each Q we produce the linked Q and the set of pages T. The latter are produced by distance-1 queries on each page Q.

For example, in Amsterdam Zuid WTC, with R1 = "stop of" and R2 = "has stop" and "located in", and using Template:rel (talk, backlinks, edit):

{{#if:{{{2|}}}|
{{#switch:{{{1}}}
 |stop of={{#switch:{{{2}}}
  |has stop={{rel|NS line 22 shl|has stop}}; {{rel|NS line 40 shl|has stop}}
  |located in={{rel|NS line 22 shl|located in}}; {{rel|NS line 40 shl|located in}}}}}}
|{{#switch:{{{1}}}
 |stop of=[[NS line 22 shl]], [[NS line 40 shl]]}}}}

Thus:

[edit] Distance-3 query

Part of distance-3 query:

There is a recursion restriction: from the include part of a page a call to the same page (even if indirectly, and even while parameters take care of avoiding infinite recursion) is not allowed.

It would happen here because "has stop" and "stop of" are each other's inverse, so the intermediate query "what are the lines through the stops on this line" has, as one of the results, the original line. Only if from there there is another query step, so in the case of a distance 3+-query, we would not get what was requested.

The lack of info is not serious, because the info can be found by a simpler query, here the distance-1 query above. However, the output (the template tag without parameters) would be ugly. Therefore redirects such as NS line 22 shl 1 are used: from the included part of NS line 22 shl, not NS line 22 shl but NS line 22 shl 1 is called.

Personal tools