Showing posts with label JSBML isStrict(). Show all posts
Showing posts with label JSBML isStrict(). Show all posts

Monday, June 30, 2014

Strictness, reduceToBinary() and ASTFactory class

Since I directed most of my attention towards ASTFactory last week, it's the one class that I've made the most progress in and as of right now it looks to be pretty much complete. There are a few things I need to verify with my mentors but I am hoping that these will be trivial and will be dealt with in a timely manner.

The newest and arguably most important addition to the ASTFactory class thus far, is the reduceToBinary() method. It is ninety-nine percent complete. I was able to work out a general algorithm, both through simple reasoning and with the assistance of the old code. The particulars of what types to deal with (and how to deal with them) still has not been finalized however. I will have more on this later.

So the general algorithm for reduceToBinary() is very simple. For a tree T, all one has to do is to traverse it node by node starting from T.root all the way down to the terminal leaf nodes. For each node x in the tree two things have to be ensured:
  1. x has only two children
  2. Every one of x's children has two children. Hence, recursion -> reduceToBinary(x.child)
To accomplish (1), all the children in x except for a single one have to be removed. Afterwards, a new ASTFunction node (y) has to be created. The children that previously had been immured in x then have to be added to y. Finally, y has to be added as a child of x. This final action ensures that the node x has only two children.

(2) requires recursion.

ASTFunction nodes are the only types of nodes in the library that have children. They are therefore the most sensible, and most general parameter to use for reduceToBinary(). This poses a difficulty, however, when you consider the children of the node. ASTFunction nodes store children in the most general type possible (ASTNode2). This is to accommodate both ASTFunction nodes (which have children) and ASTNumber nodes (which do not have children). In order to perform recursion, an instanceof check has to be performed for every single one of these nodes. Once it is verified that a node is indeed an ASTFunction node, then reduceToBinary() will be called on it. If a node is not an ASTFunction node, then it's a leaf node. Calling reduceToBinary() on it would therefore be unnecessary / redundant.

In addition to completing all the methods in ASTFactory, I also added new strictness conditional checks for both ASTBinaryFunctionNode and ASTUnaryFunctionNode. When strictness of either type of node is set to false, they will both behave exactly like Nary nodes. When it is set to false however, child capacity will be limited with methods like addChild(), insertChild() and prependChild() throwing exceptions indicating that the child limit has been exceeded (2 for ASTBinaryFunction & 1 for ASTUnaryFunction).

The only child related method for which strictness is not being enforced currently is swapChildren(). This is perhaps due to the many complications that would inevitably arise when one tried to swap children for nodes whose strictness differs. If a strict node and a non-strict node swapped children would the non-strict node be turned to a strict-node and vice versa? Let's suppose that this is not so, how would you deal with the resulting conflicts? What if a strict ASTBinaryFunctionNode (x) is swapping children with a strict ASTUnaryFunctionNode (y), would x discard one child and assimilate another? Would y discard its only child and assimilate one of x's children? Or alternatively, would it be preferable if swapping did not occur and an exception was thrown? These are questions which I am not in a position to answer conclusively at the moment, hence my hesitation with implementing swapChildren().