diff --git a/lib/Hailo/Engine/Default.pm b/lib/Hailo/Engine/Default.pm index 46523a8..18ffe5c 100644 --- a/lib/Hailo/Engine/Default.pm +++ b/lib/Hailo/Engine/Default.pm @@ -1,6 +1,8 @@ package Hailo::Engine::Default; use 5.010; +use strict; +use parent 'Hailo::Role::Engine'; use List::Util qw; use List::MoreUtils qw; diff --git a/lib/Hailo/Role/Arguments.pm b/lib/Hailo/Role/Arguments.pm index d388624..5b76d64 100644 --- a/lib/Hailo/Role/Arguments.pm +++ b/lib/Hailo/Role/Arguments.pm @@ -1,16 +1,8 @@ package Hailo::Role::Arguments; use 5.010; -use Any::Moose '::Role'; -use Any::Moose 'X::Types::'.any_moose() => [qw/HashRef Str/]; -use namespace::clean -except => 'meta'; - -has arguments => ( - isa => HashRef[Str], - is => 'ro', - documentation => "Arguments passed from Hailo", - auto_deref => 1, -); + +sub arguments { shift->{arguments} } 1; diff --git a/lib/Hailo/Role/Engine.pm b/lib/Hailo/Role/Engine.pm index 440b1a6..f380a45 100644 --- a/lib/Hailo/Role/Engine.pm +++ b/lib/Hailo/Role/Engine.pm @@ -1,25 +1,20 @@ package Hailo::Role::Engine; use 5.10.0; -use Any::Moose '::Role'; -use Any::Moose 'X::Types::'.any_moose() => [qw< Int >]; -use namespace::clean -except => 'meta'; - -has storage => ( - required => 1, - is => 'ro', - documentation => "Our copy of the current Storage object", -); - -has order => ( - required => 1, - isa => Int, - is => 'rw', - documentation => "Our copy of the current markov order", -); - -requires 'learn'; -requires 'reply'; + +# Accessors +for my $method (qw[ storage order ]) { + no strict 'refs'; + *{$method} = sub { + my ($self, @args) = @_; + return $self->{$method} unless @args; + return $self->{$method} = $args[0] if @args == 1; + die "not implemented"; + }; +} + +sub learn { die } +sub reply { die } 1; diff --git a/lib/Hailo/Role/Storage.pm b/lib/Hailo/Role/Storage.pm index 778587e..76e452e 100644 --- a/lib/Hailo/Role/Storage.pm +++ b/lib/Hailo/Role/Storage.pm @@ -1,31 +1,26 @@ package Hailo::Role::Storage; use 5.010; -use Any::Moose '::Role'; -use Any::Moose 'X::Types::'.any_moose() => [qw/Str Int/]; -use namespace::clean -except => 'meta'; - -has brain => ( - isa => Str, - is => 'rw', -); - -has order => ( - isa => Int, - is => 'rw', -); - -has hailo => ( - isa => 'Hailo', - is => 'ro', -); - -requires 'ready'; -requires 'save'; -requires 'start_learning'; -requires 'stop_learning'; -requires 'start_training'; -requires 'stop_training'; +use parent qw[ Hailo::Role::Any Hailo::Role::Arguments ]; +use strict; + +# Accessors +for my $method (qw[ brain order hailo ]) { + no strict 'refs'; + *{$method} = sub { + my ($self, @args) = @_; + return $self->{$method} unless @args; + return $self->{$method} = $args[0] if @args == 1; + die "not implemented"; + }; +} + +sub ready { die } +sub save { die } +sub start_learning { die } +sub stop_learning { die } +sub start_training { die } +sub stop_training { die } sub save { # does nothing by default diff --git a/lib/Hailo/Storage.pm b/lib/Hailo/Storage.pm index 9928a80..3a7fdb6 100644 --- a/lib/Hailo/Storage.pm +++ b/lib/Hailo/Storage.pm @@ -1,60 +1,38 @@ package Hailo::Storage; use 5.010; -use Any::Moose; -use Any::Moose 'X::Types::'.any_moose() => [qw]; -BEGIN { - return unless Any::Moose::moose_is_preferred(); - require MooseX::StrictConstructor; - MooseX::StrictConstructor->import; -} +use strict; +use parent 'Hailo::Role::Storage'; use DBI; use Hailo::Storage::Schema; -has dbd => ( - isa => Str, - is => 'ro', - lazy_build => 1, - documentation => "The DBD::* driver we're using", -); - -has dbd_options => ( - isa => HashRef, - is => 'ro', - lazy_build => 1, - documentation => 'Options passed as the last argument to DBI->connect()', -); - -sub _build_dbd_options { +# Accessors +for my $method (qw[ _engaged _boundary_token_id ]) { + no strict 'refs'; + *{$method} = sub { + my ($self, @args) = @_; + return $self->{$method} unless @args; + return $self->{$method} = $args[0] if @args == 1; + die "not implemented"; + }; +} + +sub dbd_options { my ($self) = @_; return { RaiseError => 1 }; } -has dbh => ( - isa => 'DBI::db', - is => 'ro', - lazy_build => 1, - documentation => 'Our DBD object', -); - -sub _build_dbh { +sub dbh { my ($self) = @_; - my $dbd_options = $self->dbi_options; - - return DBI->connect($self->dbi_options); + return $self->{dbh} // ($self->{dbh} = do { + my $dbd_options = $self->dbi_options; + DBI->connect(@{ $self->dbi_options }); + }); }; -has dbi_options => ( - isa => ArrayRef, - is => 'ro', - auto_deref => 1, - lazy_build => 1, - documentation => 'Options passed to DBI->connect()', -); - -sub _build_dbi_options { +sub dbi_options { my ($self) = @_; my $dbd = $self->dbd; my $dbd_options = $self->dbd_options; @@ -70,30 +48,11 @@ sub _build_dbi_options { return \@options; } -has _engaged => ( - isa => Bool, - is => 'rw', - default => 0, - documentation => 'Have we done setup work to get this database going?', -); - -has sth => ( - isa => HashRef, - is => 'ro', - lazy_build => 1, - documentation => 'A HashRef of prepared DBI statement handles', -); - -sub _build_sth { +sub sth { my ($self) = @_; - return Hailo::Storage::Schema->sth($self->dbd, $self->dbh, $self->order); + return $self->{sth} // ($self->{sth} = Hailo::Storage::Schema->sth($self->dbd, $self->dbh, $self->order)); } -has _boundary_token_id => ( - isa => Int, - is => 'rw', -); - # bootstrap the database sub _engage { my ($self) = @_; @@ -192,7 +151,7 @@ sub totals { return $token, $expr, $prev, $next; } -__PACKAGE__->meta->make_immutable; +1; =encoding utf8 diff --git a/lib/Hailo/Storage/MySQL.pm b/lib/Hailo/Storage/MySQL.pm index 734c2f1..93b02bd 100644 --- a/lib/Hailo/Storage/MySQL.pm +++ b/lib/Hailo/Storage/MySQL.pm @@ -1,28 +1,22 @@ package Hailo::Storage::MySQL; use 5.010; -use Any::Moose; -BEGIN { - return unless Any::Moose::moose_is_preferred(); - require MooseX::StrictConstructor; - MooseX::StrictConstructor->import; -} +use strict; +use parent 'Hailo::Storage'; use List::MoreUtils qw< all >; use namespace::clean -except => 'meta'; -extends 'Hailo::Storage'; -with qw(Hailo::Role::Arguments Hailo::Role::Storage); - -sub _build_dbd { return 'mysql' }; +sub dbd { 'mysql' }; -override _build_dbd_options => sub { +sub dbd_options { + my ($self) = @_; return { - %{ super() }, + %{ $self->SUPER::dbd_options }, mysql_enable_utf8 => 1, }; -}; +} -sub _build_dbi_options { +sub dbi_options { my ($self) = @_; my $dbd = $self->dbd; my $dbd_options = $self->dbd_options; @@ -55,7 +49,7 @@ sub ready { return all { exists $self->arguments->{$_} } qw(database username password); } -__PACKAGE__->meta->make_immutable; +1; =encoding utf8 diff --git a/lib/Hailo/Storage/PostgreSQL.pm b/lib/Hailo/Storage/PostgreSQL.pm index 5e4220d..c65304c 100644 --- a/lib/Hailo/Storage/PostgreSQL.pm +++ b/lib/Hailo/Storage/PostgreSQL.pm @@ -1,27 +1,20 @@ package Hailo::Storage::PostgreSQL; use 5.010; -use Any::Moose; -BEGIN { - return unless Any::Moose::moose_is_preferred(); - require MooseX::StrictConstructor; - MooseX::StrictConstructor->import; -} -use namespace::clean -except => 'meta'; - -extends 'Hailo::Storage'; -with qw(Hailo::Role::Arguments Hailo::Role::Storage); +use strict; +use parent 'Hailo::Storage'; -sub _build_dbd { return 'Pg' }; +sub dbd { 'Pg' }; -override _build_dbd_options => sub { +sub dbd_options { + my ($self) = @_; return { - %{ super() }, + %{ $self->SUPER::dbd_options }, pg_enable_utf8 => 1, }; -}; +} -sub _build_dbi_options { +sub dbi_options { my ($self) = @_; my $dbd = $self->dbd; my $dbd_options = $self->dbd_options; @@ -55,7 +48,7 @@ sub ready { return exists $self->arguments->{dbname}; } -__PACKAGE__->meta->make_immutable; +1; =encoding utf8 diff --git a/lib/Hailo/Storage/SQLite.pm b/lib/Hailo/Storage/SQLite.pm index 444d26e..e0cc8c3 100644 --- a/lib/Hailo/Storage/SQLite.pm +++ b/lib/Hailo/Storage/SQLite.pm @@ -1,28 +1,19 @@ package Hailo::Storage::SQLite; use 5.010; -use Any::Moose; -BEGIN { - return unless Any::Moose::moose_is_preferred(); - require MooseX::StrictConstructor; - MooseX::StrictConstructor->import; -} -use namespace::clean -except => 'meta'; - -extends 'Hailo::Storage'; -with qw(Hailo::Role::Arguments Hailo::Role::Storage); +use parent 'Hailo::Storage'; -sub _build_dbd { return 'SQLite' }; +sub dbd { 'SQLite' }; -override _build_dbd_options => sub { +sub dbd_options { + my ($self) = @_; return { - %{ super() }, + %{ $self->SUPER::dbd_options }, sqlite_unicode => 1, }; -}; +} -around _build_dbi_options => sub { - my $orig = shift; +sub dbi_options { my $self = shift; my $return; @@ -33,11 +24,11 @@ around _build_dbi_options => sub { $self->brain($file); } else { - $return = $self->$orig(@_); + $return = $self->SUPER::dbi_options(@_); } return $return; -}; +} # Are we running in a mixed mode where we run in memory but # restore/backup to disk? @@ -49,8 +40,8 @@ sub _backup_memory_to_disk { and $self->arguments->{in_memory}); } -before _engage => sub { - my ($self) = @_; +sub _engage { + my $self = shift; # Set any user-defined pragmas $self->_set_pragmas; @@ -59,18 +50,23 @@ before _engage => sub { $self->dbh->sqlite_backup_from_file($self->brain); } - return; + return $self->SUPER::_engage(@_); }; -before start_training => sub { - my $dbh = shift->dbh; +sub start_training { + my $self = shift; + my $dbh = $self->dbh; $dbh->do('PRAGMA synchronous=OFF;'); $dbh->do('PRAGMA journal_mode=OFF;'); - return; + return $self->SUPER::start_training(@_); }; -after stop_training => sub { - my $dbh = shift->dbh; +sub stop_training { + my $self = shift; + my $dbh = $self->dbh; + + $self->SUPER::stop_training(@_); + $dbh->do('PRAGMA journal_mode=DELETE;'); $dbh->do('PRAGMA synchronous=ON;'); return; @@ -127,7 +123,7 @@ sub save { return; }; -__PACKAGE__->meta->make_immutable; +1; =encoding utf8 diff --git a/lib/Hailo/UI/ReadLine.pm b/lib/Hailo/UI/ReadLine.pm index c1fe655..8d0ad8e 100644 --- a/lib/Hailo/UI/ReadLine.pm +++ b/lib/Hailo/UI/ReadLine.pm @@ -12,9 +12,6 @@ use Hailo; use Term::ReadLine; use namespace::clean -except => 'meta'; -with qw(Hailo::Role::Arguments - Hailo::Role::UI); - sub BUILD { $ENV{PERL_RL} = 'Perl o=0' unless $ENV{PERL_RL}; return;