blob: d63f05e33ff6d9242f3c3445323aa6580e4fcdd7 [file] [log] [blame]
Akron84ae6572017-02-03 19:26:36 +01001use strict;
2use warnings;
3use Test::Krawfish;
4use Test::More;
5
6use_ok('Krawfish::Index');
7use_ok('Krawfish::Koral::Query::Builder');
8
9my $index = Krawfish::Index->new;
10ok_index($index, '[aa|aa][bb|bb]', 'Add complex document');
11
12my $qb = Krawfish::Koral::Query::Builder->new;
13
14my $wrap = $qb->constraints(
15 [$qb->c_position('precedesDirectly')],
16 $qb->token('aa'),
17 $qb->token('bb')
18);
19
20is($wrap->to_string, "constr(pos=precedesDirectly:[aa],[bb])", 'Query is valid');
Akronc5529372017-06-21 15:56:18 +020021ok($wrap = $wrap->normalize->finalize, 'Normalization');
22is($wrap->to_string, "constr(pos=precedesDirectly:aa,bb)", 'Query is valid');
23ok($wrap = $wrap->optimize($index), 'Optimization');
24is($wrap->to_string, "constr(pos=2:'aa','bb')", 'Query is valid');
25matches($wrap, [qw/[0:0-2] [0:0-2] [0:0-2] [0:0-2]/]);
Akron84ae6572017-02-03 19:26:36 +010026
Akron84ae6572017-02-03 19:26:36 +010027
28# From t/query/positions.t
29
30$index = Krawfish::Index->new;
31ok_index($index, [qw/aa bb aa bb/], 'Add new document');
32ok($wrap = $qb->constraints(
33 [$qb->c_position('precedesDirectly')],
34 $qb->token('aa'),
35 $qb->token('bb')
36), 'Sequence');
Akronc5529372017-06-21 15:56:18 +020037
38ok(my $seq = $wrap->normalize->finalize->optimize($index), 'Optimization');
39
40# ok(my $seq = $wrap->plan_for($index), 'Rewrite');
Akron84ae6572017-02-03 19:26:36 +010041matches($seq, [qw/[0:0-2] [0:2-4]/]);
42
43
44# Reset index - situation [aa]..[bb] -> [aa][bb]
45$index = Krawfish::Index->new;
46ok_index($index, '[aa][cc][aa][bb]', 'Add complex document');
47ok($qb = Krawfish::Koral::Query::Builder->new, 'Create Koral::Builder');
48ok($wrap = $qb->constraints(
49 [$qb->c_position('precedesDirectly')],
50 $qb->token('aa'),
51 $qb->token('bb')
52), 'Sequence');
Akronc5529372017-06-21 15:56:18 +020053ok($seq = $wrap->normalize->finalize->optimize($index), 'Rewrite');
Akron84ae6572017-02-03 19:26:36 +010054matches($seq, [qw/[0:2-4]/]);
55
56
57# Reset index - situation [bb][aa] -> [aa][bb]
58$index = Krawfish::Index->new;
59ok_index($index, '[bb][aa][bb][aa]', 'Add complex document');
60ok($qb = Krawfish::Koral::Query::Builder->new, 'Create Koral::Builder');
61ok($wrap = $qb->constraints(
62 [$qb->c_position('precedesDirectly')],
63 $qb->token('aa'),
64 $qb->token('bb')
65), 'Sequence');
Akronc5529372017-06-21 15:56:18 +020066ok($seq = $wrap->normalize->finalize->optimize($index), 'Rewrite');
Akron84ae6572017-02-03 19:26:36 +010067matches($seq, [qw/[0:1-3]/]);
68
69
70# Reset index - situation [aa]..[bb] -> [aa][bb]
71$index = Krawfish::Index->new;
72ok_index($index,'[aa][cc][aa][bb]', 'Add complex document');
73ok($qb = Krawfish::Koral::Query::Builder->new, 'Create Koral::Builder');
74ok($wrap = $qb->position(['precedesDirectly'],$qb->token('aa'), $qb->token('bb')), 'Sequence');
Akronc5529372017-06-21 15:56:18 +020075ok($seq = $wrap->normalize->finalize->optimize($index), 'Rewrite');
Akron84ae6572017-02-03 19:26:36 +010076matches($seq, [qw/[0:2-4]/]);
77
78
Akronc5529372017-06-21 15:56:18 +020079
80
Akron84ae6572017-02-03 19:26:36 +010081# Reset index - situation [bb]..[aa] -> [aa][bb]
82$index = Krawfish::Index->new;
83ok_index($index,'[bb][cc][aa][bb]', 'Add complex document');
84ok($qb = Krawfish::Koral::Query::Builder->new($index), 'Create Koral::Builder');
85ok($wrap = $qb->constraints(
86 [$qb->c_position('precedesDirectly')],
87 $qb->token('aa'),
88 $qb->token('bb')
89), 'Sequence');
Akronc5529372017-06-21 15:56:18 +020090ok($seq = $wrap->normalize->finalize->optimize($index), 'Rewrite');
Akron84ae6572017-02-03 19:26:36 +010091matches($seq, [qw/[0:2-4]/]);
92
93
94
Akronc5529372017-06-21 15:56:18 +020095
Akron84ae6572017-02-03 19:26:36 +010096# Multiple matches
97# Reset index - situation [bb]..[aa] -> [aa][bb]
98$index = Krawfish::Index->new;
99ok_index($index,'[aa|aa][bb|bb]', 'Add complex document');
100ok($qb = Krawfish::Koral::Query::Builder->new, 'Create Koral::Builder');
101ok($wrap = $qb->constraints(
102 [$qb->c_position('precedesDirectly')],
103 $qb->token('aa'),
104 $qb->token('bb')
105), 'Sequence');
Akronc5529372017-06-21 15:56:18 +0200106ok($seq = $wrap->normalize->finalize->optimize($index), 'Rewrite');
Akron84ae6572017-02-03 19:26:36 +0100107matches($seq, [qw/[0:0-2] [0:0-2] [0:0-2] [0:0-2]/]);
108
109
Akronc5529372017-06-21 15:56:18 +0200110
111
Akron84ae6572017-02-03 19:26:36 +0100112# Reset index
113$index = Krawfish::Index->new;
114ok_index($index, '[aa][bb|bb]', 'Add complex document');
115ok($qb = Krawfish::Koral::Query::Builder->new, 'Create Koral::Builder');
116ok($wrap = $qb->constraints(
117 [$qb->c_position('precedesDirectly')],
118 $qb->token('aa'),
119 $qb->token('bb')
120), 'Sequence');
Akronc5529372017-06-21 15:56:18 +0200121ok($seq = $wrap->normalize->finalize->optimize($index), 'Rewrite');
Akron84ae6572017-02-03 19:26:36 +0100122# query language: [aa][bb]
123matches($seq, [qw/[0:0-2] [0:0-2]/]);
124
125
126# Reset index
127$index = Krawfish::Index->new;
128ok_index($index, '[aa|aa][bb]', 'Add complex document');
129ok($qb = Krawfish::Koral::Query::Builder->new($index), 'Create Koral::Builder');
130ok($wrap = $qb->constraints(
131 [$qb->c_position('precedesDirectly')],
132 $qb->token('aa'),
133 $qb->token('bb')
134), 'Sequence');
Akronc5529372017-06-21 15:56:18 +0200135ok($seq = $wrap->normalize->finalize->optimize($index), 'Rewrite');
Akron84ae6572017-02-03 19:26:36 +0100136matches($seq, [qw/[0:0-2] [0:0-2]/]);
137
138
Akronc5529372017-06-21 15:56:18 +0200139
Akron84ae6572017-02-03 19:26:36 +0100140# Reset index
141$index = Krawfish::Index->new;
142ok_index($index, '[aa|aa][bb|bb][aa|aa][bb|bb]', 'Add complex document');
143ok($qb = Krawfish::Koral::Query::Builder->new, 'Create Koral::Builder');
144ok($wrap = $qb->constraints(
145 [$qb->c_position('precedesDirectly')],
146 $qb->token('aa'),
147 $qb->token('bb')
148), 'Sequence');
Akronc5529372017-06-21 15:56:18 +0200149ok($seq = $wrap->normalize->finalize->optimize($index), 'Rewrite');
Akron84ae6572017-02-03 19:26:36 +0100150matches($seq, [qw/[0:0-2] [0:0-2] [0:0-2] [0:0-2] [0:2-4] [0:2-4] [0:2-4] [0:2-4]/]);
151
152
153
Akronc5529372017-06-21 15:56:18 +0200154
Akron84ae6572017-02-03 19:26:36 +0100155# Reset index
156$index = Krawfish::Index->new;
157ok_index($index, '[aa|aa][bb|bb][aa|aa][bb|bb]', 'Add complex document');
158ok_index($index, '[aa]', 'Add complex document');
159ok_index($index, '[bb]', 'Add complex document');
160ok_index($index, '[aa|aa][bb|bb][aa|aa][bb|bb]', 'Add complex document');
161ok($qb = Krawfish::Koral::Query::Builder->new, 'Create Koral::Builder');
162ok($wrap = $qb->constraints(
163 [$qb->c_position('precedesDirectly')],
164 $qb->token('aa'),
165 $qb->token('bb')
166), 'Sequence');
Akronc5529372017-06-21 15:56:18 +0200167ok($seq = $wrap->normalize->finalize->optimize($index), 'Rewrite');
Akron84ae6572017-02-03 19:26:36 +0100168matches($seq, [
169 qw/[0:0-2] [0:0-2] [0:0-2] [0:0-2] [0:2-4] [0:2-4] [0:2-4] [0:2-4]/,
170 qw/[3:0-2] [3:0-2] [3:0-2] [3:0-2] [3:2-4] [3:2-4] [3:2-4] [3:2-4]/
171]);
172
173
Akronc5529372017-06-21 15:56:18 +0200174
Akron84ae6572017-02-03 19:26:36 +0100175## Overlap
176$index = Krawfish::Index->new;
177ok_index($index, '[aa|bb][aa|bb][aa|bb][aa|bb]', 'Add new document');
178ok($wrap = $qb->position(
179 ['overlapsLeft'],
180 $qb->class($qb->repeat($qb->token('aa'), 1, undef),1),
181 $qb->class($qb->repeat($qb->token('bb'), 1, undef),2)
182), 'Sequence');
183
Akronc5529372017-06-21 15:56:18 +0200184is($wrap->to_string, 'constr(pos=overlapsLeft:{1:[aa]+},{2:[bb]+})', 'Stringification');
185ok($wrap = $wrap->normalize->finalize, 'Rewrite');
186is($wrap->to_string, 'constr(pos=overlapsLeft:{1:aa{1,100}},{2:bb{1,100}})',
187 'Stringification');
188ok(my $ov = $wrap->optimize($index), 'Optimization');
189is($ov->to_string, "constr(pos=4:class(1:rep(1-100:'aa')),class(2:rep(1-100:'bb')))",
190 'Stringification');
191
Akron84ae6572017-02-03 19:26:36 +0100192
193# [<0 {1> 2}] 3
194# [<0 {1> 2 3}]
195# [<0 {1 2> 3}]
196# [<0 1 {2> 3}]
197# 0 [<1 {2> 3}]
198
199ok($ov->next, 'Init');
200is($ov->current->to_string, '[0:0-3$0,1,0,2|0,2,1,3]', 'Match');
201ok($ov->next, 'More');
202is($ov->current->to_string, '[0:0-4$0,1,0,2|0,2,1,4]', 'Match');
203ok($ov->next, 'More');
204is($ov->current->to_string, '[0:0-4$0,1,0,3|0,2,1,4]', 'Match');
205ok($ov->next, 'More');
206is($ov->current->to_string, '[0:0-4$0,1,0,3|0,2,2,4]', 'Match');
207ok($ov->next, 'More');
208is($ov->current->to_string, '[0:1-4$0,1,1,3|0,2,2,4]', 'Match');
209ok(!$ov->next, 'No More');
210
211
212diag 'Test further';
213
214
215done_testing;
216__END__