| Akron | 84ae657 | 2017-02-03 19:26:36 +0100 | [diff] [blame] | 1 | use strict; |
| 2 | use warnings; |
| 3 | use Test::Krawfish; |
| 4 | use Test::More; |
| 5 | |
| 6 | use_ok('Krawfish::Index'); |
| 7 | use_ok('Krawfish::Koral::Query::Builder'); |
| 8 | |
| 9 | my $index = Krawfish::Index->new; |
| 10 | ok_index($index, '[aa|aa][bb|bb]', 'Add complex document'); |
| 11 | |
| 12 | my $qb = Krawfish::Koral::Query::Builder->new; |
| 13 | |
| 14 | my $wrap = $qb->constraints( |
| 15 | [$qb->c_position('precedesDirectly')], |
| 16 | $qb->token('aa'), |
| 17 | $qb->token('bb') |
| 18 | ); |
| 19 | |
| 20 | is($wrap->to_string, "constr(pos=precedesDirectly:[aa],[bb])", 'Query is valid'); |
| Akron | c552937 | 2017-06-21 15:56:18 +0200 | [diff] [blame] | 21 | ok($wrap = $wrap->normalize->finalize, 'Normalization'); |
| 22 | is($wrap->to_string, "constr(pos=precedesDirectly:aa,bb)", 'Query is valid'); |
| 23 | ok($wrap = $wrap->optimize($index), 'Optimization'); |
| 24 | is($wrap->to_string, "constr(pos=2:'aa','bb')", 'Query is valid'); |
| 25 | matches($wrap, [qw/[0:0-2] [0:0-2] [0:0-2] [0:0-2]/]); |
| Akron | 84ae657 | 2017-02-03 19:26:36 +0100 | [diff] [blame] | 26 | |
| Akron | 84ae657 | 2017-02-03 19:26:36 +0100 | [diff] [blame] | 27 | |
| 28 | # From t/query/positions.t |
| 29 | |
| 30 | $index = Krawfish::Index->new; |
| 31 | ok_index($index, [qw/aa bb aa bb/], 'Add new document'); |
| 32 | ok($wrap = $qb->constraints( |
| 33 | [$qb->c_position('precedesDirectly')], |
| 34 | $qb->token('aa'), |
| 35 | $qb->token('bb') |
| 36 | ), 'Sequence'); |
| Akron | c552937 | 2017-06-21 15:56:18 +0200 | [diff] [blame] | 37 | |
| 38 | ok(my $seq = $wrap->normalize->finalize->optimize($index), 'Optimization'); |
| 39 | |
| 40 | # ok(my $seq = $wrap->plan_for($index), 'Rewrite'); |
| Akron | 84ae657 | 2017-02-03 19:26:36 +0100 | [diff] [blame] | 41 | matches($seq, [qw/[0:0-2] [0:2-4]/]); |
| 42 | |
| 43 | |
| 44 | # Reset index - situation [aa]..[bb] -> [aa][bb] |
| 45 | $index = Krawfish::Index->new; |
| 46 | ok_index($index, '[aa][cc][aa][bb]', 'Add complex document'); |
| 47 | ok($qb = Krawfish::Koral::Query::Builder->new, 'Create Koral::Builder'); |
| 48 | ok($wrap = $qb->constraints( |
| 49 | [$qb->c_position('precedesDirectly')], |
| 50 | $qb->token('aa'), |
| 51 | $qb->token('bb') |
| 52 | ), 'Sequence'); |
| Akron | c552937 | 2017-06-21 15:56:18 +0200 | [diff] [blame] | 53 | ok($seq = $wrap->normalize->finalize->optimize($index), 'Rewrite'); |
| Akron | 84ae657 | 2017-02-03 19:26:36 +0100 | [diff] [blame] | 54 | matches($seq, [qw/[0:2-4]/]); |
| 55 | |
| 56 | |
| 57 | # Reset index - situation [bb][aa] -> [aa][bb] |
| 58 | $index = Krawfish::Index->new; |
| 59 | ok_index($index, '[bb][aa][bb][aa]', 'Add complex document'); |
| 60 | ok($qb = Krawfish::Koral::Query::Builder->new, 'Create Koral::Builder'); |
| 61 | ok($wrap = $qb->constraints( |
| 62 | [$qb->c_position('precedesDirectly')], |
| 63 | $qb->token('aa'), |
| 64 | $qb->token('bb') |
| 65 | ), 'Sequence'); |
| Akron | c552937 | 2017-06-21 15:56:18 +0200 | [diff] [blame] | 66 | ok($seq = $wrap->normalize->finalize->optimize($index), 'Rewrite'); |
| Akron | 84ae657 | 2017-02-03 19:26:36 +0100 | [diff] [blame] | 67 | matches($seq, [qw/[0:1-3]/]); |
| 68 | |
| 69 | |
| 70 | # Reset index - situation [aa]..[bb] -> [aa][bb] |
| 71 | $index = Krawfish::Index->new; |
| 72 | ok_index($index,'[aa][cc][aa][bb]', 'Add complex document'); |
| 73 | ok($qb = Krawfish::Koral::Query::Builder->new, 'Create Koral::Builder'); |
| 74 | ok($wrap = $qb->position(['precedesDirectly'],$qb->token('aa'), $qb->token('bb')), 'Sequence'); |
| Akron | c552937 | 2017-06-21 15:56:18 +0200 | [diff] [blame] | 75 | ok($seq = $wrap->normalize->finalize->optimize($index), 'Rewrite'); |
| Akron | 84ae657 | 2017-02-03 19:26:36 +0100 | [diff] [blame] | 76 | matches($seq, [qw/[0:2-4]/]); |
| 77 | |
| 78 | |
| Akron | c552937 | 2017-06-21 15:56:18 +0200 | [diff] [blame] | 79 | |
| 80 | |
| Akron | 84ae657 | 2017-02-03 19:26:36 +0100 | [diff] [blame] | 81 | # Reset index - situation [bb]..[aa] -> [aa][bb] |
| 82 | $index = Krawfish::Index->new; |
| 83 | ok_index($index,'[bb][cc][aa][bb]', 'Add complex document'); |
| 84 | ok($qb = Krawfish::Koral::Query::Builder->new($index), 'Create Koral::Builder'); |
| 85 | ok($wrap = $qb->constraints( |
| 86 | [$qb->c_position('precedesDirectly')], |
| 87 | $qb->token('aa'), |
| 88 | $qb->token('bb') |
| 89 | ), 'Sequence'); |
| Akron | c552937 | 2017-06-21 15:56:18 +0200 | [diff] [blame] | 90 | ok($seq = $wrap->normalize->finalize->optimize($index), 'Rewrite'); |
| Akron | 84ae657 | 2017-02-03 19:26:36 +0100 | [diff] [blame] | 91 | matches($seq, [qw/[0:2-4]/]); |
| 92 | |
| 93 | |
| 94 | |
| Akron | c552937 | 2017-06-21 15:56:18 +0200 | [diff] [blame] | 95 | |
| Akron | 84ae657 | 2017-02-03 19:26:36 +0100 | [diff] [blame] | 96 | # Multiple matches |
| 97 | # Reset index - situation [bb]..[aa] -> [aa][bb] |
| 98 | $index = Krawfish::Index->new; |
| 99 | ok_index($index,'[aa|aa][bb|bb]', 'Add complex document'); |
| 100 | ok($qb = Krawfish::Koral::Query::Builder->new, 'Create Koral::Builder'); |
| 101 | ok($wrap = $qb->constraints( |
| 102 | [$qb->c_position('precedesDirectly')], |
| 103 | $qb->token('aa'), |
| 104 | $qb->token('bb') |
| 105 | ), 'Sequence'); |
| Akron | c552937 | 2017-06-21 15:56:18 +0200 | [diff] [blame] | 106 | ok($seq = $wrap->normalize->finalize->optimize($index), 'Rewrite'); |
| Akron | 84ae657 | 2017-02-03 19:26:36 +0100 | [diff] [blame] | 107 | matches($seq, [qw/[0:0-2] [0:0-2] [0:0-2] [0:0-2]/]); |
| 108 | |
| 109 | |
| Akron | c552937 | 2017-06-21 15:56:18 +0200 | [diff] [blame] | 110 | |
| 111 | |
| Akron | 84ae657 | 2017-02-03 19:26:36 +0100 | [diff] [blame] | 112 | # Reset index |
| 113 | $index = Krawfish::Index->new; |
| 114 | ok_index($index, '[aa][bb|bb]', 'Add complex document'); |
| 115 | ok($qb = Krawfish::Koral::Query::Builder->new, 'Create Koral::Builder'); |
| 116 | ok($wrap = $qb->constraints( |
| 117 | [$qb->c_position('precedesDirectly')], |
| 118 | $qb->token('aa'), |
| 119 | $qb->token('bb') |
| 120 | ), 'Sequence'); |
| Akron | c552937 | 2017-06-21 15:56:18 +0200 | [diff] [blame] | 121 | ok($seq = $wrap->normalize->finalize->optimize($index), 'Rewrite'); |
| Akron | 84ae657 | 2017-02-03 19:26:36 +0100 | [diff] [blame] | 122 | # query language: [aa][bb] |
| 123 | matches($seq, [qw/[0:0-2] [0:0-2]/]); |
| 124 | |
| 125 | |
| 126 | # Reset index |
| 127 | $index = Krawfish::Index->new; |
| 128 | ok_index($index, '[aa|aa][bb]', 'Add complex document'); |
| 129 | ok($qb = Krawfish::Koral::Query::Builder->new($index), 'Create Koral::Builder'); |
| 130 | ok($wrap = $qb->constraints( |
| 131 | [$qb->c_position('precedesDirectly')], |
| 132 | $qb->token('aa'), |
| 133 | $qb->token('bb') |
| 134 | ), 'Sequence'); |
| Akron | c552937 | 2017-06-21 15:56:18 +0200 | [diff] [blame] | 135 | ok($seq = $wrap->normalize->finalize->optimize($index), 'Rewrite'); |
| Akron | 84ae657 | 2017-02-03 19:26:36 +0100 | [diff] [blame] | 136 | matches($seq, [qw/[0:0-2] [0:0-2]/]); |
| 137 | |
| 138 | |
| Akron | c552937 | 2017-06-21 15:56:18 +0200 | [diff] [blame] | 139 | |
| Akron | 84ae657 | 2017-02-03 19:26:36 +0100 | [diff] [blame] | 140 | # Reset index |
| 141 | $index = Krawfish::Index->new; |
| 142 | ok_index($index, '[aa|aa][bb|bb][aa|aa][bb|bb]', 'Add complex document'); |
| 143 | ok($qb = Krawfish::Koral::Query::Builder->new, 'Create Koral::Builder'); |
| 144 | ok($wrap = $qb->constraints( |
| 145 | [$qb->c_position('precedesDirectly')], |
| 146 | $qb->token('aa'), |
| 147 | $qb->token('bb') |
| 148 | ), 'Sequence'); |
| Akron | c552937 | 2017-06-21 15:56:18 +0200 | [diff] [blame] | 149 | ok($seq = $wrap->normalize->finalize->optimize($index), 'Rewrite'); |
| Akron | 84ae657 | 2017-02-03 19:26:36 +0100 | [diff] [blame] | 150 | matches($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 | |
| Akron | c552937 | 2017-06-21 15:56:18 +0200 | [diff] [blame] | 154 | |
| Akron | 84ae657 | 2017-02-03 19:26:36 +0100 | [diff] [blame] | 155 | # Reset index |
| 156 | $index = Krawfish::Index->new; |
| 157 | ok_index($index, '[aa|aa][bb|bb][aa|aa][bb|bb]', 'Add complex document'); |
| 158 | ok_index($index, '[aa]', 'Add complex document'); |
| 159 | ok_index($index, '[bb]', 'Add complex document'); |
| 160 | ok_index($index, '[aa|aa][bb|bb][aa|aa][bb|bb]', 'Add complex document'); |
| 161 | ok($qb = Krawfish::Koral::Query::Builder->new, 'Create Koral::Builder'); |
| 162 | ok($wrap = $qb->constraints( |
| 163 | [$qb->c_position('precedesDirectly')], |
| 164 | $qb->token('aa'), |
| 165 | $qb->token('bb') |
| 166 | ), 'Sequence'); |
| Akron | c552937 | 2017-06-21 15:56:18 +0200 | [diff] [blame] | 167 | ok($seq = $wrap->normalize->finalize->optimize($index), 'Rewrite'); |
| Akron | 84ae657 | 2017-02-03 19:26:36 +0100 | [diff] [blame] | 168 | matches($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 | |
| Akron | c552937 | 2017-06-21 15:56:18 +0200 | [diff] [blame] | 174 | |
| Akron | 84ae657 | 2017-02-03 19:26:36 +0100 | [diff] [blame] | 175 | ## Overlap |
| 176 | $index = Krawfish::Index->new; |
| 177 | ok_index($index, '[aa|bb][aa|bb][aa|bb][aa|bb]', 'Add new document'); |
| 178 | ok($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 | |
| Akron | c552937 | 2017-06-21 15:56:18 +0200 | [diff] [blame] | 184 | is($wrap->to_string, 'constr(pos=overlapsLeft:{1:[aa]+},{2:[bb]+})', 'Stringification'); |
| 185 | ok($wrap = $wrap->normalize->finalize, 'Rewrite'); |
| 186 | is($wrap->to_string, 'constr(pos=overlapsLeft:{1:aa{1,100}},{2:bb{1,100}})', |
| 187 | 'Stringification'); |
| 188 | ok(my $ov = $wrap->optimize($index), 'Optimization'); |
| 189 | is($ov->to_string, "constr(pos=4:class(1:rep(1-100:'aa')),class(2:rep(1-100:'bb')))", |
| 190 | 'Stringification'); |
| 191 | |
| Akron | 84ae657 | 2017-02-03 19:26:36 +0100 | [diff] [blame] | 192 | |
| 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 | |
| 199 | ok($ov->next, 'Init'); |
| 200 | is($ov->current->to_string, '[0:0-3$0,1,0,2|0,2,1,3]', 'Match'); |
| 201 | ok($ov->next, 'More'); |
| 202 | is($ov->current->to_string, '[0:0-4$0,1,0,2|0,2,1,4]', 'Match'); |
| 203 | ok($ov->next, 'More'); |
| 204 | is($ov->current->to_string, '[0:0-4$0,1,0,3|0,2,1,4]', 'Match'); |
| 205 | ok($ov->next, 'More'); |
| 206 | is($ov->current->to_string, '[0:0-4$0,1,0,3|0,2,2,4]', 'Match'); |
| 207 | ok($ov->next, 'More'); |
| 208 | is($ov->current->to_string, '[0:1-4$0,1,1,3|0,2,2,4]', 'Match'); |
| 209 | ok(!$ov->next, 'No More'); |
| 210 | |
| 211 | |
| 212 | diag 'Test further'; |
| 213 | |
| 214 | |
| 215 | done_testing; |
| 216 | __END__ |