Skip to content

Commit

Permalink
Using the CDK Validator now
Browse files Browse the repository at this point in the history
  • Loading branch information
egonw committed Mar 23, 2011
1 parent 35295bb commit ba75780
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 6 deletions.
Binary file added jar/cdk-extra.jar
Binary file not shown.
8 changes: 4 additions & 4 deletions src/com/github/egonw/moldbvalid/ErrorHandler.java
Expand Up @@ -12,28 +12,28 @@ public ErrorHandler(IReportHandler report) {
}

public void handleError(String message) {
report.handleError(":error", message);
report.handleError(":formatError", message);
};

public void handleError(String message,
Exception exception)
{
report.handleError(":error", message + "\n -> " +
report.handleError(":formatError", message + "\n -> " +
exception.getMessage());
};

public void handleError(String message,
int row, int colStart, int colEnd)
{
report.handleError(":error", "location: " + row + ", " +
report.handleError(":formatError", "location: " + row + ", " +
colStart + "-" + colEnd + ": " + message);
};

public void handleError(String message,
int row, int colStart, int colEnd,
Exception exception)
{
report.handleError(":error", "location: " + row + ", " +
report.handleError(":formatError", "location: " + row + ", " +
colStart + "-" + colEnd + ": " + message + " -> " +
exception.getMessage());
};
Expand Down
2 changes: 2 additions & 0 deletions src/com/github/egonw/moldbvalid/IReportHandler.java
Expand Up @@ -2,6 +2,8 @@

public interface IReportHandler {

public void setFile(String filename);

public void setSubject(String subject);

public void handleError(String type, String error);
Expand Down
10 changes: 9 additions & 1 deletion src/com/github/egonw/moldbvalid/RDFN3Handler.java
Expand Up @@ -6,7 +6,8 @@ public class RDFN3Handler implements IReportHandler {

private PrintStream output;

private String currentSubject = ":1";
private int counter = 0;
private String currentSubject = "";

public RDFN3Handler(OutputStream output) {
if (output instanceof PrintStream) {
Expand All @@ -16,8 +17,15 @@ public RDFN3Handler(OutputStream output) {
}
}

public void setFile(String filename) {
output.println(":file rdfs:label \"" + filename + "\" .");
}

public void setSubject(String subject) {
this.counter++;
this.currentSubject = subject;
output.println(":file :describes :mol" + counter + " .");
output.println(":mol" + counter + " rdfs:label \"" + subject + "\" .");
};

public void handleError(String type, String error) {
Expand Down
52 changes: 51 additions & 1 deletion src/com/github/egonw/moldbvalid/Validate.java
Expand Up @@ -4,20 +4,59 @@
import org.openscience.cdk.io.*;
import org.openscience.cdk.io.iterator.*;
import org.openscience.cdk.*;
import org.openscience.cdk.atomtype.*;
import org.openscience.cdk.exception.*;
import org.openscience.cdk.nonotify.*;
import org.openscience.cdk.io.IChemObjectReader.Mode;
import org.openscience.cdk.validate.*;
import java.io.*;

public class Validate {

IReportHandler handler;
ValidatorEngine validator;

public Validate(IReportHandler handler) {
this.handler = handler;
this.validator = new ValidatorEngine();
this.validator.addValidator(new CDKValidator());
this.validator.addValidator(new BasicValidator());
this.validator.addValidator(new AbstractValidator() {
CDKAtomTypeMatcher matcher = CDKAtomTypeMatcher.getInstance(
DefaultChemObjectBuilder.getInstance()
);
public ValidationReport validateMolecule(IMolecule molecule) {
ValidationReport report = new ValidationReport();
try {
IAtomType[] types = matcher.findMatchingAtomType(molecule);
for (int i=0; i<types.length; i++) {
ValidationTest atError = new ValidationTest(
molecule.getAtom(i),
"The atom's type is not recognized."
);
System.out.println("type: " + types[i].getAtomTypeName());
if (types[i] == null) {
report.addError(atError);
} else {
report.addError(atError);
}
}
} catch (Exception exception) {
ValidationTest test = new ValidationTest(
molecule,
"Error while atom typing of molecule."
);
test.setDetails(exception.getMessage());
report.addCDKError(test);
}
return report;
}
});
}

public void validateFile(String fileName) throws Exception {
ErrorHandler errorHandler = new ErrorHandler(this.handler);
handler.setFile(fileName);

IteratingMDLReader iterator = new IteratingMDLReader(
new FileReader(new File(fileName)),
Expand All @@ -28,7 +67,18 @@ public void validateFile(String fileName) throws Exception {

while (iterator.hasNext()) {
IMolecule mol = (IMolecule)iterator.next();
System.out.println("" + mol.getProperty(CDKConstants.TITLE));
handler.setSubject("" + mol.getProperty(CDKConstants.TITLE));
System.out.println("mol: " + mol.getProperty(CDKConstants.TITLE));
ValidationReport report = validator.validateMolecule(mol);
}
}

private void process(IReportHandler handler, ValidationReport report) {
for (ValidationTest test : report.getErrors())
handler.handleError(":error", test.getError());
for (ValidationTest test : report.getWarnings())
handler.handleError(":warning", test.getError());
for (ValidationTest test : report.getCDKErrors())
handler.handleError(":cdkError", test.getError());
}
}

0 comments on commit ba75780

Please sign in to comment.