use 5.008001; use utf8; use strict; use warnings FATAL => 'all'; ########################################################################### ########################################################################### { package Muldis::Rosetta::Interface; # module use version; our $VERSION = qv('0.10.0'); # Note: This given version applies to all of this file's packages. use Carp; use Encode qw(is_utf8); use Scalar::Util qw(blessed); ########################################################################### sub new_machine { my ($args) = @_; my ($engine_name, $machine_config) = @{$args}{'engine_name', 'machine_config'}; confess q{new_machine(): Bad :$engine_name arg; Perl 5 does not} . q{ consider it to be a character str, or it's the empty str.} if !defined $engine_name or $engine_name eq q{} or (!is_utf8 $engine_name and $engine_name =~ m/[^\x00-\x7F]/xs); # A module may be loaded due to it being embedded in a non-excl file. if (!do { no strict 'refs'; defined %{$engine_name . '::'}; }) { # Note: We have to invoke this 'require' in an eval string # because we need the bareword semantics, where 'require' # will munge the module name into file system paths. eval "require $engine_name;"; if (my $err = $@) { confess q{new_machine(): Could not load Muldis Rosetta Engine} . qq{ module '$engine_name': $err}; } confess qq{new_machine(): Could not load Muldis Rosetta Engine mod} . qq{ '$engine_name': while that file did compile without} . q{ errors, it did not declare the same-named module.} if !do { no strict 'refs'; defined %{$engine_name . '::'}; }; } confess qq{new_machine(): The Muldis Rosetta Engine mod '$engine_name'} . q{ does not provide the new_machine() constructor function.} if !$engine_name->can( 'new_machine' ); my $machine = eval { &{$engine_name->can( 'new_machine' )}({ 'machine_config' => $machine_config }); }; if (my $err = $@) { confess qq{new_machine(): Th Muldis Rosetta Eng mod '$engine_name'} . qq{ threw an exception during its new_machine() exec: $err}; } confess q{new_machine(): The new_machine() constructor function of the} . qq{ Muldis Rosetta Engine mod '$engine_name' did not ret an} . q{ obj of a Muldis::Rosetta::Interface::Machine-doing class.} if !blessed $machine or !$machine->isa( 'Moose::Object' ) or !$machine->does( 'Muldis::Rosetta::Interface::Machine' ); return $machine; } ########################################################################### } # module Muldis::Rosetta::Interface ########################################################################### ########################################################################### { package Muldis::Rosetta::Interface::Machine; # role use Moose::Role; requires 'new_process'; requires 'assoc_processes'; } # role Muldis::Rosetta::Interface::Machine ########################################################################### ########################################################################### { package Muldis::Rosetta::Interface::Process; # role use Moose::Role; requires 'assoc_machine'; requires 'command_lang'; requires 'update_command_lang'; requires 'execute'; requires 'new_value'; requires 'assoc_values'; requires 'func_invo'; requires 'upd_invo'; requires 'proc_invo'; requires 'trans_nest_level'; requires 'start_trans'; requires 'commit_trans'; requires 'rollback_trans'; } # role Muldis::Rosetta::Interface::Process ########################################################################### ########################################################################### { package Muldis::Rosetta::Interface::Value; # role use Moose::Role; requires 'assoc_process'; requires 'source_code'; } # role Muldis::Rosetta::Interface::Value ########################################################################### ########################################################################### 1; # Magic true value required at end of a reusable file's code. __END__ =pod =encoding utf8 =head1 NAME Muldis::Rosetta::Interface - Common public API for Muldis Rosetta Engines =head1 VERSION This document describes Muldis::Rosetta::Interface version 0.10.0 for Perl 5. It also describes the same-number versions for Perl 5 of Muldis::Rosetta::Interface::Machine ("Machine"), Muldis::Rosetta::Interface::Process ("Process"), Muldis::Rosetta::Interface::Value ("Value"). =head1 SYNOPSIS This simple example declares two Perl variables containing relation data, then does a (N-ary) relational join (natural inner join) on them, producing a third Perl variable holding the relation data of the result. use Muldis::Rosetta::Interface; my $machine = Muldis::Rosetta::Interface::new_machine({ 'engine_name' => 'Muldis::Rosetta::Engine::Example' }); my $process = $machine->new_process(); $process->update_command_lang({ 'lang' => [ 'Muldis_D', 'http://muldis.com', '0.46.0', 'HDMD_Perl_Tiny', {} ] }); my $r1 = $process->new_value({ 'source_code' => [ 'Relation', [ { 'x' => [ 'Int', 'perl_int', 4 ], 'y' => [ 'Int', 'perl_int', 7 ], }, { 'x' => [ 'Int', 'perl_int', 3 ], 'y' => [ 'Int', 'perl_int', 2 ], }, ] ] }); my $r2 = $process->new_value({ 'source_code' => [ 'Relation', [ { 'y' => [ 'Int', 'perl_int', 5 ], 'z' => [ 'Int', 'perl_int', 6 ], }, { 'y' => [ 'Int', 'perl_int', 2 ], 'z' => [ 'Int', 'perl_int', 1 ], }, { 'y' => [ 'Int', 'perl_int', 2 ], 'z' => [ 'Int', 'perl_int', 4 ], }, ] ] }); my $r3 = $process->func_invo({ 'function' => 'sys.std.Core.Relation.join', 'args' => { 'topic' => [ 'QuasiSet', [ $r1, $r2 ] ], } }); my $r3_as_perl = $r3->source_code(); # Then $r3_as_perl contains: # [ 'Relation', [ # { # 'x' => [ 'Int', 'perl_int', 3 ], # 'y' => [ 'Int', 'perl_int', 2 ], # 'z' => [ 'Int', 'perl_int', 1 ], # }, # { # 'x' => [ 'Int', 'perl_int', 3 ], # 'y' => [ 'Int', 'perl_int', 2 ], # 'z' => [ 'Int', 'perl_int', 4 ], # }, # ] ] For most examples of using Muldis Rosetta, and tutorials, please see the separate L distribution (when that comes to exist). =head1 DESCRIPTION B, aka I, comprises the minimal core of the Muldis Rosetta framework, the one component that probably every program would use. Together with the Muldis D language (see L), it defines the common API for Muldis Rosetta implementations to do and which applications invoke. I =head1 INTERFACE The interface of Muldis::Rosetta::Interface is fundamentally object-oriented; you use it by creating objects from its member classes (or more specifically, of implementing subclasses of its member roles) and then invoking methods on those objects. All of their attributes are private, so you must use accessor methods. To aid portability of your applications over multiple implementing Engines, the normal way to create Interface objects is by invoking a constructor-wrapping method of some other object that would provide context for it; since you generally don't have to directly invoke any package names, you don't need to change your code when the package names change due to switching the Engine. You only refer to some Engine's root package name once, as a C argument, and even that can be read from a config file rather than being hard-coded in your application. The usual way that Muldis::Rosetta::Interface indicates a failure is to throw an exception; most often this is due to invalid input. If an invoked routine simply returns, you can assume that it has succeeded, even if the return value is undefined. =head2 The Muldis::Rosetta::Interface Module The C module is the stateless root package by way of which you access the whole Muldis Rosetta API. That is, you use it to load engines and instantiate virtual machines, which provide the rest of the Muldis Rosetta API. =over =item C This constructor function creates and returns a C object that is implemented by the Muldis Rosetta Engine named by its named argument C<$engine_name>; that object is initialized using the C<$machine_config> argument. The named argument C<$engine_name> is the name of a Perl module that is expected to be the root package of a Muldis Rosetta Engine, and which is expected to declare a C subroutine with a single named argument C<$machine_config>; invoking this subroutine is expected to return an object of some class of the same Engine which does the C role. This function will start by testing if the root package is already loaded (it may be declared by some already-loaded file of another name), and only if not, will it do a Perl 'require' of the C<$engine_name>. =back =head2 The Muldis::Rosetta::Interface::Machine Role A C object represents a single active Muldis Rosetta virtual machine / Muldis D environment, which is the widest scope stateful context in which any other database activities happen. Other activities meaning the compilation and execution of Muldis D code, mounting or unmounting depots, performing queries, data manipulation, data definition, and transactions. If a C object is ever garbage collected by Perl while it has any active transactions, then those will all be rolled back, and then an exception thrown. =over =item C This method creates and returns a new C object that is associated with the invocant C; that C object is initialized using the C<$process_config> argument. =item C This method returns, as elements of a new (unordered) Array, all the currently existing C objects that are associated with the invocant C. =back =head2 The Muldis::Rosetta::Interface::Process Role A C object represents a single Muldis Rosetta in-DBMS process, which has its own autonomous transactional context, and for the most part, its own isolated environment. It is associated with a specific C object, the one whose C method created it. A new C object's "expected command language" attribute is undefined by default, meaning that each command fed to it must declare what language it is written in; if that attribute was made defined, then commands fed to it would not need to declare their language and will be interpreted according to the expected language. =over =item C This method returns the C object that the invocant C is associated with. =item C This method returns the fully qualified name of its invocant C object's "expected command language" attribute, which might be undefined; if it is defined, then is either a Perl Str that names a Plain Text language, or it is a Perl (ordered) Array that names a Perl Hosted Data language; these may be Muldis D dialects or some other language. =item C This method assigns a new (possibly undefined) value to its invocant C object's "expected command language" attribute. This method dies if the specified language is defined and its value isn't one that the invocant's Engine knows how to or desires to handle. =item C This method compiles and executes the (typically Muldis D) source code given in its C<$source_code> argument. This method dies if the source code fails to compile for some reason, or if the executing code has a runtime exception. =item C This method creates and returns a new C object that is associated with the invocant C; that C object is initialized using the (typically Muldis D) source code given in its C<$source_code> argument, which defines a value literal. If the C<$source_code> is in a Perl Hosted Data language, then it may consist partially of other C objects. If C<$source_code> is itself just a C object, then it will be cloned. =item C This method returns, as elements of a new (unordered) Array, all the currently existing C objects that are associated with the invocant C. =item C This method invokes the Muldis D function named by its C<$function> argument, giving it arguments from C<$args>, and then returning the result as a C object. Each C<$args> Hash key must match the name of a parameter of the named function, and the corresponding Hash value is the argument for that parameter; each Hash value may be either a C object or some other Perl value that would be suitable as the sole constructor argument for a new C object. =item C This method invokes the Muldis D updater named by its C<$updater> argument, giving it subject-to-update arguments from C<$upd_args> and read-only arguments from C<$ro_args>; the C objects in C<$upd_args> are possibly substituted for other C objects as a side-effect of the updater's execution. The C<$ro_args> parameter is as per the C<$args> parameter of the C method, but the C<$upd_args> parameter is a bit different; each Hash value in the C<$upd_args> argument must be a Perl scalar reference pointing to the Perl variable being bound to the subject-to-update parameter; said Perl variable is then what holds a C object et al prior to the updater's execution, and that may have been updated to hold a different C object as a side-effect. =item C This method invokes the Muldis D procedure (or system_service) named by its C<$procedure> argument, giving it subject-to-update arguments from C<$upd_args> and read-only arguments from C<$ro_args>; the C objects in C<$upd_args> are possibly substituted for other C objects as a side-effect of the procedure's execution. The parameters of C are as per those of the C method, save that only C makes C<$upd_args> mandatory, while C makes it optional. =item C This method returns the current transaction nesting level of its invocant's virtual machine process. If no explicit transactions were started, then the nesting level is zero, in which case the process is conceptually auto-committing every successful Muldis D statement. Each call of C will increase the nesting level by one, and each C or C will decrease it by one (it can't be decreased below zero). Note that all transactions started or ended within Muldis D code (except direct boot_call transaction management) are attached to a particular lexical scope in the Muldis D code (specifically a "try/catch" context), and so they will never have any effect on the nest level that Perl sees (assuming that a Muldis D host language will never be invoked by Muldis D), regardless of whether the Muldis D code successfully returns or throws an exception. =item C This method starts a new child-most transaction within the invocant's virtual machine process. =item C This method commits the child-most transaction within the invocant's virtual machine process; it dies if there isn't one. =item C This method rolls back the child-most transaction within the invocant's virtual machine process; it dies if there isn't one. =back =head2 The Muldis::Rosetta::Interface::Value Role A C object represents a single Muldis Rosetta in-DBMS value, which is conceptually immutable, eternal, and not fixed in time or space; the object is immutable. It is associated with a specific C object, the one whose C method created it. You can use C objects in Perl routines the same as normal immutable Perl values or objects, including that you just do ordinary Perl variable assignment. C objects are the normal way to directly share or move data between the Muldis Rosetta DBMS and main Perl environments. The value that a C object represents is set when the C object is created, and it can't be changed afterwards. =over =item C This method returns the C object that the invocant C is associated with. =item C This method returns (typically Muldis D) source code that defines a value literal equivalent to the in-DBMS value that the invocant C represents. The language of the source code to return must be explicitly specified, either by giving a defined C<$lang> argument, or by ensuring that the C object associated with this C has a defined "expected command language" attribute. =back =head1 DIAGNOSTICS I =head1 CONFIGURATION AND ENVIRONMENT I =head1 DEPENDENCIES This file requires any version of Perl 5.x.y that is at least 5.8.1, and recommends one that is at least 5.10.0. It also requires these Perl 5 packages that are bundled with any version of Perl 5.x.y that is at least 5.10.0, and are also on CPAN for separate installation by users of earlier Perl versions: L. It also requires these Perl 5 packages that are on CPAN: L. =head1 INCOMPATIBILITIES None reported. =head1 SEE ALSO Go to L for the majority of distribution-internal references, and L for the majority of distribution-external references. =head1 BUGS AND LIMITATIONS The Muldis Rosetta framework for Perl 5 is built according to certain old-school or traditional Perl-5-land design principles, including that there are no explicit attempts in code to enforce privacy of the framework's internals, besides not documenting them as part of the public API. (The Muldis Rosetta framework for Perl 6 is different.) That said, you should still respect that privacy and just use the public API that Muldis Rosetta provides. If you bypass the public API anyway, as Perl 5 allows, you do so at your own peril. I =head1 AUTHOR Darren Duncan (C) =head1 LICENSE AND COPYRIGHT This file is part of the Muldis Rosetta framework. Muldis Rosetta is Copyright © 2002-2008, Darren Duncan. See the LICENSE AND COPYRIGHT of L for details. =head1 TRADEMARK POLICY The TRADEMARK POLICY in L applies to this file too. =head1 ACKNOWLEDGEMENTS The ACKNOWLEDGEMENTS in L apply to this file too. =cut