-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from codetiger/main
add real-world examples for JSONLogic rules
- Loading branch information
Showing
3 changed files
with
178 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |