Skip to main content

Crate psmatcher

Crate psmatcher 

Source
Expand description

§PSMatcher

A pub/sub matcher algorithm implementation that efficiently matches events against subscriptions. The algorithm pre-processes subscriptions into a matching tree for optimized event matching.

§Overview

This crate provides a pub/sub matching system where:

  • Subscriptions are conjunctions of elementary predicates
  • Events must have at least one string attribute called “topic”
  • The matching tree is built during pre-processing for efficient matching

§Example

use psmatcher::{Matcher, Subscription, ElementaryPredicate, ElementaryTest};
use psmatcher::event_types::{DefaultEvent, AttributeValue};

// Create a matcher
let mut matcher = Matcher::new();

// Create a subscription for weather updates in San Francisco
let subscription = Subscription::new("weather-sf")
    .with_predicate(ElementaryPredicate::new(
        ElementaryTest::Equals {
            attribute: "topic".to_string(),
            value: AttributeValue::String("weather".to_string()),
        },
        false,
    ))
    .with_predicate(ElementaryPredicate::new(
        ElementaryTest::Equals {
            attribute: "city".to_string(),
            value: AttributeValue::String("San Francisco".to_string()),
        },
        false,
    ));

// Add the subscription to the matcher
matcher.add_subscription(subscription);

// Create a subscription using regex pattern matching
let regex_subscription = Subscription::new("california-cities")
    .with_predicate(ElementaryPredicate::new(
        ElementaryTest::regex("topic".to_string(), "weather|forecast").unwrap(),
        false,
    ))
    .with_predicate(ElementaryPredicate::new(
        ElementaryTest::regex("city".to_string(), "San (Francisco|Jose|Diego)").unwrap(),
        false,
    ));

// Add the regex subscription to the matcher
matcher.add_subscription(regex_subscription);

// Create an event
let event = DefaultEvent::new("weather")
    .with_attribute("city", AttributeValue::String("San Francisco".to_string()))
    .with_attribute("temperature", AttributeValue::Float(72.5));

// Match the event against subscriptions
let matches = matcher.match_event(&event).unwrap();

// Check if our subscriptions matched
assert!(matches.contains("weather-sf"));
assert!(matches.contains("california-cities"));

For more examples and detailed documentation, see the README module.

Modules§

collections
Deterministic collection aliases for simulation testing.
event_types
helpers
readme
README Documentation
traits

Macros§

attr_map
list_from_attr_values
list_from_bools
list_from_floats
list_from_ints
list_from_strs
map_from_attr_values

Structs§

ElementaryPredicate
Represents an elementary predicate, which is the result of an elementary test
Matcher
The main matcher API for pub/sub matching
MatchingTree
The matching tree for efficient event matching
Subscription
A subscription is a conjunction of elementary predicates

Enums§

ElementaryTest
Represents an elementary test that can be performed on an event attribute
MatcherError
Errors that can occur during pub/sub matching operations

Constants§

TOPIC_STR