Code Monkey home page Code Monkey logo

tau-engine's Introduction

Tau Engine

crates.io Documentation

This crate provides a library that tags documents by running and matching rules over them.

Overview

The engine makes use of a Pratt parser and a tree solver in order to evaluate the detection logic of a rule against a document, if the outcome is true the document is considered tagged by that rule.

Rules

A rule is used to tag a document and is made up of three parts:

  • detection: the logic used to evaluate a document.
  • true positives: example documents that must evaluate to true for the given detection.
  • true negatives: example documents that must evaluate to false for the given detection.

The detection block is made up of a condition, and identifiers. This allows for simple but expressive rules, below is a brief summary:

Identifiers

Identifiers are used to help keep the condition concise and generally contain the core of the matching logic. They consist of Key/Value pairs which allow for the extraction of data from the document and the evaluate of its value. It should be noted that mappings are treated as conjunctions, while sequences are treated as disjunctions.

Identifiers make use of the following matching logic:

  • foobar: an exact match of foobar
  • foobar*: starts with foobar
  • *foobar: ends with foobar
  • *foobar*: contains foobar
  • ?foobar: regex foobar

Any of the above can be made case insensitive with the i prefix, for example:

  • ifoobar
  • ifoobar*

Escaping can be achieved with a combination of ' and ".

Condition

The condition is just a boolean expression and supports the following:

  • and: logical conjunction
  • or: logical disjunction
  • ==: equality comparison
  • >, >=, <, <=: numeric comparisons
  • not: negate
  • all(i): make sequences behave as conjunctions
  • of(i, x): ensure a sequence has a minimum number of matches

Examples

This is an example of how the engine can tag a document against a provided rule:

tau-engine = "1.0"
use std::borrow::Cow;

use tau_engine::{Document, Rule, Value};

// Define a document.
struct Foo {
    foo: String,
}
impl Document for Foo {
    fn find(&self, key: &str) -> Option<Value<'_>> {
        match key {
            "foo" => Some(Value::String(Cow::Borrowed(&self.foo))),
            _ => None,
        }
    }
}

// Write a rule.
let rule = r#"
detection:
  A:
    foo: foobar
  condition: A
true_positives:
- foo: foobar
true_negatives:
- foo: foo
"#;

// Load and validate a rule.
let rule = Rule::load(rule).unwrap();
assert_eq!(rule.validate().unwrap(), true);

// Create a document.
let foo = Foo {
    foo: "foobar".to_owned(),
};

// Evalute the document with the rule.
assert_eq!(rule.matches(&foo), true);

This is an example of how the engine can be used to tag on JSON.

tau-engine = { version = "1.0", features = ["json"] }
use serde_json::json;
use tau_engine::{Document, Rule};

// Write a rule.
let rule = r#"
detection:
  A:
    foo: foobar
  condition: A
true_positives:
- foo: foobar
true_negatives:
- foo: foo
"#;

// Load and validate a rule.
let rule = Rule::load(rule).unwrap();
assert_eq!(rule.validate().unwrap(), true);

// Create a document.
let foo = json!({
    "foo": "foobar",
});

// Evalute the document with the rule.
assert_eq!(rule.matches(&foo), true);

tau-engine's People

Contributors

alexkornitzer avatar fscc-alexkornitzer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

tau-engine's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.