Skip to content

Commit 112b831

Browse files
authored
Merge pull request #129 from sanctuuary/fix_119_empty_clause_creation
Implement non-empty clause counting which fixes an empty clause error when having only one tool in the workflow
2 parents 8fa6a80 + 0b86a93 commit 112b831

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

src/main/java/nl/uu/cs/ape/models/templateFormulas/SLTLxTemplateFormula.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ public static String connectedModules(TaxonomyPredicate firstPredicate, Taxonomy
545545
for (Integer currComb : allCombinations) {
546546
constraints.append(currComb + " ");
547547
}
548+
548549
constraints.append("0\n");
549550

550551
// each combination enforces usage of the corresponding tools and output/inputs

src/main/java/nl/uu/cs/ape/solver/minisat/SATSynthesisEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ public boolean synthesisEncoding() throws IOException {
278278
* counted again.
279279
*/
280280
int variables = mappings.getSize();
281-
int clauses = APEUtils.countLines(cnfEncoding);
281+
int clauses = APEUtils.countCNFClauses(new FileInputStream(cnfEncoding));
282282
String satInputHeader = "p cnf " + variables + " " + clauses + "\n";
283283
APEUtils.timerRestartAndPrint(currLengthTimer, "Reading rows");
284284
satInputFile = APEFiles.prependToFile(satInputHeader, cnfEncoding);

src/main/java/nl/uu/cs/ape/solver/minisat/SatEncodingUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public static String encodeAPEConstraints(SATSynthesisEngine synthesisEngine, AP
7474
}
7575

7676
/**
77-
* Function used to provide SAT encoding of a constrain based on the constraint
77+
* Function used to provide SAT encoding of a constraint based on the constraint
7878
* ID specified and provided parameters.
7979
*
8080
* @param constraintID ID of the constraint.

src/main/java/nl/uu/cs/ape/utils/APEUtils.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,59 @@ public static void timerPrintText(String timerID, String text) {
469469
log.info(text + " Running time: " + (printTime / 1000F) + " sec.");
470470
}
471471

472+
/**
473+
* This function counts the CNF clauses in a stream, assuming correct CNF syntax.
474+
*
475+
* @param cnfEncoding the CNF encoding
476+
* @return the count of non-empty clauses
477+
*/
478+
public static int countCNFClauses(InputStream cnfEncoding) {
479+
480+
int count = 0;
481+
try (BufferedReader b = new BufferedReader(new InputStreamReader(cnfEncoding))) {
482+
String line = null;
483+
while ((line = b.readLine()) != null) {
484+
if (line.endsWith(" 0")) count++;
485+
}
486+
} catch (IOException e) {
487+
e.printStackTrace();
488+
}
489+
490+
return count;
491+
}
492+
493+
/**
494+
* This function counts the non-empty CNF clauses in a stream and
495+
* while also checking the CNF syntax and warns if a clause is empty.
496+
*
497+
* @param cnfEncoding the CNF encoding
498+
* @return the count of non-empty clauses
499+
*/
500+
public static int checkCNFClauses(InputStream cnfEncoding) {
501+
502+
int clauseCount = 0;
503+
int litCount = 0;
504+
505+
Scanner scanner = new Scanner(cnfEncoding);
506+
while (scanner.hasNextInt()) {
507+
int intAtom = scanner.nextInt();
508+
509+
if (intAtom == 0) {
510+
if (litCount == 0) {
511+
log.warn("Found empty clause (" + (clauseCount + 1) + ") after in CNF input, will not count for DIMACS.");
512+
} else {
513+
clauseCount++;
514+
}
515+
litCount = 0;
516+
} else {
517+
litCount++;
518+
}
519+
}
520+
scanner.close();
521+
522+
return clauseCount;
523+
}
524+
472525
/**
473526
* Convert cnf 2 human readable string.
474527
*

0 commit comments

Comments
 (0)