-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
831 additions
and
223 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
api/kie-api/src/main/java/org/kie/submarine/rules/DataSource.java
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,26 @@ | ||
package org.kie.submarine.rules; | ||
|
||
import org.kie.api.runtime.rule.FactHandle; | ||
|
||
public interface DataSource<T> { | ||
|
||
FactHandle add(T object ); | ||
|
||
/** | ||
* Updates the fact for which the given FactHandle was assigned with the new | ||
* fact set as the second parameter in this method. | ||
* It is also possible to optionally specify the set of properties that have been modified. | ||
* | ||
* @param handle the FactHandle for the fact to be updated. | ||
* @param object the new value for the fact being updated. | ||
*/ | ||
void update(FactHandle handle, T object); | ||
|
||
/** | ||
* Deletes the fact for which the given FactHandle was assigned | ||
* | ||
* @param handle the handle whose fact is to be retracted. | ||
*/ | ||
void remove(FactHandle handle); | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
api/kie-api/src/main/java/org/kie/submarine/rules/RuleUnit.java
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,5 @@ | ||
package org.kie.submarine.rules; | ||
|
||
public interface RuleUnit<T> { | ||
RuleUnitInstance<T> createInstance(T workingMemory); | ||
} |
6 changes: 6 additions & 0 deletions
6
api/kie-api/src/main/java/org/kie/submarine/rules/RuleUnitInstance.java
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,6 @@ | ||
package org.kie.submarine.rules; | ||
|
||
public interface RuleUnitInstance<T> { | ||
RuleUnit<T> unit(); | ||
void fire(); | ||
} |
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,5 @@ | ||
package org.kie.submarine.rules; | ||
|
||
public @interface Unit { | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
api/kie-api/src/main/java/org/kie/submarine/rules/impl/AbstractRuleUnit.java
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,7 @@ | ||
package org.kie.submarine.rules.impl; | ||
|
||
import org.kie.submarine.rules.RuleUnit; | ||
|
||
public abstract class AbstractRuleUnit<T> implements RuleUnit<T> { | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
api/kie-api/src/main/java/org/kie/submarine/rules/impl/AbstractRuleUnitInstance.java
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,50 @@ | ||
package org.kie.submarine.rules.impl; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
import org.kie.api.runtime.KieSession; | ||
import org.kie.submarine.rules.RuleUnit; | ||
import org.kie.submarine.rules.RuleUnitInstance; | ||
|
||
public class AbstractRuleUnitInstance<T> implements RuleUnitInstance<T> { | ||
|
||
private final T workingMemory; | ||
private final RuleUnit<T> unit; | ||
private final KieSession rt; | ||
|
||
public AbstractRuleUnitInstance(RuleUnit<T> unit, T workingMemory, KieSession rt) { | ||
this.unit = unit; | ||
this.rt = rt; | ||
this.workingMemory = workingMemory; | ||
} | ||
|
||
public void fire() { | ||
magicReflectionThingie(rt, workingMemory); | ||
rt.fireAllRules(); | ||
} | ||
|
||
@Override | ||
public RuleUnit<T> unit() { | ||
return unit; | ||
} | ||
|
||
public T workingMemory() { | ||
return workingMemory; | ||
} | ||
|
||
private void magicReflectionThingie(KieSession rt, T workingMemory) { | ||
try { | ||
for (Field f : workingMemory.getClass().getDeclaredFields()) { | ||
f.setAccessible(true); | ||
Object v = null; | ||
v = f.get(workingMemory); | ||
if (v instanceof ListDataSource) { | ||
ListDataSource o = (ListDataSource) v; | ||
o.drainInto(rt::insert); | ||
} | ||
} | ||
} catch (IllegalAccessException e) { | ||
throw new Error(e); | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
api/kie-api/src/main/java/org/kie/submarine/rules/impl/ListDataSource.java
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,41 @@ | ||
package org.kie.submarine.rules.impl; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Iterator; | ||
import java.util.function.Consumer; | ||
|
||
import org.kie.api.runtime.rule.FactHandle; | ||
import org.kie.submarine.rules.DataSource; | ||
|
||
public class ListDataSource<T> implements DataSource<T> { | ||
ArrayList<T> values = new ArrayList<>(); | ||
|
||
public FactHandle add(T t) { | ||
values.add(t); | ||
return null; | ||
} | ||
|
||
@Override | ||
public void update(FactHandle handle, T object) { | ||
|
||
} | ||
|
||
@Override | ||
public void remove(FactHandle handle) { | ||
|
||
} | ||
|
||
public void addAll(Collection<? extends T> ts) { | ||
values.addAll(ts); | ||
} | ||
|
||
public void drainInto(Consumer<Object> sink) { | ||
Iterator<T> iter = values.iterator(); | ||
while(iter.hasNext()) { | ||
T t = iter.next(); | ||
sink.accept(t); | ||
iter.remove(); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.