Next: Introduction, Previous: (dir), Up: (dir) [Contents][Index]
This is the cl-rules Reference Manual, version 0.1, generated automatically by Declt version 3.0 "Montgomery Scott" on Tue Dec 22 12:44:56 2020 GMT+0.
• Introduction | What cl-rules is all about | |
• Systems | The systems documentation | |
• Modules | The modules documentation | |
• Files | The files documentation | |
• Packages | The packages documentation | |
• Definitions | The symbols documentation | |
• Indexes | Concepts, functions, variables and data types |
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.
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.
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:
(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.
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:
(defcondn between (low-limit high-limit value)
(and (>= value low-limit) (<= value high-limit value)))
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.
(defaction pay (amount)
(decf *user-balance* amount))
(defaction report ()
(print (format nil "The balance is ~d" *user-balance*)))
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
(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")
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 format.
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.
(ql:quickload :cl-rules)
Copyright (c) 2019 Ito Dimercel (xolcman@gmail.com)
Licensed under the GPL-3.0 License.
Next: Modules, Previous: Introduction, Up: Top [Contents][Index]
The main system appears first, followed by any subsystem dependency.
• The cl-rules system |
Ito Dimercel
GPL-3.0
Simple DSL for rules that can be configured without code
# cl-rules
[](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-rules.asd (file)
src (module)
Modules are listed depth-first from the system components tree.
• The cl-rules/src module |
cl-rules (system)
src/
Files are sorted by type and then listed depth-first from the systems components trees.
• Lisp files |
• The cl-rules.asd file | ||
• The cl-rules/src/core.lisp file | ||
• The cl-rules/src/serialization.lisp file | ||
• The cl-rules/src/cl-rules.lisp file |
Next: The cl-rules/src/core․lisp file, Previous: Lisp files, Up: Lisp files [Contents][Index]
cl-rules.asd
cl-rules (system)
Next: The cl-rules/src/serialization․lisp file, Previous: The cl-rules․asd file, Up: Lisp files [Contents][Index]
src (module)
src/core.lisp
Next: The cl-rules/src/cl-rules․lisp file, Previous: The cl-rules/src/core․lisp file, Up: Lisp files [Contents][Index]
core.lisp (file)
src (module)
src/serialization.lisp
Previous: The cl-rules/src/serialization․lisp file, Up: Lisp files [Contents][Index]
src (module)
src/cl-rules.lisp
Next: Definitions, Previous: Files, Up: Top [Contents][Index]
Packages are listed by definition order.
• The cl-rules-asd package | ||
• The cl-rules.core package | ||
• The cl-rules.serialization package | ||
• The cl-rules package |
Next: The cl-rules․core package, Previous: Packages, Up: Packages [Contents][Index]
cl-rules.asd
Next: The cl-rules․serialization package, Previous: The cl-rules-asd package, Up: Packages [Contents][Index]
core.lisp (file)
common-lisp
Next: The cl-rules package, Previous: The cl-rules․core package, Up: Packages [Contents][Index]
serialization.lisp (file)
Previous: The cl-rules․serialization package, Up: Packages [Contents][Index]
cl-rules.lisp (file)
common-lisp
Definitions are sorted by export status, category, package, and then by lexicographic order.
• Exported definitions | ||
• Internal definitions |
Next: Internal definitions, Previous: Definitions, Up: Definitions [Contents][Index]
• Exported macros | ||
• Exported functions |
Next: Exported functions, Previous: Exported definitions, Up: Exported definitions [Contents][Index]
core.lisp (file)
Defines a new parameter
core.lisp (file)
core.lisp (file)
Allow traversing by all registered rules. In SYM stored name of rule
core.lisp (file)
Previous: Exported macros, Up: Exported definitions [Contents][Index]
core.lisp (file)
core.lisp (file)
Action with NAME is registered?
core.lisp (file)
core.lisp (file)
core.lisp (file)
Condition with NAME id registered?
core.lisp (file)
Return value of condition for specified arguments
core.lisp (file)
Is similar fire-rule, but evaluate actions. Actions will be evaluate only if the value of rule is true
core.lisp (file)
Will return true if all conditions is true. Linked actions not execute.
core.lisp (file)
serialization.lisp (file)
core.lisp (file)
Create condition from itself name and list of arguments ARGS. NAME is symbol or string
core.lisp (file)
Create new rule with NAME and list of conditions. Rule is true, only if all conditions is true
core.lisp (file)
Parameter with NAME is registered?
core.lisp (file)
Will return value of parameter, if he is exists. If parameter with NAME not exists, returns BAD-VAL. NAME is symbol or string
core.lisp (file)
Registers new rule. Names of rules must be unique
core.lisp (file)
NAME is symbol or string
core.lisp (file)
core.lisp (file)
core.lisp (file)
Rule with NAME is registered?
core.lisp (file)
serialization.lisp (file)
serialization.lisp (file)
Set a new value for parameter
core.lisp (file)
core.lisp (file)
Previous: Exported definitions, Up: Definitions [Contents][Index]
• Internal constants | ||
• Internal special variables | ||
• Internal macros | ||
• Internal functions | ||
• Internal structures |
Next: Internal special variables, Previous: Internal definitions, Up: Internal definitions [Contents][Index]
serialization.lisp (file)
Next: Internal macros, Previous: Internal constants, Up: Internal definitions [Contents][Index]
core.lisp (file)
core.lisp (file)
core.lisp (file)
core.lisp (file)
Next: Internal functions, Previous: Internal special variables, Up: Internal definitions [Contents][Index]
Define new condition with NAME. Represents arbitrary predicate
core.lisp (file)
Next: Internal structures, Previous: Internal macros, Up: Internal definitions [Contents][Index]
core.lisp (file)
core.lisp (file)
core.lisp (file)
Return action by specified name. Name must be a symbol or string
core.lisp (file)
core.lisp (file)
core.lisp (file)
Will return condiiton by specified name. NAME is symbol or string
core.lisp (file)
core.lisp (file)
core.lisp (file)
core.lisp (file)
core.lisp (file)
Each action linked with function. Function called with arguments of action
core.lisp (file)
If a parameter is specified among the arguments, its value is replacement
core.lisp (file)
Evaluate predicate. Will return true or false
core.lisp (file)
Does the string contain a parameter? Example: ’{{param-name}}’
serialization.lisp (file)
core.lisp (file)
core.lisp (file)
Rule is true?
core.lisp (file)
serialization.lisp (file)
serialization.lisp (file)
serialization.lisp (file)
serialization.lisp (file)
serialization.lisp (file)
serialization.lisp (file)
serialization.lisp (file)
serialization.lisp (file)
Previous: Internal functions, Up: Internal definitions [Contents][Index]
Action consists of name and arguments for the function. ARGS - list of arguments, which will be link with function
core.lisp (file)
structure-object (structure)
action-name (function)
(setf action-name) (function)
action-args (function)
(setf action-args) (function)
core.lisp (file)
structure-object (structure)
condn-name (function)
(setf condn-name) (function)
condn-args (function)
(setf condn-args) (function)
core.lisp (file)
structure-object (structure)
rule-name (function)
(setf rule-name) (function)
rule-conditions (function)
(setf rule-conditions) (function)
rule-actions (function)
(setf rule-actions) (function)
Previous: Definitions, Up: Top [Contents][Index]
• Concept index | ||
• Function index | ||
• Variable index | ||
• Data type index |
Next: Function index, Previous: Indexes, Up: Indexes [Contents][Index]
Jump to: | C F L M |
---|
Jump to: | C F L M |
---|
Next: Variable index, Previous: Concept index, Up: Indexes [Contents][Index]
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 |
---|
Next: Data type index, Previous: Function index, Up: Indexes [Contents][Index]
Jump to: | *
+
A C N S |
---|
Jump to: | *
+
A C N S |
---|
Previous: Variable index, Up: Indexes [Contents][Index]
Jump to: | A C P R S |
---|
Jump to: | A C P R S |
---|