time taken to parse and simplyify a CNF file - java

I have just starting to use the Sat4j libraries. Can you direct me how to calculate the time taken to parse and simplify a given CNF input.
i have used
ISolver solver = SolverFactory.newDefault();
Reader reader = new DimacsReader(solver);
IProblem problem = reader.parseInstance(filename.cnf);
boolean check=problem.isSatisfiable();
i wish to calculate the time taken by the reader to parse and isSatisfiable.
if possible kindly direct me to find the information in the image for every cnf file processed
screenshot of the details i look to collect using sat4j lib
Thank you for your time in advance.

I try to embedded your source code in an old fashion way to get the execution time.
Date startDate = Calendar.getInstance().getTime();
long d_StartTime = new Date().getTime();
ISolver solver = SolverFactory.newDefault();
Reader reader = new DimacsReader(solver);
IProblem problem = reader.parseInstance(filename.cnf);
Date endDate = Calendar.getInstance().getTime();
long d_endTime = new Date().getTime();
System.out.format("Milli = %s, ( D_Start : %s, D_End : %s ) \n", (d_endTime - d_StartTime),d_StartTime, d_endTime);
boolean check=problem.isSatisfiable();
With this, the time in milliseconds to parse the instance will appear :).
I hope this help !

Related

Android: PrintStream.println(String) to .txt file contains <0x00> in java

I am trying to write some data to txt file using PrintStream.println. Some times it is working fine, But some times it is crashing the application. When i saw the result file, It contains <0x00> very huge amount of time. After that nothing printed in file.
Please find the below used code.
SimpleDateFormat sdf = new SimpleDateFormat("[MM/dd HH:mm:ss:SSS] ")
Date date_value = new Date();
String currentDateandTime = sdf.format(date_value);
FileOutputStream fileOutputStream = new FileOutputStream("fileName", true);
PrintStream plt_cbl_stream = new PrintStream(fileOutputStream);
plt_cbl_stream.println(currentDateandTime);
plt_cbl_stream.flush();
Can some one suggest me, What is the issue. Please find the details captured from created file.
Log captured from created file :
[20/01 15:30:24:865]
[20/01 15:30:24:8<0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00>68]
[20/01 15:30:24:872]
[20/01 15:30:24:877]
[20/01 15:30:24:892]
<0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00><0x00>

Keep checking for a text in a file until its found or a certain timeout

