Skip to content

Commit

Permalink
Add a custom progress bar to monitor materialization process in console.
Browse files Browse the repository at this point in the history
  • Loading branch information
johardi committed Feb 25, 2014
1 parent 79a868e commit 452c8c6
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 3 deletions.
107 changes: 107 additions & 0 deletions src/com/obidea/semantika/cli/ConsoleProgressBar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package com.obidea.semantika.cli;

import java.text.DecimalFormat;

import com.obidea.semantika.materializer.IProgressMonitor;

public class ConsoleProgressBar implements IProgressMonitor
{
private static final int PROGRESS_LENGTH = 70;
private static final int REFRESH_RATE = 500; // in milliseconds

private int mMax = 0;
private int mCurrent = 0;
private int mProduced = 0;
private int mTotalProduced = 0;
private long mStart = 0;
private long mLastUpdate = 0;

private static DecimalFormat mOutputTriplesFormat = new DecimalFormat("###,###,###,###"); //$NON-NLS-1$
private static DecimalFormat mThroughputFormat = new DecimalFormat("###,###,###"); //$NON-NLS-1$

public ConsoleProgressBar()
{
// NO-OP
}

@Override
public void start(int max)
{
reset();
mMax = max;
mStart = System.currentTimeMillis();
}

protected void reset()
{
mMax = 0;
mCurrent = 0;
mProduced = 0;
mTotalProduced = 0;
mStart = 0;
mLastUpdate = 0;
}

@Override
public void advanced()
{
// NO-OP
}

@Override
public void advanced(int value)
{
mCurrent++;
mProduced = value;
mTotalProduced += mProduced;
if ((System.currentTimeMillis() - mLastUpdate) > REFRESH_RATE) {
mLastUpdate = System.currentTimeMillis();
printBar(false);
}
}

@Override
public void finish()
{
mCurrent = mMax;
printBar(true);
}

private void printBar(boolean finished)
{
double numbar = Math.floor(PROGRESS_LENGTH * (double) mCurrent / (double) mMax);
String strbar = ""; //$NON-NLS-1$
int ii = 0;
for (ii = 0; ii < numbar; ii++) {
strbar += "="; //$NON-NLS-1$
}
strbar += ">"; //$NON-NLS-1$
for (ii = (int) numbar; ii < PROGRESS_LENGTH; ii++) {
strbar += " "; //$NON-NLS-1$
}

int percent = (int) Math.floor(100 * (double) mCurrent / (double) mMax);
long elapsed = (System.currentTimeMillis() - mStart);
int throughput = 0;
if (elapsed > 0) {
throughput = (int) (mTotalProduced / (double) (elapsed / 1000));
}

String strtriples = mOutputTriplesFormat.format(mTotalProduced);
String strthroughput = mThroughputFormat.format(throughput);
String strend = String.format("%-15s (%sT/s)", strtriples, strthroughput); //$NON-NLS-1$
String outputMsg = String.format("%3d%%[%s] %s", percent, strbar, strend); //$NON-NLS-1$
System.out.print(outputMsg);

if (finished) {
int seconds = (int) (elapsed / 1000) % 60;
int minutes = (int) (elapsed / 1000) / 60;
String finishMsg = String.format("%02d:%02d (%sT/s) | %s Triples - Done.", minutes, seconds, strthroughput, strtriples); //$NON-NLS-1$
System.out.print("\n"); //$NON-NLS-1$
System.out.println(finishMsg);
}
else {
System.out.print("\r"); //$NON-NLS-1$
}
}
}
6 changes: 3 additions & 3 deletions src/com/obidea/semantika/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
@SuppressWarnings("static-access")
public class Main
{
private static final String VERSION_NUMBER = "1.0.1"; //$NON-NLS-1$
private static final String SEMANTIKA_CORE_VERSION_NUMBER = "1.0"; //$NON-NLS-1$
private static final String VERSION_NUMBER = "1.1"; //$NON-NLS-1$
private static final String SEMANTIKA_CORE_VERSION_NUMBER = "1.1"; //$NON-NLS-1$

private static Options sOptions = new Options();
static {
Expand Down Expand Up @@ -193,7 +193,7 @@ private static IQueryResult evaluateQuery(String sparql, IQueryEngine engine, in
private static void materialize(IMaterializerEngine engine, File fout) throws MaterializerEngineException, MaterializationException
{
engine.start();
engine.materialize(fout);
engine.materialize(fout, new ConsoleProgressBar());
engine.stop();
}

Expand Down

0 comments on commit 452c8c6

Please sign in to comment.