public void ReturnFirstMatchedRuleByRefAndRepositoryUrl() { var rules = new WebHookOptions { Rules = new List <WebHookRule> { new WebHookRule { Name = "no match", Ref = "/test/test", RepositoryUrl = "repo1" }, new WebHookRule { Name = "match", Ref = "/test/test", RepositoryUrl = "repo2" } } }; var sup = new RuleMatcher(new OptionsWrapper <WebHookOptions>(rules), new NullLogger <RuleMatcher>()); var actual = sup.Match("/test/test", "repo2"); actual.Name.Should().Be("match"); }
//From the failed task, get a node in hierarchy with a valid method in current state public static CompoundTask GetValidNode(PrimitiveTask failedTask, State state) { CompoundTask parent = new CompoundTask(); Task current = failedTask; //Get parent of task, until a parent with a valid method is found, or the root of hierarchy is reached while (current.parent != null) { parent = current.parent; foreach (Method method in parent.methods) { if (RuleMatcher.MatchCondition(method.preconditions, state, method.logicalOperator)) { return(parent); } } current = parent; } return(parent); }
public void ReturnFirstMatchedRuleByMatch_IfRepositoryUrlIsNotSpecified() { var rules = new WebHookOptions { Rules = new List <WebHookRule> { new WebHookRule { Name = "no match", Match = "/test/test1" }, new WebHookRule { Name = "match", Match = "/test/test" }, new WebHookRule { Name = "match2", Match = "/test/test" } } }; var sup = new RuleMatcher(new OptionsWrapper <WebHookOptions>(rules), new NullLogger <RuleMatcher>()); var actual = sup.Match("/test/test", null); actual.Name.Should().Be("match"); }
public override bool OnRuleMatchedElement(RuleMatcher matcher, VisualElement element) { if (element == this.m_Target) { this.selectedElementRules.Add(new MatchedRulesExtractor.MatchedRule(matcher)); } return(false); }
public void GetMatchedRulesAcceptsMailRuleIfAnyMatcherMatchesIt() { // Assemble var startTime = DateTime.Now; var mailRule1 = new MailRule(); var mailRule2 = new MailRule(); var mailRule3 = new MailRule(); var mailRule4 = new MailRule(); var mailRules = new List <MailRule> { mailRule1, mailRule2, mailRule3, mailRule4 }; var matcher1 = MockRepository.GenerateStrictMock <IMailRuleMatcher>(); var matcher2 = MockRepository.GenerateStrictMock <IMailRuleMatcher>(); var matcher3 = MockRepository.GenerateStrictMock <IMailRuleMatcher>(); var matchers = new List <IMailRuleMatcher> { matcher1, matcher2, matcher3 }; // Matcher1 is not proper for any mail rule. matcher1.Expect(e1 => e1.IsProperMatcher(mailRule1)).Return(false); matcher1.Expect(e1 => e1.IsProperMatcher(mailRule2)).Return(false); matcher1.Expect(e1 => e1.IsProperMatcher(mailRule3)).Return(false); matcher1.Expect(e1 => e1.IsProperMatcher(mailRule4)).Return(false); // Matcher2 does not evaluate rule 1, accepts rule 2, refuses rules 3 and 4 matcher2.Expect(e2 => e2.IsProperMatcher(mailRule1)).Return(false); matcher2.Expect(e2 => e2.IsProperMatcher(mailRule2)).Return(true); matcher2.Expect(e2 => e2.IsProperMatcher(mailRule3)).Return(true); matcher2.Expect(e2 => e2.IsProperMatcher(mailRule4)).Return(true); matcher2.Expect(e2 => e2.ShouldBeRun(mailRule2, startTime)).Return(true); matcher2.Expect(e2 => e2.ShouldBeRun(mailRule3, startTime)).Return(false); matcher2.Expect(e2 => e2.ShouldBeRun(mailRule4, startTime)).Return(false); // Matcher3 rejects rule 1, accepts rules 2 and 3, rejects rule 4 matcher3.Expect(e3 => e3.IsProperMatcher(mailRule1)).Return(true); matcher3.Expect(e3 => e3.IsProperMatcher(mailRule2)).Return(true); matcher3.Expect(e3 => e3.IsProperMatcher(mailRule3)).Return(true); matcher3.Expect(e3 => e3.IsProperMatcher(mailRule4)).Return(true); matcher3.Expect(e3 => e3.ShouldBeRun(mailRule1, startTime)).Return(false); matcher3.Expect(e3 => e3.ShouldBeRun(mailRule2, startTime)).Return(true); matcher3.Expect(e3 => e3.ShouldBeRun(mailRule3, startTime)).Return(true); matcher3.Expect(e3 => e3.ShouldBeRun(mailRule4, startTime)).Return(false); // Act this.ruleMatcher = new RuleMatcher(matchers); var result = this.ruleMatcher.GetMatchedRules(mailRules, startTime); // Assert Assert.AreEqual(result.Count, 2); Assert.IsFalse(result.Contains(mailRule1)); Assert.IsTrue(result.Contains(mailRule2)); Assert.IsTrue(result.Contains(mailRule3)); Assert.IsFalse(result.Contains(mailRule4)); }
//Instantiate action from action schema static PrimitiveTask InstantiateAction(PrimitiveTask schema, State state, HTNAgent agent) { //If conditions found matches, instantiate action from schema, with a variable binding if (RuleMatcher.MatchCondition(schema.preconditions, state, schema.logicalOperator)) { PrimitiveTask groundAction = new PrimitiveTask(schema.name, schema.parent); if (schema.actionType == ActionTypes.MOVEMENT) { if (schema.groundData.animationState != "") { groundAction = new Move(agent, schema.name, schema.groundData.radius, schema.groundData.speed, schema.groundData.animationState); } else { groundAction = new Move(agent, schema.name, schema.groundData.radius, schema.groundData.speed); } } else if (schema.actionType == ActionTypes.ANIMATION) { groundAction = new Animate(agent, schema.name, schema.groundData.animationState); } else if (schema.actionType == ActionTypes.CUSTOM) { Type customType = Type.GetType(schema.groundData.name); ConstructorInfo customConstructor = customType.GetConstructor(new Type[2] { typeof(HTNAgent), typeof(string) }); groundAction = (PrimitiveTask)customConstructor.Invoke(new object[] { agent, schema.name }); } groundAction.parent = schema.parent; groundAction.actionType = schema.actionType; groundAction.cost = schema.cost; groundAction.logicalOperator = schema.logicalOperator; groundAction.arguments = schema.arguments; groundAction.preconditions = schema.preconditions; groundAction.cuncurrentTasks = schema.cuncurrentTasks; groundAction.effects = schema.effects; return(groundAction); } return(null); }
//This starts agent's action execution public void Start(ActionManager manager, State state) { //Monitor precondition validity in current state: if not valid, notify to manager if (preconditions.Count > 0 && !RuleMatcher.MatchCondition(preconditions, state, logicalOperator)) { manager.NotValid(); } else { //Check cuncurrency foreach (CuncurrentTask cuncurrent in cuncurrentTasks) { if (!CheckCuncurrency(cuncurrent, agentObj.GetComponent <HTNAgent>())) { manager.Suspend(); return; } } if (agentObj.GetComponent <CharacterController> () == null) { agentObj.AddComponent <CharacterController> (); } target = (GameObject)arguments [0].value; moving = true; try { anim = agentObj.GetComponent <Animator>(); if (anim != null) { anim.CrossFade(animationState, 0.0f); } } catch (MissingComponentException e) { Debug.Log(e.Message); } } }
public MatchedRule(RuleMatcher ruleMatcher) { this = default(MatchedRulesExtractor.MatchedRule); this.ruleMatcher = ruleMatcher; this.fullPath = AssetDatabase.GetAssetPath(ruleMatcher.sheet); this.lineNumber = ruleMatcher.complexSelector.rule.line; if (this.fullPath != null) { if (this.fullPath == "Library/unity editor resources") { this.displayPath = ruleMatcher.sheet.name + ":" + this.lineNumber; } else { this.displayPath = Path.GetFileNameWithoutExtension(this.fullPath) + ":" + this.lineNumber; } } }
public void ConstructorAssignsProperDefaultMatchers() { // Assemble this.ruleMatcher = new RuleMatcher(); // Act var matchers = this.ruleMatcher.Matchers; // Assert Assert.IsTrue(matchers.Count == 7); Assert.IsTrue(matchers.Count(m => m.GetType() == typeof(WeeklyMatcher)) == 1); Assert.IsTrue(matchers.Count(m => m.GetType() == typeof(DateOfMonthMatcher)) == 1); Assert.IsTrue(matchers.Count(m => m.GetType() == typeof(DayOfWeekOfMonthOfYearMatcher)) == 1); Assert.IsTrue(matchers.Count(m => m.GetType() == typeof(DayOfWeekOfMonthMatcher)) == 1); Assert.IsTrue(matchers.Count(m => m.GetType() == typeof(DateOfMonthOfYearMatcher)) == 1); Assert.IsTrue(matchers.Count(m => m.GetType() == typeof(EveryDayMatcher)) == 1); Assert.IsTrue(matchers.Count(m => m.GetType() == typeof(EveryWeekDayMatcher)) == 1); }
public MatchedRule(RuleMatcher ruleMatcher) : this() { this.ruleMatcher = ruleMatcher; fullPath = AssetDatabase.GetAssetPath(ruleMatcher.sheet); lineNumber = ruleMatcher.complexSelector.rule.line; if (fullPath != null) { if (fullPath == "Library/unity editor resources") { displayPath = ruleMatcher.sheet.name + ":" + lineNumber; } else { displayPath = Path.GetFileNameWithoutExtension(fullPath) + ":" + lineNumber; } } }
//Next action in queue public void NextAction() { //If there are still actions or agent hasn't reached the goal, get and start next action. Otherwise change state to COMPLETED plan if (actions.Count > 0) { IGroundAction action = actions.Dequeue(); currentAction = action; //Start action action.Start(this, agent.state); } else if (actions.Count == 0 || RuleMatcher.MatchCondition(agent.goalCondition, agent.state, agent.logicalOperator)) { state = ManagerState.EMPTY; currentAction = null; } }
//Runs at every frame during action's execution public void Update(ActionManager manager, State state) { //Check cuncurrency foreach (CuncurrentTask cuncurrent in cuncurrentTasks) { if (!CheckCuncurrency(cuncurrent, agentObj.GetComponent <HTNAgent>())) { manager.Suspend(); anim.CrossFade("Idle", 0.0f); return; } } //Monitor preconditions validity in current state: if not valid, notify to manager if (preconditions.Count > 0 && !RuleMatcher.MatchCondition(preconditions, state, logicalOperator)) { manager.NotValid(); } else { //Add custom code if (anim != null && anim.GetCurrentAnimatorStateInfo(0).IsName("Idle")) { anim.CrossFade(animationState, 0.0f); } if (anim != null && !anim.GetCurrentAnimatorStateInfo(0).IsName(animationState)) { OnComplete(manager, state); } } }
//Runs at every frame during action's execution public void Update(ActionManager manager, State state) { //Check cuncurrency foreach (CuncurrentTask cuncurrent in cuncurrentTasks) { if (!CheckCuncurrency(cuncurrent, agentObj.GetComponent <HTNAgent>())) { manager.Suspend(); return; } } //Monitor preconditions validity in current state: if not valid, notify to manager if (preconditions.Count > 0 && !RuleMatcher.MatchCondition(preconditions, state, logicalOperator)) { manager.NotValid(); } else { if (anim != null && anim.GetCurrentAnimatorStateInfo(0).IsName("Idle")) { anim.CrossFade(animationState, 0.0f); } CharacterController controller = agentObj.GetComponent <CharacterController> (); MoveTowardsTarget(controller, target.transform.position); //Monitor action completeness if (!moving) { OnComplete(manager, state); } } }
//This starts agent's action execution public void Start(ActionManager manager, State state) { //Monitor precondition validity in current state: if not valid, notify to manager if (preconditions.Count > 0 && !RuleMatcher.MatchCondition(preconditions, state, logicalOperator)) { manager.NotValid(); } else { //Check cuncurrency foreach (CuncurrentTask cuncurrent in cuncurrentTasks) { if (!CheckCuncurrency(cuncurrent, agentObj.GetComponent <HTNAgent>())) { manager.Suspend(); anim.CrossFade("Idle", 0.0f); return; } } //Add custom code try { anim = agentObj.GetComponent <Animator>(); if (anim != null) { anim.CrossFade("Idle", 0.0f); } } catch (MissingComponentException e) { Debug.Log(e.Message); } } }
public RuleSetDeterminator(GenderRuleSet ruleSet) { _exceptionsMathcer = new RuleMatcher(ruleSet?.Exceptions, true); _suffixMathcer = new RuleMatcher(ruleSet?.Suffixes, false); }
static List <PrimitiveTask> SHOPNoBack(List <Task> tasks, List <PrimitiveTask> plan, string personality, State state, HTNAgent agent) { Task task = tasks [0]; if (task.GetType() == typeof(PrimitiveTask)) { PrimitiveTask pt = (PrimitiveTask)task; PrimitiveTask groundAction = InstantiateAction(pt, state, agent); if (groundAction != null) { plan.Add(groundAction); tasks.Remove(pt); if (tasks.Count > 0) { return(SHOPNoBack(tasks, plan, personality, pt.apply(state), agent)); } } else { return(null); } } else if (task.GetType() == typeof(CompoundTask)) { CompoundTask ct = (CompoundTask)task; List <Method> orderdedMethods = new List <Method>(); switch (agent.methodsOrdering) { case MethodsOrdering.NONE: orderdedMethods = ct.methods; break; case MethodsOrdering.PREF: orderdedMethods = ct.methods.OrderBy(m => m.preference != personality).ToList(); break; case MethodsOrdering.COSTANDPREF: orderdedMethods = ct.methods.OrderBy(m => m.leastCost()).ThenBy(m => m.preference != personality).ToList(); break; } foreach (Method m in orderdedMethods) { if (RuleMatcher.MatchCondition(m.preconditions, state, m.logicalOperator)) { List <Task> tempTasks = new List <Task>(tasks); tempTasks.Remove(task); int j = 0; foreach (Task subtask in m.subtasks) { tempTasks.Insert(0 + j, subtask); j++; } List <PrimitiveTask> tempPlan = SHOPNoBack(tempTasks, plan, personality, state, agent); if (tempPlan != null) { plan.Concat(tempPlan); break; } else { return(null); } } } } return(plan); }