The cl-rules Reference Manual

This is the cl-rules Reference Manual, version 0.1, generated automatically by Declt version 4.0 beta 2 "William Riker" on Sun Dec 15 05:20:32 2024 GMT+0.

Table of Contents


1 Introduction


2 Systems

The main system appears first, followed by any subsystem dependency.


2.1 cl-rules

Simple DSL for rules that can be configured without code

Author

Ito Dimercel

License

GPL-3.0

Long Description

# cl-rules

[![Build Status](https://travis-ci.org/Dimercel/cl-rules.svg?branch=master)](https://travis-ci.org/Dimercel/cl-rules)

Simple DSL for rules that can be configured without code. If part of your program logic is set of rules, this package will help you. You can create custom rules and conditions, bind actions to rules, load and save your rules from/in yaml format.

## Usage

Consider a simple example: system of different tariffs, which defines in a declarative style. Each tariff contains a set of limits from which the cost is calculated.

### 1. Define your set of parameters

Parameters represent basic variables of your system. They may contain absolutely any information and arbitrary structure. For creating them, specify the name and initial value. Any parameter can change own value over time, for this purpose is intended ‘setparam‘.
In our example, parameters - this is basic characteristics of a tariff. Define them:

“‘common-lisp
(in-package :cl-user)
(defpackage my-package
(:use :cl)
(:import-from :cl-rules
:defparam
:defaction
:defcondn
:defrule
:eval-rule
:fire-rule)))
(in-package :my-package)

(defparam ram 0)
(defparam cpu 0)
(defparam disk 0)

(defvar *user-balance* 1000) ;; Starting value of user balance. Only for illustration.
“‘

### 2. Define your conditons

Condition represent predicate, which may be only a true or false. In the base case, the condition is a function of previously defined parameters. With help of ‘param-val‘ you can get a parameter value within the condition or specify it in arguments.

All values of the tariff characteristics are in a certain range, therefore it is sufficient for us to define only one condition:

“‘common-lisp
(defcondn between (low-limit high-limit value)
(and (>= value low-limit) (<= value high-limit value)))
“‘

### 3. Define your actions

Action - arbitrary kind of code. Any actions may be linked with a rule. The action called only if a rule is true.

Define two actions: first - withdraw money from the user account, second - print a user account balance.

“‘common-lisp
(defaction pay (amount)
(decf *user-balance* amount))

(defaction report ()
(print (format nil "The balance is ~d" *user-balance*)))
“‘

### 4. Define your rules!!!

The rules - heart of our system. They consist of several conditions and optional actions. Conditions specified with concrete values of arguments and can be specified in the arbitrary order. Only if all conditions are true, a rule is true.

Now we can define the rules for our tariff system. On this stage exists two ways:

define your rules in code

“‘common-lisp
(defrule mini-tariff
:actions
((pay 100)
(report))

(between 0 512 ram)
(between 1 1 cpu)
(between 0 20 disk))

(defrule base-tariff
:actions
((pay 200)
(report))

(between 513 1024 ram)
(between 1 1 cpu)
(between 21 30 disk))

(defrule super-tariff
:actions
((pay 300)
(report))

(between 1025 2048 ram)
(between 2 2 cpu)
(between 31 50 disk))
“‘
or load them from file using function ‘(loads "/path/to/your/file")‘

“‘yml
rules:
mini-tariff:
conditions:
- [between, 0, 512, "{{ram}}"]
- [between, 1, 1, "{{cpu}}"]
- [between, 0, 20, "{{disk}}"]
actions:
- [pay, 100]
- [report]
base-tariff:
conditions:
- [between, 513, 1024, "{{ram}}"]
- [between, 1, 1, "{{cpu}}"]
- [between, 21, 30, "{{disk}}"]
actions:
- [pay, 200]
- [report]
super-tariff:
conditions:
- [between, 1025, 2048, "{{ram}}"]
- [between, 2, 2, "{{cpu}}"]
- [between, 31, 50, "{{disk}}"]
actions:
- [pay, 300]
- [report]
“‘
For storage is used [yaml](http://yaml.org/) format.

### Time to run your rules

Rules running with this command: ‘(fire-rule ’mini-tariff ’base-tariff ’super-tariff)‘, but in this case, actions not called. ‘fire-rule‘ - only return a logic value, true - if all rules are true and false - otherwise.

If you want to call actions, using this: ‘(eval-rule ’mini-tariff ’base-tariff ’super-tariff)‘. After this command, ‘*user-balance*‘ will be less.

More examples in [tests](https://github.com/Dimercel/cl-rules/tree/master/t).

## Installation

“‘common-lisp
(ql:quickload :cl-rules)
“‘

## Author

* Ito Dimercel (xolcman@gmail.com)

## Copyright

Copyright (c) 2019 Ito Dimercel (xolcman@gmail.com)

## License

Licensed under the GPL-3.0 License.

Version

0.1

Dependencies
  • cl-yaml (system).
  • alexandria (system).
Source

cl-rules.asd.

Child Component

src (module).


3 Modules

Modules are listed depth-first from the system components tree.


3.1 cl-rules/src

Source

cl-rules.asd.

Parent Component

cl-rules (system).

Child Components

4 Files

Files are sorted by type and then listed depth-first from the systems components trees.


4.1 Lisp


4.1.1 cl-rules/cl-rules.asd

Source

cl-rules.asd.

Parent Component

cl-rules (system).

ASDF Systems

cl-rules.

Packages

cl-rules-asd.


4.1.2 cl-rules/src/core.lisp

Source

cl-rules.asd.

Parent Component

src (module).

Packages

cl-rules.core.

Public Interface
Internals

4.1.3 cl-rules/src/serialization.lisp

Dependency

core.lisp (file).

Source

cl-rules.asd.

Parent Component

src (module).

Packages

cl-rules.serialization.

Public Interface
Internals

4.1.4 cl-rules/src/cl-rules.lisp

Dependencies
Source

cl-rules.asd.

Parent Component

src (module).

Packages

cl-rules.


5 Packages

Packages are listed by definition order.


5.1 cl-rules-asd

Source

cl-rules.asd.

Use List
  • asdf/interface.
  • common-lisp.

5.2 cl-rules

Source

cl-rules.lisp.

Use List

common-lisp.


5.3 cl-rules.serialization

Source

serialization.lisp.

Use List
  • cl-yaml.
  • common-lisp.
Public Interface
Internals

5.4 cl-rules.core

Source

core.lisp.

Use List

common-lisp.

Public Interface
Internals

6 Definitions

Definitions are sorted by export status, category, package, and then by lexicographic order.


6.1 Public Interface


6.1.1 Macros

Macro: defaction (name args &body forms)
Package

cl-rules.core.

Source

core.lisp.

Macro: defparam (name form)

Defines a new parameter

Package

cl-rules.core.

Source

core.lisp.

Macro: defrule (name &body forms)
Package

cl-rules.core.

Source

core.lisp.

Macro: with-rules (sym &body forms)

Allow traversing by all registered rules. In SYM stored name of rule

Package

cl-rules.core.

Source

core.lisp.


6.1.2 Ordinary functions

Reader: action-args (instance)
Writer: (setf action-args) (instance)
Package

cl-rules.core.

Source

core.lisp.

Target Slot

args.

Reader: action-name (instance)
Writer: (setf action-name) (instance)
Package

cl-rules.core.

Source

core.lisp.

Target Slot

name.

Function: action-reg-p (name)

Action with NAME is registered?

Package

cl-rules.core.

Source

core.lisp.

Reader: condn-args (instance)
Writer: (setf condn-args) (instance)
Package

cl-rules.core.

Source

core.lisp.

Target Slot

args.

Reader: condn-name (instance)
Writer: (setf condn-name) (instance)
Package

cl-rules.core.

Source

core.lisp.

Target Slot

name.

Function: condn-reg-p (name)

Condition with NAME id registered?

Package

cl-rules.core.

Source

core.lisp.

Function: condn-val (name args &optional bad-val)

Return value of condition for specified arguments

Package

cl-rules.core.

Source

core.lisp.

Function: eval-rule (&rest rule-names)

Is similar fire-rule, but evaluate actions. Actions will be evaluate only if the value of rule is true

Package

cl-rules.core.

Source

core.lisp.

Function: fire-rule (&rest rule-names)

Will return true if all conditions is true. Linked actions not execute.

Package

cl-rules.core.

Source

core.lisp.

Function: loads (str-or-path)
Package

cl-rules.serialization.

Source

serialization.lisp.

Function: make-action (name args)
Package

cl-rules.core.

Source

core.lisp.

Function: make-condn (name args)

Create condition from itself name and list of arguments ARGS. NAME is symbol or string

Package

cl-rules.core.

Source

core.lisp.

Function: make-rule (name conditions &optional actions)

Create new rule with NAME and list of conditions. Rule is true, only if all conditions is true

Package

cl-rules.core.

Source

core.lisp.

Function: param-reg-p (name)

Parameter with NAME is registered?

Package

cl-rules.core.

Source

core.lisp.

Function: param-val (name &optional bad-val)

Will return value of parameter, if he is exists. If parameter with NAME not exists, returns BAD-VAL. NAME is symbol or string

Package

cl-rules.core.

Source

core.lisp.

Function: register-rule (rule)

Registers new rule. Names of rules must be unique

Package

cl-rules.core.

Source

core.lisp.

Function: rule-by-name (name)

NAME is symbol or string

Package

cl-rules.core.

Source

core.lisp.

Reader: rule-conditions (instance)
Writer: (setf rule-conditions) (instance)
Package

cl-rules.core.

Source

core.lisp.

Target Slot

conditions.

Reader: rule-name (instance)
Writer: (setf rule-name) (instance)
Package

cl-rules.core.

Source

core.lisp.

Target Slot

name.

Function: rule-reg-p (name)

Rule with NAME is registered?

Package

cl-rules.core.

Source

core.lisp.

Function: save-to-file (path &rest rule-names)
Package

cl-rules.serialization.

Source

serialization.lisp.

Function: save-to-str (&rest rule-names)
Package

cl-rules.serialization.

Source

serialization.lisp.

Function: setparam (name val)

Set a new value for parameter

Package

cl-rules.core.

Source

core.lisp.

Function: unregister-rule (name-of-rule)
Package

cl-rules.core.

Source

core.lisp.


6.2 Internals


6.2.1 Constants

Constant: +root-key+
Package

cl-rules.serialization.

Source

serialization.lisp.


6.2.2 Special variables

Special Variable: *actions*
Package

cl-rules.core.

Source

core.lisp.

Special Variable: *conditions*
Package

cl-rules.core.

Source

core.lisp.

Special Variable: *parameters*
Package

cl-rules.core.

Source

core.lisp.

Special Variable: *rules*
Package

cl-rules.core.

Source

core.lisp.


6.2.3 Macros

Macro: defcondn (name args &body forms)

Define new condition with NAME. Represents arbitrary predicate

Package

cl-rules.core.

Source

core.lisp.


6.2.4 Ordinary functions

Function: %make-action (&key name args)
Package

cl-rules.core.

Source

core.lisp.

Function: %make-condn (&key name args)
Package

cl-rules.core.

Source

core.lisp.

Function: %make-rule (&key name conditions actions)
Package

cl-rules.core.

Source

core.lisp.

Function: action-by-name (name)

Return action by specified name. Name must be a symbol or string

Package

cl-rules.core.

Source

core.lisp.

Function: action-p (object)
Package

cl-rules.core.

Source

core.lisp.

Function: actions-specified-p (rule-args)
Package

cl-rules.core.

Source

core.lisp.

Function: condn-by-name (name)

Will return condiiton by specified name. NAME is symbol or string

Package

cl-rules.core.

Source

core.lisp.

Function: condn-p (object)
Package

cl-rules.core.

Source

core.lisp.

Function: copy-action (instance)
Package

cl-rules.core.

Source

core.lisp.

Function: copy-condn (instance)
Package

cl-rules.core.

Source

core.lisp.

Function: copy-rule (instance)
Package

cl-rules.core.

Source

core.lisp.

Function: eval-action (action)

Each action linked with function. Function called with arguments of action

Package

cl-rules.core.

Source

core.lisp.

Function: eval-params (args)

If a parameter is specified among the arguments, its value is replacement

Package

cl-rules.core.

Source

core.lisp.

Function: fire-condition (condition)

Evaluate predicate. Will return true or false

Package

cl-rules.core.

Source

core.lisp.

Function: is-param-p (item)

Does the string contain a parameter? Example: ’{{param-name}}’

Package

cl-rules.serialization.

Source

serialization.lisp.

Reader: rule-actions (instance)
Writer: (setf rule-actions) (instance)
Package

cl-rules.core.

Source

core.lisp.

Target Slot

actions.

Function: rule-p (object)
Package

cl-rules.core.

Source

core.lisp.

Function: rule-value (name)

Rule is true?

Package

cl-rules.core.

Source

core.lisp.

Function: serialize-action (action)
Package

cl-rules.serialization.

Source

serialization.lisp.

Function: serialize-cond (condition)
Package

cl-rules.serialization.

Source

serialization.lisp.

Function: serialize-param (name)
Package

cl-rules.serialization.

Source

serialization.lisp.

Function: serialize-rule (rule ruleset)
Package

cl-rules.serialization.

Source

serialization.lisp.

Function: unserialize-action (raw-action)
Package

cl-rules.serialization.

Source

serialization.lisp.

Function: unserialize-cond (raw-condition)
Package

cl-rules.serialization.

Source

serialization.lisp.

Function: unserialize-param (raw-param)
Package

cl-rules.serialization.

Source

serialization.lisp.

Function: unserialize-rule (name rules)
Package

cl-rules.serialization.

Source

serialization.lisp.


6.2.5 Structures

Structure: action

Action consists of name and arguments for the function. ARGS - list of arguments, which will be link with function

Package

cl-rules.core.

Source

core.lisp.

Direct superclasses

structure-object.

Direct slots
Slot: name
Readers

action-name.

Writers

(setf action-name).

Slot: args
Readers

action-args.

Writers

(setf action-args).

Structure: condn
Package

cl-rules.core.

Source

core.lisp.

Direct superclasses

structure-object.

Direct slots
Slot: name
Readers

condn-name.

Writers

(setf condn-name).

Slot: args
Readers

condn-args.

Writers

(setf condn-args).

Structure: rule
Package

cl-rules.core.

Source

core.lisp.

Direct superclasses

structure-object.

Direct slots
Slot: name
Readers

rule-name.

Writers

(setf rule-name).

Slot: conditions
Readers

rule-conditions.

Writers

(setf rule-conditions).

Slot: actions
Readers

rule-actions.

Writers

(setf rule-actions).


Appendix A Indexes


A.1 Concepts


A.2 Functions

Jump to:   %   (  
A   C   D   E   F   I   L   M   P   R   S   U   W  
Index Entry  Section

%
%make-action: Private ordinary functions
%make-condn: Private ordinary functions
%make-rule: Private ordinary functions

(
(setf action-args): Public ordinary functions
(setf action-name): Public ordinary functions
(setf condn-args): Public ordinary functions
(setf condn-name): Public ordinary functions
(setf rule-actions): Private ordinary functions
(setf rule-conditions): Public ordinary functions
(setf rule-name): Public ordinary functions

A
action-args: Public ordinary functions
action-by-name: Private ordinary functions
action-name: Public ordinary functions
action-p: Private ordinary functions
action-reg-p: Public ordinary functions
actions-specified-p: Private ordinary functions

C
condn-args: Public ordinary functions
condn-by-name: Private ordinary functions
condn-name: Public ordinary functions
condn-p: Private ordinary functions
condn-reg-p: Public ordinary functions
condn-val: Public ordinary functions
copy-action: Private ordinary functions
copy-condn: Private ordinary functions
copy-rule: Private ordinary functions

D
defaction: Public macros
defcondn: Private macros
defparam: Public macros
defrule: Public macros

E
eval-action: Private ordinary functions
eval-params: Private ordinary functions
eval-rule: Public ordinary functions

F
fire-condition: Private ordinary functions
fire-rule: Public ordinary functions
Function, %make-action: Private ordinary functions
Function, %make-condn: Private ordinary functions
Function, %make-rule: Private ordinary functions
Function, (setf action-args): Public ordinary functions
Function, (setf action-name): Public ordinary functions
Function, (setf condn-args): Public ordinary functions
Function, (setf condn-name): Public ordinary functions
Function, (setf rule-actions): Private ordinary functions
Function, (setf rule-conditions): Public ordinary functions
Function, (setf rule-name): Public ordinary functions
Function, action-args: Public ordinary functions
Function, action-by-name: Private ordinary functions
Function, action-name: Public ordinary functions
Function, action-p: Private ordinary functions
Function, action-reg-p: Public ordinary functions
Function, actions-specified-p: Private ordinary functions
Function, condn-args: Public ordinary functions
Function, condn-by-name: Private ordinary functions
Function, condn-name: Public ordinary functions
Function, condn-p: Private ordinary functions
Function, condn-reg-p: Public ordinary functions
Function, condn-val: Public ordinary functions
Function, copy-action: Private ordinary functions
Function, copy-condn: Private ordinary functions
Function, copy-rule: Private ordinary functions
Function, eval-action: Private ordinary functions
Function, eval-params: Private ordinary functions
Function, eval-rule: Public ordinary functions
Function, fire-condition: Private ordinary functions
Function, fire-rule: Public ordinary functions
Function, is-param-p: Private ordinary functions
Function, loads: Public ordinary functions
Function, make-action: Public ordinary functions
Function, make-condn: Public ordinary functions
Function, make-rule: Public ordinary functions
Function, param-reg-p: Public ordinary functions
Function, param-val: Public ordinary functions
Function, register-rule: Public ordinary functions
Function, rule-actions: Private ordinary functions
Function, rule-by-name: Public ordinary functions
Function, rule-conditions: Public ordinary functions
Function, rule-name: Public ordinary functions
Function, rule-p: Private ordinary functions
Function, rule-reg-p: Public ordinary functions
Function, rule-value: Private ordinary functions
Function, save-to-file: Public ordinary functions
Function, save-to-str: Public ordinary functions
Function, serialize-action: Private ordinary functions
Function, serialize-cond: Private ordinary functions
Function, serialize-param: Private ordinary functions
Function, serialize-rule: Private ordinary functions
Function, setparam: Public ordinary functions
Function, unregister-rule: Public ordinary functions
Function, unserialize-action: Private ordinary functions
Function, unserialize-cond: Private ordinary functions
Function, unserialize-param: Private ordinary functions
Function, unserialize-rule: Private ordinary functions

I
is-param-p: Private ordinary functions

L
loads: Public ordinary functions

M
Macro, defaction: Public macros
Macro, defcondn: Private macros
Macro, defparam: Public macros
Macro, defrule: Public macros
Macro, with-rules: Public macros
make-action: Public ordinary functions
make-condn: Public ordinary functions
make-rule: Public ordinary functions

P
param-reg-p: Public ordinary functions
param-val: Public ordinary functions

R
register-rule: Public ordinary functions
rule-actions: Private ordinary functions
rule-by-name: Public ordinary functions
rule-conditions: Public ordinary functions
rule-name: Public ordinary functions
rule-p: Private ordinary functions
rule-reg-p: Public ordinary functions
rule-value: Private ordinary functions

S
save-to-file: Public ordinary functions
save-to-str: Public ordinary functions
serialize-action: Private ordinary functions
serialize-cond: Private ordinary functions
serialize-param: Private ordinary functions
serialize-rule: Private ordinary functions
setparam: Public ordinary functions

U
unregister-rule: Public ordinary functions
unserialize-action: Private ordinary functions
unserialize-cond: Private ordinary functions
unserialize-param: Private ordinary functions
unserialize-rule: Private ordinary functions

W
with-rules: Public macros