NAME

Kvasir::Declare - Declarative interface for Kvasir engines

SYNOPSIS

use Kvasir::Constants;
use Kvasir::Declare;

my $input = MyApp::MyOtherInput->new();
my $rule  = MyApp::ComplexRule->new();

my $engine = engine {
    defaults "d1" => {
      some_arg => 1,
    };
    
    input "input1" => instanceof "MyApp::Input" => with_defaults "d1";
    input "input2" => $input;

    rule "rule1" => instanceof "MyApp::Rule" => with_args { input => "input1" };
    rule "rule2" => $rule;

    rule "rule3" => does {
        my ($input, $global, $local) = @_[KV_INPUT, KV_GLOBAL_DATA, KV_LOCAL_DATA];

        if ($input->get("input1") < 5 &&
            $input->get("input1") > 10) {
            return KV_MATCH;  
        }

        return KV_NO_MATCH;
    }; 

    action "action1" => does {
        my $result = complex_calculation();
        $_[KV_LOCAL]->set("result" => $result);
    };
          
    prehook "check_date" => does {
        return KV_CONTINUE;
    };
    
    run "action1" => when qw(rule1 rule2 rule3);
};

$engine->run();

INTERFACE

FUNCTIONS

engine BLOCK

Creates a new engine.

action NAME [=> instanceof CLASS [ => with_defaults DEFAULTS ] [ => with_args ARGS]]
action NAME => INSTANCE
action NAME => does BLOCK

Creates a new action and registers it in the engine as NAME. If an object is passed it must conform to Kvasir::Action.

input NAME [=> instanceof CLASS [ => with_defaults DEFAULTS ] [ => with_args ARGS]]
input NAME => INSTANCE
input NAME => does BLOCK

Creates a new input and registers it in the engine as NAME. If an object is passed it must conform to Kvasir::Input.

output NAME [=> instanceof CLASS [ => with_defaults DEFAULTS ] [ => with_args ARGS]]
output NAME => INSTANCE
output NAME => does BLOCK

Creates a new output and registers it in the engine as NAME. If an object is passed it must conform to Kvasir::Output.

prehook NAME [=> instanceof CLASS [ => with_defaults DEFAULTS ] [ => with_args ARGS]]
prehook NAME => INSTANCE
prehook NAME => does BLOCK

Creates a new prehook and registers it in the engine as NAME. If an object is passed it must conform to Kvasir::Hook.

Prehooks are evaulated in the order they are declared.

posthook NAME [=> instanceof CLASS [ => with_defaults DEFAULTS ] [ => with_args ARGS]]
posthook NAME => INSTANCE
posthook NAME => does BLOCK

Creates a new posthook and registers it in the engine as NAME. If an object is passed it must conform to Kvasir::Hook.

Posthooks are evaulated in the order they are declared.

rule NAME [=> instanceof CLASS [ => with_defaults DEFAULTS ] [ => with_args ARGS]]
rule NAME => INSTANCE
rule NAME => does BLOCK

Creates a new rule and registers it in the engine as NAME. If an object is passed it must conform to Kvasir::Rule.

Rules are evaulated in the order they are declared unless an order has explicitly been defined using rule_order. d

run ACTIONS => when RULES

Runs the list of ACTION when the given RULES matches.

with_args HASHREF

Creates a argument set for the entity.

with_defaults DEFAULT | DEFAULTS

Use the defaults defined by DEFAULT or multiple defaults defined by the ARRAY referene DEFAULTS.

as NAME

Checks that NAME is a valid name and returns it if so. Otherwise throws an exception.

instanceof CLASS

Marks the declared entity to be an instance of the given CLASS.

defaults NAME => ARGUMENTS

Creates a new arguent set with the given NAME and arguments. ARGUMENTS must be a hash reference.

does BLOCK

Marks the declared entity to be implemented via a Perl subroutine.

load_module MODULE

Load the module MODULE.