{"id":23274641,"url":"https://github.com/adversing/decisions4j","last_synced_at":"2025-04-06T10:46:28.063Z","repository":{"id":170131075,"uuid":"602311192","full_name":"Adversing/Decisions4J","owner":"Adversing","description":"Decisions4J is a Java library for creating decision trees. It provides a simple API for making decisions based on custom rules and conditions. Ideal for lightweight, simple decision-making applications.","archived":false,"fork":false,"pushed_at":"2024-02-02T23:26:02.000Z","size":10,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-12T16:32:22.475Z","etag":null,"topics":["decision-making","decision-tree","java-8"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Adversing.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2023-02-15T23:57:16.000Z","updated_at":"2024-07-26T01:39:55.000Z","dependencies_parsed_at":"2024-02-03T00:28:29.213Z","dependency_job_id":"e5b16139-a44c-4d83-9967-4f110ce43b42","html_url":"https://github.com/Adversing/Decisions4J","commit_stats":null,"previous_names":["adversing/decisions4j"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Adversing%2FDecisions4J","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Adversing%2FDecisions4J/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Adversing%2FDecisions4J/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Adversing%2FDecisions4J/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Adversing","download_url":"https://codeload.github.com/Adversing/Decisions4J/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247471396,"owners_count":20944154,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["decision-making","decision-tree","java-8"],"created_at":"2024-12-19T20:14:56.095Z","updated_at":"2025-04-06T10:46:28.026Z","avatar_url":"https://github.com/Adversing.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Decisions4J\n`Decisions4J` is a Java API that allows you to define a set of rules to apply to an object and execute the appropriate action based on the first matching rule. It's useful when you need to make a decision based on a complex set of conditions. \n\n---\n\n## Usage\nTo use `Decisions4J`, create a new instance of it by passing an object of the type that you want to evaluate as its argument:\n```java\nDecisionsTree\u003cString\u003e tree = new DecisionsTree\u003c\u003e(\"test\"); // in this case we're evaluating a String\n```\n\n### Adding rules\nOnce you have a `Decisions4J` instance, you can add rules to it. A rule consists of a condition and an action. The condition is a `Predicate` that evaluates to true if the rule should be applied, and the action is a `Consumer` that specifies what should be done if the rule is applied.\n\nTo add a rule to the `DecisionsTree`, call the `caseCondition()` method with the condition and the action as arguments. Here's an example:\n```java\ntree.caseCondition(s -\u003e s.equals(\"test\"), s -\u003e System.out.println(\"test\"));\n```\nIn this example, we're adding a rule that says \"if the string is 'test', then print 'test'\".\n\n### Adding a default rule\nIf none of the rules match, you can specify a default action to be executed by calling the `defaultCase()` method:\n```java\ntree.defaultCase(s -\u003e System.out.println(\"default\"));\n```\nIn this example, we're specifying that if none of the other rules match, the action should be to print \"default\".\n\n### Applying the rules\nOnce you've added all the rules, you can apply them to the object by calling the `decide()` method:\n```java\ntree.decide();\n```\nThis will evaluate the object and execute the first matching rule.\n\n---\n\n## Example\nHere's an example of using `Decisions4J` to evaluate a `Person` object:\n```java\n// Create a new DecisionsTree with a Person object\nPerson person = new Person(\"Alice\", 30);\nDecisionsTree\u003cPerson\u003e tree = new DecisionsTree\u003c\u003e(person);\n\n// Add a rule\ntree.caseCondition(p -\u003e p.getAge() \u003e 18, p -\u003e System.out.println(p.getName() + \" is an adult\"));\n\n// Add a default rule\ntree.defaultCase(p -\u003e System.out.println(person.getName() + \" is a minor\"));\n\n// Apply the rules\ntree.decide();\n```\nIn this example, we're creating a new `DecisionsTree` instance with a `Person` object. We're adding a rule that says \"if the person's age is greater than 18, then print 'Alice is an adult'\" (where \"Alice\" is the person's name). We're also adding a default rule that says \"print 'Alice is a minor' if none of the other rules match\". Finally, we're applying the rules to the person object by calling `decide()`.\n\nYou may also use this syntax:\n```java\n// Create a Person object\nPerson person = new Person(\"Alice\", 30);\n\n// Create a DecisionsTree object\nDecisionsTree\u003cPerson\u003e tree = new DecisionsTree\u003c\u003e(person)\n        .caseCondition(p -\u003e p.getAge() \u003e 18, p -\u003e System.out.println(p.getName() + \" is an adult\"))\n        .defaultCase(p -\u003e System.out.println(person.getName() + \" is a minor\"));\n\n// Apply the rules\ntree.decide();\n```\n\n---\n\n## Error handling\n`Decisions4J` provides some basic error handling. If you don't add any rules to the `DecisionsTree`, calling `decide()` will result in an `IllegalStateException`. Similarly, if you don't call `decide()` after adding rules, you'll get an `IllegalStateException` as well. If you call `defaultCase()` with a null action, you'll get a `NullPointerException`.\n\n---\n###### check out the javascript version [here](https://github.com/Adversing/decisions.js/) and the python version [here](https://github.com/Adversing/decisions.py/) :)   \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadversing%2Fdecisions4j","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fadversing%2Fdecisions4j","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadversing%2Fdecisions4j/lists"}