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

type1fool
WebAuthnLiveComponent WebAuthnComponents See this post about renaming the package. Passwordless authentication for Phoenix LiveView app...
New
GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
woylie
I released Doggo, a collection of unstyled Phoenix components. https://github.com/woylie/doggo Features Unstyled Phoenix components....
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
ahamez
Hi everyone, I’ve been working on this protobuf library for 3 years. We use it in the company I work for, EasyMile, to communicate with ...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
kip
I’ll shortly be launching Text, a nascent text analysis library. Current functionality In this early version (not ready for prime time) ...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
bartblast
Hey folks, I just published a post about Hologram’s funding and where the project goes next - the short version: Curiosum as Main Spons...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews