OPTiMaDe-Filter-0.7.0/0000755000200400020040000000000013620716603014165 5ustar andriusandriusOPTiMaDe-Filter-0.7.0/parse0000755000200400020040000000216413573430201015222 0ustar andriusandrius#!/usr/bin/env perl use strict; use warnings; use Data::Compare; use Data::Dumper; use OPTiMaDe::Filter::Parser; use Scalar::Util qw(blessed); $Data::Dumper::Sortkeys = 1; my %options = map { $_ => 1 } grep { /^--/ } @ARGV; @ARGV = grep { !/^--/ } @ARGV; if( $options{'--allow-LIKE-operator'} ) { $OPTiMaDe::Filter::Parser::allow_LIKE_operator = 1; } my $use_placeholders = 0; if( $options{'--use-placeholders'} ) { $use_placeholders = 1; } @ARGV = ( '-' ) unless @ARGV; for (@ARGV) { my $parser = new OPTiMaDe::Filter::Parser; my $tree = $parser->Run( $_ ); print Dumper $tree; print "== Filter ==\n"; print $tree->to_filter . "\n"; print "== SQL ==\n"; if( $use_placeholders ) { my( $sql, $values ) = $tree->to_SQL( { placeholder => '?' } ); print "$sql\n"; print "=== Values ===\n"; print Dumper $values; } else { print $tree->to_SQL . "\n"; } my $filter = $tree->to_filter; $parser = new OPTiMaDe::Filter::Parser; my $tree_now = $parser->parse_string( $filter ); Compare( $tree, $tree_now ) || print "Roundtrip NOT passed\n"; } OPTiMaDe-Filter-0.7.0/Parser.yp0000644000200400020040000003375013574402571016007 0ustar andriusandrius# Header section %{ use warnings; use Scalar::Util qw(blessed); use OPTiMaDe::Filter::AndOr; use OPTiMaDe::Filter::Comparison; use OPTiMaDe::Filter::Known; use OPTiMaDe::Filter::ListComparison; use OPTiMaDe::Filter::Negation; use OPTiMaDe::Filter::Property; use OPTiMaDe::Filter::Zip; our $allow_LIKE_operator = 0; %} %% # Rules section # The top-level 'filter' rule filter: expression ; # Values constant: string | number ; value: string | number | property ; value_list: value { return [ [ '=', $_[1] ] ]; } | operator value { return [ [ @_[1..$#_] ] ]; } | value_list comma value { push @{$_[1]}, [ '=', $_[3] ]; return $_[1]; } | value_list comma operator value { push @{$_[1]}, [ $_[3], $_[4] ]; return $_[1]; } ; value_zip: value value_zip_part { return [ [ '=', $_[1] ], $_[2] ]; } | operator value value_zip_part { return [ [ $_[1], $_[2] ], $_[3] ]; } | value_zip value_zip_part { push @{$_[1]}, $_[2]; return $_[1]; } ; value_zip_part: colon value { return [ '=', $_[2] ]; } | colon operator value { return [ $_[2], $_[3] ]; } ; value_zip_list: value_zip { return [ $_[1] ]; } | value_zip_list comma value_zip { push @{$_[1]}, $_[3]; return $_[1]; } ; # Expressions expression: expression_clause | expression_clause OR expression { return OPTiMaDe::Filter::AndOr->new( @_[1..$#_] ); } ; expression_clause: expression_phrase | expression_phrase AND expression_clause { return OPTiMaDe::Filter::AndOr->new( @_[1..$#_] ); } ; expression_phrase: comparison | openingbrace expression closingbrace { return $_[2]; } | NOT comparison { return OPTiMaDe::Filter::Negation->new( $_[2] ); } | NOT openingbrace expression closingbrace { return OPTiMaDe::Filter::Negation->new( $_[3] ); } ; comparison: constant_first_comparison | property_first_comparison ; constant_first_comparison: constant value_op_rhs { $_[2]->unshift_operand( $_[1] ); return $_[2]; } ; property_first_comparison: property value_op_rhs { $_[2]->unshift_operand( $_[1] ); return $_[2]; } | property known_op_rhs { $_[2]->property( $_[1] ); return $_[2]; } | property fuzzy_string_op_rhs { $_[2]->unshift_operand( $_[1] ); return $_[2]; } | property set_op_rhs { $_[2]->property( $_[1] ); return $_[2]; } | property set_zip_op_rhs { $_[2]->unshift_property( $_[1] ); return $_[2]; } | property length_op_rhs { $_[2]->property( $_[1] ); return $_[2]; } ; value_op_rhs: operator value { my $cmp = OPTiMaDe::Filter::Comparison->new( $_[1] ); $cmp->push_operand( $_[2] ); return $cmp; } ; known_op_rhs: IS KNOWN { return OPTiMaDe::Filter::Known->new( 1 ); } | IS UNKNOWN { return OPTiMaDe::Filter::Known->new( 0 ); } ; fuzzy_string_op_rhs: CONTAINS value { my $cmp = OPTiMaDe::Filter::Comparison->new( $_[1] ); $cmp->push_operand( $_[2] ); return $cmp; } | STARTS value { my $cmp = OPTiMaDe::Filter::Comparison->new( $_[1] ); $cmp->push_operand( $_[2] ); return $cmp; } | STARTS WITH value { my $cmp = OPTiMaDe::Filter::Comparison->new( "$_[1] $_[2]" ); $cmp->push_operand( $_[3] ); return $cmp; } | ENDS value { my $cmp = OPTiMaDe::Filter::Comparison->new( $_[1] ); $cmp->push_operand( $_[2] ); return $cmp; } | ENDS WITH value { my $cmp = OPTiMaDe::Filter::Comparison->new( "$_[1] $_[2]" ); $cmp->push_operand( $_[3] ); return $cmp; } | LIKE value { my $cmp = OPTiMaDe::Filter::Comparison->new( $_[1] ); $cmp->push_operand( $_[2] ); return $cmp; } ; set_op_rhs: HAS value { my $lc = OPTiMaDe::Filter::ListComparison->new( $_[1] ); $lc->values( [ [ '=', $_[2] ] ] ); return $lc; } | HAS operator value { my $lc = OPTiMaDe::Filter::ListComparison->new( $_[1] ); $lc->values( [ [ $_[2], $_[3] ] ] ); return $lc; } | HAS ALL value_list { my $lc = OPTiMaDe::Filter::ListComparison->new( "$_[1] $_[2]" ); $lc->values( $_[3] ); return $lc; } | HAS ANY value_list { my $lc = OPTiMaDe::Filter::ListComparison->new( "$_[1] $_[2]" ); $lc->values( $_[3] ); return $lc; } | HAS ONLY value_list { my $lc = OPTiMaDe::Filter::ListComparison->new( "$_[1] $_[2]" ); $lc->values( $_[3] ); return $lc; } ; set_zip_op_rhs: property_zip_addon HAS value_zip { $_[1]->operator( $_[2] ); $_[1]->values( [ $_[3] ] ); return $_[1]; } | property_zip_addon HAS ONLY value_zip_list { $_[1]->operator( "$_[2] $_[3]" ); $_[1]->values( $_[4] ); return $_[1]; } | property_zip_addon HAS ALL value_zip_list { $_[1]->operator( "$_[2] $_[3]" ); $_[1]->values( $_[4] ); return $_[1]; } | property_zip_addon HAS ANY value_zip_list { $_[1]->operator( "$_[2] $_[3]" ); $_[1]->values( $_[4] ); return $_[1]; } ; property_zip_addon: colon property { my $zip = OPTiMaDe::Filter::Zip->new; $zip->push_property( $_[2] ); return $zip; } | property_zip_addon colon property { $_[1]->push_property( $_[3] ); return $_[1]; } ; length_op_rhs: LENGTH value { my $cmp = OPTiMaDe::Filter::ListComparison->new( $_[1] ); $cmp->values( [ [ '=', $_[2] ] ] ); return $cmp; } | LENGTH operator value { my $cmp = OPTiMaDe::Filter::ListComparison->new( $_[1] ); $cmp->values( [ [ $_[2], $_[3] ] ] ); return $cmp; } ; # Property property: identifier { return OPTiMaDe::Filter::Property->new( $_[1] ); } | property dot identifier { push @{$_[1]}, $_[3]; return $_[1]; } ; # Separators openingbrace: '(' ; closingbrace: ')' ; dot: '.' ; comma: ',' ; colon: ':' ; # OperatorComparison operator tokens operator: '<' | '<' '=' { return join( '', @_[1..$#_] ); } | '>' | '>' '=' { return join( '', @_[1..$#_] ); } | '=' | '!' '=' { return join( '', @_[1..$#_] ); } ; %% # Footer section sub _Error { my( $self ) = @_; close $self->{USER}{FILEIN} if $self->{USER}{FILEIN}; my $msg = "$0: syntax error at line $self->{USER}{LINENO}, " . "position $self->{USER}{CHARNO}"; if( $self->YYData->{INPUT} ) { $self->YYData->{INPUT} =~ s/\n$//; die "$msg: '" . $self->YYData->{INPUT} . "'.\n"; } else { die "$msg.\n"; } } sub _Lexer { my( $self ) = @_; # If the line is empty and the input is originating from the file, # another line is read. if( !$self->YYData->{INPUT} && $self->{USER}{FILEIN} ) { my $filein = $self->{USER}{FILEIN}; $self->YYData->{INPUT} = <$filein>; $self->{USER}{LINENO} = -1 unless exists $self->{USER}{LINENO}; $self->{USER}{LINENO}++; $self->{USER}{CHARNO} = 0; } $self->YYData->{INPUT} =~ s/^(\s+)//; $self->{USER}{CHARNO} += length( $1 ) if defined $1; # Escaped double quote or backslash are detected here and returned # as is to the caller in order to be detected as syntax errors. if( $self->YYData->{INPUT} =~ s/^(\\"|\\\\)// ) { $self->{USER}{CHARNO} += length( $1 ); return( $1, $1 ); } # Handling strings if( $self->YYData->{INPUT} =~ s/^"// ) { $self->{USER}{CHARNO} ++; my $string = ''; while( 1 ) { if( $self->YYData->{INPUT} =~ s/^([A-Za-z_0-9 \t!#\$\%&\'\(\)\*\+,\-\.\/\:;<=>\?@\[\]\^`\{\|\}\~\P{ASCII}]+)// ) { $self->{USER}{CHARNO} += length( $1 ); $string .= $1; } elsif( $self->YYData->{INPUT} =~ s/^\\([\\"])// ) { $self->{USER}{CHARNO} ++; $string .= $1; next; } elsif( $self->YYData->{INPUT} =~ s/^"// ) { $self->{USER}{CHARNO} ++; return( 'string', $string ); } else { return( undef, undef ); } } } # Handling identifiers if( $self->YYData->{INPUT} =~ s/^([a-z_][a-z0-9_]*)// ) { $self->{USER}{CHARNO} += length( $1 ); return( 'identifier', $1 ); } # Handling boolean relations if( $self->YYData->{INPUT} =~ s/^(AND|NOT|OR| IS|UNKNOWN|KNOWN| CONTAINS|STARTS|ENDS|WITH| LENGTH|HAS|ALL|ONLY|ANY)//x ) { $self->{USER}{CHARNO} += length( $1 ); return( $1, $1 ); } # Handling LIKE operator if allowed if( $allow_LIKE_operator && $self->YYData->{INPUT} =~ s/^(LIKE)// ) { $self->{USER}{CHARNO} += length( $1 ); return( $1, $1 ); } # Handling numbers if( $self->YYData->{INPUT} =~ s/^([+-]? (\d+\.?\d*|\.\d+) ([eE][+-]?\d+)?)//x ) { $self->{USER}{CHARNO} += length( $1 ); return( 'number', $1 ); } my $char = substr( $self->YYData->{INPUT}, 0, 1 ); if( $char ne '' ) { $self->YYData->{INPUT} = substr( $self->YYData->{INPUT}, 1 ); } $self->{USER}{CHARNO}++; return( $char, $char ); } sub Run { my( $self, $filename ) = @_; open $self->{USER}{FILEIN}, $filename; my $result = $self->YYParse( yylex => \&_Lexer, yyerror => \&_Error ); close $self->{USER}{FILEIN}; return $result; } sub parse_string { my( $self, $string ) = @_; $self->YYData->{INPUT} = $string; $self->{USER}{LINENO} = 0; $self->{USER}{CHARNO} = 0; return $self->YYParse( yylex => \&_Lexer, yyerror => \&_Error ); } sub modify { my $node = shift; my $code = shift; if( blessed $node && $node->can( 'modify' ) ) { return $node->modify( $code, @_ ); } elsif( ref $node eq 'ARRAY' ) { return [ map { modify( $_, $code, @_ ) } @$node ]; } else { return $code->( $node, @_ ); } } 1; OPTiMaDe-Filter-0.7.0/Changes0000644000200400020040000000431513620716514015464 0ustar andriusandrius0.7.0 2020-01-12 - Acknowledging conformance to OPTiMaDe specification v1.0.0-rc.1. - Validating objects prior to generating their representations in order not to produce erroneous representations. - Fixing the build system as it did not build the Yapp parser properly. - Adding repository metadata (thanks Mohammad S Anwar). 0.6.1 2020-01-30 - Fixing the build system to create source-only tarballs, and to build the parser via 'perl Makefile.PL && make'. 0.6.0 2019-12-12 - Changing the syntax of 'LENGTH' comparison as per Materials-Consortia/OPTiMaDe#221. 0.5.0 2019-12-09 - Renaming package from OPTiMaDe::FilterParser to OPTiMaDe::Filter. - Implementing right-hand side properties as per Materials-Consortia/OPTiMaDe#178. - Properties of three or more identifiers have no SQL representation. - Replacing OPTiMaDe::Filter::Comparison::set_operator() with operator(). - Replacing OPTiMaDe::Filter::ListComparison::set_() with (). - Replacing OPTiMaDe::Filter::Zip::set_() with (). 0.4.1 2019-12-05 - Fixing SQL placeholders for fuzzy search of strings. 0.4.0 2019-12-05 - Adding overloads for OPTiMaDe::FilterParser::Property. Replacing push_identifier() via direct push to object. - Adding convenience methods left() and right() for OPTiMaDe::FilterParser::Comparison. - Introducing classes OPTiMaDe::FilterParser::Negation, OPTiMaDe::FilterParser::AndOr and OPTiMaDe::FilterParser::Known. - Changing the way the delimiter is passed to to_SQL() methods. - Implementing SQL output with placeholders instead of values. 0.3.0 2019-08-19 - Introducing method modify(), which traverses and modifies the built filter tree. - Modifying the constructor for OPTiMaDe::FilterParser::Property. - Adding 'Changes' (this file) to MANIFEST. 0.2.0 2019-08-12 - Combining 'IS' and 'KNOWN'/'UNKNOWN' lexems together. - Fixing 'IS KNOWN'/'IS UNKNOWN' translation to SQL. - Implementing backwards translation to the string representation. - Unifying layout of parsed lists. - Adding roundtrip tests. 0.1.0 2019-07-20 - Initial release. OPTiMaDe-Filter-0.7.0/lib/0000755000200400020040000000000013620716603014733 5ustar andriusandriusOPTiMaDe-Filter-0.7.0/lib/OPTiMaDe/0000755000200400020040000000000013620716603016275 5ustar andriusandriusOPTiMaDe-Filter-0.7.0/lib/OPTiMaDe/Filter/0000755000200400020040000000000013620716603017522 5ustar andriusandriusOPTiMaDe-Filter-0.7.0/lib/OPTiMaDe/Filter/Negation.pm0000644000200400020040000000167413620177436021641 0ustar andriusandriuspackage OPTiMaDe::Filter::Negation; use strict; use warnings; sub new { my( $class, $inner ) = @_; return bless { inner => $inner }, $class; } sub inner { my( $self, $inner ) = @_; my $previous_inner = $self->{inner}; $self->{inner} = $inner if defined $inner; return $previous_inner; } sub to_filter { my( $self ) = @_; $self->validate; return '(NOT ' . $self->inner->to_filter . ')'; } sub to_SQL { my( $self, $options ) = @_; $self->validate; my( $sql, $values ) = $self->inner->to_SQL( $options ); if( wantarray ) { return ( "(NOT $sql)", $values ); } else { return "(NOT $sql)"; } } sub modify { my $self = shift; my $code = shift; $self->inner( OPTiMaDe::Filter::modify( $self->inner, $code, @_ ) ); return $code->( $self, @_ ); } sub validate { my $self = shift; die 'inner undefined for OPTiMaDe::Filter::Negation' if !$self->inner; } 1; OPTiMaDe-Filter-0.7.0/lib/OPTiMaDe/Filter/Comparison.pm0000644000200400020040000000705713620177436022210 0ustar andriusandriuspackage OPTiMaDe::Filter::Comparison; use strict; use warnings; use Scalar::Util qw(blessed); sub new { my( $class, $operator ) = @_; return bless { operands => [], operator => $operator }, $class; } sub push_operand { my( $self, $operand ) = @_; die 'attempt to insert more than two operands' if @{$self->{operands}} >= 2; push @{$self->{operands}}, $operand; } sub unshift_operand { my( $self, $operand ) = @_; die 'attempt to insert more than two operands' if @{$self->{operands}} >= 2; unshift @{$self->{operands}}, $operand; } sub operator { my( $self, $operator ) = @_; my $previous_operator = $self->{operator}; $self->{operator} = $operator if defined $operator; return $previous_operator; } sub left { my( $self, $operand ) = @_; my $previous_operand = $self->{operands}[0]; $self->{operands}[0] = $operand if defined $operand; return $previous_operand; } sub right { my( $self, $operand ) = @_; my $previous_operand = $self->{operands}[1]; $self->{operands}[1] = $operand if defined $operand; return $previous_operand; } sub to_filter { my( $self ) = @_; $self->validate; my $operator = $self->{operator}; my @operands; for my $i (0..$#{$self->{operands}}) { my $arg = $self->{operands}[$i]; if( blessed $arg && $arg->can( 'to_filter' ) ) { $arg = $arg->to_filter; } else { $arg =~ s/\\/\\\\/g; $arg =~ s/"/\\"/g; $arg = "\"$arg\""; } push @operands, $arg; } return "($operands[0] $operator $operands[1])"; } sub to_SQL { my( $self, $options ) = @_; $self->validate; $options = {} unless $options; my( $delim, $placeholder ) = ( $options->{delim}, $options->{placeholder}, ); $delim = "'" unless $delim; my $operator = $self->{operator}; my @operands = @{$self->{operands}}; # Handle STARTS/ENDS WITH if( $operator eq 'CONTAINS' ) { $operator = 'LIKE'; $operands[1] = '%' . $operands[1] . '%' if !blessed $operands[1]; } elsif( $operator =~ /^STARTS( WITH)?$/ ) { $operator = 'LIKE'; $operands[1] = $operands[1] . '%' if !blessed $operands[1]; } elsif( $operator =~ /^ENDS( WITH)?$/ ) { $operator = 'LIKE'; $operands[1] = '%' . $operands[1] if !blessed $operands[1]; } my @values; my @operands_now; for my $arg (@operands) { if( blessed $arg && $arg->can( 'to_SQL' ) ) { ( $arg, my $values ) = $arg->to_SQL( $options ); push @values, @$values; } else { push @values, $arg; if( $placeholder ) { $arg = $placeholder; } else { $arg =~ s/"/""/g; $arg = "\"$arg\""; } } push @operands_now, $arg; } @operands = @operands_now; if( wantarray ) { return ( "($operands[0] $operator $operands[1])", \@values ); } else { return "($operands[0] $operator $operands[1])"; } } sub modify { my $self = shift; my $code = shift; $self->{operands} = [ map { OPTiMaDe::Filter::modify( $_, $code, @_ ) } @{$self->{operands}} ]; return $code->( $self, @_ ); } sub validate { my $self = shift; if( @{$self->{operands}} != 2 ) { die 'number of operands for OPTiMaDe::Filter::Comparison must be 2, ' . 'got ' . @{$self->{operands}}; } die 'operator undefined for OPTiMaDe::Filter::Comparison' if !$self->operator; } 1; OPTiMaDe-Filter-0.7.0/lib/OPTiMaDe/Filter/ListComparison.pm0000644000200400020040000000421413620177436023034 0ustar andriusandriuspackage OPTiMaDe::Filter::ListComparison; use strict; use warnings; use Scalar::Util qw(blessed); sub new { my( $class, $operator ) = @_; return bless { property => undef, operator => $operator, values => undef }, $class; } sub property { my( $self, $property ) = @_; my $previous_property = $self->{property}; $self->{property} = $property if defined $property; return $previous_property; } sub operator { my( $self, $operator ) = @_; my $previous_operator = $self->{operator}; $self->{operator} = $operator if defined $operator; return $previous_operator; } sub values { my( $self, $values ) = @_; my $previous_values = $self->{values}; $self->{values} = $values if defined $values; return $previous_values; } sub to_filter { my( $self ) = @_; $self->validate; my @values; for my $i (0..$#{$self->{values}}) { my( $operator, $arg ) = @{$self->{values}[$i]}; if( blessed $arg && $arg->can( 'to_filter' ) ) { $arg = $arg->to_filter; } else { $arg =~ s/\\/\\\\/g; $arg =~ s/"/\\"/g; $arg = "\"$arg\""; } push @values, "$operator $arg"; } return '(' . join( ' ', $self->{property}->to_filter, $self->{operator}, join( ', ', @values ) ) . ')'; } sub to_SQL { die "no SQL representation\n"; } sub modify { my $self = shift; my $code = shift; $self->{property} = $code->( $self->{property}, @_ ); $self->{values} = [ map { [ OPTiMaDe::Filter::modify( $_->[0], $code, @_ ), OPTiMaDe::Filter::modify( $_->[1], $code, @_ ) ] } @{$self->{values}} ]; return $code->( $self, @_ ); } sub validate { my $self = shift; if( !$self->property ) { die 'property undefined for OPTiMaDe::Filter::ListComparison'; } if( !$self->operator ) { die 'operator undefined for OPTiMaDe::Filter::ListComparison'; } if( !$self->values ) { die 'values undefined for OPTiMaDe::Filter::ListComparison'; } } 1; OPTiMaDe-Filter-0.7.0/lib/OPTiMaDe/Filter/AndOr.pm0000644000200400020040000000654713620177436021104 0ustar andriusandriuspackage OPTiMaDe::Filter::AndOr; use strict; use warnings; use Scalar::Util qw(blessed); sub new { my $class = shift; my $operator; my @operands; if( @_ == 2 ) { @operands = @_; } elsif( @_ == 3 ) { ( $operands[0], $operator, $operands[1] ) = @_; } return bless { operands => \@operands, operator => $operator }, $class; } sub operator { my( $self, $operator ) = @_; my $previous_operator = $self->{operator}; $self->{operator} = $operator if defined $operator; return $previous_operator; } sub push_operand { my( $self, $operand ) = @_; die 'attempt to insert more than two operands' if @{$self->{operands}} >= 2; push @{$self->{operands}}, $operand; } sub unshift_operand { my( $self, $operand ) = @_; die 'attempt to insert more than two operands' if @{$self->{operands}} >= 2; unshift @{$self->{operands}}, $operand; } sub left { my( $self, $operand ) = @_; my $previous_operand = $self->{operands}[0]; $self->{operands}[0] = $operand if defined $operand; return $previous_operand; } sub right { my( $self, $operand ) = @_; my $previous_operand = $self->{operands}[1]; $self->{operands}[1] = $operand if defined $operand; return $previous_operand; } sub to_filter { my( $self ) = @_; $self->validate; my $operator = $self->{operator}; my @operands; for my $i (0..$#{$self->{operands}}) { my $arg = $self->{operands}[$i]; if( blessed $arg && $arg->can( 'to_filter' ) ) { $arg = $arg->to_filter; } else { $arg =~ s/\\/\\\\/g; $arg =~ s/"/\\"/g; $arg = "\"$arg\""; } push @operands, $arg; } return "($operands[0] $operator $operands[1])"; } sub to_SQL { my( $self, $options ) = @_; $self->validate; $options = {} unless $options; my( $delim, $placeholder ) = ( $options->{delim}, $options->{placeholder}, ); $delim = "'" unless $delim; my $operator = $self->{operator}; my @operands; my @values; for my $i (0..$#{$self->{operands}}) { my $arg = $self->{operands}[$i]; if( blessed $arg && $arg->can( 'to_SQL' ) ) { my $values = []; eval { ( $arg, $values ) = $arg->to_SQL( $options ) }; if( $@ ) { chomp $@; $arg = "<$@>"; } push @values, @$values; } else { push @values, $arg; if( $placeholder ) { $arg = $placeholder; } else { $arg =~ s/"/""/g; $arg = "\"$arg\""; } } push @operands, $arg; } if( wantarray ) { return ( "($operands[0] $operator $operands[1])", \@values ); } else { return "($operands[0] $operator $operands[1])"; } } sub modify { my $self = shift; my $code = shift; $self->{operands} = [ map { OPTiMaDe::Filter::modify( $_, $code, @_ ) } @{$self->{operands}} ]; return $code->( $self, @_ ); } sub validate { my $self = shift; if( @{$self->{operands}} != 2 ) { die 'number of operands for OPTiMaDe::Filter::AndOr must be 2, ' . 'got ' . @{$self->{operands}}; } die 'operator undefined for OPTiMaDe::Filter::AndOr' if !$self->operator; } 1; OPTiMaDe-Filter-0.7.0/lib/OPTiMaDe/Filter/Property.pm0000644000200400020040000000205513620177436021713 0ustar andriusandriuspackage OPTiMaDe::Filter::Property; use strict; use warnings; use overload '@{}' => sub { return $_[0]->{name} }, '""' => sub { return $_[0]->to_filter }; sub new { my $class = shift; return bless { name => \@_ }, $class; } sub to_filter { my( $self ) = @_; $self->validate; return join '.', @$self; } sub to_SQL { my( $self, $options ) = @_; $self->validate; $options = {} unless $options; my( $delim, $placeholder ) = ( $options->{delim}, $options->{placeholder}, ); $delim = "'" unless $delim; if( @$self > 2 ) { die 'no SQL representation for properties of more than two ' . "identifiers\n"; } my $sql = join '.', map { "${delim}$_${delim}" } @$self; if( wantarray ) { return ( $sql, [] ); } else { return $sql; } } sub modify { my $self = shift; my $code = shift; return $code->( $self, @_ ); } sub validate { my $self = shift; die 'name undefined for OPTiMaDe::Filter::Property' if !@$self; } 1; OPTiMaDe-Filter-0.7.0/lib/OPTiMaDe/Filter/Zip.pm0000644000200400020040000000442213620177436020631 0ustar andriusandriuspackage OPTiMaDe::Filter::Zip; use strict; use warnings; use Scalar::Util qw(blessed); sub new { my( $class ) = @_; return bless { properties => [], operator => undef, values => [] }, $class; } sub push_property { my( $self, $property ) = @_; push @{$self->{properties}}, $property; } sub unshift_property { my( $self, $property ) = @_; unshift @{$self->{properties}}, $property; } sub operator { my( $self, $operator ) = @_; my $previous_operator = $self->{operator}; $self->{operator} = $operator if defined $operator; return $previous_operator; } sub values { my( $self, $values ) = @_; my $previous_values = $self->{values}; $self->{values} = $values if defined $values; return $previous_values; } sub to_filter { my( $self ) = @_; $self->validate; my @zip_list; foreach my $zip (@{$self->{values}}) { my @zip; for my $i (0..$#$zip) { my( $operator, $arg ) = @{$zip->[$i]}; if( blessed $arg && $arg->can( 'to_filter' ) ) { $arg = $arg->to_filter; } else { $arg =~ s/\\/\\\\/g; $arg =~ s/"/\\"/g; $arg = "\"$arg\""; } push @zip, "$operator $arg"; } push @zip_list, join( ' : ', @zip ); } return '(' . join( ':', map { $_->to_filter } @{$self->{properties}} ) . ' ' . $self->{operator} . ' ' . join( ', ', @zip_list ) . ')'; } sub to_SQL { die "no SQL representation\n"; } sub modify { my $self = shift; my $code = shift; $self->{properties} = [ map { $_->modify( $code, @_ ) } @{$self->{properties}} ]; $self->{values} = [ map { [ OPTiMaDe::Filter::modify( $_->[0], $code, @_ ), OPTiMaDe::Filter::modify( $_->[1], $code, @_ ) ] } @{$self->{values}} ]; return $code->( $self, @_ ); } sub validate { my $self = shift; if( !$self->{properties} ) { die 'properties undefined for OPTiMaDe::Filter::Zip'; } if( !$self->operator ) { die 'operator undefined for OPTiMaDe::Filter::Zip'; } if( !$self->values ) { die 'values undefined for OPTiMaDe::Filter::Zip'; } } 1; OPTiMaDe-Filter-0.7.0/lib/OPTiMaDe/Filter/Known.pm0000644000200400020040000000244313620177436021164 0ustar andriusandriuspackage OPTiMaDe::Filter::Known; use strict; use warnings; sub new { my( $class, $is_known, $property ) = @_; return bless { is_known => $is_known, property => $property }, $class; } sub is_known { my( $self, $is_known ) = @_; my $previous_is_known = $self->{is_known}; $self->{is_known} = $is_known if defined $is_known; return $previous_is_known; } sub property { my( $self, $property ) = @_; my $previous_property = $self->{property}; $self->{property} = $property if defined $property; return $previous_property; } sub to_filter { my( $self ) = @_; $self->validate; return $self->property->to_filter . ' IS ' . ($self->is_known ? 'KNOWN' : 'UNKNOWN'); } sub to_SQL { my( $self, $options ) = @_; $self->validate; my( $sql, $values ) = $self->property->to_SQL( $options ); $sql = "$sql IS " . ($self->is_known ? 'NOT NULL' : 'NULL'); if( wantarray ) { return ( $sql, $values ); } else { return $sql; } } sub modify { my $self = shift; my $code = shift; $self->property( OPTiMaDe::Filter::modify( $self->property, $code, @_ ) ); return $code->( $self, @_ ); } sub validate { my $self = shift; die 'property undefined for OPTiMaDe::Filter::Known' if !$self->property; } 1; OPTiMaDe-Filter-0.7.0/lib/OPTiMaDe/Filter.pm0000644000200400020040000000071413620177436020067 0ustar andriusandriuspackage OPTiMaDe::Filter; use strict; use warnings; use Scalar::Util qw(blessed); our $VERSION = '0.7.0'; our $OPTiMaDe_VERSION = '1.0.0-rc.1'; sub modify { my $node = shift; my $code = shift; if( blessed $node && $node->can( 'modify' ) ) { return $node->modify( $code, @_ ); } elsif( ref $node eq 'ARRAY' ) { return [ map { modify( $_, $code, @_ ) } @$node ]; } else { return $code->( $node, @_ ); } } 1; OPTiMaDe-Filter-0.7.0/COPYING0000644000200400020040000001674313502412301015216 0ustar andriusandrius GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. 0. Additional Definitions. As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the "GNU GPL" refers to version 3 of the GNU General Public License. "The Library" refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. An "Application" is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. A "Combined Work" is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the "Linked Version". The "Minimal Corresponding Source" for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. The "Corresponding Application Code" for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. 1. Exception to Section 3 of the GNU GPL. You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. 2. Conveying Modified Versions. If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. 3. Object Code Incorporating Material from Library Header Files. The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the object code with a copy of the GNU GPL and this license document. 4. Combined Works. You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the Combined Work with a copy of the GNU GPL and this license document. c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. d) Do one of the following: 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) 5. Combined Libraries. You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 6. Revised Versions of the GNU Lesser General Public License. The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library. OPTiMaDe-Filter-0.7.0/META.json0000644000200400020040000000215213620716603015606 0ustar andriusandrius{ "abstract" : "unknown", "author" : [ "unknown" ], "dynamic_config" : 1, "generated_by" : "ExtUtils::MakeMaker version 7.24, CPAN::Meta::Converter version 2.150010", "license" : [ "unknown" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "OPTiMaDe-Filter", "no_index" : { "directory" : [ "t", "inc" ] }, "prereqs" : { "build" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "configure" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "runtime" : { "requires" : { "Parse::Yapp" : "0", "Scalar::Util" : "0" } } }, "release_status" : "stable", "version" : "v0.7.0", "x_repository" : { "type" : "git", "url" : "https://github.com/Materials-Consortia/OPTiMaDe-Filter.git", "web" : "https://github.com/Materials-Consortia/OPTiMaDe-Filter" }, "x_serialization_backend" : "JSON::PP version 2.27400_02" } OPTiMaDe-Filter-0.7.0/META.yml0000644000200400020040000000125713620716603015443 0ustar andriusandrius--- abstract: unknown author: - unknown build_requires: ExtUtils::MakeMaker: '0' configure_requires: ExtUtils::MakeMaker: '0' dynamic_config: 1 generated_by: 'ExtUtils::MakeMaker version 7.24, CPAN::Meta::Converter version 2.150010' license: unknown meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: OPTiMaDe-Filter no_index: directory: - t - inc requires: Parse::Yapp: '0' Scalar::Util: '0' version: v0.7.0 x_repository: type: git url: https://github.com/Materials-Consortia/OPTiMaDe-Filter.git web: https://github.com/Materials-Consortia/OPTiMaDe-Filter x_serialization_backend: 'CPAN::Meta::YAML version 0.018' OPTiMaDe-Filter-0.7.0/MANIFEST0000644000200400020040000000074613620716603015325 0ustar andriusandriusChanges COPYING Parser.yp lib/OPTiMaDe/Filter.pm lib/OPTiMaDe/Filter/AndOr.pm lib/OPTiMaDe/Filter/Comparison.pm lib/OPTiMaDe/Filter/Known.pm lib/OPTiMaDe/Filter/ListComparison.pm lib/OPTiMaDe/Filter/Negation.pm lib/OPTiMaDe/Filter/Property.pm lib/OPTiMaDe/Filter/Zip.pm Makefile.PL MANIFEST This list of files parse META.yml Module YAML meta-data (added by MakeMaker) META.json Module JSON meta-data (added by MakeMaker) OPTiMaDe-Filter-0.7.0/Makefile.PL0000644000200400020040000000115613620716364016146 0ustar andriusandriususe ExtUtils::MakeMaker; WriteMakefile( NAME => 'OPTiMaDe::Filter', VERSION_FROM => 'lib/OPTiMaDe/Filter.pm', PREREQ_PM => { Parse::Yapp => 0, Scalar::Util => 0, }, depend => { pm_to_blib => 'blib/lib/OPTiMaDe/Filter/Parser.pm' }, META_MERGE => { "meta-spec" => { version => 2 }, repository => { type => 'git', url => 'https://github.com/Materials-Consortia/OPTiMaDe-Filter.git', web => 'https://github.com/Materials-Consortia/OPTiMaDe-Filter', }, }, ); sub MY::postamble { <<'EOT' blib/lib/OPTiMaDe/Filter/%.pm: %.yp mkdir --parents $(@D) yapp -v -m OPTiMaDe::Filter::Parser -o $@ $< EOT }