woylie

woylie

I just released Spek, a boolean expression engine based on the one written for LetMe 2.0.0.

Features

  • Expression structs and builder functions for boolean logic: AllOf, AnyOf, Not, Literal, Check.
  • Evaluation of boolean expressions with optional early stopping and optional evaluation tree output.
  • Optimization of boolean expressions using boolean algebra transformation.
  • Macros for concisely defining reusable check functions.

The readme is quite comprehensive, so let me just post some code examples here.

Defining a check module

defmodule DeviceChecks do
	import Spek.Macros

	defcheck device_online(device, reason: :device_offline) do
		device.online?
	end

	defcheck battery_above_20(device, reason: :battery_too_low) do
		device.battery_level > 20
	end

	defcheck charging(device) do
		device.charging?
	end

	defcheck low_power_mode_enabled(device) do
		device.low_power_mode?
	end
end

(The use of the defcheck macro is optional.)

Composing, optimizing, and evaluating expressions

iex> import Spek
iex> battery_safe =
...>   all_of([
...>     DeviceChecks.device_online_check(),
...>     DeviceChecks.battery_above_20_check()
...>   ])
iex> charging_safe =
...>   all_of([
...>     DeviceChecks.device_online_check(),
...>     DeviceChecks.charging_check()
...>   ])
iex> rule =
...>   any_of([
...>     battery_safe,
...>     charging_safe,
...>     DeviceChecks.low_power_mode_enabled_check()
...>   ])
%Spek.AnyOf{
	children: [
		%Spek.AllOf{
			children: [
				%Spek.Check{
					module: DeviceChecks,
					fun: :device_online,
					args: [:ctx]
				},
				%Spek.Check{
					module: DeviceChecks,
					fun: :battery_above_20,
					args: [:ctx]
				}
			]
		},
		%Spek.AllOf{
			children: [
				%Spek.Check{
					module: DeviceChecks,
					fun: :device_online,
					args: [:ctx]
				},
				%Spek.Check{
					module: DeviceChecks,
					fun: :charging,
					args: [:ctx]
				}
			]
		},
		%Spek.Check{
			module: DeviceChecks,
			fun: :low_power_mode_enabled,
			args: [:ctx]
		}
	]
}
iex> rule = optimize(rule)
%Spek.AnyOf{
	children: [
		%Spek.AllOf{
			children: [
				%Spek.Check{
					module: DeviceChecks,
					fun: :device_online,
					args: [:ctx]
				},
				%Spek.AnyOf{
					children: [
						%Spek.Check{
							module: DeviceChecks,
							fun: :battery_above_20,
							args: [:ctx]
						},
						%Spek.Check{
							module: DeviceChecks,
							fun: :charging,
							args: [:ctx]
						}
					]
				}
			]
		},
		%Spek.Check{
			module: DeviceChecks,
			fun: :low_power_mode_enabled,
			args: [:ctx]
		}
	]
}
iex> device = %{
...>   online?: true,
...>   battery_level: 12,
...>   charging?: false,
...>   low_power_mode?: false
...> }
iex> Spek.eval?(rule, device)
false
iex> device = %{
...>   online?: false,
...>   battery_level: 25,
...>   charging?: false,
...>   low_power_mode?: false
...> }
iex> Spek.eval_tree(rule, device)
{
	:error,
	%Spek.EvaluationError{
		expression: %Spek.AnyOf{
			children: [
				%Spek.AllOf{
					satisfied?: false,
					children: [
						%Spek.Check{
							module: DeviceChecks,
							fun: :device_online,
							args: [:ctx],
							result: {:error, :device_offline},
							satisfied?: false
						}
					]
				},
				%Spek.Check{
					module: DeviceChecks,
					fun: :low_power_mode_enabled,
					args: [:ctx],
					result: {:error, :failed},
					satisfied?: false
				}
			],
			satisfied?: false
		},
		message: "rule evaluation failed"
	}
}

Feedback

Your feedback is appreciated, especially when it comes to API design or feature ideas.

What may come in a future release:

  • Serialization/deserialization of expressions
  • Extract errors of an evaluated expression into a list of error reasons

https://github.com/woylie/spek

Showing Posts 1 to 3

Asd

Asd

Hi, congratulations on the release :tada:. I have a question: when and what to use this library for?

woylie

woylie OP

Use cases can be:

  • Complex domain rules with composable conditions
  • Specification pattern implementations
  • Workflow, pipeline, and feature gating conditions
  • User-configurable decision systems
  • Auditable decision logs with per-check results and success/failure reasons
woylie

woylie OP

Version 0.2.0 adds various functions to collect evaluation results into flat lists.

— All posts loaded —

Where Next? Top

Trending in Announcing Top

restlessronin
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub. Docs are at OpenaiEx User Gu...
152 12366 136
New
type1fool
WebAuthnLiveComponent WebAuthnComponents See this post about renaming the package. Passwordless authentication for Phoenix LiveView app...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
kip
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
New
quatermain
Hello, I’m sharing my plugin here in forum after some time so it has time to mature and proof yourself. I use Claude Code daily on a pr...
New

Other Trending Topics Top

KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
Null-logic-0
What IDE or editor are you using for Elixir development? Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews