Skip to content

Commit

Permalink
implemented the methods in SBMLDocument that are dealing with validat…
Browse files Browse the repository at this point in the history
…ing the SBML model. Anyway, the overall checkConsistency is not working anymore but the code in SBMLDocument should be fine now + added a test to easily try to validate a model
  • Loading branch information
niko-rodrigue committed Feb 4, 2011
1 parent f274cae commit a096664
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 23 deletions.
96 changes: 74 additions & 22 deletions src/org/sbml/jsbml/SBMLDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@

package org.sbml.jsbml;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.xml.stream.XMLStreamException;

import org.sbml.jsbml.util.NotImplementedException;
import org.sbml.jsbml.util.StringTools;
import org.sbml.jsbml.validator.SBMLValidator;

/**
* Represents the 'sbml' root node of a SBML file.
Expand Down Expand Up @@ -76,7 +80,13 @@ public class SBMLDocument extends AbstractSBase {
* Memorizes all {@link SBMLError} when parsing the file containing this
* document.
*/
private List<SBMLError> listOfErrors;
private SBMLErrorLog listOfErrors;

/**
* Contains all the parameter to validate the SBML document
*/
private HashMap<String, String> checkConsistencyParameters = new HashMap<String, String>();

/**
* Represents the 'model' XML subnode of a SBML file.
*/
Expand Down Expand Up @@ -155,25 +165,58 @@ public void addNamespace(String namespaceName, String prefix, String URI) {
}

/**
* Not yet implemented!
*
* It is supposed to fill this {@link SBMLDocument}'s {@link #listOfErrors}
* Validates the {@link SBMLDocument} using the
* SBML.org online validator (http://sbml.org/validator/).
* <p>
* It will fill this {@link SBMLDocument}'s {@link #listOfErrors}
* with {@link SBMLError}s for each problem within this whole data
* structure. You will then be able to obtain this list by calling
* {@link #getError(int)} or {@link #getListOfErrors()}.
*
* @return
* @throws NotImplementedException
* Currently, this method is not implemented, so please catch a
* {@link NotImplementedException} in your program until we
* remove this error.
* @return the number of errors found
*/
public int checkConsistency() {
listOfErrors = getListOfErrors();
// TODO: IMPLEMENT!
throw new NotImplementedException();

File tmpFile = null;

try {
tmpFile = File.createTempFile("jsbml-", ".xml");
} catch (IOException e) {
e.printStackTrace();
}

try {
new SBMLWriter().writeSBML(this, tmpFile);
} catch (FileNotFoundException e) { // TODO : log the errors. Send an exception ??
e.printStackTrace();
return -1;
} catch (XMLStreamException e) {
e.printStackTrace();
return -1;
} catch (SBMLException e) {
e.printStackTrace();
return -1;
}

HashMap<String, String> parameters = new HashMap<String, String>();

// System.out.println("SBMLDocument.checkConsistency : tmp file = " + tmpFile.getAbsolutePath());

listOfErrors = SBMLValidator.checkConsistency(tmpFile.getAbsolutePath(), parameters);

return listOfErrors.getNumErrors();
}

public void printErrors() {
int nbErrors = listOfErrors.getNumErrors();

for (int i = 0; i < nbErrors; i++) {
System.out.println(listOfErrors.getError(i));
}
}



/*
* (non-Javadoc)
*
Expand Down Expand Up @@ -306,16 +349,15 @@ public String getElementName() {
}

/**
* Not yet implemented!
*
* @param i
* @return
* @return
*/
public SBMLError getError(int i) {
if (isSetListOfErrors()) {
return listOfErrors.get(i);
return listOfErrors.getError(i);
}
throw new IndexOutOfBoundsException(Integer.toString(i));
throw new IndexOutOfBoundsException(Integer.toString(i)); // TODO : test if the number is too low or too big
}

/**
Expand All @@ -324,13 +366,23 @@ public SBMLError getError(int i) {
*
* @return
*/
public List<SBMLError> getListOfErrors() {
public SBMLErrorLog getListOfErrors() {
if (listOfErrors == null) {
listOfErrors = new LinkedList<SBMLError>();
listOfErrors = new SBMLErrorLog();
}
return listOfErrors;
}

/**
* This method returns a collection of all {@link SBMLError}s reflecting
* problems in the overall data structure of this {@link SBMLDocument}.
*
* @return
*/
public SBMLErrorLog getErrorLog() {
return getListOfErrors();
}

/**
* Returns the model of this {@link SBMLDocument}.
*
Expand All @@ -345,7 +397,7 @@ public Model getModel() {
* @return
*/
public int getNumErrors() {
return isSetListOfErrors() ? listOfErrors.size() : 0;
return isSetListOfErrors() ? listOfErrors.getNumErrors() : 0;
}

/**
Expand All @@ -369,7 +421,7 @@ public Map<String, String> getSBMLDocumentNamespaces() {
* @return
*/
private boolean isSetListOfErrors() {
return (listOfErrors != null) && (listOfErrors.size() > 0);
return (listOfErrors != null) && (listOfErrors.getNumErrors() > 0);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/org/sbml/jsbml/validator/SBMLValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -341,13 +341,15 @@ public static SBMLErrorLog checkConsistency(String fileName,
String output = "xml";

parameters.put("output", output);
parameters.put("offcheck", "u");

// getting an XML output of the error log
// describe there :
// http://sbml.org/Facilities/Validator/Validator_Web_API
result = Validator.validateSBML(fileName, parameters);

// print(result, System.out);
// DEBUG
print(result, System.out);

XStream xstream = new XStream(new DomDriver()); // To parse XML
// using DOM
Expand Down
66 changes: 66 additions & 0 deletions test/org/sbml/jsbml/test/ValidateSBML.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.sbml.jsbml.test;

import java.io.File;
import java.io.FileNotFoundException;

import javax.xml.stream.XMLStreamException;

import org.sbml.jsbml.SBMLDocument;
import org.sbml.jsbml.SBMLReader;

public class ValidateSBML {

public static void main (String[] args) throws FileNotFoundException, XMLStreamException
{
if (args.length < 1)
{
System.out.println("Usage: java validateSBML filename");
System.exit(1);
}

String filename = args[0];
SBMLReader reader = new SBMLReader();
SBMLDocument document;
long start, stop;

start = System.currentTimeMillis();
document = reader.readSBML(filename);
stop = System.currentTimeMillis();

if (document.getNumErrors() > 0)
{
print("Encountered the following errors while reading the SBML file:\n");
document.printErrors();
print("\nFurther consistency checking and validation aborted.\n");
System.exit(1);
}
else
{
long errors = document.checkConsistency();
long size = new File(filename).length();

println(" filename: " + filename);
println(" file size: " + size);
println(" read time (ms): " + (stop - start));
println(" validation error(s): " + errors);

if (errors > 0)
{
document.printErrors();
System.exit(1);
}
}
}


static void print (String msg)
{
System.out.print(msg);
}

static void println (String msg)
{
System.out.println(msg);
}

}

0 comments on commit a096664

Please sign in to comment.