examples

command
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 14, 2026 License: MIT Imports: 3 Imported by: 0

README ΒΆ

Examples - Go Rules Engine

This folder contains usage examples of the Go rules engine.

πŸ“š Available Examples

1. basic/main.go

Basic Example - Simple age verification with a single rule.

go run examples/basic/main.go

Demonstrates:

  • βœ… Engine creation
  • βœ… Simple rule with condition
  • βœ… greater_than operator
  • βœ… Tests with different values
2. json/main.go

JSON Loading - Load rules and facts from JSON.

go run examples/json/main.go

Demonstrates:

  • βœ… Unmarshaling JSON rules
  • βœ… Unmarshaling JSON facts
  • βœ… Adding rules to engine
  • βœ… Adding facts to almanac
  • βœ… VIP and regular rules
3. custom-operator/main.go

Custom Operators - Creating custom operators.

go run examples/custom-operator/main.go

Demonstrates:

  • βœ… Operator interface
  • βœ… CustomOperator implementation
  • βœ… starts_with, ends_with, between operators
  • βœ… RegisterOperator to register operators
4. advanced/main.go

Advanced Features - Callbacks, handlers and dynamic facts.

go run examples/advanced/main.go

Demonstrates:

  • βœ… Named callbacks with RegisterCallback
  • βœ… Global handler OnSuccess
  • βœ… Specific handler per event type On()
  • βœ… Dynamic facts (discount calculation)
  • βœ… Multiple simultaneous handlers
5. full-demo.go

Complete Demonstration - All features in a single example.

go run examples/full-demo.go

Demonstrates:

  • βœ… Simple and complex rules
  • βœ… Nested conditions (all/any)
  • βœ… Callbacks and handlers
  • βœ… JSON loading
  • βœ… Dynamic facts
  • βœ… JSONPath
  • βœ… Event history

πŸš€ Execution

From the project root:

# Basic example
go run examples/basic/main.go

# JSON
go run examples/json/main.go

# Custom operators
go run examples/custom-operator/main.go

# Advanced
go run examples/advanced/main.go

# Full demo
go run examples/full-demo.go

πŸ“– Complete Documentation

See the main README for complete API documentation.

πŸ’‘ Quick Start

To create your own application:

  1. Import:

    import gorulesengine "github.com/deadelus/go-rules-engine/src"
    
  2. Engine:

    engine := gorulesengine.NewEngine()
    
  3. Rule:

    rule := &gorulesengine.Rule{
        Name:     "my-rule",
        Priority: 100,
        Conditions: gorulesengine.ConditionSet{
            All: []gorulesengine.ConditionNode{
                {
                    Condition: &gorulesengine.Condition{
                        Fact:     "age",
                        Operator: "greater_than",
                        Value:    18,
                    },
                },
            },
        },
        Event: gorulesengine.Event{
            Type: "adult",
        },
    }
    engine.AddRule(rule)
    
  4. Almanac:

    almanac := gorulesengine.NewAlmanac([]*gorulesengine.Fact{})
    almanac.AddFact("age", 25)
    
  5. Run:

    results, err := engine.Run(almanac)
    if err != nil {
        log.Fatal(err)
    }
    
    for _, result := range results {
        if result.Result {
            fmt.Printf("βœ… %s\n", result.Event.Type)
        }
    }
    

πŸ“ Example Structure

examples/
β”œβ”€β”€ README.md           # This file
β”œβ”€β”€ full-demo.go        # Complete demo
β”œβ”€β”€ basic/              # Basic example
β”‚   └── main.go
β”œβ”€β”€ json/               # JSON loading
β”‚   └── main.go
β”œβ”€β”€ custom-operator/    # Custom operators
β”‚   └── main.go
└── advanced/           # Advanced features
    └── main.go

Check each example for specific use cases!

Documentation ΒΆ

The Go Gopher

There is no documentation for this package.

Directories ΒΆ

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL