/** * Utility method to create a {@link FireCondition} either from a {@link Rule} annotation or * from {@link TagInjection}s. The values defined in the annotation overrules the * {@link TagInjection}s. * * @param rule * The {@link Rule} annotation. * @param tagInjections * The list of {@link TagInjection}s to extract a {@link FireCondition}. * @return A new {@link FireCondition} */ public static FireCondition describeFireCondition(Rule rule, List<TagInjection> tagInjections) { if ((rule != null) && ArrayUtils.isNotEmpty(rule.fireCondition())) { return new FireCondition(Sets.newHashSet(Arrays.asList(rule.fireCondition()))); } else { Set<String> requiredTypes = new HashSet<>(); for (TagInjection injection : tagInjections) { requiredTypes.add(injection.getType()); } return new FireCondition(requiredTypes); } }
/** * Utility method to determine the next executable rules. The next rules are determined by * comparing all, so far collected, types of tags in {@link IRuleOutputStorage} and the * {@link FireCondition} of each {@link RuleDefinition}. * * @param availableTagTypes * Set of strings determining the available tag types for input of potential next * rules. * @param ruleDefinitions * Set of available rule definitions. * * @return Collection of {@link RuleDefinition}s. * @see rocks.inspectit.server.diagnosis.engine.rule.FireCondition * @see RuleDefinition * @see IRuleOutputStorage */ Collection<RuleDefinition> findNextRules(Set<String> availableTagTypes, Set<RuleDefinition> ruleDefinitions) { Set<RuleDefinition> nextRules = new HashSet<>(); Iterator<RuleDefinition> iterator = ruleDefinitions.iterator(); while (iterator.hasNext()) { RuleDefinition rule = iterator.next(); if (rule.getFireCondition().canFire(availableTagTypes)) { nextRules.add(rule); } } return nextRules; }
/** * {@inheritDoc} */ @Override public int hashCode() { final int prime = 31; int result = 1; result = (prime * result) + ((this.actionMethod == null) ? 0 : this.actionMethod.hashCode()); result = (prime * result) + ((this.conditionMethods == null) ? 0 : this.conditionMethods.hashCode()); result = (prime * result) + ((this.description == null) ? 0 : this.description.hashCode()); result = (prime * result) + ((this.fireCondition == null) ? 0 : this.fireCondition.hashCode()); result = (prime * result) + ((this.implementation == null) ? 0 : this.implementation.hashCode()); result = (prime * result) + ((this.name == null) ? 0 : this.name.hashCode()); result = (prime * result) + ((this.tagInjections == null) ? 0 : this.tagInjections.hashCode()); result = (prime * result) + ((this.variableInjections == null) ? 0 : this.variableInjections.hashCode()); return result; }
Set<String> requiredInputTags = definition.getFireCondition().getTagTypes();
return false; } else if (!this.fireCondition.equals(other.fireCondition)) { return false;
@Test public void testWithRuleAnnotation() throws Exception { RuleDefinition definition = Rules.define(ValidAndAnnotated.class); assertThat(definition.getName(), is("AnnotatedRule")); assertThat(definition.getDescription(), is("Description")); assertThat(definition.getFireCondition().getTagTypes(), containsInAnyOrder("T1", "T2")); // Test tag injections TagInjection tagInjection = new TagInjection("T1", ValidAndAnnotated.class.getDeclaredField("t1AsTag"), TagValue.InjectionStrategy.BY_TAG); TagInjection tagInjection1 = new TagInjection("T2", ValidAndAnnotated.class.getDeclaredField("t2TagValue"), TagValue.InjectionStrategy.BY_VALUE); assertThat(definition.getTagInjections(), is(notNullValue())); assertThat(definition.getTagInjections(), containsInAnyOrder(tagInjection, tagInjection1)); // Test session variables SessionVariableInjection s1 = new SessionVariableInjection("baseline", false, ValidAndAnnotated.class.getDeclaredField("baseline")); SessionVariableInjection s2 = new SessionVariableInjection("baseline2", true, ValidAndAnnotated.class.getDeclaredField("baseline2")); assertThat(definition.getSessionVariableInjections(), containsInAnyOrder(s1, s2)); // Test action method assertThat(definition.getActionMethod(), is(new ActionMethod(ValidAndAnnotated.class.getDeclaredMethod("action"), "T2", Action.Quantity.SINGLE))); // Test condition method ConditionMethod conditionMethod = new ConditionMethod("myCondition", "No way out", ValidAndAnnotated.class.getDeclaredMethod("condition")); assertThat(definition.getConditionMethods(), containsInAnyOrder(conditionMethod)); }