Created
September 7, 2018 06:14
-
-
Save vamdt/7c8cd764c38acd32c6729b884e929090 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public class AutomateProcessor { | |
private UserExceptionAnalysisListener userExceptionAnalysisListener = new UserExceptionAnalysisListener(); | |
private UserModelingListener userModelingListener = new UserModelingListener(); | |
private HardRuleFilterListener hardRuleFilterListener = new HardRuleFilterListener(); | |
private AntiRubbishListener antiRubbishListener = new AntiRubbishListener(); | |
private CommentDeleteListener commentDeleteListener = new CommentDeleteListener(); | |
private static class Action { | |
private CommentEventListener listener; | |
private Action nextAction; | |
private Map<Predicate, Action> children; | |
public Action(CommentEventListener listener) { | |
this.listener = listener; | |
this.children = new HashMap<>(); | |
} | |
public Action addNextAction(CommentEventListener nextListener) { | |
nextAction = new Action(nextListener); | |
return nextAction; | |
} | |
public Action getNextAction() { | |
return nextAction; | |
} | |
public void doAction(CommentContext context) { | |
// listener.onAction(context); | |
System.out.println("do current action " + listener.getClass().getSimpleName()); | |
} | |
public Map<Predicate, Action> getChildren() { | |
return children; | |
} | |
public Action findAction(CommentEventListener listener) { | |
if (this.listener == listener) { | |
return this; | |
} | |
if (this.nextAction.findAction(listener) != null) { | |
return this.nextAction.findAction(listener); | |
} | |
for (Action value : this.children.values()) { | |
if (value.findAction(listener) != null) { | |
return value.findAction(listener); | |
} | |
} | |
return null; | |
} | |
public <T> Action addConditionAction(Predicate<CommentContext> predicate, CommentEventListener listener) { | |
Action action = new Action(listener); | |
children.putIfAbsent(predicate, action); | |
return action; | |
} | |
} | |
private Action startAction; | |
public void process() { | |
startAction = new Action(userModelingListener); | |
startAction | |
.addNextAction(userExceptionAnalysisListener) | |
.addNextAction(hardRuleFilterListener) | |
.addNextAction(antiRubbishListener); | |
startAction.findAction(hardRuleFilterListener) | |
.addConditionAction(hardRuleFilterListener::hit, commentDeleteListener); | |
startAction.findAction(antiRubbishListener) | |
.addConditionAction(antiRubbishListener::isHit, commentDeleteListener); | |
} | |
public void start(Object object) { | |
if (startAction == null) { | |
return; | |
} | |
Action action = startAction; | |
CommentContext context = new CommentContext(); | |
context.setAttr("input", object); | |
while (action != null) { | |
action.doAction(context); | |
action = findMatchedConditionAction(action, context); | |
} | |
} | |
private Action findMatchedConditionAction(Action action, CommentContext context) { | |
if (action.getChildren().size() <=0 ) { | |
return action.getNextAction(); | |
} | |
Map<Predicate, Action> children = action.getChildren(); | |
Set<Predicate> predicates = children.keySet(); | |
for (Predicate predicate: predicates) { | |
//condition invoke | |
if (predicate.test(context)) { | |
return children.get(predicate); | |
} | |
} | |
return action.getNextAction(); | |
} | |
public static void main(String[] args) { | |
AutomateProcessor automateProcessor = new AutomateProcessor(); | |
automateProcessor.process(); | |
automateProcessor.start(new Object()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment