This program builds the above Bayes net in memory
  and then saves it to file:
/* 
 *  BuildNet.c
 *
 *  Example use of Netica-C-API to construct a Bayes net and save it to file.
 */

#include "Netica.h"
#include "NeticaEx.c"

#define CHKERR  {if (GetError_ns (env, ERROR_ERR, NULL))  goto error;}

environ_ns* env;

int main (void){
	net_bn* net;
	node_bn *VisitAsia, *Tuberculosis, *Smoking, *Cancer, *TbOrCa, *XRay;
	char mesg[MESG_LEN_ns];
	int res;
	
	env = NewNeticaEnviron_ns (NULL, NULL, NULL);
	res = InitNetica2_bn (env, mesg);
	printf ("%s\n", mesg);
	if (res < 0)  exit (-1);
	
	net = NewNet_bn ("AsiaEx", env);
	CHKERR

	VisitAsia =    NewNode_bn ("VisitAsia", 2, net);
	Tuberculosis = NewNode_bn ("Tuberculosis", 2, net);
	Smoking =      NewNode_bn ("Smoking", 2, net);
	Cancer =       NewNode_bn ("Cancer", 2, net);
	TbOrCa =       NewNode_bn ("TbOrCa", 2, net);
	XRay =         NewNode_bn ("XRay", 2, net);
	CHKERR

	SetNodeStateNames_bn (VisitAsia,   "Visit,   No_Visit");
	SetNodeStateNames_bn (Tuberculosis,"Present, Absent");
	SetNodeStateNames_bn (Smoking,     "Smoker,  NonSmoker");
	SetNodeStateNames_bn (Cancer,      "Present, Absent");
	SetNodeStateNames_bn (TbOrCa,      "True,    False");
	SetNodeStateNames_bn (XRay,        "Abnormal,Normal");
	SetNodeTitle_bn (TbOrCa, "Tuberculosis or Cancer");
	SetNodeTitle_bn (Cancer, "Lung Cancer");
	CHKERR

	AddLink_bn (VisitAsia, Tuberculosis);
	AddLink_bn (Smoking, Cancer);
	AddLink_bn (Tuberculosis, TbOrCa);
	AddLink_bn (Cancer, TbOrCa);
	AddLink_bn (TbOrCa, XRay);
	CHKERR
	
	// WARNING: floats must be passed to SetNodeProbs, ie, 0.0 not 0

	SetNodeProbs (VisitAsia, 0.01, 0.99);
	
	SetNodeProbs (Tuberculosis, "Visit",    0.05, 0.95);
	SetNodeProbs (Tuberculosis, "No_Visit", 0.01, 0.99);

	SetNodeProbs (Smoking, 0.5, 0.5);

	SetNodeProbs (Cancer, "Smoker",    0.1,  0.9);
	SetNodeProbs (Cancer, "NonSmoker", 0.01, 0.99);

	//                   Tuberculosis Cancer
	SetNodeProbs (TbOrCa, "Present", "Present", 1.0, 0.0);
	SetNodeProbs (TbOrCa, "Present", "Absent",  1.0, 0.0);
	SetNodeProbs (TbOrCa, "Absent",  "Present", 1.0, 0.0);
	SetNodeProbs (TbOrCa, "Absent",  "Absent",  0.0, 1.0);

	//                  TbOrCa  Abnormal Normal
	SetNodeProbs (XRay, "True",  0.98,  0.02);
	SetNodeProbs (XRay, "False", 0.05,  0.95);
	CHKERR

	WriteNet_bn (net,  NewStreamFile_ns ("AsiaEx.dne", env, NULL));
	CHKERR

end:
	FreeNet_bn (net);
	res = CloseNetica_bn (env, mesg);
	printf ("%s\n", mesg);
	return (res < 0 ? -1 : 0);

error:
	fprintf (stderr, "BuildNet: Error in %s\n", 
	         ErrorMessage_ns (GetError_ns (env, ERROR_ERR, NULL)));
	goto end;
}