Hi all,
I really like code snippets and usage examples when I'm trying to learn a new API. So in case it is useful to others, here is the code for a minimal Scala app to load an object oriented network from a set of oobn files in a given working directory.
Warning: first attempt so beware of bugs.
Michael
package fdr.bn
import COM.hugin.HAPI.ClassCollection
import COM.hugin.HAPI.ClassParseListener
import COM.hugin.HAPI.Domain
import COM.hugin.HAPI.ExceptionHugin
import COM.hugin.HAPI.Node
/** Minimal test app to load an OO network. */
object TestApp extends App {
val workingDir = "c:/michael/hugin/api-testing"
val topClass = "model"
OOBNLoader.load(workingDir, topClass) match {
case Some(domain) =>
println("loader returned a domain")
// All we do in this test app is retrieve the network nodes and
// print their names
import scala.collection.JavaConverters._
val nodes = domain.getNodes().asScala map (_.asInstanceOf[Node])
nodes foreach (node => println(node.getName()))
domain.delete()
case None =>
println("loader returned nothing")
}
}
/**
* Used to load a set of oobn files for an object oriented network
* from a given working directory and create a Domain object.
*/
class OOBNLoader(dir: String) {
val workingDir =
if (dir.endsWith("/")) dir
else dir + "/"
def nameToPath(className: String) =
workingDir + className + ".oobn"
/**
* Loads a network, given the name of the top class,
* and returns an Option[Domain].
*/
@throws(classOf[ExceptionHugin])
def load(topClassName: String): Option[Domain] = {
val cc = new ClassCollection
try {
val parseListener = new ParseListener(workingDir)
val topFile = nameToPath(topClassName)
cc.parseClasses(topFile, parseListener)
val top = cc.getClassByName(topClassName)
if (top == null) {
println("Class not found: " + topClassName)
None
} else {
Some(top.createDomain())
}
} finally {
cc.delete()
}
}
/**
* In the Hugin Java API, a ClassParseListener is responsible for
* loading a given network class. This implementation takes an
* argument for the working directory which is assumed to contain
* all of the class files (.oobn) associated with a network.
*/
class ParseListener(dir: String) extends ClassParseListener {
def parseError(line: Int, msg: String) {
println("Parse error in line " + line + ": " + msg);
}
def insertClass(className: String, cc: ClassCollection) {
try {
cc.parseClasses(nameToPath(className), this);
} catch {
case e: Exception =>
println("Parsing failed: " + e.getMessage());
}
}
}
}
/**
* Companion object with convenience methods.
*/
object OOBNLoader {
/** Creates a loader for a given working directory. */
def apply(dir: String) = new OOBNLoader(dir)
/** Loads a network given a working directory and top class name. */
def load(dir: String, topClass: String): Option[Domain] =
OOBNLoader(dir).load(topClass)
}