⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlbifv02.jj

📁 java编写的贝叶斯网络分类器(貌似没有模型构建和参数学习过程)
💻 JJ
字号:
/**
 * Definitions for the XML BIF 0.2 (XML-based BayesNet Interchange Format version 0.2) parser
 * Author: Fabio Cozman
 * <fgcozman@cs.cmu.edu><http://www.cs.cmu.edu/~fgcozman/home.html>
 *
 * This parser is free software; you can
 * redistribute it and/or modify it under the terms of the GNU General
 * Public License as published by the Free Software Foundation, provided
 * that this notice and the name of the author appear in all copies.
 * If you're using the software, please notify fgcozman@cs.cmu.edu so
 * that you can receive updates and patches.
 * This parser is distributed "as is", in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with the BayesianNetworks package. If not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * Options for JavaCC (only non-default options).
 */

options {
  STATIC = false;
}

/*
 * Java code that is incorporated into the parser.
 */

PARSER_BEGIN(XMLBIFv02)

package Parsers.XMLBIFv02;

import InterchangeFormat.*;

import java.util.Vector;
import java.util.Enumeration;

/*
 * Definition of the Interchange Format class and its
 * variables. The IFBayesNet ifbn contains the
 * parsed Bayesian network.
 */

public class XMLBIFv02 extends InterchangeFormat {
  IFBayesNet ifbn;

  public IFBayesNet get_ifbn() { return(ifbn); }

  String pcdata() throws ParseException {
      StringBuffer p = new StringBuffer("");
      Token t;
      while (true) {
		t = getToken(1);
		if ((t.kind == 0) || (t.kind == SOT) || (t.kind == EOT)) break;
	    else { p.append(t.image); getNextToken(); }
      }
      return(p.toString());
  }

  void glob_heading() throws ParseException {
      Token t;
      while (true) {
		t = getToken(1);
		if (t.kind == 0) break;
		else {
		    if (t.kind == SOT) {
		        getNextToken(); t = getToken(1);
		        if (t.kind == BIF) {
		            getNextToken(); t = getToken(1);
		            if (t.kind == CT) { getNextToken(); break; }
		        }
		        else { getNextToken(); }
		    }
		    else { getNextToken(); }
		}
		getNextToken();
	  }
  }
}

PARSER_END(XMLBIFv02)

/*
 * List of tokens.
 */

SKIP : /* WHITE SPACE */
{
  < ( " " | "\t" | "\n" | "\r" | "\f" )+ >
}

SPECIAL_TOKEN : /* COMMENTS and XML DECLARATIONS */
{
  < "<!" (~[">"])* ">">
}

TOKEN: /* Open tags */
{
    < SOT: "<" >
|   < EOT: "</" >
}

TOKEN: /* Close tags */
{
    < CT: ">" >
}

TOKEN [IGNORE_CASE] :  /* Opening tag */
{
  < OPENTAG: "<?XML" (~[">"])* ">" >
}

TOKEN [IGNORE_CASE] :  /* Keywords */
{
  < BIF: "BIF" >
| < DEFAUL: "DEFAULT" >
| < ENTRY: "ENTRY">
| < FOR: "FOR" >
| < GIVEN: "GIVEN" >
| < NAME: "NAME" >
| < NETWORK: "NETWORK">
| < PROBABILITY: "PROBABILITY" >
| < PROPERTY: "PROPERTY" >
| < TABLE: "TABLE" >
| < TYPE: "TYPE" >
| < VALUE: "VALUE" >
| < VARIABLE: "VARIABLE" >
}

TOKEN:
{
  < DISCRETE: "discrete" >
}

TOKEN : /* Definition of a non-negative number */
{
  < NON_NEGATIVE_NUMBER:
      ["1"-"9"] (["0"-"9"])*
      | (["0"-"9"])+ "." (["0"-"9"])* (<EXPONENT>)?
      | "." (["0"-"9"])+ (<EXPONENT>)?
      | (["0"-"9"])+ (<EXPONENT>)?
  >
|
  < #EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
}

TOKEN: /* Ordinary data in strings; token used to consume strings */
{
	<PCDATA_CHARACTER : (~["<"]) >
}

/*
 * THE INTERCHANGE FORMAT GRAMMAR STARTS HERE.
 */

/*
 * Basic parsing function. First looks for a Network Declaration,
 * then looks for an arbitrary number of VariableDeclaration or
 * ProbabilityDeclaration non-terminals. The objects are
 * in the vectors ifbn.pvs and ifbn.upfs.
 */
void CompilationUnit() :
{
IFProbabilityVariable pv;
IFProbabilityFunction upf;
}
{
  OpenTag()

  { glob_heading(); }

  NetworkDeclaration()
  ( <SOT>
    ( pv=VariableDeclaration() { ifbn.add(pv); }
    | upf=ProbabilityDeclaration() { ifbn.add(upf); } ) )*
  <EOT> <NETWORK> <CT>
  ( ( <EOT> <BIF> <CT> ) | <EOF> )
}

void OpenTag() :
{
}
{
  <OPENTAG>
}

/*
 * Detect and initialize the network.
 */
void NetworkDeclaration() :
{
String s, ss;
Vector properties = new Vector();
}
{
  <SOT> <NETWORK> <CT>
  <SOT> <NAME> s=getString() <EOT> <NAME> <CT>
  ( LOOKAHEAD(2)
  ss=Property() { properties.addElement(ss); } )*
	{ ifbn = new IFBayesNet(s, properties); }
}

/*
 * Detect a variable declaration.
 */
IFProbabilityVariable VariableDeclaration() :
{
String s;
IFProbabilityVariable pv;
}
{
  <VARIABLE> <CT>
  s=ProbabilityVariableName() ProbabilityVariableType() pv=VariableContent(s)
  <EOT> <VARIABLE> <CT>
	{ return(pv); }
}

