Example 4: Node naming scheme for OOBNs
When creating a runtime domain from a Class, all nodes are named by concatenating the names of the nodes in the list of source nodes (see Node.GetSource()) using a dot character ('.') as separator. This naming scheme makes it easy to work with the nodes in the runtime domain, as one can use the dot naming convention when calling NetworkModel.GetNodeByName(String).
(A detailed explanation for the naming convention is given in the Hugin API Reference Manual, section 3.10: Creating a runtime domain).
Example OOBN:
The node names in the runtime domain for the example OOBN will look like:
B nested_2.C nested_2.B nested_1.C nested_1.B C A
Please note that dots are only allowed for node names in the runtime domains, and not in the classes themselves. This way potential name clashing is effectively avoided, when the runtime domain is constructed.
The following example loads an OOBN, writes out the names of the nodes in the classes, creates a runtime domain, and writes out the names of the nodes in the runtime domain.
# if X64
using size_t = System.UInt64;
using h_index_t = System.Int64;
# else
using size_t = System.UInt32;
using h_index_t = System.Int32;
# endif
# if H_DOUBLE
using h_number_t = System.Double;
# else
using h_number_t = System.Single;
# endif
using System;
using HAPI;
namespace Example
{
public class NodeNaming
{
// The HUGIN GUI saves classes with an ".oobn" extension.
// The OOBNParseListener tries to load a nested class by
// looking for the class in a file with the name className + ".oobn"
public class OOBNParseListener : ClassParseListener
{
public void ParseError(size_t position, String msg)
{
Console.Error.WriteLine("Parse error location: "
+ position + " : " + msg);
}
public void InsertClass(String className, ClassCollection cc)
{
try
{
cc.ParseClasses(className + ".oobn", this);
}
catch (ExceptionHugin e)
{
Console.Error.WriteLine("Parsing failed: " + e.Message);
}
}
}
// Print the name of each node in the list.
public static void PrintNodes(NodeList list)
{
foreach (Node n in list)
Console.Write(n.GetName() + " ");
Console.WriteLine();
}
// Load a Hugin Object Oriented Bayesian Network, unfold classes
// to a domain and print out the node names.
public static int Main(String[] args)
{
if (args.Length < 1)
{
Console.WriteLine("Usage: NodeNaming <classname>");
Console.WriteLine("(the class <classname> is located in the file \"<classname>.oobn\")");
return -1;
}
try
{
String className = args[0];
ClassCollection cc = new ClassCollection();
Console.WriteLine("Parsing classes..");
cc.ParseClasses(className + ".oobn", new OOBNParseListener());
Class myClass = cc.GetClassByName(className);
if (myClass == null)
{
Console.Error.WriteLine("Class is NULL");
return -1;
}
else
{
foreach (Class c in cc.GetMembers()) {
Console.WriteLine("Nodes in class: " + c.GetName());
PrintNodes(c.GetNodes());
}
Console.WriteLine("\nNodes in runtime domain:");
Domain dom = myClass.CreateDomain();
PrintNodes(dom.GetNodes());
dom.Delete();
cc.Delete();
}
}
catch (ExceptionHugin e)
{
Console.WriteLine(e);
return -1;
}
return 0;
}
}
}