I need to check for a error log file for certain time after starting a process. Either the word found or the timeout reached, I need to exit informing the text is found or Text is not found until the timeout. I tried like below but couldnt achieve
public void waitFortext(String expectedText,
String filePath){
long timeout = 50000 + System.currentTimeMillis();
File file = new File(filePath);
String content = FileUtils.readFileToString(file, "UTF-8");
boolean available = false;
while (available || System.currentTimeMillis() > timeout) {
available = content.contains(expectedText);
Thread.sleep(500);
if (available) {
return;
}
}
}`
Make a variable oldTime and set it to System.nanoTime when you want the time to start. Make a variable newTime and update it to System.nanoTime in every time the code loops. Compare the difference of these two values to your wanted amount of time, exiting the loop when the difference is greater.
The problem here is you read file only once.
Move line
String content = FileUtils.readFileToString(file, "UTF-8");
inside loop (make it first statement.

Java scan a log file and get the time information then calculate the elapsed time

I have a txt file with a line like below:
0 Apr 12 08:42:44.000009 (+0.000009) *** START ***
The information I want to get is:
Apr 12 08:42:44
The current method I'm using use is using a scanner to read this line:
public void getTime() throws IOException {
String time = "";
Scanner scan = new Scanner(location);
String firstLine = scan.nextLine();
String[] splitString = firstLine.split("\\.");
String[] rebootTime = splitString[0].split(" ");
for(int i = 0; i < rebootTime.length; i++) {
if(i != 0) {
time = time + rebootTime[i] + " ";
}
}
System.out.println(time);
}
Is there a smarter way to get the time information?
After I get the time, how do I transfer it to a date format then calculate the duration?
I'm trying to use JAVA 8 Instant with this method, how can I transfer the time value to a Instant type?
If I understand you properly your goal is to extract reboot time from each string of a log file. Using Scanner is ok to my mind. After extracting a line from log file you might as well use regular expressions on it, like this:
String firstLine = scan.nextLine();
Pattern pattern = Pattern.compile("[a-zA-Z]{3}\\s\\d{2}\\s\\d{2}:\\d{2}:\\d{2}");
Matcher matcher = pattern.matcher(firstLine);
if (matcher.find()) {
String rebootTime = matcher.group();
}
This regexp is not perfect but it works on your line. You can make it more or less strict.
As to formatting the string to a LocalDateTime, you can use following method:
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendPattern("MMM dd HH:mm:ss")
.parseDefaulting(ChronoField.YEAR, 1)
.toFormatter();
LocalDateTime date = LocalDateTime.parse(rebootTime, formatter);
So parseDefaulting(ChronoField.YEAR, 1) means that you ignore year in string and set it as 1 in resulting LocalDateTime. After that you can calculate durations using LocalDateTimes.
I like Mongwo's elegant solution.
There are many ways to skin this cat. Other than regular expression, you can simply use a quick-n-dirty one liner, if it is in fixed length and always starting from the fixed index of a string:
String rawStr = "0 Apr 12 08:42:44.000009 (+0.000009) *** START ***";
System.out.println(rawStr.substring(5, 20));
If the file is small enough that you are ok with reading the whole thing, it is generated by another process so that you can guarantee the format, and the line you want is first (which from the question I think all above things should be true), your solution can be as simple as
private SimpleDateFormat format = new SimpleDateFormat("MMM dd HH:mm:ss");
public Date read(String filePath) throws URISyntaxException, IOException, ParseException {
Path fileLocation = Paths.get(filePath);
byte[] data = Files.readAllBytes(fileLocation);
return format.parse(new String(data, 5, 15));
}
If the file is longer, you may want to use a scanner if you dont want to read the whole thing, but imho it is still simplest to use indices to get the part of the string that you want.
If you want a very elegant solution maybe you could use a regex, but I really dont think there is much of a need.
My try here is:-
First let's extract value before dot(".") and then value after double space(" ").
public static void main(String[] args) {
String str = "0 Apr 12 08:42:44.000009 (+0.000009) *** START ***";
String[] str1 = str.split("\\.");
String[] str2 = str1[0].split("\\s{2,}");
System.out.println((str2[1]));
}
To understand \\s{2,} you can look into saved regex.
regex to match 2 spaces

Decorating date formats

I'm looking for a way to parse a string which would replace some patterns with components of a date that I provide.
Do you know of a standard way to do this?
One usage would be:
parseForDate("fileName%YYYY%MM.csv", new Date()); // returns: filename201301.csv
Best regards
Consider using format:
String filename = String.format("fileName%1$tY%1$tm.csv", new Date());
String filename = "fileName%" + new SimpleDateFormat("yyyy%MM").format(new Date())+".csv"; // returns: filename201301.csv

how to input invalid formula in Excel using Java

I'm currently making a code that creates an Excel using Java jxl. I've encountered no problem as long as the formula I'm using exists. The problem is, I need to input a formula which excel will not be able to find because we use a special excel to read it. I need my code to retain the invalid formula instead of writing =1 ERROR() on the cell so that when my file is opened using our special excel, it gets the value. How do I go about this?
Here is my code:
WorkbookSettings ws = new WorkbookSettings();
ws.setLocale(new Locale("en", "EN"));
workbook = Workbook.createWorkbook(file, ws);
WritableSheet s = workbook.createSheet("Sheet1", 0);
WritableFont wf = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD);
DateFormat dateFormat = new DateFormat("M/d/yyyy H:mm");
NumberFormat dp = new NumberFormat("#");
NumberFormat dp2 = new NumberFormat("#.##");
ResultSetMetaData rsmd = rs.getMetaData();
int nRow = 0;
Label l = null;
Formula f = null;
for(int index = 1; index <= rsmd.getColumnCount(); index++) {
l = new Label(index-1,nRow,rsmd.getColumnName(index),new WritableCellFormat(wf));
s.addCell(l);
}
nRow++;
while(rs.next()) {
String valueDate = VerifyNull.ifNull(rs.getString(1), Constant.BLANK);
f = new Formula(0,nRow,valueDate,new WritableCellFormat(dp));
s.addCell(f);
String rateDate = VerifyNull.ifNull(rs.getString(2), Constant.BLANK);
f = new Formula(1,nRow,rateDate,new WritableCellFormat(dp));
s.addCell(f);
String market = VerifyNull.ifNull(rs.getString(3), Constant.BLANK);
l = new Label(3,nRow,market,new WritableCellFormat(wf));
s.addCell(l);
String ccy = VerifyNull.ifNull(rs.getString(4), Constant.BLANK);
l = new Label(4,nRow,ccy,new WritableCellFormat(wf));
s.addCell(l);
String label = VerifyNull.ifNull(rs.getString(5), Constant.BLANK);
l = new Label(5,nRow,label,new WritableCellFormat(wf));
s.addCell(l);
String quoteDate = VerifyNull.ifNull(rs.getString(6), Constant.BLANK);
f = new Formula(1,nRow,quoteDate,new WritableCellFormat(dp));
s.addCell(f);
String bid = "="+VerifyNull.ifNull(rs.getString(7), Constant.BLANK);
/** The value in the bid = "BDP("EURON Curncy","PX_BID","DATA")1"; */
f = new Formula(6,nRow,bid,new WritableCellFormat(dp2));
s.addCell(f);
/** The value in the offer = "BDP("EURON Curncy","PX_ASK","DATA")1"; */
String offer = "="+VerifyNull.ifNull(rs.getString(8), Constant.BLANK);
f = new Formula(7,nRow,offer,new WritableCellFormat(dp2));
s.addCell(f);
String timeStamp = VerifyNull.ifNull(rs.getString(9), Constant.BLANK);
f = new Formula(8,nRow,timeStamp,new WritableCellFormat(dateFormat));
s.addCell(f);
nRow++;
}
workbook.write();
workbook.close();
I supose that your "special excel" is that you have a plug in with visual basic functions to use whithin cell formulas
I dont know if the current version of JXL support macros or at least don't delete it when you modify an existent Workbook, but in your code your are not updating an existing Workbook, your are creating a new one
have you tried to use your code opening an existing workbook in which the visual basic function works? (it can be an empty one). If it works, you can use that file as a template, copy it to the location you want, rename it, and edit it with JXL
Sorry, I only have time for a quick reply, but have you looked at Apache POI at all? Its API might be of use to you.
However, I'm not 100% sure this is for writing Excel as I think its primary use is reading MS Office files. Still, might be worth a look though.
The problem is, I need to input a formula which excel will not be able to find because we use a special excel to read it. I need my code to retain the invalid formula instead of writing =1 ERROR() on the cell so that when my file is opened using our special excel, it gets the value.
Your problem isn't JExcel or POI. No API will be able to help you with a formula that it cannot be found.
What is this "special excel"? If it's another file, simply read it in and use it.
Your question is confusing. I'm having a hard time understanding what exactly is going on. I'm voting to close unless you can clarify.

Categories