Research Article

An Algorithm for Generating Boolean Expressions in VHDL Based on Ladder Diagrams

Algorithm 4

The AND-OR tree construction algorithm.
(a)  Function-2: ConstructAndOrTree()
Function-2: ConstructAndOrTree
Input: first node of a rung named “nd” and a dictionary typed Dictionary<Node, List<Node>> named “dic”
Output: an And-Or tree
childrenList new List<Node>()
ConstructAndOrTreeHelper(nd, dic, childrenList)
if (childrenList.Count > 1)
   andNode new AndNode();
   andNode.preNodes childrenList.preNodes
   andNode.postNodes childrenListchildrenList.Count − 1.postNodes
   andNode.children.Add(childrenList)
   Update postNodes list of node of which postNodes contains nd
   Update preNodes list of node of which preNodes contains nd
   Remove nodes included in childrenList in the rung
   Update values which contain nd in the dic
end // if
(b)  ConstructAndOrTreeHelper(), called by ConstructAndOrTree()
Function-3: ConstructAndOrTreeHelper
Input: a node typed Node named “node”, a dictionary typed Dictionary<Node, List<Node>> named “dic”, a list typed
List<Node> named “childrenList”
childrenList.add(node)
if (node.postNodes.count == 0)
  Return
end // if
if (node.postNodes.count == 1)
   postNode node.postNodes
  if (postNode.preNodes.count == 1)
   ConstructAndOrTreeHelper(postNode, dic, childrenList)
  End
  Else
   insert node into dic[postNode]
  end
else
  subDic new Dictionary<Node, List<Node>>()
  for currentNode: node.postNodes
   ConstructAndOrTree(currentNode, subDic)
  end // for
   while (subDic.Key.count > 1)
  begin
    for keyValuePair: subDic.keyValuePairs
    begin
     if (keyValuePair.Key.preNodes.count == keyValuePair.Value.count)
      orNode ConstructOrNode(keyValuePair, node)
      remove keyValuePair in the subDic
      ConstructAndOrTree(orNode, subDic)
   end // for
  end // while
  orNode ConstructOrNode(keyValuePair which is the only item in the subDic, node)
  ConstructAndOrTreeHelper(orNode, dic, childrenList)
(c)  ConstructOrNode(), called by ConstructAndOrTreeHelper()
Function-4: ConstructOrNode
Input: a key-value-pair named “kvp” of which key is the successor node of the constructed orNode and value is the children
of the orNode, the predecessor node named “nd” of the orNode
Output: an orNode
orNode new OrNode()
orNode.children kvp.value
orNode.preNodes nd
orNode.postNodes kvp.key
delete elements included in kvp.value in kvp.key.preNodes
kvp.key.preNodes.Add(orNodes)
delete elements included in kvp.value in nd.postNodes
nd.postNodes.append(orNode)
delete elements includes in kvp.value in ladderdiagram and insert orNode into ladderdiagram
return orNode