/** * Given an initial set of rows produced by an {@link org.batfish.common.Answerer}, this procedure * processes exclusions, assertions, and summary to update this object. * * @param question The question that generated the initial set of rows * @param initialSet The initial set of rows */ public void postProcessAnswer(Question question, Iterable<Row> initialSet) { initialSet.forEach( initialRow -> { // exclude or not? Exclusion exclusion = Exclusion.covered(initialRow, question.getExclusions()); if (exclusion != null) { addExcludedRow(initialRow, exclusion.getName()); } else { addRow(initialRow); } }); setSummary(computeSummary(question.getAssertion())); }
/** * Checks is this row is covered by the provided exclusion. * * @param exclusion The exclusion to check against. * @return The result of the check */ public boolean isCovered(ObjectNode exclusion) { return Exclusion.firstCoversSecond(exclusion, _data); }
/** * Checks if {@code row} is covered by one of the exlusions * * @param row The object whose coverage is to be checked * @return The exclusion that covers {@code row}. null if no such exclusion exists */ public static Exclusion covered(Row row, List<Exclusion> exclusions) { if (exclusions == null) { return null; } for (Exclusion exclusion : exclusions) { if (row.isCovered(exclusion.getExclusion())) { return exclusion; } } return null; }
new Exclusion( null, (ObjectNode)
testQuestion.setExclusions( ImmutableList.of( new Exclusion( "exc", BatfishObjectMapper.mapper()
boolean covered = false; for (JsonNode secondElement : second) { if (firstCoversSecond(firstElement, secondElement)) { covered = true; break; return false; if (!firstCoversSecond(first.get(key), second.get(key))) { return false;
Row answerValues = computeRowValues(query.getExtractions(), query.getCompositions(), result, tableMetadata); Exclusion exclusion = Exclusion.covered(answerValues, question.getExclusions()); if (exclusion != null) { answer.addExcludedRow(answerValues, exclusion.getName()); } else { answer.addRow(answerValues);
@Test public void firstCoversSecondObjectCovers() { JsonNode node1 = BatfishObjectMapper.mapper().createObjectNode().set("key1", new TextNode("value")); JsonNode node2 = BatfishObjectMapper.mapper().createObjectNode().set("key1", new TextNode("value")); // we couldn't just chain the two sets because set returns JsonNode, not ObjectNode ((ObjectNode) node2).set("key2", new TextNode("value")); boolean result = Exclusion.firstCoversSecond(node1, node2); assertThat(result, equalTo(true)); }
@Test public void firstCoversSecondObjectMissingKey() { JsonNode node1 = BatfishObjectMapper.mapper().createObjectNode().set("key1", new TextNode("value")); JsonNode node2 = BatfishObjectMapper.mapper().createObjectNode().set("key2", new TextNode("value")); boolean result = Exclusion.firstCoversSecond(node1, node2); assertThat(result, equalTo(false)); }
@Test public void firstCoversSecondValueMismatch() { JsonNode node1 = new TextNode("2"); JsonNode node2 = new IntNode(2); // different type boolean result = Exclusion.firstCoversSecond(node1, node2); assertThat(result, equalTo(false)); } }
@Test public void firstCoversSecondValueMatch() { JsonNode node1 = new TextNode("abc"); JsonNode node2 = new TextNode("abc"); boolean result = Exclusion.firstCoversSecond(node1, node2); assertThat(result, equalTo(true)); }
@Test public void firstCoversSecondObjectMismatchType() { JsonNode node1 = BatfishObjectMapper.mapper().createObjectNode().set("key1", new TextNode("value")); JsonNode node2 = BatfishObjectMapper.mapper().createArrayNode().add(new TextNode("value")); boolean result = Exclusion.firstCoversSecond(node1, node2); assertThat(result, equalTo(false)); }
@Test public void firstCoversSecondArrayCovers() { JsonNode node1 = BatfishObjectMapper.mapper().createArrayNode().add(new TextNode("value")); JsonNode node2 = BatfishObjectMapper.mapper() .createArrayNode() .add(new TextNode("value2")) .add(new TextNode("value")); boolean result = Exclusion.firstCoversSecond(node1, node2); assertThat(result, equalTo(true)); }