blob: ef5add8346b252f71eda3fc82b63da0943c3aaee [file] [log] [blame]
Akron93271d82016-11-24 09:18:41 +01001package Krawfish::Koral::Corpus::FieldGroup;
Akronacde0ba2017-12-08 14:05:13 +01002use Role::Tiny::With;
Akron93271d82016-11-24 09:18:41 +01003use Krawfish::Log;
Akron2ea61aa2017-06-03 16:30:23 +02004use Krawfish::Koral::Corpus::AndNot;
Akron655a10a2017-09-11 14:13:18 +02005use Krawfish::Koral::Corpus::Anywhere;
Akron93271d82016-11-24 09:18:41 +01006use Krawfish::Corpus::Or;
Akron349747d2016-12-05 11:05:53 +01007use Krawfish::Corpus::And;
Akron93271d82016-11-24 09:18:41 +01008use strict;
9use warnings;
10
Akronacde0ba2017-12-08 14:05:13 +010011with 'Krawfish::Koral::Util::Boolean';
12with 'Krawfish::Koral::Corpus';
13
Akron2ea61aa2017-06-03 16:30:23 +020014# TODO:
15# Preparation should be:
16# -> normalize()
17# -> finalize()
18# -> memoize(cache)
19# -> optimize(index)
20
Akron4f9eef42017-07-24 11:41:09 +020021
22# TODO:
23# In normalization phase make
24# X geq Y & X leq Y -> X eq Y
Akron965f5d92017-01-20 18:38:08 +010025
Akronbc7dd432017-07-18 14:21:51 +020026use constant DEBUG => 0;
Akron93271d82016-11-24 09:18:41 +010027
Akron2c6c7162017-05-15 18:15:33 +020028
Akron93271d82016-11-24 09:18:41 +010029sub new {
30 my $class = shift;
31 bless {
32 operation => shift,
33 operands => [@_]
34 }, $class;
35};
36
Akron2c6c7162017-05-15 18:15:33 +020037
Akron93271d82016-11-24 09:18:41 +010038sub type {
39 'fieldGroup';
40};
41
Akron2c6c7162017-05-15 18:15:33 +020042
Akron93271d82016-11-24 09:18:41 +010043sub operation {
Akron5b6264f2017-07-19 01:14:01 +020044 my $self = shift;
45 if (@_) {
46 $self->{operation} = shift;
47 return $self;
48 };
49 $self->{operation};
Akron93271d82016-11-24 09:18:41 +010050};
51
Akron2c6c7162017-05-15 18:15:33 +020052
Akronb945c572017-07-23 14:55:00 +020053# normalize() is provided by Boolean
54
55# optimize() is provided by Boolean
56
57sub bool_and_query {
Akron0e782bc2017-05-14 14:04:41 +020058 my $self = shift;
Akronb945c572017-07-23 14:55:00 +020059 Krawfish::Corpus::And->new(
60 $_[0],
61 $_[1]
62 );
Akron0e782bc2017-05-14 14:04:41 +020063};
64
Akronb945c572017-07-23 14:55:00 +020065sub bool_or_query {
66 my $self = shift;
67 Krawfish::Corpus::Or->new(
68 $_[0],
69 $_[1]
70 );
Akron2ea61aa2017-06-03 16:30:23 +020071};
72
Akron655a10a2017-09-11 14:13:18 +020073#sub is_anywhere {
Akronce10cb42017-06-14 01:12:40 +020074# my $self = shift;
Akron5a5595b2017-09-10 13:00:57 +020075# return 0 if $self->is_nowhere;
Akronce10cb42017-06-14 01:12:40 +020076# return 1 if @{$self->operands} == 0;
77# return 0;
78#};
79
Akron2ea61aa2017-06-03 16:30:23 +020080# Check for classes
81sub has_classes {
82 my $self = shift;
83
84 # Check operands for classes
85 foreach (@{$self->operands}) {
86
87 # Has classes
88 return 1 if $_->has_classes;
89 };
90 return;
91};
92
93
94# Return koral
95sub to_koral_fragment {
96 my $self = shift;
97 return {
98 '@type' => 'koral:fieldGroup',
99 operation => 'operation:' . $self->operation,
100 operands => [ map { $_->to_koral_fragment } @{$self->{operands}} ]
101 };
102};
103
104
105sub to_string {
Akron321d2002017-10-11 19:15:52 +0200106 my ($self, $id) = @_;
Akron2ea61aa2017-06-03 16:30:23 +0200107 my $op = $self->operation eq 'and' ? '&' : '|';
108
109 my $str = $self->is_negative ? '!(' : '';
110
111 $str .= join($op, map {
Akronc5529372017-06-21 15:56:18 +0200112 $_ ? (
113 $_->type eq 'fieldGroup' ?
114 (
Akron655a10a2017-09-11 14:13:18 +0200115 $_->is_anywhere ?
Akronc5529372017-06-21 15:56:18 +0200116 '[1]' :
Akron321d2002017-10-11 19:15:52 +0200117 '(' . $_->to_string($id) . ')'
Akronc5529372017-06-21 15:56:18 +0200118 )
119 :
Akron3d1df332017-12-23 16:21:21 +0100120 $_->to_string($id)
Akronc5529372017-06-21 15:56:18 +0200121 ) : '()'
Akron2ea61aa2017-06-03 16:30:23 +0200122 } @{$self->operands_in_order});
123
124 $str .= $self->is_negative ? ')' : '';
125 $str;
126};
127
128
Akronacde0ba2017-12-08 14:05:13 +0100129sub from_koral {
130 ...
131};
132
133
Akron8a271422017-06-08 01:58:32 +02001341;
135
136
137__END__
Akron2ea61aa2017-06-03 16:30:23 +0200138