Skip to main content

thoughts framework

thoughts is a lightweight rules-based engine

How To Use:

add a .json file that contains your rules:

[ { "when": "hello", "then": {"#output": "hello, world!"} } ]

import the engine

from thoughts.rules_engine import RulesEngine

start a new engine and load the rule file above

engine = RulesEngine() engine.load_rules("\..\rules\rules.json")

alternatively, you can create a manual rule without loading a file

rule = {"when": "what time is it", "then": "time to get a new watch"} engine.add_rule(rule)

define and run assertions

assertion = "hello" engine.run_assert(assertion)

assertions will match the "when" portion of rules, based on a unification algorithm:

  • Strings will match direct string matches, "when": "hello" will match "hello"
  • Strings will match using variables, "when": "my dog is ?name" will match "my dog is fido"
  • Dictionaries will match a dictionary, "when": {"name": "fido"} will match {"name": "fido"}

if the assertion matches, the "then" portion will fire

rules will "forward chain" - the "then" portion of rules will cause the engine to match against rules

[ { "when": "hello", "then": {"user-intent": "greet"} },

{   "when": {"user-intent": "greet"},
    "then": {"#output": "hello, world"}
}

]

you can have more than one command (action) in the "then" portion

{ "when": "hello", "then": [{"#output": "hello there"}, {"#output": "nice to meet you"}] }

you can store "item" knowledge (facts)

{"item": "user", "name": "jeremy", "dog": "fido"}

you can reference the items (facts) and their properties in your rules,

using $itemname.property syntax

{"when": "what is my name", "then": [{"#output": "your name is $user.name"} }

Commands

You can use commands in the "then" portion of your rules. The engine will run the commands if the "when" portion matches.

In this version, only three commands are available

#output

  • Behavior: Will echo the text to the console (using print)
  • Example: {"#output": "hello, world"}
  • Optional: specifiy a "rate" to slow output the contents to the console

#prompt

  • Behavior: Will ask for input and store into an item
  • Example: {"#input": "what is your name", "into": "username"}

#read-rss

  • Behavior: Will read the specified rss feed into an item
  • Example: {"#read-rss": "https://rss-feed.rss", "into": "rss"}

Examples

read an rss feed and output it to the console at a readable rate

[ {"when": "rss digg.top", "then": [{"#read-rss": "$?feed", "into": "rss"}, {"#output": "$rss.title", "rate": 0.0225}] },

{"item": "digg", "top": "https://digg.com/rss/top.rss"}

]

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

thoughts-0.0.1.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

thoughts-0.0.1-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file thoughts-0.0.1.tar.gz.

File metadata

  • Download URL: thoughts-0.0.1.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.7.8

File hashes

Hashes for thoughts-0.0.1.tar.gz
Algorithm Hash digest
SHA256 d198f48a76227ca9c3cd093f5e77f4caa5d80308212643cd074b16c68f08056a
MD5 98a6049f8a5442a7fc61a70e88f6da27
BLAKE2b-256 dfd74d2609d65dddace3197c353723d86b52139fc3091b4870e71d70f00664de

See more details on using hashes here.

File details

Details for the file thoughts-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: thoughts-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.7.8

File hashes

Hashes for thoughts-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 310c1caf2ed8efb98ce8650d1726028539fbd98d72c42dfccc70a985021bd6f6
MD5 891382f8740c61765f613e30d3e53a80
BLAKE2b-256 d02992fe96c797243e0a477791e4cbea8af51776f4b0e1ac28ce609e4699ce2a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page