1 <!doctype linuxdoc system>
8 <title>IrTcl User's Guide and Reference
9 <author>Index Data, <tt/info@index.ping.dk/
12 This document describes IrTcl - an information retrieval toolkit for
13 Tcl and Tk that provides access to the Z39.50/SR protocol.
21 This document describes the <sf/IrTcl/ information retrieval toolkit,
22 which offers a high-level, client interface to the Z39.50 and SR protocols.
23 The toolkit is based on the Tcl/Tk toolkit developed by Prof. John
24 K. Ousterhout at the University of California [ref 1].
25 Tcl is a simple, somewhat shell-like, interpreted language. What
26 makes Tcl attractive is that it also offers a C API, which makes
27 extensions to the language possible. The most important Tcl extension is
28 probably Tk --- A Motif look-and-feel interface to the X window
31 To interface the Z39.50/SR protocol <sf/IrTcl/ uses <bf/YAZ.
32 <bf/YAZ/ offers two transport types: RFC1729/BER on TCP/IP and the mOSI
34 However, the mOSI transport is only an option, and hence it is not
35 needed unless you wish to communicate within an OSI environment.
36 See [ref 2] for more information about the XTI/mOSI implementation.
38 <sf/IrTcl/ provides two system environments:
41 <item> A simple command line shell --- useful for
43 <item> A system which operates within the Tk environment which
44 makes it very easy to implement GUI clients.
50 Basically, <sf/IrTcl/ is a set of commands introduced to Tcl.
51 When extending Tcl there are two approaches: action-oriented commands
52 and object-oriented commands.
54 Action-oriented commands manipulate
55 Tcl variables and each command introduces only one action.
56 The string manipulation commands in Tcl are action oriented.
58 Object-oriented commands are added for every declared
59 variable (object). Object-oriented commands usually provide a set of
60 actions (methods) to manipulate the object.
61 The widgets in Tk (X objects) are examples of the object-oriented style.
63 <sf/IrTcl/ commands are object-oriented. The main reason
64 for this is that the data structures involved in the IR protocol
65 are not easily represented by Tcl data structures.
66 Also, the <sf/IrTcl/ objects tend to exist for a relativly long time.
67 Note that although we use the term object-oriented commands, this
68 does not mean that the programming style is strictly object-oriented. For
69 example, there is such no such thing as inheritance.
71 We are now ready to present the three commands introduced to Tcl by
75 <item> ir: The ir object represents a connection to a target. More
76 precisely it describes a Z-association.
77 <item> ir-set: The ir-set describes a result set, which is
78 conceptually a collection of records returned by the target.
79 The ir-set object may retrieve records from a target by means of
80 the ir object; it may read/write records from/to a local file or it may be
81 updated with a user-edited record.
82 <item> ir-scan: The scan object represents a list of scan lines
83 retrieved from a target.
88 To create a new IR object called <tt/z-assoc/ write:
95 Each object provides a set of <em/settings/ which may either be
96 readable, writeable of both. All settings immediately follow
97 the name of the object. If a value is present the setting
102 We wish to set the preferred-message-size to 18000 on the
106 z-assoc preferredMessageSize 18000
109 To read the current value of preferred-message-size use:
112 z-assoc preferredMessageSize
116 One important category consists of settings is those that relate to the
117 event-driven model. When <sf/IrTcl/ receives responses from the target, i.e.
118 init responses, search responses, etc., a <em/callback/ routine
119 is called. Callback routines are represented in Tcl as
120 a list, which is re-interpreted prior to invocation.
121 The method is similar to the one used in Tk to capture X events.
123 For each SR/Z39.50 request there is a corresponding object action. The most
124 important actions are:
126 <item> connect Establishes connection with a target
127 <item> init Sends an initialize request.
128 <item> search Sends a search request.
129 <item> present Sends a present request.
130 <item> scan Sends a scan request.
134 This example shows a complete
135 connect - init - search - present scenario.
137 First an IR object, called <tt/z/, is created.
138 Also a result set <tt/z.1/ is introduced by the <tt/ir-set/
139 and it is specified that the result set uses <tt/z/ as its association.
141 The setting <tt/databaseNames/ is set to the
142 database <tt/books/ to which the following searches are directed.
143 A connection is established to <tt/fake.com/ by the <tt/connect/ action.
144 A callback is then defined <em/before/ the init request is executed.
145 The Tcl procedure <tt/init-response/ is called when a
146 init-response is returned from the target.
148 The <tt/init-response/ procedure sets up a <tt/search-response/
149 callback handler and sends a search-request by using a query which
150 consists of a single word <tt/science/.
152 When the <tt/search-response/ procedure is called it defines
153 a variable <tt/hits/ and sets it to the value of the setting
154 <tt/resultCount/. If <tt/hits/ is positive a present-request is
155 sent --- asking for 5 records from position 1.
157 Finally, a present-response is received and the number of records
158 returned is stored in the variable <tt/ret/.
163 z databaseNames books
165 z callback {init-response}
168 proc init-response {} {
169 z.1 callback {search-response}
173 proc search-response {} {
174 set hits [z.1 resultCount]
177 z.1 callback {present-response}
182 proc present-response {} {
183 set ret [z.1 numberOfRecordsReturned]
184 puts "$ret records returned"
189 The previous example program doesn't care about error conditions.
190 If errors occur in the program they will be trapped by the Tcl error
191 handler. This is not always appropriate. However, Tcl offers a
192 <tt/catch/ command to support error handling by the program itself.
197 The ir object describes an association with a target.
198 This section covers the connect-init-disconnect actions provided
200 An ir object is created by the <tt/ir/ command and the
201 created object enters a 'not connected' state, because it isn't
202 connected to a target yet.
207 A connection is established by the <tt/connect/ action which is
208 immediately followed by a hostname. Table ref{tab:irconnect} lists the
209 settings that affect the <tt/connect/ action.
210 Obviously, these settings should be set <bf/before/ connecting.
213 <tag><tt>comstack </tt><tt>mosi|tcpip</tt></tag>
215 <tag><tt>protocol </tt><tt>Z3950|SR</tt></tag>
216 ANSI/NISO Z39.50 or ISO SR
217 <tag><tt>failback </tt><em>list</em></tag>
218 Fatal error Tcl script. Called on protocol errors or if target
225 If the connect operation succeeds the <tt/init/ action should be used.
226 Table ref{tab:irinit} lists the init related settings.
229 <tag><tt>preferredMessageSize </tt><em>integer</em></tag>
230 Preferred-message-size
231 <tag><tt>maximumRecordSize </tt><em>integer</em></tag>
233 <tag><tt>idAuthentication </tt><em>string</em></tag>
235 <tag><tt>implementationName </tt><em>string</em></tag>
236 Implementation-name of origin system
237 <tag><tt>implementationId </tt><em>string</em></tag>
238 Implementation-id of origin system
239 <tag><tt>options </tt><em>list</em></tag>
240 Options to be negotiated in init. The list contains the options that
242 <tag><tt>protocolVersion </tt><em>integer</em></tag>
243 Protocol version: 2, 3, etc.
244 <tag><tt>initResponse </tt><em>list</em></tag>
245 Init-response Tcl script
246 <tag><tt>callback </tt><em>list</em></tag>
247 General response Tcl script. Only used if initResponse is not specified
250 The init-response handler should inspect some of the settings in table
251 ref{tab:irinitresponse}
254 <tag><tt>initResult </tt><em>boolean</em></tag>
256 <tag><tt>preferredMessageSize </tt><em>integer</em></tag>
257 Preferred-message-size
258 <tag><tt>maximumRecordSize </tt><em>integer</em></tag>
260 <tag><tt>targetImplementationName </tt><em>string</em></tag>
261 Implementation-name of target system
262 <tag><tt>targetImplementationId </tt><em>string</em></tag>
263 Implementation-id of target system
264 <tag><tt>targetImplementationVersion </tt><em>string</em></tag>
265 Implementation-version of target system
266 <tag><tt>options </tt><em>list</em></tag>
267 Options negotiated after init. The list contains the options that are set.
268 <tag><tt>protocolVersion </tt><em>integer</em></tag>
269 Protocol version: 2, 3, etc.
270 <tag><tt>userInformationField </tt><em>string</em></tag>
271 User information field
275 Consider a client with the ability to access multiple targets.
277 We define a list of targets that we wish to connect to.
278 Each item in the list describes the target parameters with
279 the following four components: association-name, comstack-type,
280 protocol-type and a hostname.
282 The list for the two targets: ISO/SR target DANBIB and TCP/Z39.50
283 target Data Research, will be defined as:
285 set targetList { {danbib mosi sr 0103/find2.denet.dk:4500}
286 {drs tcpip z39 dranet.dra.com} }
289 The Tcl code below defines, connect and initialize the
290 targets in <tt/targetList/:
293 \foreach target $targetList {
294 set assoc [lindex $target 0]
296 $assoc comstack [lindex $target 1]
297 $assoc protocol [lindex $target 2]
298 $assoc failback [list fail-response $assoc]
299 $assoc connect [lindex $target 3]
300 $assoc initResponse [list init-response $assoc]
304 proc fail-response {assoc} {
305 puts "$assoc closed connection or protocol error"
308 proc init-response {assoc} {
309 if {[$assoc initResult]} {
310 puts "$assoc initialized ok"
312 puts "$assoc didn't initialize"
317 <tt/target/ is bound to each item in the list of targets.
318 The <tt/assoc/ is set to the ir object name.
319 Then, the comstack, protocol and failback are set for the <tt/assoc/ object.
320 The ir object name is argument to the <tt/fail-response/ routine.
321 Note the use of the Tcl <tt/list/ command which
322 is necessary here because the argument contains variables
323 (<tt/assoc/) that should be substituted before the handler is defined.
324 After the connect operation, the <tt/init-response/ handler
325 is defined in much the same way as the failback handler.
326 And, finally, an init request is executed.
332 To terminate the connection the <tt/disconnect/ action should be used.
333 This action has no parameters.
334 Another connection may be established by a new <tt/connect/ action on
340 This section covers the queries used by <sf/IrTcl/, and how searches and
341 presents are handled.
343 A search operation and a result set is described by the ir set object.
344 The ir set object is defined by the <tt/ir-set/ command which
345 has two parameters. The first is the name of the new ir set object, and
346 the second, which is optional, is the name of an assocation --- an ir
347 object. The second argument is required if the ir set object should be able
348 to perform searches and presents. However, it is not required if
349 only ``local'' operations is done with the ir set object.
351 When the ir set object is created a number of settings are inherited
352 from the ir object, such as the selected databass, query type,
353 etc. Thus, the ir object contains what we could call default
359 Search requests are sent by the <tt/search/ action which
360 takes a query as parameter. There are two types of queries,
361 RPN and CCL, controlled by the setting <tt/queryType/.
362 A string representation for the query is used in <sf/IrTcl/ since
363 Tcl has reasonably powerful string manipulaton capabilities.
364 The RPN query used in <sf/IrTcl/ is the prefix query notation also used in
365 the <bf/YAZ/ test client.
367 The CCL query is an uninterpreted octet-string which is parsed by the target.
368 We refer to the standard: ISO 8777. Note that only a few targets
369 actually support the CCL query and the interpretation of
370 the standard may vary.
372 The prefix query notation (which is converted to RPN) offer a few
373 operators, shown in table ref{tab:prefixop}.
376 <tag><tt>@attr </tt><em>list op</em></tag>
377 The attributes in list are applied to op
378 <tag><tt>@and </tt><em>op1 op2</em></tag>
379 Boolean <em/and/ on op1 and op2
380 <tag><tt>@or </tt><em>op1 op2</em></tag>
381 Boolean <em/or/ on op1 and op2
382 <tag><tt>@not </tt><em>op1 op2</em></tag>
383 Boolean <em/not/ on op1 and op2
384 <tag><tt>@prox </tt><em>list op1 op2</em></tag>
385 Proximity operation on op1 and op2
388 It is simple to build RPN queries in <sf/IrTcl/. Search terms
389 are sequences of characters, as in:
394 Boolean operators use the prefix notation (instead of the suffix/RPN),
397 @and science technology
400 Search terms may be associated with attributes. These
401 attributes are indicated by the <tt/@attr/ operator.
402 Assuming the bib-1 attribute set, we can set the use-attribute
403 (type is 1) to title (value is 4):
409 Also, it is possible to apply attributes to a range of search terms.
410 In the query below, both search terms have use=title but the <tt/tech/
411 term is right truncated:
414 @attr 1=4 @and @attr 5=1 tech beta
420 Table ref{tab:irsearchrequest} lists settings that affect the search.
421 Setting the <tt/databaseNames/ is mandatory. All other settings
422 have reasonable defaults.
427 \begin{tabular}{l|l|p{8cm}}
428 Setting & Value & Description \\\hline
429 databaseNames & list & database-names \\
430 smallSetUpperBound & integer & small set upper bound \\
431 largeSetLowerBound & integer & large set lower bound \\
432 mediumSetPresentNumber & integer & medium set present number \\
433 replaceIndicator & boolean & replace-indicator \\
434 setName & string & name of result set \\
435 queryType & rpn & query type-1 \\
436 & ccl & query type-2 \\
437 preferredRecordSyntax & string & preferred record syntax.
438 See table ref{tab:recordtypes} on page \pageref{tab:recordtypes} \\
439 smallSetElementSetNames & string & small-set-element-set names \\
440 mediumSetElementSetNames & string & medium-set-element-set names \\
441 searchResponse & list & Search-response Tcl script \\
442 callback & list & General response Tcl script. Only used
443 if searchResponse is not specified \\
445 \caption{Search request settings}
446 \label{tab:irsearchrequest}
449 The search-response handler, specified by the <tt/callback/ - or
450 the <tt/searchResponse/ setting,
451 should read some of the settings shown in table ref{tab:irsearchresponse}.
454 \begin{tabular}{l|l|p{8cm}}
455 Setting & Value & Description \\\hline
456 searchStatus & boolean & search-status \\
457 responseStatus & list & response status information \\
458 resultCount & integer & result-count \\
459 numberOfRecordsReturned & integer & number of records retrieved \\
461 \caption{Search response settings}
462 \label{tab:irsearchresponse}
465 The <tt/responseStatus/ signals one of three conditions which
466 is indicated by the value of the first item in the list:
469 \item[NSD] indicates that the target has returned one or more non-surrogate
470 diagnostic messages. The <tt/NSD/ item is followed by a list with
471 all non-surrogate messages. Each non-surrogate message consists
472 of three items. The first item of the three items is the error
473 code (integer); the next item is a textual representation of the error
474 code in plain english; the third item is additional information, possibly
475 empty if no additional information was returned by the target.
477 \item[DBOSD] indicates a successful operation where the
478 target has returned one or more records. Each record may be
479 either a database record or a surrogate diagnostic.
481 \item[OK] indicates a successful operation --- no records are
482 returned from the target.
486 We continue with the multiple-targets example.
487 The <tt/init-response/ procedure will attempt to make searches:
490 proc init-response {assoc} {
491 puts "$assoc connected"
492 ir-set ${assoc}.1 $assoc
493 $assoc.1 queryType rpn
494 $assoc.1 databaseNames base-a base-b
495 $assoc.1 searchResponse [list search-response $assoc ${assoc}.1]
496 $assoc.1 search "@attr 1=4 @and @attr 5=1 tech beta"
500 An ir set object is defined and the
501 ir object is told about the name of ir object.
502 The ir set object use the name of the ir object as prefix.
504 Then, the query-type is defined to be RPN, i.e. we will
505 use the prefix query notation later on.
507 Two databases, <tt/base-a/ and <tt/base-b/, are selected.
509 A <tt/search-response/ handler is defined with the
510 ir object and the ir-set object as parameters and
511 the search is executed.
513 The first part of the <tt/search-response/ looks like:
515 proc search-response {assoc rset} {
516 set status [$rset responseStatus]
517 set type [lindex $status 0]
519 set code [lindex $status 1]
520 set msg [lindex $status 2]
521 set addinfo [lindex $status 3]
522 puts "NSD $code: $msg: $addinfo"
525 set hits [$rset resultCount]
526 if {$type == DBOSD} {
527 set ret [$rset numberOfRecordsReturned]
532 The response status is stored in variable <tt/status/ and
533 the first element indicates the condition.
534 If non-surrogate diagnostics are returned they are displayed.
535 Otherwise, the search was a success and the number of hits
536 is read. Finally, it is tested whether the search response
537 returned records (database or diagnostic).
539 Note that we actually didn't inspect the search status (setting
540 <tt/searchStatus/) to determine whether the search was successful or not,
541 because the standard specifies that one or more non-surrogate
542 diagnostics should be returned by the target in case of errors.
545 If one or more records are returned from the target they
546 will be stored in the result set object.
547 In the case in which the search response contains records, it is
548 very similar to the present response case. Therefore, some settings
549 are common to both situations.
554 The <tt/present/ action sends a present request. The <tt/present/ is
555 followed by two optional integers. The first integer is the
556 result-set starting position --- defaults to 1. The second integer
557 is the number of records requested --- defaults to 10.
558 The settings which could be modified before a <tt/present/
559 action are shown in table ref{tab:irpresentrequest}.
562 \begin{tabular}{l|l|p{8cm}}
563 Setting & Value & Description \\\hline
564 preferredRecordSyntax & string & preferred record syntax.
565 See table ref{tab:recordtypes} on page \pageref{tab:recordtypes} \\
566 elementSetElementSetNames & string & element-set names \\
567 presentResponse & list & Present-response Tcl script \\
568 callback & list & General response Tcl script. Only used
569 if presentResponse is not specified \\
571 \caption{Present request settings}
572 \label{tab:irpresentrequest}
575 The present-response handler should inspect the settings
576 shown in table ref{tab:irpresentresponse}.
577 Note that <tt/responseStatus/ and <tt/numberOfRecordsReturned/
578 settings were also used in the search-response case.
580 As in the search-response case, records returned from the
581 target are stored in the result set object.
584 \begin{tabular}{l|l|p{8cm}}
585 Setting & Value & Description \\\hline
586 presentStatus & boolean & present-status \\
587 responseStatus & list & Response status information \\
588 numberOfRecordsReturned & integer & number of records returned \\
589 nextResultSetPosition & integer & next result set position \\
591 \caption{Present response settings}
592 \label{tab:irpresentresponse}
598 Search responses and present responses may result in
599 one or more records stored in the ir set object if
600 the <tt/responseStatus/ setting indicates database or
601 surrogate diagnostics (<tt/DBOSD/). The individual
602 records, indexed by an integer position, should be
605 The action <tt/Type/ followed by an integer returns information
606 about a given position in an ir set. There are three possiblities:
609 <item> SD The item is a surrogate diagnostic.
610 <item> DB The item is a database record.
611 <item> <em/empty/ There is no record at the specified position.
614 To handle the first case, surrogate diagnostic, the
615 <tt/Diag/ action should be used. It returns three
616 items: error code (integer), text representation in plain english
617 (string), and additional information (string, possibly empty).
619 In the second case, database record, the <tt/recordType/ action should
620 be used. It returns the record type at the given position.
621 Some record types are shown in table ref{tab:recordtypes}.
639 \caption{Record types}
640 \label{tab:recordtypes}
644 We continue our search-response example. In the case,
645 <tt/DBOSD/, we should inspect the result set items.
646 Recall that the ir set name was passed to the
647 search-response handler as argument <tt/rset/.
650 if {$type == DBOSD} {
651 set ret [$rset numberOfRecordsReturned]
652 for {set i 1} {$i<=$ret} {incr i} {
653 set itype [$rset Type $i]
655 set diag [$rset Diag $i]
656 set code [lindex $diag 0]
657 set msg [lindex $diag 1]
658 set addinfo [lindex $diag 2]
659 puts "$i: NSD $code: $msg: $addinfo"
660 } else if {$itype == DB} {
661 set rtype [$rset RecordType $i]
662 puts "$i: type is $rtype"
667 Each item in the result-set is examined.
668 If an item is a diagnostic message it is displayed; otherwise
669 if it's a database record its type is displayed.
675 In the case, where there is a MARC record at a given position we
676 want to display it somehow. The action <tt/getMarc/ is what we need.
677 The <tt/getMarc/ is followed by a position integer and the type of
678 extraction we want to make: <tt/field/ or <tt/line/.
680 The <tt/field/ and <tt/line/ type are followed by three
681 parameters that serve as extraction masks.
682 They are called tag, indicator and field.
683 If the mask matches a tag/indicator/field of a record the information
684 is extracted. Two characters have special meaning in masks: the
685 dot (any character) and star (any number of any character).
687 The <tt/field/ type returns one or more lists of field information
688 that matches the mask specification. Only the content of fields
691 The <tt/line/ type, on the other hand, returns a Tcl list that
692 completely describe the layout of the MARC record --- including
695 The <tt/field/ type is sufficient and efficient in the case, where only a
696 small number of fields are extracted, and in the case where no
697 further processing (in Tcl) is necessary.
699 However, if the MARC record is to be edited or altered in any way, the
700 <tt/line/ extraction is more powerful --- only limited by the Tcl
704 Consider the record below:
709 008 910710c19910701nju 00010 eng
713 100 10 $a Jack Collins
714 245 10 $a How to program a computer
720 Assuming this record is at position 1 in ir-set <tt/z.1/, we
721 might extract the title-field (245 * a), with the following command:
723 z.1 getMarc 1 field 245 * a
728 {How to program a computer}
731 Using the <tt/line/ instead of <tt/field/ gives:
733 {245 {10} {{a {How to program a computer}} }}
736 If we wish to extract the whole record as a list, we use:
738 z.1 getMarc 1 line * * *
743 {001 {} {{{} { 11224466 }} }}
745 {005 {} {{{} 00000000000000.0} }}
746 {008 {} {{{} {910710c19910701nju 00010 eng }} }}
747 {010 { } {{a { 11224466 }} }}
748 {040 { } {{a DLC} {c DLC} }}
749 {050 {00} {{a 123-xyz} }}
750 {100 {10} {{a {Jack Collins}} }}
751 {245 {10} {{a {How to program a computer}} }}
752 {260 {1 } {{a Penguin} }}
753 {263 { } {{a 8710} }}
754 {300 { } {{a {p. cm.}} }}
760 This example demonstrates how Tcl can be used to examine
761 a MARC record in the list notation.
763 The procedure <tt/extract-format/ makes an extraction of
764 fields in a MARC record based on a number of masks.
765 There are 5 parameters, <tt/r/: a
766 record in list notation, <tt/tag/: regular expression to
767 match the record tags, <tt/ind/: regular expression to
768 match indicators, <tt/field/: regular expression to
769 match fields, and finally <tt/text/: regular expression to
770 match the content of a field.
773 proc extract-format {r tag ind field text} {
775 if {[regexp $tag [lindex $line 0]] && \
776 [regexp $ind [lindex $line 1]]} {
777 foreach f [lindex $line 2] {
778 if {[regexp $field [lindex $f 0]]} {
779 if {[regexp $text [lindex $f 1]]} {
789 To match <tt/comput/ followed by any number of character(s) in the
790 245 fields in the record from the previous example, we could use:
792 set r [z.1 getMarc 1 line * * *]
794 extract-format $r 245 .. . comput
798 How to program a computer
803 The <tt/putMarc/ action does the opposite of <tt/getMarc/. It
804 copies a record in Tcl list notation to a ir set object and is
805 needed if a result-set must be updated by a Tcl modified (user-edited)
819 \label{sec:references}
822 <item> <bf/Ousterhout, John K./:
823 Tcl and the Tk Toolkit. Addison-Wesley Company Inc (ISBN
824 0-201-63337-X). Source and documentation
825 can be found in <tt/URL:ftp://ftp.cs.berkeley.edu/pub/tcl/
827 <item> <bf/Furniss, Peter/:
828 RFC 1698: Octet Sequences for Upper-Layer OSI to Support
829 Basic Communications Applications.