TacKbp2017KBWriter implements KnowledgeBaseWriter { @Override public void write(final KnowledgeBase kb, final Random random, final CharSink sink) throws IOException { final Ordering<Node> arbitraryOrderOfNodes = Ordering.explicit(kb.nodes().asList()); final Ordering<Assertion> orderBySubject = arbitraryOrderOfNodes.onResultOf(Assertion.SubjectFunction.INSTANCE); final Ordering<Assertion> orderBySubjectThenAssertionType = orderBySubject.compound(ASSERTION_TYPE_ORDERING); try (final Writer writer = sink.openBufferedStream()) { writer.write(kb.runId().asString() + "\n"); final TacKbp2017KBWriting writing = new TacKbp2017KBWriting(kb.nodesToNames(), random); for (final Assertion assertion : orderBySubjectThenAssertionType .immutableSortedCopy(kb.assertions())) { final StringBuilder assertionOutputString = new StringBuilder(); assertionOutputString.append(writing.assertionToString(Optional.of(kb), assertion)); if (kb.confidence().containsKey(assertion)) { final double confidence = kb.confidence().get(assertion); if (confidence >= 0.000001) { assertionOutputString.append("\t").append(String.format(Locale.US, "%.6f", confidence)); } else { assertionOutputString.append("\t").append(String.format(Locale.US, "%.6f", 0.000001)); } } assertionOutputString.append("\n"); writer.write(assertionOutputString.toString()); } } }  static TacKbp2017KBWriter create(); @Override void write(final KnowledgeBase kb, final Random random, final CharSink sink);  }
@Test public void testWrite() throws IOException { final KnowledgeBaseWriter writer = TacKbp2017KBWriter.create(); final Assertion assertion1 = TypeAssertion.of(eventNode0, Symbol.from("CONFLICT.ATTACK")); final Assertion assertion2 = EventMentionAssertion.of( eventNode0, "dummy\tmention", Symbol.from("actual"), DUMMY); final Assertion assertion3 = TypeAssertion.of(stringNode0, Symbol.from("STRING")); final Assertion assertion4 = NormalizedMentionAssertion.of( stringNode0, "XXXX-XX-XX", DUMMY); final KnowledgeBase kb = KnowledgeBase.builder() .runId(Symbol.from("dummy_runID")) .addNodes(eventNode0, stringNode0) .nameNode(eventNode0, nodeNames.get(eventNode0)) .nameNode(stringNode0, nodeNames.get(stringNode0)) .addAssertions(assertion4, assertion3, assertion2, assertion1) .putConfidence(assertion1, 0.9) .putConfidence(assertion3, 0.0000000000000000000000000000000001) .build(); final String expectedOutput = "dummy_runID\n" + ":Event_000000\ttype\tCONFLICT.ATTACK\t0.900000\n" + ":Event_000000\tmention.actual\t\"dummy mention\"\tdocID:5-12\n" + ":String_000000\ttype\tSTRING\t0.000001\n" + ":String_000000\tnormalized_mention\t\"XXXX-XX-XX\"\tdocID:5-12"; final File outputFile = File.createTempFile("kb-writer-test", ".tmp"); writer.write(kb, new Random(0), Files.asCharSink(outputFile, Charsets.UTF_8)); final String actualOutput = Joiner.on("\n").join(Files.readLines(outputFile, Charsets.UTF_8)); outputFile.deleteOnExit(); assertEquals(expectedOutput, actualOutput); }