/** * Factory method to create a new Tag. * * @param type * The type of the {@link Tag}. * @param input * The input object to be analyzed. * @param parent * The parent {@link Tag}. * @return A new Tag */ public static Tag tag(String type, Object input, Tag parent) { return new Tag(type, input, parent); }
/** * Returns the InvocationSequenceData of GlobalContext Tag. * * @param leafTag * leafTag for which the InvocationSequenceData should be returned * @return InvocationSequenceData of GlobalContext */ private Optional<InvocationSequenceData> getGlobalContext(Tag leafTag) { while (null != leafTag) { if (leafTag.getType().equals(RuleConstants.DIAGNOSIS_TAG_GLOBAL_CONTEXT)) { if (leafTag.getValue() instanceof InvocationSequenceData) { return Optional.of((InvocationSequenceData) leafTag.getValue()); } else { throw new RuntimeException("Global context has wrong datatype!"); } } leafTag = leafTag.getParent(); } return Optional.empty(); }
/** * {@inheritDoc} */ @Override public Multimap<String, Tag> mapTags(TagState state) { Multimap<String, Tag> tags = ArrayListMultimap.create(); for (Map.Entry<String, RuleOutput> entry : allOutputs.entries()) { if (entry.getValue().hasResultTags()) { for (Tag tag : entry.getValue().getTags()) { if (tag.getState().equals(state)) { tags.put(tag.getType(), tag); } } } } return tags; }
@Test public void validRootTagInput() { rocks.inspectit.server.diagnosis.engine.tag.Tag tag = rocks.inspectit.server.diagnosis.engine.tag.Tags.rootTag("TestInput"); assertThat(tag.getType(), equalTo(rocks.inspectit.server.diagnosis.engine.tag.Tags.ROOT_TAG)); assertThat(tag.getValue().toString(), equalTo("TestInput")); assertThat(tag.getParent(), equalTo(null)); assertThat(tag.getState(), equalTo(TagState.LEAF)); }
/** * {@inheritDoc} * * @throws RuleExecutionException * If tag injection fails. */ @Override public Object determineValueToInject(ExecutionContext context) throws RuleExecutionException { // extract the required type form the unraveled list of input Tags for (Tag tag : context.getRuleInput().getUnraveled()) { if (tag.getType().equals(getType())) { return InjectionStrategy.BY_VALUE.equals(getInjectionStrategy()) ? tag.getValue() : tag; } } throw new RuleExecutionException("Unable to find Tag: " + getType() + " in RuleInput.", context); }
@Test public void fromActivated() throws Exception { session = new Session<>(Rules.define(RuleA.class, RuleB.class, RuleC.class), new DefaultSessionResultCollector<String>()); String input = "input"; session.activate(input, Session.EMPTY_SESSION_VARIABLES); DefaultSessionResult<String> result = session.call(); assertThat(session.getState(), equalTo(State.PROCESSED)); assertThat(result.getConditionFailures().keys(), empty()); assertThat(result.getEndTags().keySet(), containsInAnyOrder("C")); assertThat(result.getEndTags().get("C"), hasSize(1)); assertThat(((Integer) result.getEndTags().get("C").iterator().next().getValue()), equalTo((input.length() * 2) + 2)); }
/** * {@inheritDoc} */ @Override public int hashCode() { final int prime = 31; int result = 1; result = (prime * result) + ((this.root == null) ? 0 : this.root.hashCode()); result = (prime * result) + ((this.unraveled == null) ? 0 : this.unraveled.hashCode()); return result; }
return false; } else if (!this.root.equals(other.root)) { return false;
/** * Creates a Tag with a value and a parent. * * @param type * The type of this <code>Tag</code>. * @param value * The value of this <code>Tag</code> * @param parent * The <code>Tag</code> which is considered as the predecessor of this * <code>Tag</code>. */ public Tag(String type, Object value, Tag parent) { this.value = value; this.type = type; this.parent = parent; if (this.parent != null) { this.parent.markParent(); } }
@Test public void nullRootTagInput() { rocks.inspectit.server.diagnosis.engine.tag.Tag tag = rocks.inspectit.server.diagnosis.engine.tag.Tags.rootTag(null); assertThat(tag.getType(), equalTo(rocks.inspectit.server.diagnosis.engine.tag.Tags.ROOT_TAG)); assertThat(tag.getValue(), equalTo(null)); assertThat(tag.getParent(), equalTo(null)); assertThat(tag.getState(), equalTo(TagState.LEAF)); } }
@Test public void simpleRuleExecution() throws RuleDefinitionException, RuleExecutionException { RuleDefinition ruleDefinition = Rules.define(RuleA.class); String inputStr = "hallo"; RuleInput input = new RuleInput(Tags.tag(Tags.ROOT_TAG, inputStr)); RuleOutput output = ruleDefinition.execute(input, Collections.<String, Object> emptyMap()); assertThat(output.getRuleName(), equalTo(ruleDefinition.getName())); assertThat(output.getTags(), hasSize(1)); assertThat(output.getTags().iterator().next().getValue(), equalTo((Object) (inputStr + inputStr))); }
/** * {@inheritDoc} */ @Override public int hashCode() { final int prime = 31; int result = 1; result = (prime * result) + ((this.parent == null) ? 0 : this.parent.hashCode()); result = (prime * result) + ((this.state == null) ? 0 : this.state.hashCode()); result = (prime * result) + ((this.type == null) ? 0 : this.type.hashCode()); result = (prime * result) + ((this.value == null) ? 0 : this.value.hashCode()); return result; }
return false; } else if (!this.parent.equals(other.parent)) { return false;
@Test public void nullParentTag() { rocks.inspectit.server.diagnosis.engine.tag.Tag tag = rocks.inspectit.server.diagnosis.engine.tag.Tags.tag("A", "TestInput"); assertThat(tag.getType(), equalTo("A")); assertThat(tag.getValue().toString(), equalTo("TestInput")); assertThat(tag.getParent(), equalTo(null)); assertThat(tag.getState(), equalTo(TagState.LEAF)); }
/** * Returns the CauseStructure of the CauseStructure Tag. * * @param leafTag * leafTag for which the CauseStructure should be returned * @return CauseStructure of leafTag */ private Optional<CauseStructure> getCauseStructure(Tag leafTag) { while (null != leafTag) { if (leafTag.getType().equals(RuleConstants.DIAGNOSIS_TAG_CAUSE_STRUCTURE)) { if (leafTag.getValue() instanceof CauseStructure) { return Optional.of((CauseStructure) leafTag.getValue()); } else { throw new RuntimeException("Cause structure has wrong datatype!"); } } leafTag = leafTag.getParent(); } return Optional.empty(); } }
@Test public void shouldProduceSingleTagWithSingleObjectValue() throws Exception { // prepare Mocks Tag rootTag = Tags.rootTag("Input"); Tag expectedResultTag = new Tag("T1", "oneResult", rootTag); when(dummy.action()).thenReturn("oneResult"); when(input.getRoot()).thenReturn(rootTag); // Create TestMethod ActionMethod action = new ActionMethod(RuleDummy.actionMethod(), "T1", Action.Quantity.SINGLE); // execute Collection<Tag> result = action.execute(context); // verify assertThat(result, hasSize(1)); assertThat(result, hasItem(expectedResultTag)); }
@Test public void ruleWithSessionVariableExecution() throws RuleDefinitionException, RuleExecutionException { RuleDefinition ruleDefinition = Rules.define(RuleWithSessionVariable.class); String inputStr = "hallo"; String sessionVar = "sessionVar"; RuleInput input = new RuleInput(Tags.tag(Tags.ROOT_TAG, inputStr)); RuleOutput output = ruleDefinition.execute(input, Collections.singletonMap(sessionVar, (Object) sessionVar)); assertThat(output.getRuleName(), equalTo(ruleDefinition.getName())); assertThat(output.getTags(), hasSize(1)); assertThat(output.getTags().iterator().next().getValue(), equalTo((Object) (inputStr + sessionVar))); }
@Test public void nullParentTag() { Collection<rocks.inspectit.server.diagnosis.engine.tag.Tag> tags = rocks.inspectit.server.diagnosis.engine.tag.Tags.tags("A", null, "TestInput1", "TestInput2"); rocks.inspectit.server.diagnosis.engine.tag.Tag[] tagArray = tags.toArray(new rocks.inspectit.server.diagnosis.engine.tag.Tag[0]); assertThat(tags, hasSize(2)); assertThat(tagArray[0].getType(), equalTo("A")); assertThat(tagArray[1].getType(), equalTo("A")); assertThat(tagArray[0].getValue().toString(), equalTo("TestInput1")); assertThat(tagArray[1].getValue().toString(), equalTo("TestInput2")); assertThat(tagArray[0].getParent(), equalTo(null)); assertThat(tagArray[1].getParent(), equalTo(null)); assertThat(tagArray[0].getState(), equalTo(TagState.LEAF)); assertThat(tagArray[1].getState(), equalTo(TagState.LEAF)); }
/** * Returns the InvocationSequenceData of ProblemContext Tag. * * @param leafTag * leafTag for which the InvocationSequenceData should be returned * @return InvocationSequenceData of ProblemContext */ private Optional<CauseCluster> getProblemContext(Tag leafTag) { while (null != leafTag) { if (leafTag.getType().equals(RuleConstants.DIAGNOSIS_TAG_PROBLEM_CONTEXT)) { if (leafTag.getValue() instanceof CauseCluster) { return Optional.of((CauseCluster) leafTag.getValue()); } else { throw new RuntimeException("Problem context has wrong datatype!"); } } leafTag = leafTag.getParent(); } return Optional.empty(); }