-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #136 from cmu-phil/code-cleanup-2
Code cleanup 2
- Loading branch information
Showing
44 changed files
with
4,613 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
tetrad-cli/src/main/java/edu/cmu/tetrad/cli/ExtendedCommandLineParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright (C) 2016 University of Pittsburgh. | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | ||
* MA 02110-1301 USA | ||
*/ | ||
package edu.cmu.tetrad.cli; | ||
|
||
import org.apache.commons.cli.CommandLine; | ||
import org.apache.commons.cli.CommandLineParser; | ||
import org.apache.commons.cli.Option; | ||
import org.apache.commons.cli.Options; | ||
import org.apache.commons.cli.ParseException; | ||
|
||
/** | ||
* An extension of the CommandLineParser class. It provides additional | ||
* functionalities that are not avaliable in CommandLineParser class. | ||
* | ||
* Jan 5, 2016 12:42:38 PM | ||
* | ||
* @author Kevin V. Bui ([email protected]) | ||
*/ | ||
public class ExtendedCommandLineParser implements CommandLineParser { | ||
|
||
private final CommandLineParser commandLineParser; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param commandLineParser a command-line parser | ||
*/ | ||
public ExtendedCommandLineParser(CommandLineParser commandLineParser) { | ||
this.commandLineParser = commandLineParser; | ||
} | ||
|
||
/** | ||
* Query to see if the option is passed in from command-line. | ||
* | ||
* @param option option to search for | ||
* @param arguments inputs from the command-line | ||
* @return true if the option is passed in from command-line | ||
*/ | ||
public boolean hasOption(Option option, String[] arguments) { | ||
if (option == null || arguments == null) { | ||
return false; | ||
} | ||
|
||
for (String argument : arguments) { | ||
if (argument.startsWith("--")) { | ||
argument = argument.substring(2, argument.length()); | ||
if (argument.equals(option.getLongOpt())) { | ||
return true; | ||
} | ||
} else if (argument.startsWith("-")) { | ||
argument = argument.substring(1, argument.length()); | ||
if (argument.equals(option.getOpt())) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Parse the arguments according to the specified options. | ||
* | ||
* @param options the specified Options | ||
* @param arguments the command line arguments | ||
* @return the list of atomic option and value tokens | ||
* @throws ParseException whenever there are any problems encountered while | ||
* parsing the command line tokens | ||
*/ | ||
@Override | ||
public CommandLine parse(Options options, String[] arguments) throws ParseException { | ||
return this.commandLineParser.parse(options, arguments); | ||
} | ||
|
||
/** | ||
* Parse the arguments according to the specified options. | ||
* | ||
* @param options the specified Options | ||
* @param arguments the command line arguments | ||
* @param stopAtNonOption if true an unrecognized argument stops the parsing | ||
* and the remaining arguments are added to the CommandLines args list. If | ||
* false an unrecognized argument triggers a ParseException. | ||
* @return the list of atomic option and value tokens | ||
* @throws ParseException if there are any problems encountered while | ||
* parsing the command line tokens | ||
*/ | ||
@Override | ||
public CommandLine parse(Options options, String[] arguments, boolean stopAtNonOption) throws ParseException { | ||
return this.commandLineParser.parse(options, arguments, stopAtNonOption); | ||
} | ||
|
||
} |
77 changes: 77 additions & 0 deletions
77
tetrad-cli/src/main/java/edu/cmu/tetrad/cli/TetradCliApp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright (C) 2016 University of Pittsburgh. | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | ||
* MA 02110-1301 USA | ||
*/ | ||
package edu.cmu.tetrad.cli; | ||
|
||
import edu.cmu.tetrad.cli.search.FgsCli; | ||
import edu.cmu.tetrad.cli.util.Args; | ||
import org.apache.commons.cli.HelpFormatter; | ||
import org.apache.commons.cli.Option; | ||
import org.apache.commons.cli.Options; | ||
|
||
/** | ||
* | ||
* Feb 9, 2016 3:23:56 PM | ||
* | ||
* @author Kevin V. Bui ([email protected]) | ||
*/ | ||
public class TetradCliApp { | ||
|
||
private static final Options MAIN_OPTIONS = new Options(); | ||
|
||
static { | ||
Option requiredOption = new Option(null, "algorithm", true, "Algorithm name."); | ||
requiredOption.setRequired(true); | ||
MAIN_OPTIONS.addOption(requiredOption); | ||
} | ||
|
||
private static String algorithm; | ||
|
||
/** | ||
* @param args the command line arguments | ||
*/ | ||
public static void main(String[] args) { | ||
if (args == null || args.length == 0) { | ||
showHelp(); | ||
return; | ||
} | ||
|
||
algorithm = Args.getOptionValue(args, "algorithm"); | ||
if (algorithm == null) { | ||
showHelp(); | ||
} else { | ||
switch (algorithm) { | ||
case "fgs": | ||
FgsCli.main(Args.removeOption(args, "algorithm")); | ||
break; | ||
default: | ||
System.err.printf("Unknow algorithm: %s\n", algorithm); | ||
showHelp(); | ||
} | ||
} | ||
} | ||
|
||
private static void showHelp() { | ||
String cmdLineSyntax = "java -jar tetrad-cli.jar"; | ||
String header = ""; | ||
String footer = "algorithm: fgs"; | ||
HelpFormatter formatter = new HelpFormatter(); | ||
formatter.printHelp(cmdLineSyntax, header, MAIN_OPTIONS, footer); | ||
} | ||
|
||
} |
122 changes: 122 additions & 0 deletions
122
tetrad-cli/src/main/java/edu/cmu/tetrad/cli/data/AbstractDatasetReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* Copyright (C) 2016 University of Pittsburgh. | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | ||
* MA 02110-1301 USA | ||
*/ | ||
package edu.cmu.tetrad.cli.data; | ||
|
||
import java.io.IOException; | ||
import java.io.RandomAccessFile; | ||
import java.nio.MappedByteBuffer; | ||
import java.nio.channels.FileChannel; | ||
import java.nio.file.Path; | ||
|
||
/** | ||
* | ||
* Feb 3, 2016 11:52:42 AM | ||
* | ||
* @author Kevin V. Bui ([email protected]) | ||
*/ | ||
public abstract class AbstractDatasetReader implements DatasetReader { | ||
|
||
protected static final byte NEW_LINE = '\n'; | ||
|
||
protected static final byte CARRIAGE_RETURN = '\r'; | ||
|
||
protected static final byte DOUBLE_QUOTE = '"'; | ||
|
||
protected static final byte SINGLE_QUOTE = '\''; | ||
|
||
protected Path dataFile; | ||
protected char delimiter; | ||
|
||
protected int lineCount; | ||
protected int columnCount; | ||
|
||
public AbstractDatasetReader(Path dataFile, char delimiter) { | ||
this.dataFile = dataFile; | ||
this.delimiter = delimiter; | ||
this.lineCount = -1; | ||
this.columnCount = -1; | ||
} | ||
|
||
@Override | ||
public int countNumberOfColumns() throws IOException { | ||
if (columnCount == -1) { | ||
int count = 0; | ||
try (FileChannel fc = new RandomAccessFile(dataFile.toFile(), "r").getChannel()) { | ||
MappedByteBuffer buffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); | ||
byte currentChar = -1; | ||
byte prevChar = NEW_LINE; | ||
while (buffer.hasRemaining()) { | ||
currentChar = buffer.get(); | ||
if (currentChar == CARRIAGE_RETURN) { | ||
currentChar = NEW_LINE; | ||
} | ||
|
||
if (currentChar == delimiter || (currentChar == NEW_LINE && prevChar != NEW_LINE)) { | ||
count++; | ||
if (currentChar == NEW_LINE) { | ||
break; | ||
} | ||
} | ||
|
||
prevChar = currentChar; | ||
} | ||
|
||
// cases where file has no newline at the end of the file | ||
if (!(currentChar == -1 || currentChar == NEW_LINE)) { | ||
count++; | ||
} | ||
} | ||
columnCount = count; | ||
} | ||
|
||
return columnCount; | ||
} | ||
|
||
@Override | ||
public int countNumberOfLines() throws IOException { | ||
if (lineCount == -1) { | ||
int count = 0; | ||
try (FileChannel fc = new RandomAccessFile(dataFile.toFile(), "r").getChannel()) { | ||
MappedByteBuffer buffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); | ||
byte prevChar = NEW_LINE; | ||
while (buffer.hasRemaining()) { | ||
byte currentChar = buffer.get(); | ||
if (currentChar == CARRIAGE_RETURN) { | ||
currentChar = NEW_LINE; | ||
} | ||
|
||
if (currentChar == NEW_LINE && prevChar != NEW_LINE) { | ||
count++; | ||
} | ||
|
||
prevChar = currentChar; | ||
} | ||
|
||
// cases where file has no newline at the end of the file | ||
if (prevChar != NEW_LINE) { | ||
count++; | ||
} | ||
} | ||
lineCount = count; | ||
} | ||
|
||
return lineCount; | ||
} | ||
|
||
} |
Oops, something went wrong.