📄 bifparser.java
字号:
/*
* This program 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; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed 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 this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* BIFParser.java
* Copyright (C) 2003 Ashraf M. Kibriya
*
*/
package weka.gui.graphvisualizer;
import java.io.InputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.util.StringTokenizer;
import weka.core.FastVector;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;
/**
* This class parses an inputstream or a string in
* XMLBIF ver. 0.3 format, and builds the datastructures
* that are passed to it through the constructor.
*
* @author Ashraf M. Kibriya (amk14@cs.waikato.ac.nz)
* @version $Revision: 1.1 $ - 24 Apr 2003 - Initial version (Ashraf M. Kibriya)
*/
public class BIFParser implements GraphConstants {
/** These holds the nodes and edges of the graph */
protected FastVector m_nodes, m_edges;
/** This holds the name of the graph (i.e. the name of network tag in XMLBIF
* input)
*/
protected String graphName;
/** This holds the string to be parsed */
protected String inString;
/** This holds the InputStream to be parsed */
protected InputStream inStream;
/**
* Constructor (if our input is a String)
*
* @param input the string to be parsed (should not be null)
* @param nodes vector containing GraphNode objects (should be empty)
* @param edges vector containing GraphEdge objects (should be empty)
*/
public BIFParser(String input, FastVector nodes, FastVector edges) {
m_nodes = nodes; m_edges = edges; inString = input;
}
/**
* Constructor (if our input is an InputStream)
*
* @param instream the InputStream to be parsed (should not be null)
* @param nodes vector containing GraphNode objects (should be empty)
* @param edges vector containing GraphEdge objects (should be empty)
*/
public BIFParser(InputStream instream, FastVector nodes, FastVector edges) {
m_nodes = nodes; m_edges = edges; inStream = instream;
}
/**
* This method parses the string or the InputStream that we
* passed in through the constructor and builds up the
* m_nodes and m_edges vectors
* @exception - Exception if both the inString and inStream are
* null, i.e. no input has been provided
* @exception - BIFFormatException if there is format of the
* input is not correct. The format should conform to
* XMLBIF version 0.3
* @exception - NumberFormatException if there is an invalid
* char in the probability table of a node.
* @return - returns the name of the graph
*/
public String parse() throws Exception {
Document dc=null;
javax.xml.parsers.DocumentBuilderFactory dbf =
javax.xml.parsers.DocumentBuilderFactory.newInstance();
dbf.setIgnoringElementContentWhitespace(true);
javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
if(inStream!=null)
dc = db.parse(inStream);
else if(inString!=null)
dc = db.parse(new org.xml.sax.InputSource(new StringReader(inString)));
else
{ throw new Exception("No input given"); }
NodeList nl = dc.getElementsByTagName( "NETWORK" );
if(nl.getLength()==0) {
throw new BIFFormatException( "NETWORK tag not found" );
}
//take only the first network node
NodeList templist = ((Element)nl.item(0)).getElementsByTagName( "NAME" );
graphName = templist.item(0).getFirstChild().getNodeValue();
//System.out.println("The name of the network is "+
//templist.item(0).getFirstChild().getNodeValue());
//Get all the variables
nl = dc.getElementsByTagName("VARIABLE");
for(int i=0; i<nl.getLength(); i++) {
templist = ((Element)nl.item(i)).getElementsByTagName("NAME");
if(templist.getLength()>1)
throw new BIFFormatException("More than one name tags found for "+
"variable no. "+(i+1));
String nodename =
((org.w3c.dom.Node)templist.item(0)).getFirstChild().getNodeValue();
GraphNode n = new GraphNode( nodename, nodename, GraphNode.NORMAL );
m_nodes.addElement(n);
//getting nodes position
templist = ((Element)nl.item(i)).getElementsByTagName("PROPERTY");
for(int j=0; j<templist.getLength(); j++) {
if( ((org.w3c.dom.Node)templist.item(j)).getFirstChild()
.getNodeValue().startsWith("position") ) {
String xy = templist.item(j).getFirstChild().getNodeValue();
//System.out.println("x: "+
// xy.substring(xy.indexOf('(')+1, xy.indexOf(','))+
// " y: "+
// xy.substring(xy.indexOf(',')+1, xy.indexOf(')'))
// );
n.x = Integer.parseInt( xy.substring(xy.indexOf('(')+
1, xy.indexOf(',')).trim() );
n.y = Integer.parseInt( xy.substring(xy.indexOf(',')+
1, xy.indexOf(')')).trim() );
break;
}
}
//getting all the outcomes of the node
templist = ((Element)nl.item(i)).getElementsByTagName("OUTCOME");
n.outcomes = new String[templist.getLength()];
for(int j=0; j<templist.getLength(); j++) {
n.outcomes[j] = templist.item(j).getFirstChild().getNodeValue();
//System.out.println("Outcome["+j+"]: "+n.outcomes[j]);
}
} //end for (for variables)
//Get all the edges and probability tables by getting all the definitions
nl = dc.getElementsByTagName("DEFINITION");
for(int i=0; i<nl.getLength(); i++) {
templist = ((Element)nl.item(i)).getElementsByTagName("FOR");
//the Label of the node the edges are coming into
String nid = templist.item(0).getFirstChild().getNodeValue();
//getting the GraphNode object with the above label
GraphNode n = (GraphNode)m_nodes.elementAt(0);
for(int j=1; j<m_nodes.size() && !n.ID.equals(nid); j++)
n = (GraphNode)m_nodes.elementAt(j);
templist = ((Element)nl.item(i)).getElementsByTagName("GIVEN");
int parntOutcomes = 1; //for creating the probability table later on
//creating all the edges coming into the node
for(int j=0; j<templist.getLength(); j++) {
nid = templist.item(j).getFirstChild().getNodeValue();
GraphNode n2 = (GraphNode)m_nodes.elementAt(0);
for(int k=1; k<m_nodes.size() && !n2.ID.equals(nid); k++)
n2 = (GraphNode)m_nodes.elementAt(k);
m_edges.addElement( new GraphEdge(m_nodes.indexOf(n2),
m_nodes.indexOf(n), 1) );
parntOutcomes *= n2.outcomes.length;
}
//creating the probability table for the node
templist = ((Element)nl.item(i)).getElementsByTagName("TABLE");
if(templist.getLength()>1)
throw new BIFFormatException("More than one Probability Table for "+
n.ID);
String probs = templist.item(0).getFirstChild().getNodeValue();
StringTokenizer tk = new StringTokenizer(probs, " \n\t");
if(parntOutcomes*n.outcomes.length > tk.countTokens())
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -