Skip to content

Commit

Permalink
Merge pull request #136 from cmu-phil/code-cleanup-2
Browse files Browse the repository at this point in the history
Code cleanup 2
  • Loading branch information
jdramsey committed Feb 15, 2016
2 parents ba26d8a + 6080bdb commit 0d68767
Show file tree
Hide file tree
Showing 44 changed files with 4,613 additions and 187 deletions.
66 changes: 64 additions & 2 deletions tetrad-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,65 @@
<artifactId>tetrad-cli</artifactId>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- required to override vendor standard -->
<extension>true</extension>
<schemaFiles>graphml.xsd</schemaFiles>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<mainClass>edu.cmu.tetrad.cli.TetradCliApp</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>edu.cmu</groupId>
Expand All @@ -23,10 +79,16 @@
<artifactId>commons-cli</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
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 tetrad-cli/src/main/java/edu/cmu/tetrad/cli/TetradCliApp.java
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);
}

}
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;
}

}
Loading

0 comments on commit 0d68767

Please sign in to comment.