Wednesday, June 12, 2019

#4 Writing My First Constraints

The last week was quite interesting as I got a hang of writing my own constraints, and things started moving ahead smoothly, for most of it!

To make my work easier, I created a template of a Constraint Class, with all the documentation and the constructs common to all constraint classes. I pretty much just need to insert the class name at 2-3 odd places to make it ready for the actual checks.

JSBML (core) has a bunch of 'helper classes' which contain some frequently used validations. Most other validations are extensions of these helper functions, and sometimes, I use them directly.

I'll describe some of these classes briefly here:

1) DuplicatedElementValidationFunction
    This class is used to check that a child XML element was not present more than once.

2) InvalidAttributeValidationFunction
    This class is used to check if any invalid XML attributes were found in an element. The argument for the constructor of this class is the attribute name.

3) UnknownPackageAttributeValidationFunction
    This class is used to check if any unknown XML attributes from a specified package/namespace were found. The constructor for this class takes the package name as an argument. For spatial, we pass SpatialConstants.shortLabel (SpatialConstants is the class containing various constant strings related to the spatial package. The shortLabel constant contains the namespace string)

4) UnknownPackageElementValidationFunction
    This class is used to check if any unknown XML elements from a specified package/namespace were found. The argument for the constructor is the same as the previous class.

5) UnknownCoreAttributeValidationFunction
    This class used to check if any unknown XML attributes from SBML core were found.

6) UnknownCoreElementValidationFunction
    This class used to check if any unknown XML elements from SBML core were found.

I had studied the various packages for which constraints had already been implemented, and that helped me to figure out in advance on how to use these classes.

Now I'll come to the modifications that were required in the existing code of spatial.

The first major change had to be made to the SpatialParser.java . In the method processStartElement, every time we got a match for the contextObject as an instanceof some class, I needed to add the element being read to a list which would be utilised in later validations by classes such the DuplicatedElementValidationFunction. The classes would then be required only to get the data from this list. This had to be done by using the AbstractReaderWriter class and call its method storeElementsOrder(). Also, if we did not get a match for any element after the previous check, I had to invoke the processUnknownElement() method of the AbstractReaderWriter to handle the unknown element.

The next change was in StringTools.java from JSBML Core. I had to add a method to parse a string as double and throw an exception if it was not able to. The existing method did not throw an exception, and only registered a warning in the logger.

There is one change which I have to make to the source class (for which I am writing the constraint) of every constraint class. In the readAttribute() method of the class, I need to add AbstractReaderWriter.processInvalidAttribute() wherever we have a try/catch construct to assign a value to an attribute. This must be done to handle invalid attributes as we encounter them.

This was pretty much what I explored and worked on in the past week. I have implemented constraints for around 13 classes successfully by now!
I am experiencing some issues in 1 or 2 odd classes, which I shall discuss once I solve them!

Till next time,
Cheers! :-D

No comments:

Post a Comment