Some stuff about scan.
[ir-tcl-moved-to-github.git] / doc / ir-tcl.sgml
1 <!doctype linuxdoc system>
2
3 <!--
4   $Id: ir-tcl.sgml,v 1.7 1995-06-19 08:09:35 adam Exp $
5 -->
6
7 <article>
8 <title>IrTcl User's Guide and Reference
9 <author>Index Data, <tt/info@index.ping.dk/
10 <date>$Revision: 1.7 $
11 <abstract>
12 This document describes IrTcl &mdash an information retrieval toolkit for
13 Tcl and Tk that provides access to the Z39.50/SR protocol.
14 </abstract>
15
16 <toc>
17
18 <sect>Introduction
19
20 <p>
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 &lsqb;ref 1&rsqb;. 
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 &mdash A Motif look-and-feel interface to the X window
29 system.
30
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
33 protocol stack.
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 &lsqb;ref 2&rsqb; for more information about the XTI/mOSI implementation.
37
38 <sf/IrTcl/ provides two system environments:
39
40 <itemize>
41 <item> A simple command line shell &mdash useful for
42 testing purposes.
43 <item> A system which operates within the Tk environment which
44 makes it very easy to implement GUI clients.
45 </itemize>
46
47 <sect>Overview
48
49 <p>
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.
53
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.
57
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.
62
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.
70
71 We are now ready to present the three commands introduced to Tcl by
72 <sf/IrTcl/:
73
74 <descrip>
75 <tag/ir/ The ir object represents a connection to a target. More
76 precisely it describes a Z-association. 
77 <tag/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 <tag/ir-scan/ The scan object represents a list of scan lines
83 retrieved from a target.
84 </descrip>
85
86 <bf/Example/
87
88 To create a new IR object called <tt/z-assoc/ write:
89 <tscreen><verb>
90    ir z-assoc
91 </verb></tscreen>
92
93 <bf/End of example/
94
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
98 is set to <em/value/.
99
100 <bf/Example/
101
102 We wish to set the preferred-message-size to 18000 on the
103 <tt/z-assoc/ object:
104
105 <tscreen><verb>
106    z-assoc preferredMessageSize 18000
107 </verb></tscreen>
108
109 To read the current value of preferred-message-size use:
110
111 <tscreen><verb>
112    z-assoc preferredMessageSize
113 </verb></tscreen>
114 <bf/End of example/
115
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.
122
123 For each SR/Z39.50 request there is a corresponding object action. The most
124 important actions are:
125 <descrip>
126 <tag/connect/ Establishes connection with a target
127 <tag/init/ Sends an initialize request.
128 <tag/search/ Sends a search request.
129 <tag/present/ Sends a present request.
130 <tag/scan/ Sends a scan request.
131 </descrip>
132
133 <bf/Example/
134
135 This example shows a complete connect - init - search - present scenario. 
136
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.
140
141 The setting <tt/databaseNames/ is set to the 
142 database <tt/books/ to which the following searches are directed.
143 A callback is then defined and a connection is established to
144 <tt/fake.com/ by the <tt/connect/ action.
145 If the connect succeeds the <tt/connect-response/ is called.
146
147 In the Tcl procedure, <tt/connect-response/, a callback is defined
148 <em/before/ the init request is executed. 
149 The Tcl procedure <tt/init-response/ is called when a 
150 init response is returned from the target.
151
152 The <tt/init-response/ procedure sets up a <tt/search-response/ 
153 callback handler and sends a search-request by using a query which
154 consists of a single word <tt/science/.
155
156 When the <tt/search-response/ procedure is called it defines
157 a variable <tt/hits/ and sets it to the value of the setting
158 <tt/resultCount/. If <tt/hits/ is positive a present-request is
159 sent &mdash asking for 5 records from position 1. 
160
161 Finally, a present response is received and the number of records
162 returned is stored in the variable <tt/ret/.
163
164 <tscreen><verb>
165 ir z
166 ir-set z.1 z
167 z databaseNames books
168 z callback {connect-response}
169 z connect fake.com
170
171 proc connect-response {} {
172     z callback {init-response}
173     z init
174 }
175
176 proc init-response {} {
177     z.1 callback {search-response}
178     z.1 search science
179 }
180
181 proc search-response {} {
182     set hits [z.1 resultCount]
183     puts "$hits hits"
184     if {$hits > 0} {
185         z.1 callback {present-response}
186         z.1 present 1 5
187     }
188 }
189
190 proc present-response {} {
191     set ret [z.1 numberOfRecordsReturned]
192     puts "$ret records returned"
193 }
194 </verb></tscreen>
195 <bf/End of example/
196
197 The previous example program doesn't care about error conditions. 
198 If errors occur in the program they will be trapped by the Tcl error 
199 handler. This is not always appropriate. However, Tcl offers a 
200 <tt/catch/ command to support error handling by the program itself.
201
202 <sect>Associations
203
204 <p>
205 The ir object describes an association with a target.
206 This section covers the connect-init-disconnect actions provided
207 by the ir object.
208 An ir object is created by the <tt/ir/ command and the
209 created object enters a 'not connected' state, because it isn't
210 connected to a target yet. 
211
212 <sect1>Connect
213
214 <p>
215 A connection is established by the <tt/connect/ action which is
216 immediately followed by a hostname. Obviously, these settings should
217 be set <bf/before/ connecting.
218 The settings that affect the <tt/connect/ action are:
219
220 <descrip>
221 <tag><tt>comstack </tt><tt>mosi|tcpip</tt></tag>
222  Comstack type.
223 <tag><tt>protocol </tt><tt>Z39|SR</tt></tag>
224  Protocol type - ANSI/NISO Z39.50 or ISO SR.
225 <tag><tt>callback </tt><em>list</em></tag>
226  Tcl script called when the connection is established
227 <tag><tt>failback </tt><em>list</em></tag>
228  Fatal error Tcl script. Called on protocol errors or if target
229  closes connection
230 </descrip>
231
232 If the connect is unsuccessful either the connect action itself
233 will return an error code or the failback handler is invoked.
234
235 <sect1>Init
236
237 <p>
238 If the connect operation succeeds the <tt/init/ action should be used. 
239 The init related settings are:
240
241 <descrip>
242 <tag><tt>preferredMessageSize </tt><em>integer</em></tag>
243  Preferred-message-size. Default value is 30000.
244 <tag><tt>maximumRecordSize </tt><em>integer</em></tag>
245  Maximum-record-size. Default value is 30000.
246 <tag><tt>idAuthentication </tt><em>string</em> ...</tag>
247  Id-authentication. There are three forms. If any empty is
248  given, the Id-authentication is not used.  If one non-empty string
249  is given, the 'open' authentication is used. If three strings are 
250  specified, the version 'id-pass' authentication (version 3 only) 
251  is used in which case the first string is groupId; the second string 
252  is userId and the third string is password.
253 <tag><tt>implementationName </tt><em>string</em></tag>
254  Implementation-name of origin system. 
255 <tag><tt>implementationId</tt></tag>
256  Implementation-id of origin system. This setting is read-only.
257 <tag><tt>options </tt><em>list</em></tag>
258  Options to be negotiated in the init service. The list contains 
259  the options that are set. These are <tt>search</tt>, <tt>present</tt>,
260  <tt>delSet</tt>, <tt>resourceReport</tt>, <tt>triggerResourceCtrl</tt>,
261  <tt>resourceCtrl</tt>, <tt>accessCtrl</tt>, <tt>scan</tt>, <tt>sort</tt>,
262  <tt>extendedServices</tt>, <tt>level-1Segmentation</tt>, 
263  <tt>level-2Segmentation</tt>, <tt>concurrentOperations</tt> and
264  <tt>namedResultSets</tt>. Currently the default options are:
265  <tt>search</tt>, <tt>present</tt>, <tt>scan</tt> and 
266  <tt>namedResultSets</tt>. The options setting is set to its default
267  value when an ir object is created and when a disconnect is performed.
268 <tag><tt>protocolVersion </tt><em>integer</em></tag>
269  Protocol version: 2, 3, etc. 
270 <tag><tt>initResponse </tt><em>list</em></tag>
271  Init-response Tcl script. Note: not implemented - use <tt>callback</tt>
272  instead.
273 <tag><tt>callback </tt><em>list</em></tag>
274  General response Tcl script. Only used if <tt>initResponse</tt>
275  is not specified.
276 </descrip>
277
278 The init-response handler should inspect some of the settings shown
279 below:
280
281 <descrip>
282 <tag><tt>initResult </tt><em>boolean</em></tag>
283  Init response status. True if init operation was successful; 
284  false otherwise.
285 <tag><tt>preferredMessageSize </tt><em>integer</em></tag>
286  Preferred-message-size.
287 <tag><tt>maximumRecordSize </tt><em>integer</em></tag>
288  Maximum-record-size.
289 <tag><tt>targetImplementationName </tt><em>string</em></tag>
290  Implementation-name of target system.
291 <tag><tt>targetImplementationId </tt><em>string</em></tag>
292  Implementation-id of target system.
293 <tag><tt>targetImplementationVersion </tt><em>string</em></tag>
294  Implementation-version of target system.
295 <tag><tt>options </tt><em>list</em></tag>
296  Options negotiated after init. The list contains the options that are set.
297 <tag><tt>protocolVersion </tt><em>integer</em></tag>
298  Protocol version: 2, 3, etc.
299 <tag><tt>userInformationField </tt><em>string</em></tag>
300  User information field
301 </descrip>
302
303 <bf/Example/
304
305 Consider a client with the ability to access multiple targets.
306
307 We define a list of targets that we wish to connect to.
308 Each item in the list describes the target parameters with
309 the following four components: association-name, comstack-type,
310 protocol-type and a hostname.
311
312 The list for the two targets: ISO/SR target DANBIB and TCP/Z39.50 
313 target Data Research, will be defined as:
314 <tscreen><verb>
315 set targetList { {danbib mosi SR 0103/find2.denet.dk:4500}
316                  {drs tcpip Z39 dranet.dra.com} }
317 </verb></tscreen>
318
319 The Tcl code below defines, connect and initialize the
320 targets in <tt/targetList/:
321
322 <tscreen><verb>
323 \foreach target $targetList {
324     set assoc [lindex $target 0]
325     ir $assoc
326     $assoc comstack [lindex $target 1]
327     $assoc protocol [lindex $target 2]
328     $assoc failback [list fail-response $assoc]
329     $assoc callback [list connect-response $assoc]
330     $assoc connect [lindex $target 3]
331 }
332
333 proc connect-response {assoc} {
334     $assoc callback [list init-response $assoc]
335     $assoc init
336 }
337
338 proc fail-response {assoc} {
339     puts "$assoc closed connection or protocol error"
340 }
341
342 proc init-response {assoc} {
343     if {[$assoc initResult]} {
344         puts "$assoc initialized ok"
345     } else {
346         puts "$assoc didn't initialize"
347     }
348 }
349 </verb></tscreen>
350
351 <tt/target/ is bound to each item in the list of targets.
352 The <tt/assoc/ is set to the ir object name.
353 Then, the comstack, protocol and failback are set for the <tt/assoc/ object.
354 The ir object name is argument to the <tt/fail-response/ and 
355 <tt/connect-response/ routines. 
356 Note the use of the Tcl <tt/list/ command which 
357 is necessary here because the argument contains variables 
358 (<tt/assoc/) that should be substituted before the handler is defined.
359 After the connect operation, the <tt/init-response/ handler
360 is defined in much the same way as the failback handler.
361 And, finally, an init request is executed.
362 <bf/End of example/
363
364 <sect1>Disconnect
365
366 <p>
367 To terminate the connection the <tt/disconnect/ action should be used. 
368 This action has no parameters. 
369 Another connection may be established by a new <tt/connect/ action on
370 the same ir object.
371
372 <sect>Result sets
373
374 <p>
375 This section covers the queries used by <sf/IrTcl/, and how searches and
376 presents are handled.
377
378 A search operation and a result set is described by the ir set object.
379 The ir set object is defined by the <tt/ir-set/ command which
380 has two parameters. The first is the name of the new ir set object, and
381 the second, which is optional, is the name of an assocation &mdash an ir
382 object. The second argument is required if the ir set object should be able
383 to perform searches and presents. However, it is not required if
384 only ``local'' operations is done with the ir set object.
385
386 When the ir set object is created a number of settings are inherited
387 from the ir object, such as the selected databass, query type,
388 etc. Thus, the ir object contains what we could call default
389 settings.
390
391 <sect1>Queries
392
393 <p>
394 Search requests are sent by the <tt/search/ action which
395 takes a query as parameter. There are two types of queries,
396 RPN and CCL, controlled by the setting <tt/queryType/.
397 A string representation for the query is used in <sf/IrTcl/ since
398 Tcl has reasonably powerful string manipulaton capabilities.
399 The RPN query used in <sf/IrTcl/ is the prefix query notation also used in
400 the <bf/YAZ/ test client.
401
402 The CCL query is an uninterpreted octet-string which is parsed by the target.
403 We refer to the standard: ISO 8777. Note that only a few targets
404 actually support the CCL query and the interpretation of
405 the standard may vary.
406
407 The prefix query notation (which is converted to RPN) offer a few
408 operators. They are:
409
410 <descrip>
411 <tag><tt>@attr </tt><em>list op</em></tag>
412  The attributes in list are applied to op
413 <tag><tt>@and </tt><em>op1 op2</em></tag>
414  Boolean <em/and/ on op1 and op2
415 <tag><tt>@or </tt><em>op1 op2</em></tag>
416  Boolean <em/or/ on op1 and op2
417 <tag><tt>@not </tt><em>op1 op2</em></tag>
418  Boolean <em/not/ on op1 and op2
419 <tag><tt>@prox </tt><em>list op1 op2</em></tag>
420  Proximity operation on op1 and op2. Not implemented yet.
421 <tag><tt>@set </tt><em>name</em></tag>
422  Result set reference
423 </descrip>
424
425 It is simple to build RPN queries in <sf/IrTcl/. Search terms
426 are sequences of characters, as in:
427 <tscreen><verb>
428    science
429 </verb></tscreen>
430
431 Boolean operators use the prefix notation (instead of the suffix/RPN),
432 as in:
433 <tscreen><verb>
434    @and science technology
435 </verb></tscreen>
436
437 Search terms may be associated with attributes. These
438 attributes are indicated by the <tt/@attr/ operator.
439 Assuming the bib-1 attribute set, we can set the use-attribute 
440 (type is 1) to title (value is 4):
441
442 <tscreen><verb>
443    @attr 1=4 science
444 </verb></tscreen>
445
446 Also, it is possible to apply attributes to a range of search terms.
447 In the query below, both search terms have use=title but the <tt/tech/
448 term is right truncated:
449
450 <tscreen><verb>
451    @attr 1=4 @and @attr 5=1 tech beta
452 </verb></tscreen>
453
454 <sect1>Search
455
456 <p>
457 The settings that affect the search are listed below:
458
459 <descrip>
460 <tag><tt>databaseNames </tt><em>list</em></tag>
461  database-names.
462 <tag><tt>smallSetUpperBound </tt><em>integer</em></tag>
463  small set upper bound. Default 0.
464 <tag><tt>largeSetLowerBound </tt><em>integer</em></tag>
465  large set lower bound. Default 2.
466 <tag><tt>mediumSetPresentNumber </tt><em>integer</em></tag>
467  medium set present number. Default 0.
468 <tag><tt>replaceIndicator </tt><em>boolean</em></tag>
469  replace-indicator.
470 <tag><tt>setName </tt><em>string</em></tag>
471  name of result set.
472 <tag><tt>queryType rpn|ccl</tt></tag>
473  query type-1 or query type-2 
474 <tag><tt>preferredRecordSyntax </tt><em>string</em></tag>
475  preferred record syntax &mdash UNIMARC, USMARC, etc. 
476 <tag><tt>smallSetElementSetNames </tt><em>string</em></tag>
477  small-set-element-set names. Not implemented yet.
478 <tag><tt>mediumSetElementSetNames </tt><em>string</em></tag>
479  medium-set-element-set names. Not implemented yet.
480 <tag><tt>searchResponse </tt><em>list</em></tag>
481  Search-response Tcl script. Not implemented yet. Use <tt>callback</tt>
482  instead.
483 <tag><tt>callback </tt><em>list</em></tag>
484  General response Tcl script. Only used if searchResponse is not specified 
485 </descrip>
486
487 Setting the <tt/databaseNames/ is mandatory. All other settings
488 have reasonable defaults.
489 The search-response handler, specified by the <tt/callback/ - or
490 the <tt/searchResponse/ setting, 
491 should read some of the settings shown below:
492
493 <descrip>
494 <tag><tt>searchStatus </tt><em>boolean</em></tag>
495  search-status. True if search operation was successful; false
496  otherwise.
497 <tag><tt>responseStatus </tt><em>list</em></tag>
498  response status information.
499 <tag><tt>resultCount </tt><em>integer</em></tag>
500  result-count
501 <tag><tt>numberOfRecordsReturned </tt><em>integer</em></tag>
502  number of records returned.
503 </descrip>
504
505 The <tt/responseStatus/ signals one of three conditions which
506 is indicated by the value of the first item in the list:
507
508 <descrip>
509 <tag><tt>NSD</tt></tag> indicates that the target has returned one or 
510 more non-surrogate diagnostic messages. The <tt/NSD/ item is followed by 
511 a list with all non-surrogate messages. Each non-surrogate message consists
512 of three items. The first item of the three items is the error
513 code (integer); the next item is a textual representation of the error
514 code in plain english; the third item is additional information, possibly
515 empty if no additional information was returned by the target.
516
517 <tag><tt>DBOSD</tt></tag> indicates a successful operation where the
518 target has returned one or more records. Each record may be
519 either a database record or a surrogate diagnostic.
520
521 <tag><tt>OK</tt></tag> indicates a successful operation &mdash no records are
522 returned from the target. 
523 </descrip>
524
525 <bf/Example/
526
527 We continue with the multiple-targets example. 
528 The <tt/init-response/ procedure will attempt to make searches:
529
530 <tscreen><verb>
531 proc init-response {assoc} {
532     puts "$assoc connected"
533     ir-set ${assoc}.1 $assoc
534     $assoc.1 queryType rpn
535     $assoc.1 databaseNames base-a base-b
536     $assoc.1 callback [list search-response $assoc ${assoc}.1]
537     $assoc.1 search "@attr 1=4 @and @attr 5=1 tech beta"
538 }
539 </verb></tscreen>
540
541 An ir set object is defined and the
542 ir object is told about the name of ir object.
543 The ir set object use the name of the ir object as prefix.
544
545 Then, the query-type is defined to be RPN, i.e. we will
546 use the prefix query notation later on.
547
548 Two databases, <tt/base-a/ and <tt/base-b/, are selected.
549
550 A <tt/search-response/ handler is defined with the
551 ir object and the ir-set object as parameters and
552 the search is executed.
553
554 The first part of the <tt/search-response/ looks like:
555 <tscreen><verb>
556 proc search-response {assoc rset} {
557     set status [$rset responseStatus]
558     set type [lindex $status 0]
559     if {$type == "NSD"} {
560         set code [lindex $status 1]
561         set msg [lindex $status 2]
562         set addinfo [lindex $status 3]
563         puts "NSD $code: $msg: $addinfo"
564         return
565     } 
566     set hits [$rset resultCount]
567     if {$type == "DBOSD"} {
568         set ret [$rset numberOfRecordsReturned]
569         ...
570     }
571 }
572 </verb></tscreen>
573 The response status is stored in variable <tt/status/ and 
574 the first element indicates the condition.
575 If non-surrogate diagnostics are returned they are displayed.
576 Otherwise, the search was a success and the number of hits
577 is read. Finally, it is tested whether the search response
578 returned records (database or diagnostic).
579
580 Note that we actually didn't inspect the search status (setting
581 <tt/searchStatus/) to determine whether the search was successful or not, 
582 because the standard specifies that one or more non-surrogate
583 diagnostics should be returned by the target in case of errors.
584 <bf/End of example/
585
586 If one or more records are returned from the target they
587 will be stored in the result set object.
588 In the case in which the search response contains records, it is
589 very similar to the present response case. Therefore, some settings
590 are common to both situations.
591
592 <sect1>Present
593
594 <p>
595 The <tt/present/ action sends a present request. The <tt/present/ is
596 followed by two optional integers. The first integer is the 
597 result-set starting position &mdash defaults to 1. The second integer 
598 is the number of records requested &mdash defaults to 10. 
599 The settings which could be modified before a <tt/present/
600 action are:
601
602 <descrip>
603 <tag><tt>preferredRecordSyntax </tt><em>string</em></tag>
604  preferred record syntax &mdash UNIMARC, USMARC, etc.
605 <tag><tt>elementSetElementSetNames </tt><em>string</em></tag>
606  element-set names 
607 <tag><tt>presentResponse </tt><em>list</em></tag>
608  Present-response Tcl script. Not implemented yet. Use <tt>callback</tt>
609  instead.
610 <tag><tt>callback </tt><em>list</em></tag>
611  General response Tcl script. Only used if presentResponse is not specified 
612 </descrip>
613
614 The present-response handler should inspect the settings
615 shown in table below.
616 Note that <tt/responseStatus/ and <tt/numberOfRecordsReturned/
617 settings were also used in the search-response case.
618
619 As in the search response case, records returned from the
620 target are stored in the result set object.
621
622 <descrip>
623 <tag><tt>presentStatus </tt><em>boolean</em></tag>
624  present-status 
625 <tag><tt>responseStatus </tt><em>list</em></tag>
626  Response status information 
627 <tag><tt>numberOfRecordsReturned </tt><em>integer</em></tag>
628  number of records returned 
629 <tag><tt>nextResultSetPosition </tt><em>integer</em></tag>
630  next result set position
631 </descrip>
632
633 <sect1>Records
634
635 <p>
636 Search responses and present responses may result in
637 one or more records stored in the ir set object if
638 the <tt/responseStatus/ setting indicates database or
639 surrogate diagnostics (<tt/DBOSD/). The individual
640 records, indexed by an integer position, should be 
641 inspected.
642
643 The action <tt/type/ followed by an integer returns information 
644 about a given position in an ir set. There are three possiblities:
645
646 <descrip>
647 <tag><tt/SD/</tag> The item is a surrogate diagnostic record.
648 <tag><tt/DB/</tag> The item is a database record.
649 <tag><em/empty/</tag> There is no record at the specified position.
650 </descrip>
651
652 To handle the first case, surrogate diagnostic record, the
653 <tt/Diag/ action should be used. It returns three
654 items: error code (integer), text representation in plain english
655 (string), and additional information (string, possibly empty).
656
657 In the second case, database record, the <tt/recordType/ action should
658 be used. It returns the record type at the given position.
659 Some record types are:
660
661 <tscreen>
662 UNIMARC
663 INTERMARC
664 CCF
665 USMARC
666 UKMARC
667 NORMARC
668 LIBRISMARC
669 DANMARC
670 FINMARC
671 SUTRS
672 </tscreen>
673
674 <bf/Example/
675
676 We continue our search-response example. In the case,
677 <tt/DBOSD/, we should inspect the result set items. 
678 Recall that the ir set name was passed to the
679 search-response handler as argument <tt/rset/.
680
681 <tscreen><verb>
682     if {$type == "DBOSD"} {
683         set ret [$rset numberOfRecordsReturned]
684         for {set i 1} {$i<=$ret} {incr i} {
685             set itype [$rset type $i]
686             if {$itype == "SD"} {
687                 set diag [$rset Diag $i]
688                 set code [lindex $diag 0]
689                 set msg [lindex $diag 1]
690                 set addinfo [lindex $diag 2]
691                 puts "$i: NSD $code: $msg: $addinfo"
692             } elseif {$itype == "DB"} {
693                 set rtype [$rset recordType $i]
694                 puts "$i: type is $rtype"
695             }
696         }
697     }
698 </verb></tscreen>
699 Each item in the result set is examined. 
700 If an item is a diagnostic message it is displayed; otherwise
701 if it's a database record its type is displayed.
702 <bf/End of example/
703
704 <sect1>MARC records
705
706 <p>
707 In the case, where there is a MARC record at a given position we
708 want to display it somehow. The action <tt/getMarc/ is what we need.
709 The <tt/getMarc/ is followed by a position integer and the type of
710 extraction we want to make: <tt/field/ or <tt/line/.
711
712 The <tt/field/ and <tt/line/ type are followed by three
713 parameters that serve as extraction masks.
714 They are called tag, indicator and field.
715 If the mask matches a tag/indicator/field of a record the information
716 is extracted. Two characters have special meaning in masks: the
717 dot (any character) and star (any number of any character).
718
719 The <tt/field/ type returns one or more lists of field information
720 that matches the mask specification. Only the content of fields
721 is returned.
722
723 The <tt/line/ type, on the other hand, returns a Tcl list that
724 completely describe the layout of the MARC record &mdash including
725 tags, fields, etc.
726
727 The <tt/field/ type is sufficient and efficient in the case, where only a
728 small number of fields are extracted, and in the case where no
729 further processing (in Tcl) is necessary.
730
731 However, if the MARC record is to be edited or altered in any way, the
732 <tt/line/ extraction is more powerful &mdash only limited by the Tcl
733 language itself.
734
735 <bf/Example/
736
737 Consider the record below:
738 <tscreen><verb>
739 001       11224466 
740 003    DLC
741 005    00000000000000.0
742 008    910710c19910701nju           00010 eng  
743 010    $a    11224466 
744 040    $a DLC $c DLC
745 050 00 $a 123-xyz
746 100 10 $a Jack Collins
747 245 10 $a How to program a computer
748 260 1  $a Penguin
749 263    $a 8710
750 300    $a p. cm.
751 </verb></tscreen>
752
753 Assuming this record is at position 1 in ir-set <tt/z.1/, we
754 might extract the title-field (245 * a), with the following command:
755 <tscreen><verb>
756 z.1 getMarc 1 field 245 * a
757 </verb></tscreen>
758
759 which gives:
760 <tscreen><verb>
761 {How to program a computer}
762 </verb></tscreen>
763
764 Using the <tt/line/ instead of <tt/field/ gives:
765 <tscreen><verb>
766 {245 {10} {{a {How to program a computer}} }}
767 </verb></tscreen>
768
769 If we wish to extract the whole record as a list, we use:
770 <tscreen><verb>
771 z.1 getMarc 1 line * * *
772 </verb></tscreen>
773
774 giving:
775 <tscreen><verb>
776 {001 {} {{{} {   11224466 }} }}
777 {003 {} {{{} DLC} }}
778 {005 {} {{{} 00000000000000.0} }}
779 {008 {} {{{} {910710c19910701nju           00010 eng  }} }}
780 {010 {  } {{a {   11224466 }} }}
781 {040 {  } {{a DLC} {c DLC} }}
782 {050 {00} {{a 123-xyz} }}
783 {100 {10} {{a {Jack Collins}} }}
784 {245 {10} {{a {How to program a computer}} }}
785 {260 {1 } {{a Penguin} }}
786 {263 {  } {{a 8710} }}
787 {300 {  } {{a {p. cm.}} }}
788 </verb></tscreen>
789
790 <bf/End of example/
791
792 <bf/Example/
793
794 This example demonstrates how Tcl can be used to examine
795 a MARC record in the list notation.
796
797 The procedure <tt/extract-format/ makes an extraction of
798 fields in a MARC record based on a number of masks.
799 There are 5 parameters, <tt/r/: a
800 record in list notation, <tt/tag/: regular expression to
801 match the record tags, <tt/ind/: regular expression to
802 match indicators, <tt/field/: regular expression to 
803 match fields, and finally <tt/text/: regular expression to 
804 match the content of a field.
805
806 <tscreen><verb>
807 proc extract-format {r tag ind field text} {
808     foreach line $r {
809         if {[regexp $tag [lindex $line 0]] && \
810                 [regexp $ind [lindex $line 1]]} {
811             foreach f [lindex $line 2] {
812                 if {[regexp $field [lindex $f 0]]} {
813                     if {[regexp $text [lindex $f 1]]} {
814                         puts [lindex $f 1]
815                     }
816                 }
817             }
818         }
819     }
820 }
821 </verb></tscreen>
822
823 To match <tt/comput/ followed by any number of character(s) in the
824 245 fields in the record from the previous example, we could use:
825 <tscreen><verb>
826 set r [z.1 getMarc 1 line * * *]
827
828 extract-format $r 245 .. . comput
829 </verb></tscreen>
830 which gives:
831 <tscreen><verb>
832 How to program a computer
833 </verb></tscreen>
834
835 <bf/End of example/
836
837 The <tt/putMarc/ action does the opposite of <tt/getMarc/. It
838 copies a record in Tcl list notation to a ir set object and is
839 needed if a result-set must be updated by a Tcl modified (user-edited)
840 record.
841
842 <sect>Scan
843
844 <p>
845 To perform scan, a scan object must be created by the <tt>ir-scan</tt>
846 command. This command has two arguments - name of the scan object and
847 name of the ir object. Basically, the scan object, provides one <tt>scan</tt>
848 action which sends a scan request to the target. The <tt>action</tt>
849 is followed by a string describing starting point of the term list. The
850 format used is a simple subset of the query used in search requests. Only
851 <tt>@attr</tt> specifications and simple terms are allowed.
852 The settings that affect the scan are:
853
854 <descrip>
855 <tag><tt>stepSize </tt><em>integer</em></tag>
856  Step size. Default is 0.
857 <tag><tt>numberOfTermsRequested </tt><em>integer</em></tag>
858  Number of terms requested. Default is 20.
859 <tag><tt>preferredPositionInResponse </tt><em>integer</em></tag>
860  Preferred position in response. Default is 1.
861 <tag><tt>databaseNames </tt><em>list</em></tag>
862  Database names. Note that this setting is not (yet) supported for
863  the scan object. You must set this for the ir object instead.
864 <tag><tt>callback </tt><em>list</em></tag>
865  General response Tcl script. This setting is not (yet) supported for
866  the scan object. You must set this for the ir object instead.
867 </descrip>
868
869 The scan object normally holds one or more scan line entries upon
870 successful completion. The table below summarizes the settings
871 that should be used in a response handler.
872
873 <descrip>
874 <tag><tt>scanStatus</tt></tag>
875  Scan status. An integer between 0 and 6.
876 <tag><tt>numberOfTermsReturned </tt><em>integer</em></tag>
877  Number of terms returned. 
878 <tag><tt>positionOfTerm</tt></tag>
879  An integer describing the position of term.
880 <tag><tt>scanLine </tt> <em>integer</em></tag>
881  This function returns information about a given scan line at a given
882  index specified by the integer. The first scan line is numbered zero;
883  the second 1 and so on. A list is returned by the <tt>scanLine</tt>
884  setting. The first element is <tt>T</tt> if the scan entry
885  is a normal term and <tt>SD</tt> if the scan entry is a surrogate 
886  diagnostic. In the first case (normal) the scan term is second element 
887  in the list and the number of occurences is the third element.
888  In the other case (surrogate diagnostic), the second element
889  is the diagnostic code, the third a text representation of the error
890  code and the fourth element is additional information.
891 </descrip>
892
893 <bf/Example/
894
895 We will scan for the terms after <tt>science</tt> in the Title index.
896 We will assume that an ir object called <tt>z-assoc</tt> has already
897 been created.
898
899 <tscreen><verb>
900    z-assoc callback {scan-response}
901    ir-scan z-scan z-assoc
902    z-scan scan "@attr 1=4 science"
903   
904    proc scan-response {} {
905        set status [z-scan status]
906        if {$status == 0} {
907            set no [z-scan numberOfTermsReturned]
908            for {set i 0} {$i < $no} {incr i} {
909                set line [z-scan scanLine $i]
910                set type [lindex $line 0]
911                if {$type == "T"} {
912                    puts [lindex $line 1]
913                } elseif {$type == "SD"} {
914                    puts [lindex $line 1]
915                }
916            }
917        }
918    }
919 </verb></tscreen>
920 <bf/End of examle/
921
922 <sect>License
923
924 <p>
925 Copyright (c) 1995, Index Data.
926
927 Permission to use, copy, modify, distribute, and sell this software and
928 its documentation, in whole or in part, for any purpose, is hereby granted,
929 provided that:
930
931 1. This copyright and permission notice appear in all copies of the
932 software and its documentation. Notices of copyright or attribution
933 which appear at the beginning of any file must remain unchanged.
934
935 2. The names of Index Data or the individual authors may not be used to
936 endorse or promote products derived from this software without specific
937 prior written permission.
938
939 THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
940 EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
941 WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
942 IN NO EVENT SHALL INDEX DATA BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
943 INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES
944 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR
945 NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
946 LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
947 OF THIS SOFTWARE.
948
949 <sect>About Index Data
950
951 <p>
952 Index Data is a consulting and software-development enterprise that
953 specialises in library and information management systems. Our
954 interests and expertise span a broad range of related fields, and one
955 of our primary, long-term objectives is the development of a powerful
956 information management
957 system with open network interfaces and hypermedia capabilities.
958
959 We make this software available free of charge, on a fairly unrestrictive
960 license; as a service to the networking community, and to further the
961 development of quality software for open network communication.
962
963 We'll be happy to answer questions about the software, and about ourselves
964 in general.
965
966 <tscreen>
967 Index Data&nl
968 Ryesgade 3&nl
969 2200 K&oslash;benhavn N&nl
970 DENMARK
971 </tscreen>
972
973 <p>
974 <tscreen><verb>
975 Phone: +45 3536 3672
976 Fax  : +45 3536 0449
977 Email: info@index.ping.dk
978 </verb></tscreen>
979
980 <sect>References
981
982 <p>
983
984 <descrip>
985 <tag>1 Ousterhout, John K.:</tag>
986 Tcl and the Tk Toolkit. Addison-Wesley Company Inc (ISBN
987 0-201-63337-X). Source and documentation
988 can be found in <tt>URL:ftp://ftp.cs.berkeley.edu/pub/tcl</tt>
989 and mirrors.
990 <tag>2 Furniss, Peter:</tag>
991 RFC 1698: Octet Sequences for Upper-Layer OSI to Support
992 Basic Communications Applications.
993 </descrip>
994
995 </article>