/** we create a rule which listens for the incoming calls, rejects it, and sends back an sms */ public void testCallEventRule() { if (mRuleManager.loadAllRules().size() != 0) { return; } /* * Create the conditions */ // ToDo: to be handled an event without conditions and without building the condition tree RuleCondition cTrue = mConditionManager.getDefaultCondition(); mConditionManager.saveCondition(cTrue); // create dependencies between conditions // Build the condition tree RuleConditionTree.Builder builder = new ConditionTree.Builder(); builder.add(cTrue, new RuleCondition[] {cTrue, cTrue}, ConditionTree.Operator.OR); RuleConditionTree root = mConditionManager.buildConditionTree(builder); /* * Create actions */ RuleAction aRejectCall = generateNewAction(Action.Type.REJECT_CALL_ACTION.getType()); RuleAction aSms = generateNewAction(Action.Type.SEND_SMS_ACTION.getType()); ((SendSmsActionPlugin) aSms.getActionPlugin()).setMessage("I'll call you back later."); mActionManager.saveActions(aRejectCall, aSms); // Create a rule with these actions and conditions RuleRecord ruleRecord = new RuleRecord(); // set the event ruleRecord.setRuleName("Reject call Rule"); ruleRecord.setEventCode(Event.Type.RULE_EVENT_CALL.getType()); ruleRecord.setRuleConditionTree(root); ruleRecord.addRuleActions(aRejectCall, aSms); mRuleManager.saveRuleRecord(ruleRecord); }
/** * We create a rule which responds to Number events and with some conditions it calculates * fibonacci and multiplications. */ public void testSimpleArithmetricRule() { if (mRuleManager.loadAllRules().size() != 0) { return; } // Create the event NumberEvent event = new NumberEvent(); /* * Create the conditions */ RuleCondition cTrue = mConditionManager.getDefaultCondition(); RuleCondition cTrue1 = mConditionManager.getDefaultCondition(); RuleCondition cInterval = generateNewCondition(Condition.Type.ARITHMETRIC_IS_NUMBER_IN_INTERVAL.getType()); ((IsNumberInIntervalConditionPlugin) cInterval.getConditionPlugin()).setMin(5.0); ((IsNumberInIntervalConditionPlugin) cInterval.getConditionPlugin()).setMax(500.0); RuleCondition cPrime = generateNewCondition(Condition.Type.ARITHMETRIC_IS_NUMBER_PRIME.getType()); RuleCondition cEqual = generateNewCondition(Condition.Type.ARITHMETRIC_IS_NUMBER_EQUAL.getType()); ((IsNumberEqualConditionPlugin) cEqual.getConditionPlugin()).setValue(1); List<RuleCondition> ruleConditions = new ArrayList<>(); Collections.addAll(ruleConditions, cTrue, cTrue1, cInterval, cPrime, cEqual); mConditionManager.saveConditions(ruleConditions); // create dependencies between conditions // Build the condition tree RuleConditionTree.Builder builder = new ConditionTree.Builder(); builder.add(cTrue, new RuleCondition[] {cEqual, cTrue1}, ConditionTree.Operator.OR); builder.add(cTrue1, new RuleCondition[] {cInterval, cPrime}, ConditionTree.Operator.AND); RuleConditionTree root = mConditionManager.buildConditionTree(builder); /* * Create actions */ RuleAction aMultiply = generateNewAction(Action.Type.ARITHMETRIC_ACTION_MULTIPLY.getType()); ((MultiplyActionPlugin) aMultiply.getActionPlugin()).setValue(5); RuleAction aFib = generateNewAction(Action.Type.ARITHMETRIC_ACTION_FIBONACCI.getType()); RuleAction aMultiply10 = generateNewAction(Action.Type.ARITHMETRIC_ACTION_MULTIPLY.getType()); ((MultiplyActionPlugin) aMultiply10.getActionPlugin()).setValue(10); mActionManager.saveActions(aMultiply, aFib, aMultiply10); // Create a rule with these actions and conditions RuleRecord ruleRecord = new RuleRecord(); // set the event ruleRecord.setRuleName("Sample Rule"); ruleRecord.setEventCode(event.getType()); ruleRecord.setState(Rule.State.STATE_ACTIVE); ruleRecord.setRuleConditionTree(root); ruleRecord.addRuleActions(aMultiply, aFib, aMultiply10); mRuleManager.saveRuleRecord(ruleRecord); }