String ProbabilityVariableName() :
{
String s;
}
{
   <SOT> <NAME> s=getString() <EOT> <NAME> <CT>
	{ return(s); }
}

void ProbabilityVariableType() :
{
String values[] = null;
}
{ /* Only one type is possible (discrete) */
  <SOT> <TYPE> <CT> <DISCRETE> <EOT> <TYPE> <CT>
}

IFProbabilityVariable VariableContent(String name) :
{
int i;
String s, v, svalues[];
Vector properties = new Vector();
Vector values = new Vector();
Enumeration e;
IFProbabilityVariable pv = new IFProbabilityVariable();
}
{
    ( LOOKAHEAD(2)
    s=Property() { properties.addElement(s); }
    |
    v=VariableValue() { values.addElement(v); } )*
	{ pv.set_name(name);
	  pv.set_properties(properties);
	  svalues = new String[ values.size() ];
	  for (e = values.elements(), i = 0; e.hasMoreElements(); i++)
	    svalues[i] = (String)(e.nextElement());
	  pv.set_values(svalues);
	  return(pv); }
}

String VariableValue() :
{
String s;
}
{
    <SOT> <VALUE> s=getString() <EOT> <VALUE> <CT>
        { return(s); }
}

/*
 * Detect a probability declaration.
 */
IFProbabilityFunction ProbabilityDeclaration() :
{
String vs[];
IFProbabilityFunction upf = new IFProbabilityFunction();
}
{
  <PROBABILITY> <CT> ProbabilityContent(upf) <EOT> <PROBABILITY> <CT>
	{ return(upf); }
}

void ProbabilityContent(IFProbabilityFunction upf) :
{
int i, j;
double def[] = null;
double tab[] = null;
String s, vs[];
IFProbabilityEntry entry = null;
Enumeration e;

Vector fors = new Vector();
Vector givens = new Vector();
Vector properties = new Vector();
Vector entries = new Vector();
Vector defaults = new Vector();
Vector tables = new Vector();
}
{
  ( <SOT>
  ( s=ProbabilityFor() { fors.addElement(s); }
  | s=ProbabilityGiven() { givens.addElement(s); }
  | s=Property() { properties.addElement(s); }
  | def=ProbabilityDefault() { defaults.addElement(def); }
  | entry=ProbabilityEntry() { entries.addElement(entry); }
  | tab=ProbabilityTable() { tables.addElement(tab); } ) )*
	{ upf.set_properties(properties);
	  upf.set_defaults(defaults);
	  upf.set_entries(entries);
	  upf.set_tables(tables);
	  upf.set_conditional_index(fors.size());
	  vs = new String[ fors.size() + givens.size() ];
      for (e = fors.elements(), i = 0; e.hasMoreElements(); i++)
        vs[i] = (String)(e.nextElement());
      for (e = givens.elements(), j = i; e.hasMoreElements(); j++)
        vs[j] = (String)(e.nextElement());
	  upf.set_variables(vs);
	}
}

String ProbabilityFor() :
{
String s;
}
{
    <FOR> s=getString() <EOT> <FOR> <CT>
        { return(s); }
}

String ProbabilityGiven() :
{
String s;
}
{
    <GIVEN> s=getString() <EOT> <GIVEN> <CT>
        { return(s); }
}

IFProbabilityEntry ProbabilityEntry() :
{
int i;
Enumeration e;
String variable_name, vs[];
Vector v_list = new Vector();
double d[];
}
{
   <ENTRY> <CT>
   ( LOOKAHEAD(2)
   <SOT> <VALUE> variable_name=getString() <EOT> <VALUE> <CT>
    	{ v_list.addElement(variable_name); }  )*
   d=ProbabilityTable() <EOT> <ENTRY> <CT>
	{ vs = new String[v_list.size()];
	  for (e=v_list.elements(), i=0; e.hasMoreElements(); i++)
		vs[i] = (String)(e.nextElement());
	  return( new IFProbabilityEntry(vs, d) ); }
}

double[] ProbabilityDefault() :
{
double d[];
}
{
  <DEFAUL> <CT> d=FloatingPointList() <EOT> <DEFAUL> <CT>
	{ return(d); }
}

double[] ProbabilityTable() :
{
double d[];
}
{
  <TABLE> <CT> d=FloatingPointList() <EOT> <TABLE> <CT>
	{ return(d); }
}

/*
 * Some general purpose non-terminals.
 */

/*
 * Pick a list of non-negative floating numbers.
 */
double[] FloatingPointList() :
{
int i;
Double d;
double ds[];
Vector d_list = new Vector();
Enumeration e;
}
{
  d=FloatingPointNumber()
	{ d_list.addElement(d); }
  ( d=FloatingPointNumber()
	{ d_list.addElement(d); }
  )*
	{ ds = new double[d_list.size()];
	  for (e=d_list.elements(), i=0; e.hasMoreElements(); i++) {
		d = (Double)(e.nextElement());
		ds[i] = d.doubleValue();
	  }
	  return(ds); }
}

/*
 * Pick a non-negative floating number; necessary to allow
 * ignored characters and comments to exist in the middle
 * of a FloatingPointList().
 */
Double FloatingPointNumber() :
{
Token t;
}
{
  t=<NON_NEGATIVE_NUMBER>
	{ return( Double.valueOf(t.image) ); }
}

/*
 * Property definition.
 */
String Property() :
{
String s;
}
{
  <SOT> <PROPERTY> s=getString() <EOT> <PROPERTY> <CT>
	{ return(s); }
}

/*
 * String.
 */
String getString() :
{
}
{
    <CT> { return( pcdata() ); }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -