DisjunctiveNormalFormConverter { public static DocumentVisibility convertToDisjunctiveNormalForm(final DocumentVisibility documentVisibility) { final List<String> terms = findNodeTerms(documentVisibility.getParseTree(), documentVisibility.getExpression()); final byte[][] truthTable = createTruthTableInputs(terms); final List<List<String>> termRowsThatPass = new ArrayList<>(); for (final byte[] row : truthTable) { final List<String> termRowToCheck = new ArrayList<>(); for (int i = 0; i < row.length; i++) { final byte entry = row[i]; if (entry == 1) { termRowToCheck.add(terms.get(i)); } } final List<String> authList = new ArrayList<>(); for (final String auth : termRowToCheck) { String formattedAuth = auth; formattedAuth = StringUtils.removeStart(formattedAuth, "\""); formattedAuth = StringUtils.removeEnd(formattedAuth, "\""); authList.add(formattedAuth); } final Authorizations auths = new Authorizations(authList.toArray(new String[0])); final boolean hasAccess = DocumentVisibilityUtil.doesUserHaveDocumentAccess(auths, documentVisibility, false); if (hasAccess) { boolean alreadyCoveredBySimplerTerms = false; for (final List<String> existingTermRowThatPassed : termRowsThatPass) { alreadyCoveredBySimplerTerms = termRowToCheck.containsAll(existingTermRowThatPassed); if (alreadyCoveredBySimplerTerms) { break; } } if (!alreadyCoveredBySimplerTerms) { termRowsThatPass.add(termRowToCheck); } } } final StringBuilder sb = new StringBuilder(); boolean isFirst = true; final boolean hasMultipleGroups = termRowsThatPass.size() > 1; for (final List<String> termRowThatPassed : termRowsThatPass) { if (isFirst) { isFirst = false; } else { sb.append("|"); } if (hasMultipleGroups && termRowThatPassed.size() > 1) { sb.append("("); } sb.append(Joiner.on("&").join(termRowThatPassed)); if (hasMultipleGroups && termRowThatPassed.size() > 1) { sb.append(")"); } } log.trace(sb.toString()); final DocumentVisibility dnfDv = new DocumentVisibility(sb.toString()); return dnfDv; } private  DisjunctiveNormalFormConverter(); static DocumentVisibility createDnfDocumentVisibility(final String expression); static DocumentVisibility createDnfDocumentVisibility(final byte[] expression); static DocumentVisibility convertToDisjunctiveNormalForm(final DocumentVisibility documentVisibility); static List<String> findNodeTerms(final Node node, final byte[] expression); static byte[][] createTruthTableInputs(final Node node, final byte[] expression); static byte[][] createTruthTableInputs(final List<String> terms); static byte[][] createTruthTableInputs(final int termNumber);  }
@Test public void testConvertToDnf() { for (final Pair<String, String> pair : INPUT_AND_EXPECTED_BOOLEAN_EXPRESSIONS) { final String input = pair.getLeft(); final String expected = pair.getRight(); final DocumentVisibility inputDv = new DocumentVisibility(input); final DocumentVisibility resultDv = DisjunctiveNormalFormConverter.convertToDisjunctiveNormalForm(inputDv); assertEquals(expected, new String(resultDv.flatten())); } }