This repository was archived by the owner on Jun 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExampleLoader.java
More file actions
147 lines (128 loc) · 3.55 KB
/
Copy pathExampleLoader.java
File metadata and controls
147 lines (128 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class ExampleLoader {
private class Content{
private int numOfVars;
private int numOfClauses;
private ArrayList<ArrayList<Integer>> data;
}
/**
* The example file.
*/
private final String fileName;
/**
* the content of the example file loaded in this object
*/
private final ArrayList<ArrayList<Integer>> content;
/**
* the number of propositional variables in the example file
*/
private final int numOfVars;
/**
* the number of clauses in the example file
*/
private final int numOfClauses;
public ExampleLoader(String fileName){
this.fileName = fileName;
Content content = loadFile();
this.content = content.data;
this.numOfVars = content.numOfVars;
this.numOfClauses = content.numOfClauses;
}
/**
* Loads an example file.
* @return the example file as a list.
*/
private Content loadFile(){
Content result = new Content();
result.data = new ArrayList<ArrayList<Integer>>();
try {
Scanner scanner = new Scanner(new FileInputStream(fileName));
try{
while (scanner.hasNextLine()){
String currentLine = scanner.nextLine().trim();
if(currentLine.length() == 0){
continue;
}
else if(currentLine.charAt(0) == '%'){
break;
}
else if(currentLine.charAt(0) == 'c'){
//skip the comment line
continue;
}
else if(currentLine.charAt(0) == 'p'){
Content temp = parseProperties(currentLine);
result.numOfVars = temp.numOfVars;
result.numOfClauses = temp.numOfClauses;
}
else{
//parse the CNF
ArrayList<Integer> clause = parseLine(currentLine);
result.data.add(clause);
}
}
}
finally{
scanner.close();
}
}
catch(Exception e){
System.err.println(e.getMessage());
}
return result;
}
private Content parseProperties(String propertiesLine){
Content result = new Content();
String str = propertiesLine.trim();
int index = str.indexOf(' ');
//getting rid of 'p' at the beginning of the line;
str = str.substring(index + 1).trim();
index = str.indexOf(' ');
//getting rid of file format;
str = str.substring(index + 1).trim();
index = str.indexOf(' ');
result.numOfVars = new Integer(str.substring(0, index)).intValue();
str = str.substring(index + 1).trim();
result.numOfClauses = new Integer(str).intValue();
return result;
}
/**
* Parses a line corresponding to a CNF and returns a list of propositions
* in the CNF.
* @param line a line from the input file.
* @return a list of propositions.
*/
private static ArrayList<Integer> parseLine(String line){
ArrayList<Integer> result = new ArrayList<Integer>();
String str = line.trim();
int index = str.indexOf(' ');
while(index != -1){
result.add(new Integer(str.substring(0, index)));
str = str.substring(index + 1).trim();
index = str.indexOf(' ');
}
/*if(!str.equals("0")){
System.err.println("The CNF did not terminate with a 0.");
System.exit(0);
}*/
return result;
}
/**
* getter for content of this object
* @return the content
*/
public ArrayList<ArrayList<Integer>> getContent(){
return content;
}
public int getNumOfVars(){
return numOfVars;
}
public int getNumOfClauses(){
return numOfClauses;
}
public static void main(String[] args){
new ExampleLoader("/Users/Salman/Dropbox/PhD projects/programs/MinSolver/Examples/ex1/uf75-01.cnf");
}
}