-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
issue #37 : add Facts abstraction and update APIs accordingly
- Loading branch information
1 parent
e8effd0
commit e2eb3dc
Showing
5 changed files
with
151 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* The MIT License | ||
* | ||
* Copyright (c) 2017, Mahmoud Ben Hassine ([email protected]) | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package org.easyrules.api; | ||
|
||
import java.util.*; | ||
|
||
import static java.lang.String.format; | ||
|
||
public class Facts implements Iterable<Map.Entry<String, Object>> { | ||
|
||
private Map<String, Object> facts = new HashMap<>(); | ||
|
||
public void add(String name, Object fact) { | ||
facts.put(name, fact); | ||
} | ||
|
||
public void remove(String name) { | ||
facts.remove(name); | ||
} | ||
|
||
public Object get(String name) { | ||
return facts.get(name); | ||
} | ||
|
||
@Override | ||
public Iterator<Map.Entry<String, Object>> iterator() { | ||
return facts.entrySet().iterator(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder stringBuilder = new StringBuilder("Facts {").append("\n"); | ||
for (Map.Entry<String, Object> fact : facts.entrySet()) { | ||
stringBuilder.append(format(" Fact { %s : %s }", fact.getKey(), fact.getValue().toString())); | ||
stringBuilder.append("\n"); | ||
} | ||
stringBuilder.append("}"); | ||
return stringBuilder.toString(); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -30,7 +30,7 @@ | |
* | ||
* @author Mahmoud Ben Hassine ([email protected]) | ||
*/ | ||
public interface Rule { | ||
public interface Rule extends Comparable<Rule> { | ||
|
||
/** | ||
* Default rule name. | ||
|
@@ -67,14 +67,14 @@ public interface Rule { | |
|
||
/** | ||
* Rule conditions abstraction : this method encapsulates the rule's conditions. | ||
* @return true if the rule should be applied, false else | ||
* @return true if the rule should be applied given the provided facts, false else | ||
*/ | ||
boolean evaluate(); | ||
boolean evaluate(Facts facts); | ||
|
||
/** | ||
* Rule actions abstraction : this method encapsulates the rule's actions. | ||
* @throws Exception thrown if an exception occurs during actions performing | ||
*/ | ||
void execute() throws Exception; | ||
void execute(Facts facts) throws Exception; | ||
|
||
} |
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,75 @@ | ||
/** | ||
* The MIT License | ||
* | ||
* Copyright (c) 2017, Mahmoud Ben Hassine ([email protected]) | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package org.easyrules.api; | ||
|
||
import org.easyrules.core.RuleProxy; | ||
|
||
import java.util.Collections; | ||
import java.util.Iterator; | ||
import java.util.Set; | ||
import java.util.TreeSet; | ||
|
||
public class Rules implements Iterable<Rule> { | ||
|
||
private Set<Rule> rules = new TreeSet<>(); | ||
|
||
public Rules(Set<Rule> rules) { | ||
this.rules = rules; | ||
} | ||
|
||
public Rules(Rule... rules ) { | ||
Collections.addAll(this.rules, rules); | ||
} | ||
|
||
public Rules(Object... rules ) { | ||
for (Object rule : rules) { | ||
this.register(RuleProxy.asRule(rule)); | ||
} | ||
} | ||
|
||
public void register(Object rule) { | ||
rules.add(RuleProxy.asRule(rule)); | ||
} | ||
|
||
public void unregister(Object rule) { | ||
rules.remove(RuleProxy.asRule(rule)); | ||
} | ||
|
||
public boolean isEmpty() { | ||
return rules.isEmpty(); | ||
} | ||
|
||
public void clear() { | ||
rules.clear(); | ||
} | ||
|
||
@Override | ||
public Iterator<Rule> iterator() { | ||
return rules.iterator(); | ||
} | ||
|
||
public void sort() { | ||
rules = new TreeSet<>(rules); | ||
} | ||
} |
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