1
Java / Re: Direct entry of data into tables
« on: March 13, 2014, 00:15:30 »
Thanks Frank - that makes sense
Welcome the HUGIN Forum!
HUGIN is a tool for probabilistic graphical models.
We hope you enjoy using our forum.
Thanks!
HUGIN Expert A/S
Registration and Spam Robots
We have disabled the automatic member registration function due to the increasing number of spam-robot member registrations.
How To Create a Member Account
Simply send an email with your user name to forum (at) hugin (dot) com, and we will create a member account for you.
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
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)
}