From 4e738d9131106878f8b560e53034241db99d6099 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Tue, 24 Jun 2003 23:03:04 +0000 Subject: [PATCH] Move ccl_pquery to ccl module (ccl.h). Add proper CCL test. --- ccl/cclptree.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++++- ccl/tstccl.c | 52 +++++++++++++++++++------ include/yaz/ccl.h | 5 ++- include/yaz/yaz-ccl.h | 5 +-- zutil/yaz-ccl.c | 99 +----------------------------------------------- 5 files changed, 145 insertions(+), 116 deletions(-) diff --git a/ccl/cclptree.c b/ccl/cclptree.c index d1c7d64..e108645 100644 --- a/ccl/cclptree.c +++ b/ccl/cclptree.c @@ -44,7 +44,7 @@ /* CCL print rpn tree - infix notation * Europagate, 1995 * - * $Id: cclptree.c,v 1.13 2003-06-23 10:22:21 adam Exp $ + * $Id: cclptree.c,v 1.14 2003-06-24 23:03:04 adam Exp $ * * Old Europagate Log: * @@ -166,6 +166,104 @@ void ccl_pr_tree (struct ccl_rpn_node *rpn, FILE *fd_out) } +static void ccl_pquery_complex (WRBUF w, struct ccl_rpn_node *p) +{ + switch (p->kind) + { + case CCL_RPN_AND: + wrbuf_puts(w, "@and "); + break; + case CCL_RPN_OR: + wrbuf_puts(w, "@or "); + break; + case CCL_RPN_NOT: + wrbuf_puts(w, "@not "); + break; + case CCL_RPN_PROX: + if (p->u.p[2] && p->u.p[2]->kind == CCL_RPN_TERM) + { + const char *cp = p->u.p[2]->u.t.term; + /* exlusion distance ordered relation which-code unit-code */ + if (*cp == '!') + { + /* word order specified */ + if (isdigit(cp[1])) + wrbuf_printf(w, "@prox 0 %s 1 2 k 2 ", cp+1); + else + wrbuf_printf(w, "@prox 0 1 1 2 k 2 "); + } + else if (*cp == '%') + { + /* word order not specified */ + if (isdigit(cp[1])) + wrbuf_printf(w, "@prox 0 %s 0 2 k 2 ", cp+1); + else + wrbuf_printf(w, "@prox 0 1 0 2 k 2 "); + } + } + else + wrbuf_puts(w, "@prox 0 2 0 1 k 2 "); + break; + default: + wrbuf_puts(w, "@ bad op (unknown) "); + } + ccl_pquery(w, p->u.p[0]); + ccl_pquery(w, p->u.p[1]); +} + +void ccl_pquery (WRBUF w, struct ccl_rpn_node *p) +{ + struct ccl_rpn_attr *att; + const char *cp; + + switch (p->kind) + { + case CCL_RPN_AND: + case CCL_RPN_OR: + case CCL_RPN_NOT: + case CCL_RPN_PROX: + ccl_pquery_complex (w, p); + break; + case CCL_RPN_SET: + wrbuf_puts (w, "@set "); + wrbuf_puts (w, p->u.setname); + wrbuf_puts (w, " "); + break; + case CCL_RPN_TERM: + for (att = p->u.t.attr_list; att; att = att->next) + { + char tmpattr[128]; + wrbuf_puts (w, "@attr "); + if (att->set) + { + wrbuf_puts (w, att->set); + wrbuf_puts (w, " "); + } + switch(att->kind) + { + case CCL_RPN_ATTR_NUMERIC: + sprintf(tmpattr, "%d=%d ", att->type, att->value.numeric); + wrbuf_puts (w, tmpattr); + break; + case CCL_RPN_ATTR_STRING: + sprintf(tmpattr, "%d=", att->type); + wrbuf_puts (w, tmpattr); + wrbuf_puts(w, att->value.str); + wrbuf_puts (w, " "); + break; + } + } + for (cp = p->u.t.term; *cp; cp++) + { + if (*cp == ' ' || *cp == '\\') + wrbuf_putc (w, '\\'); + wrbuf_putc (w, *cp); + } + wrbuf_puts (w, " "); + break; + } +} + /* * Local variables: * tab-width: 4 diff --git a/ccl/tstccl.c b/ccl/tstccl.c index e46cad2..6ae30a4 100644 --- a/ccl/tstccl.c +++ b/ccl/tstccl.c @@ -2,19 +2,27 @@ * Copyright (c) 2002-2003, Index Data * See the file LICENSE for details. * - * $Id: tstccl.c,v 1.1 2003-06-23 10:22:21 adam Exp $ + * $Id: tstccl.c,v 1.2 2003-06-24 23:03:04 adam Exp $ */ /* CCL test */ #include -static char *query_str[] = { - "x1", - "x1 and x2", - "ti=x3", - "dc.title=x4", - 0 +struct ccl_tst { + char *query; + char *result; +}; + +static struct ccl_tst query_str[] = { + { "x1", "@attr 4=2 @attr 1=1016 x1 "}, + { "(((((x1)))))", "@attr 4=2 @attr 1=1016 x1 "}, + {"x1 and x2", "@and @attr 4=2 @attr 1=1016 x1 @attr 4=2 @attr 1=1016 x2 "}, + { "ti=x3", "@attr 4=2 @attr 1=4 x3 "}, + { "dc.title=x4", "@attr 1=/my/title x4 "}, + { "x1 and", 0}, + { "tix=x5", 0}, + {0, 0} }; void tst1(void) @@ -29,20 +37,40 @@ void tst1(void) parser->bibset = bibset; - for (i = 0; query_str[i]; i++) + for (i = 0; query_str[i].query; i++) { struct ccl_token *token_list = - ccl_parser_tokenize(parser, query_str[i]); + ccl_parser_tokenize(parser, query_str[i].query); struct ccl_rpn_node *rpn = ccl_parser_find(parser, token_list); ccl_token_del (token_list); if (rpn) { + WRBUF wrbuf = wrbuf_alloc(); + ccl_pquery(wrbuf, rpn); + + if (!query_str[i].result) + { + printf ("Failed %s\n", query_str[i].query); + printf (" got:%s:\n", wrbuf_buf(wrbuf)); + printf (" expected failure\n"); + exit(3); + } + else if (strcmp(wrbuf_buf(wrbuf), query_str[i].result)) + { + printf ("Failed %s\n", query_str[i].query); + printf (" got:%s:\n", wrbuf_buf(wrbuf)); + printf (" expected:%s:\n", query_str[i].result); + exit(2); + } ccl_rpn_delete(rpn); + wrbuf_free(wrbuf, 1); } - else + else if (query_str[i].result) { - printf ("failed %s\n", query_str[i]); - exit(1+i); + printf ("Failed %s\n", query_str[i].query); + printf (" got failure\n"); + printf (" expected:%s:\n", query_str[i].result); + exit(4); } } ccl_parser_destroy (parser); diff --git a/include/yaz/ccl.h b/include/yaz/ccl.h index 175a37b..14c34f3 100644 --- a/include/yaz/ccl.h +++ b/include/yaz/ccl.h @@ -45,7 +45,7 @@ /* * CCL - header file * - * $Id: ccl.h,v 1.14 2003-06-23 10:22:21 adam Exp $ + * $Id: ccl.h,v 1.15 2003-06-24 23:03:04 adam Exp $ * * Old Europagate Log: * @@ -88,6 +88,7 @@ #include #include #include +#include YAZ_BEGIN_CDECL @@ -332,6 +333,8 @@ YAZ_EXPORT char *ccl_strdup (const char *str); YAZ_EXPORT const char *ccl_qual_search_special (CCL_bibset b, const char *name); +YAZ_EXPORT void ccl_pquery (WRBUF w, struct ccl_rpn_node *p); + #ifndef ccl_assert #define ccl_assert(x) ; #endif diff --git a/include/yaz/yaz-ccl.h b/include/yaz/yaz-ccl.h index 0f8fb9c..e92e041 100644 --- a/include/yaz/yaz-ccl.h +++ b/include/yaz/yaz-ccl.h @@ -23,7 +23,7 @@ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE * OF THIS SOFTWARE. * - * $Id: yaz-ccl.h,v 1.5 2003-03-03 19:57:35 adam Exp $ + * $Id: yaz-ccl.h,v 1.6 2003-06-24 23:03:04 adam Exp $ */ #ifndef YAZ_CCL_H @@ -33,14 +33,11 @@ #include #include #include -#include YAZ_BEGIN_CDECL YAZ_EXPORT Z_RPNQuery *ccl_rpn_query (ODR o, struct ccl_rpn_node *p); YAZ_EXPORT Z_AttributesPlusTerm *ccl_scan_query (ODR o, struct ccl_rpn_node *p); -YAZ_EXPORT void ccl_pquery (WRBUF w, struct ccl_rpn_node *p); - YAZ_END_CDECL #endif diff --git a/zutil/yaz-ccl.c b/zutil/yaz-ccl.c index 33f46b3..0aa4c5a 100644 --- a/zutil/yaz-ccl.c +++ b/zutil/yaz-ccl.c @@ -2,7 +2,7 @@ * Copyright (c) 1996-2003, Index Data. * See the file LICENSE for details. * - * $Id: yaz-ccl.c,v 1.22 2003-06-23 12:41:44 adam Exp $ + * $Id: yaz-ccl.c,v 1.23 2003-06-24 23:03:04 adam Exp $ */ #include @@ -44,100 +44,3 @@ Z_AttributesPlusTerm *ccl_scan_query (ODR o, struct ccl_rpn_node *p) return q; } -static void ccl_pquery_complex (WRBUF w, struct ccl_rpn_node *p) -{ - switch (p->kind) - { - case CCL_RPN_AND: - wrbuf_puts(w, "@and "); - break; - case CCL_RPN_OR: - wrbuf_puts(w, "@or "); - break; - case CCL_RPN_NOT: - wrbuf_puts(w, "@not "); - break; - case CCL_RPN_PROX: - if (p->u.p[2] && p->u.p[2]->kind == CCL_RPN_TERM) - { - const char *cp = p->u.p[2]->u.t.term; - /* exlusion distance ordered relation which-code unit-code */ - if (*cp == '!') - { - /* word order specified */ - if (isdigit(cp[1])) - wrbuf_printf(w, "@prox 0 %s 1 2 k 2 ", cp+1); - else - wrbuf_printf(w, "@prox 0 1 1 2 k 2 "); - } - else if (*cp == '%') - { - /* word order not specified */ - if (isdigit(cp[1])) - wrbuf_printf(w, "@prox 0 %s 0 2 k 2 ", cp+1); - else - wrbuf_printf(w, "@prox 0 1 0 2 k 2 "); - } - } - else - wrbuf_puts(w, "@prox 0 2 0 1 k 2 "); - break; - default: - wrbuf_puts(w, "@ bad op (unknown) "); - }; - ccl_pquery(w, p->u.p[0]); - ccl_pquery(w, p->u.p[1]); -} - -void ccl_pquery (WRBUF w, struct ccl_rpn_node *p) -{ - struct ccl_rpn_attr *att; - const char *cp; - - switch (p->kind) - { - case CCL_RPN_AND: - case CCL_RPN_OR: - case CCL_RPN_NOT: - case CCL_RPN_PROX: - ccl_pquery_complex (w, p); - break; - case CCL_RPN_SET: - wrbuf_puts (w, "@set "); - wrbuf_puts (w, p->u.setname); - wrbuf_puts (w, " "); - break; - case CCL_RPN_TERM: - for (att = p->u.t.attr_list; att; att = att->next) - { - char tmpattr[128]; - wrbuf_puts (w, "@attr "); - if (att->set) - { - wrbuf_puts (w, att->set); - wrbuf_puts (w, " "); - } - switch(att->kind) - { - case CCL_RPN_ATTR_NUMERIC: - sprintf(tmpattr, "%d=%d ", att->type, att->value.numeric); - wrbuf_puts (w, tmpattr); - break; - case CCL_RPN_ATTR_STRING: - sprintf(tmpattr, "%d=", att->type); - wrbuf_puts (w, tmpattr); - wrbuf_puts(w, att->value.str); - wrbuf_puts (w, " "); - break; - } - } - for (cp = p->u.t.term; *cp; cp++) - { - if (*cp == ' ' || *cp == '\\') - wrbuf_putc (w, '\\'); - wrbuf_putc (w, *cp); - } - wrbuf_puts (w, " "); - break; - } -} -- 1.7.10.4