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.
The main system appears first, followed by any subsystem dependency.
cl-rules
Simple DSL for rules that can be configured without code
Ito Dimercel
GPL-3.0
# 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.
0.1
cl-yaml
(system).
alexandria
(system).
src
(module).
Modules are listed depth-first from the system components tree.
cl-rules/src
cl-rules
(system).
core.lisp
(file).
serialization.lisp
(file).
cl-rules.lisp
(file).
Files are sorted by type and then listed depth-first from the systems components trees.
cl-rules/cl-rules.asd
cl-rules/src/core.lisp
cl-rules/src/serialization.lisp
cl-rules/src/cl-rules.lisp
cl-rules/src/core.lisp
src
(module).
action-args
(reader).
(setf action-args)
(writer).
action-name
(reader).
(setf action-name)
(writer).
action-reg-p
(function).
condn-args
(reader).
(setf condn-args)
(writer).
condn-name
(reader).
(setf condn-name)
(writer).
condn-reg-p
(function).
condn-val
(function).
defaction
(macro).
defparam
(macro).
defrule
(macro).
eval-rule
(function).
fire-rule
(function).
make-action
(function).
make-condn
(function).
make-rule
(function).
param-reg-p
(function).
param-val
(function).
register-rule
(function).
rule-by-name
(function).
rule-conditions
(reader).
(setf rule-conditions)
(writer).
rule-name
(reader).
(setf rule-name)
(writer).
rule-reg-p
(function).
setparam
(function).
unregister-rule
(function).
with-rules
(macro).
%make-action
(function).
%make-condn
(function).
%make-rule
(function).
*actions*
(special variable).
*conditions*
(special variable).
*parameters*
(special variable).
*rules*
(special variable).
action
(structure).
action-by-name
(function).
action-p
(function).
actions-specified-p
(function).
condn
(structure).
condn-by-name
(function).
condn-p
(function).
copy-action
(function).
copy-condn
(function).
copy-rule
(function).
defcondn
(macro).
eval-action
(function).
eval-params
(function).
fire-condition
(function).
rule
(structure).
rule-actions
(reader).
(setf rule-actions)
(writer).
rule-p
(function).
rule-value
(function).
cl-rules/src/serialization.lisp
core.lisp
(file).
src
(module).
loads
(function).
save-to-file
(function).
save-to-str
(function).
+root-key+
(constant).
is-param-p
(function).
serialize-action
(function).
serialize-cond
(function).
serialize-param
(function).
serialize-rule
(function).
unserialize-action
(function).
unserialize-cond
(function).
unserialize-param
(function).
unserialize-rule
(function).
cl-rules/src/cl-rules.lisp
core.lisp
(file).
serialization.lisp
(file).
src
(module).
Packages are listed by definition order.
cl-rules.serialization
cl-yaml
.
common-lisp
.
loads
(function).
save-to-file
(function).
save-to-str
(function).
+root-key+
(constant).
is-param-p
(function).
serialize-action
(function).
serialize-cond
(function).
serialize-param
(function).
serialize-rule
(function).
unserialize-action
(function).
unserialize-cond
(function).
unserialize-param
(function).
unserialize-rule
(function).
cl-rules.core
common-lisp
.
action-args
(reader).
(setf action-args)
(writer).
action-name
(reader).
(setf action-name)
(writer).
action-reg-p
(function).
condn-args
(reader).
(setf condn-args)
(writer).
condn-name
(reader).
(setf condn-name)
(writer).
condn-reg-p
(function).
condn-val
(function).
defaction
(macro).
defparam
(macro).
defrule
(macro).
eval-rule
(function).
fire-rule
(function).
make-action
(function).
make-condn
(function).
make-rule
(function).
param-reg-p
(function).
param-val
(function).
register-rule
(function).
rule-by-name
(function).
rule-conditions
(reader).
(setf rule-conditions)
(writer).
rule-name
(reader).
(setf rule-name)
(writer).
rule-reg-p
(function).
setparam
(function).
unregister-rule
(function).
with-rules
(macro).
%make-action
(function).
%make-condn
(function).
%make-rule
(function).
*actions*
(special variable).
*conditions*
(special variable).
*parameters*
(special variable).
*rules*
(special variable).
action
(structure).
action-by-name
(function).
action-p
(function).
actions-specified-p
(function).
condn
(structure).
condn-by-name
(function).
condn-p
(function).
copy-action
(function).
copy-condn
(function).
copy-rule
(function).
defcondn
(macro).
eval-action
(function).
eval-params
(function).
fire-condition
(function).
rule
(structure).
rule-actions
(reader).
(setf rule-actions)
(writer).
rule-p
(function).
rule-value
(function).
Definitions are sorted by export status, category, package, and then by lexicographic order.
Defines a new parameter
Allow traversing by all registered rules. In SYM stored name of rule
args
.
name
.
Action with NAME is registered?
args
.
name
.
Condition with NAME id registered?
Return value of condition for specified arguments
Is similar fire-rule, but evaluate actions. Actions will be evaluate only if the value of rule is true
Will return true if all conditions is true. Linked actions not execute.
Create condition from itself name and list of arguments ARGS. NAME is symbol or string
Create new rule with NAME and list of conditions. Rule is true, only if all conditions is true
Parameter with NAME is registered?
Will return value of parameter, if he is exists. If parameter with NAME not exists, returns BAD-VAL. NAME is symbol or string
Registers new rule. Names of rules must be unique
NAME is symbol or string
name
.
Rule with NAME is registered?
Set a new value for parameter
Define new condition with NAME. Represents arbitrary predicate
Return action by specified name. Name must be a symbol or string
Will return condiiton by specified name. NAME is symbol or string
Each action linked with function. Function called with arguments of action
If a parameter is specified among the arguments, its value is replacement
Evaluate predicate. Will return true or false
Does the string contain a parameter? Example: ’{{param-name}}’
Rule is true?
Action consists of name and arguments for the function. ARGS - list of arguments, which will be link with function
Jump to: | %
(
A C D E F I L M P R S U W |
---|
Jump to: | %
(
A C D E F I L M P R S U W |
---|
Jump to: | *
+
A C N S |
---|
Jump to: | *
+
A C N S |
---|
Jump to: | A C F M P R S |
---|
Jump to: | A C F M P R S |
---|