📄 catgraph.java
字号:
Node node2;
for(node2 = cGraph.first_node() ; node2 == null ; node2 = cGraph.succ_node(node2)) {
if (node == node2)
return true;
}
if (fatalOnFalse) {
if (node == null)
Error.fatalErr("CatGraph::check_node_in_graph: The given node 0x0 "
+ "is not in this graph");
else
Error.fatalErr("CatGraph::check_node_in_graph: The given node "
+node+ " is not in this graph");
}
return false;
}
/** Creates a postscript file of the receiving graph via a dot description.
* This method applies only to the DotGraph DisplayPref.
* @param stream The stream to be written to.
* @param dp The preferences for display.
* @param hasNodeLosses TRUE if this Node contains loss values.
* @param hasLossMatrix TRUE if this CatGraph has a loss matrix assigned to
* it, FALSE otherwise.
*/
protected void process_DotGraph_display(Writer stream,
DisplayPref dp,
boolean hasNodeLosses,
boolean hasLossMatrix) {
try {
FileWriter tempfile = new FileWriter("catgraph.dot-graph");
convertToDotFormat(tempfile, dp, hasNodeLosses, hasLossMatrix);
} catch(IOException e) {
e.printStackTrace();
}
}
/** Creates a postscript file of the receiving graph in a graphical form.
* This method applies only to the DotPostscript DisplayPref.
* @param stream The stream to be written to.
* @param dp The preferences for display.
* @param hasNodeLosses TRUE if this Node contains loss values.
* @param hasLossMatrix TRUE if this CatGraph has a loss matrix assigned to
* it, FALSE otherwise.
*/
protected void process_DotPostscript_display(Writer stream,
DisplayPref dp,
boolean hasNodeLosses,
boolean hasLossMatrix) {
try {
//Originally this used a temporary file name for dot file output -JL
FileWriter tempfile = new FileWriter("catgraph.dot-in");
convertToDotFormat(tempfile, dp, hasNodeLosses, hasLossMatrix);
tempfile.close();
//This is a system call to the dot program -JL
//TmpFileName tmpfile2(".dot-out");
//if(system(*GlobalOptions::dotUtil + " -Tps " + tmpfile1 + " -o " + tmpfile2))
//Mcerr << "CatGraph::display: Call to dot failed." << endl;
//stream.include_file(tmpfile2); // this feeds the correct output
} catch(IOException e) {
e.printStackTrace();
System.exit(1);
}
}
/** Prints a representation of the CatGraph to the specified stream, using the
* Categorizer descriptions to label the nodes. This mehtod takes into account
* the different combinations of streams and display preferences. See
* DisplayPref.java for more details with regards to valid combinations,
* functionality, and options.
* @param hasNodeLosses TRUE if this Node contains loss values.
* @param hasLossMatrix TRUE if this CatGraph has a loss matrix assigned to
* it, FALSE otherwise.
* @param stream The stream to be written to.
* @param dp The preferences for display.
*/
// The hasNodeLosses is first because it's non-default here.
// The default is handled by the virtual base class that calls
// the header file, which in turn passes a FALSE here.
public void display(boolean hasNodeLosses, boolean hasLossMatrix,
Writer stream, DisplayPref dp) {
// XStream is a special case--the only option so far where you don't
// just send something to the MLCOStream.
// if (stream.output_type() == XStream) {
// process_XStream_display(dp, hasNodeLosses, hasLossMatrix);
// return;
// }
try {
// Other cases are depend only on DisplayPreference
switch (dp.preference_type()) {
case DisplayPref.ASCIIDisplay:
// Note that we're calling get_fstream and not get_stream to avoid
// overflow when cout is auto-wrapped (since it's a strstream).
// This means that there is no wrapping here.
cGraph.print(stream);
stream.flush();
break;
case DisplayPref.DotPostscriptDisplay:
process_DotPostscript_display(stream, dp, hasNodeLosses, hasLossMatrix);
break;
case DisplayPref.DotGraphDisplay:
process_DotGraph_display(stream,dp, hasNodeLosses, hasLossMatrix);
break;
default:
Error.fatalErr("CatGraph::display: Unrecognized output type: "
+ dp.toString());
}
} catch(IOException e) {
e.printStackTrace();
}
}
/***************************************************************************
Converts the representation of the graph to dot format and directs it to
the specified stream.
@param stream The stream to be written to.
@param pref The preferences for display.
@param hasNodeLosses
@param hasLossMatrix TRUE if this CatGraph has a loss matrix assigned to
it, FALSE otherwise.
***************************************************************************
protected void convertToDotFormat(Writer stream,
DisplayPref pref,
boolean hasNodeLosses,
boolean hasLossMatrix)
{
try
{
stream.write(convertToDotFormat(pref,hasNodeLosses,hasLossMatrix));
} catch(IOException e)
{
e.printStackTrace();
}
}
*/
/** Returns a representation of the graph to dot format.
* @param stream The stream to be written to.
* @param pref The preferences for display.
* @param hasNodeLosses TRUE if this Node contains loss values.
* @param hasLossMatrix TRUE if this CatGraph has a loss matrix assigned to
* it, FALSE otherwise.
*/
protected void convertToDotFormat(Writer stream,
DisplayPref pref,
boolean hasNodeLosses,
boolean hasLossMatrix) {
try {
GetEnv getenv = new GetEnv();
boolean displayDistr = getenv.get_option_bool("DIST_DISP", defaultDistDisp, distDispHelp, true);
// send header and open brace to stream.
stream.write("/* Machine generated dot file */ \n\n"
+"digraph G { \n\n");
// Preferences that only make sense for the Postscript Display
if (pref.preference_type() == DisplayPref.DotPostscriptDisplay) {
process_DotPoscript_preferences(stream, pref);
}
// We add each node to the dot output.
Node v = null;
for(ListIterator NLI = cGraph.nodeIterator() ; NLI.hasNext() ;) {
v =(Node) NLI.next();
stream.write("/* node " + v.index() +": */\n");
stream.write("node_" + v.index() +" [label=\"" + get_categorizer(v).description());
if (hasNodeLosses) {
stream.write("\\nEstimated ");
if (hasLossMatrix)
stream.write("losses: ");
else
stream.write("error: ");
NodeLoss na = get_categorizer(v) .get_loss();
if (Math.abs(na.totalWeight) < MLJ.realEpsilon)
stream.write("?");
else {
double loss = na.totalLoss/ na.totalWeight;
stream.write(MLJ.numberToString(loss*100, 2) +"% (" + na.totalWeight +')');
}
}
if (displayDistr && get_categorizer(v).has_distr())
stream.write("\\n" + get_categorizer(v).get_distr());
stream.write("\"]\n");
Edge e;
for(ListIterator ELI = cGraph.edgeIterator() ; ELI.hasNext() ;) {
e =(Edge) ELI.next();
stream.write("node_" + v.index() +"->" +"node_"
+ e.target() .index() +" [label=\""
+((AugCategory) cGraph.inf(e)) .description() +"\"] \n");
}
MLJ.ASSERT(v != null, "CatGraph::convertToDotFormat"
+ "(DisplayPref,boolean,boolean): v equals NULL");
}
MLJ.ASSERT(v == null, "CatGraph::convertToDotFormat"
+ "(DisplayPref,boolean,boolean): v does not equal NULL");
stream.write("}\n");
} catch(IOException e) {
e.printStackTrace();
}
}
/** Gets the preferences from the DisplayPref class. This method applies only to
* DotPostscript display type.
* @param stream The Writer to which the CatGraph will be displayed.
* @param pref The preferences to use in displaying the CatGraph.
*/
void process_DotPoscript_preferences(Writer stream,
DisplayPref pref) {
// Remember: These are preferences that only make sense for the
// Postscript Display
float pageSizeX = pref.get_page_size_x();
float pageSizeY = pref.get_page_size_y();
float graphSizeX = pref.get_graph_size_x();
float graphSizeY = pref.get_graph_size_y();
int orientation;
orientation = pref.get_orientation();
int ratio;
ratio = pref.get_ratio();
try{
stream.write("page = \"" + pageSizeX + ","
+ pageSizeY + "\";\n"
+ "size = \"" + graphSizeX + ","
+ graphSizeY + "\";\n");
if (orientation == pref.DisplayLandscape)
stream.write("orientation = landscape;\n");
else
stream.write("orientation = portrait;\n");
if (ratio == pref.RatioFill)
stream.write("ratio = fill;\n");
}catch(IOException e){e.printStackTrace();}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -