Skip to content

Commit

Permalink
Merge pull request #2 from codetiger/main
Browse files Browse the repository at this point in the history
add real-world examples for JSONLogic rules
  • Loading branch information
codetiger authored Nov 20, 2024
2 parents 74f5a15 + 367f4b4 commit 53f865d
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 22 deletions.
46 changes: 24 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,32 @@ use serde_json::json;

fn main() {
let logic = JsonLogic::new();

// Rule: Check if user is 21 or older
let rule = json!({
">=" : [
{"var": "age"},
21

// Complex discount rule:
// - 20% off if cart total > $100 AND user is premium member
// - OR 10% off if cart has more than 5 items
let discount_rule = json!({
"or": [
{"and": [
{">": [{"var": "cart.total"}, 100]},
{"==": [{"var": "user.membership"}, "premium"]}
]},
{">": [{"var": "cart.item_count"}, 5]}
]
});

// Data to evaluate
let data = json!({
"age": 25

let customer_data = json!({
"cart": {
"total": 120.00,
"item_count": 3
},
"user": {
"membership": "premium"
}
});

let result = logic.apply(&rule, &data).unwrap();
assert_eq!(result, json!(true));
let applies_for_discount = logic.apply(&discount_rule, &customer_data).unwrap();
assert_eq!(applies_for_discount, json!(true));
}
```

Expand All @@ -49,17 +59,9 @@ This implementation supports all standard JSONLogic operations including:
- Basic operators (`==`, `===`, `!=`, `!==`, `>`, `>=`, `<`, `<=`)
- Logic operators (`!`, `!!`, `or`, `and`, `if`)
- Numeric operations (`+`, `-`, `*`, `/`, `%`)
- Array operations (

map

, `reduce`, `filter`, `all`, `none`, `some`, `merge`)
- Array operations (`merge`)
- String operations (`cat`, `substr`)
- Data access (

var

)
- Data access (`var`)

For detailed documentation of operations and examples, visit [jsonlogic.com](http://jsonlogic.com).

Expand Down
32 changes: 32 additions & 0 deletions examples/cart-discount.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use datalogic_rs::JsonLogic;
use serde_json::json;

fn main() {
let logic = JsonLogic::new();

// Complex discount rule:
// - 20% off if cart total > $100 AND user is premium member
// - OR 10% off if cart has more than 5 items
let discount_rule = json!({
"or": [
{"and": [
{">": [{"var": "cart.total"}, 100]},
{"==": [{"var": "user.membership"}, "premium"]}
]},
{">": [{"var": "cart.item_count"}, 5]}
]
});

let customer_data = json!({
"cart": {
"total": 120.00,
"item_count": 3
},
"user": {
"membership": "premium"
}
});

let applies_for_discount = logic.apply(&discount_rule, &customer_data).unwrap();
assert_eq!(applies_for_discount, json!(true));
}
122 changes: 122 additions & 0 deletions examples/complex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
use datalogic_rs::JsonLogic;
use serde_json::json;

fn main() {
let logic = JsonLogic::new();

// Example 1: Dynamic Pricing Rules
let pricing_rule = json!({
"if": [
// If holiday season (month 11-12) and premium member
{"and": [
{"in": [{"var": "date.month"}, [11, 12]]},
{"==": [{"var": "user.membership"}, "premium"]}
]},
// Then apply 25% discount
{"*": [{"var": "cart.total"}, 0.75]},
// Else if cart total > 200
{">": [{"var": "cart.total"}, 200]},
// Then apply 10% discount
{"*": [{"var": "cart.total"}, 0.90]},
// Else no discount
{"var": "cart.total"}
]
});

// Example 2: Loan Approval Rules
let loan_eligibility = json!({
"and": [
// Age between 21 and 65
{">=": [{"var": "applicant.age"}, 21]},
{"<=": [{"var": "applicant.age"}, 65]},
// Credit score above 700
{">": [{"var": "applicant.credit_score"}, 700]},
// Debt-to-income ratio below 40%
{"<": [
{"/": [
{"var": "applicant.monthly_debt"},
{"var": "applicant.monthly_income"}
]},
0.4
]},
// Employment history > 2 years
{">": [{"var": "applicant.years_employed"}, 2]}
]
});

// Example 3: Fraud Detection Rules
let fraud_check = json!({
"or": [
// Multiple high-value transactions in short time
{"and": [
{">": [{"var": "transaction.amount"}, 1000]},
{"<": [
{"-": [
{"var": "transaction.timestamp"},
{"var": "last_transaction.timestamp"}
]},
300 // 5 minutes in seconds
]}
]},
// Transaction from unusual location
{"!": {"in": [
{"var": "transaction.country"},
{"var": "user.usual_countries"}
]}},
// Unusual shopping pattern
{"and": [
{">": [{"var": "daily_transaction_count"}, 10]},
{">": [{"var": "transaction.amount"}, {"*": [{"var": "user.average_transaction"}, 3]}]}
]}
]
});

// Example data for testing
let transaction_data = json!({
"transaction": {
"amount": 1200.00,
"timestamp": 1677649200,
"country": "FR"
},
"last_transaction": {
"timestamp": 1677649100
},
"user": {
"usual_countries": ["US", "CA", "GB"],
"average_transaction": 200
},
"daily_transaction_count": 12
});

let loan_data = json!({
"applicant": {
"age": 35,
"credit_score": 720,
"monthly_debt": 2000,
"monthly_income": 6000,
"years_employed": 5
}
});

let pricing_data = json!({
"date": {
"month": 12
},
"user": {
"membership": "premium"
},
"cart": {
"total": 300.00
}
});

// Test the rules
let final_price = logic.apply(&pricing_rule, &pricing_data).unwrap();
println!("Final price after discounts: ${}", final_price);

let is_eligible = logic.apply(&loan_eligibility, &loan_data).unwrap();
println!("Loan application approved: {}", is_eligible);

let is_fraudulent = logic.apply(&fraud_check, &transaction_data).unwrap();
println!("Transaction flagged as fraudulent: {}", is_fraudulent);
}

0 comments on commit 53f865d

Please sign in to comment.