This is a short blog post where I will explain how to call SPMF as an external program from another Java program to execute an algorithm.
Before we start, it should be said that there are multiple ways to use SPMF. It can be used as a standalone program with a graphical user interface and from the command line. Moreover, the code of SPMF can be directly integrated in other Java programs, and SPMF can also be called using unofficial wrappers from other languages such as Python and R.
If you want to use SPMF from a Java program, it can be desirable in some cases to call SPMF using its command line interface rather than integrating the code of SPMF directly in your Java program. The adavantage is that SPMF is then executed as a separated process on your computer and it may be easier to maintain. How to do this?
First, you should download spmf.jar from the download page of the SPMF website and put it in the same folder as your Java program.
Second, you should lookup which algorithm you want to use in the documentation webpage of the SPMF website. For this example, lets say that we want to call the Apriori algorithm on a file called “contextPasquier99.txt” with the parameter minsup = 40% and save the result in a file “output.txt“.
According to the documentation of Apriori, we should write a command like this to execute it from the command line:
java -jar spmf.jar run Apriori contextPasquier99.txt output.txt 40%
From Java code, we can write do like this:
List commandWithParameters = new ArrayList(); commandWithParameters.add("java"); commandWithParameters.add("-jar"); commandWithParameters.add("spmf.jar"); commandWithParameters.add("run"); commandWithParameters.add("Apriori"); // Algorithm name commandWithParameters.add("contextPasquier99.txt"); // input file commandWithParameters.add("output.txt"); // output file commandWithParameters.add("0.5%"); // parameter ProcessBuilder pb = new ProcessBuilder(commandWithParameters); pb.redirectOutput(Redirect.INHERIT); // This will redirect the output of SPMF to the console Process process = pb.start(); // Run SPMF in a separated process
Running this code, will execute the Apriori algorithm and write some statistics in the console:
============= APRIORI - STATS ============= Candidates count : 11 The algorithm stopped at size 4 Frequent itemsets count : 9 Maximum memory usage : 7.322685241699219 mb Total time ~ 0 ms ===================================================
If you want your Java program to wait for the completion of SPMF before continuing, you can add this line:
int exitValue = process.waitFor();
It is also possible to stop the process using this:
That is all for today!