📄 report.java
字号:
datatable.getDefaultCell().setGrayFill(0.97f);
}
datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
datatable.addCell(new Phrase(numberFormatter.format(i+1),numberFont));
datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
datatable.addCell(new Phrase(cities[i].getName(),tableHeaderFont));
datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
datatable.addCell(new Phrase(numberFormatter.format(cities[i].getSJTSKX()),numberFont));
datatable.addCell(new Phrase(numberFormatter.format(cities[i].getSJTSKY()),numberFont));
String distanceText="0";
String totalDistanceText="0";
double distance=0;
if(i>0) {
if(i==49) {
int ii=0;
ii++;
}
distance=cities[i].distance(cities[i-1],false);
distanceText=numberFormatter.format((int)distance);
totalDistance+=distance;
totalDistanceText=numberFormatter.format((int)totalDistance);
}
datatable.addCell(new Phrase(distanceText,numberFont));
datatable.addCell(new Phrase(totalDistanceText,numberFont));
//switch of gray colour
datatable.getDefaultCell().setGrayFill(0.0f);
}
document.add(datatable);
document.close();
}
/** Image for page header **/
Image headerImage;
/** The headertable. **/
PdfPTable headerTable;
/** A template that will hold the total number of pages. **/
PdfTemplate tpl;
/** The font that will be used. for header/footer **/
BaseFont headerFont;
/**
* @see com.lowagie.text.pdf.PdfPageEventHelper#onOpenDocument(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
*/
@Override
public void onOpenDocument(PdfWriter writer, Document document) {
try {
headerFont = BaseFont.createFont("Helvetica", BaseFont.WINANSI, false);
String projectURL="http://www.saiko.cz/ai/tsp/";
// initialization of the header table
headerImage = Image.getInstance(getClass().getResource("/org/saiko/etc/logo2.jpg"));
headerTable=new PdfPTable(1);
PdfPTable table = new PdfPTable(2);
Paragraph p = new Paragraph();
p.setLeading(0,2);
p.add(new Chunk("Traveling Salesman Problem\n", new Font(Font.HELVETICA, 16, Font.BOLDITALIC, Color.blue)));
p.add(new Chunk("Application report\n\n", new Font(Font.HELVETICA, 12, Font.NORMAL)));
Anchor link = new Anchor(projectURL, new Font(Font.HELVETICA, 11, Font.NORMAL));
link.setReference(projectURL);
p.add(link);
p.add(new Chunk("\n\n"+parameters.get(PARAM_COMPUTATION_TIME)+"\n", new Font(Font.HELVETICA, 11, Font.NORMAL)));
table.getDefaultCell().setBackgroundColor(Color.WHITE);
table.getDefaultCell().setBorderWidth(0);
PdfPCell cell = new PdfPCell(p);
cell.setPadding(20);
cell.setBorderWidth(0);
table.addCell(cell);
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(new Phrase(new Chunk(headerImage, 0, 0)));
headerTable.addCell(table);
// initialization of the template
tpl = writer.getDirectContent().createTemplate(100, 100);
tpl.setBoundingBox(new Rectangle(-20, -20, 100, 100));
}
catch(Exception e) {
throw new ExceptionConverter(e);
}
}
/**
* @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
*/
@Override
public void onEndPage(PdfWriter writer, Document document) {
PdfContentByte cb = writer.getDirectContent();
cb.saveState();
// write the headertable
if(writer.getPageNumber()!=2) {
headerTable.setTotalWidth(document.getPageSize().width() - 60);
headerTable.writeSelectedRows(0, -1, 30, document.getPageSize().top()-30, cb);
}
// compose the footer
String text = "page " + writer.getPageNumber() + " of ";
float textSize = headerFont.getWidthPoint(text, 10);
float textBase = 40;
cb.beginText();
cb.setFontAndSize(headerFont, 10);
float adjust = headerFont.getWidthPoint("0", 10);
cb.setTextMatrix(document.right() - textSize - adjust, textBase);
cb.showText(text);
cb.endText();
cb.addTemplate(tpl, document.right() - adjust, textBase);
cb.saveState();
// draw a Rectangle around the page
cb.setLineWidth(1);
cb.rectangle(30, 30, document.getPageSize().width() - 60, document.getPageSize().height() - 60);
cb.stroke();
cb.restoreState();
}
/**
* @see com.lowagie.text.pdf.PdfPageEventHelper#onCloseDocument(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
*/
@Override
public void onCloseDocument(PdfWriter writer, Document document) {
//add total page count to footer
tpl.beginText();
tpl.setFontAndSize(headerFont, 10);
tpl.setTextMatrix(0, 0);
tpl.showText("" + (writer.getPageNumber() - 1));
tpl.endText();
}
/**
* @return map of system properties for creating report
*/
protected static Map<String,String> getSystemProperties() {
Map<String,String> p=new LinkedHashMap<String,String>();
p.put("availableProcessors",String.valueOf(Runtime.getRuntime().availableProcessors()));
p.put("freeMemory",numberFormatter.format(Runtime.getRuntime().freeMemory()));
p.put("totalMemory",numberFormatter.format(Runtime.getRuntime().totalMemory()));
p.put("maxMemory",numberFormatter.format(Runtime.getRuntime().maxMemory()));
//info from System.getProperty
String props[] = new String[] {
"java.vm.name",
"java.vm.vendor",
"java.vm.version",
"os.name",
"os.version",
"os.arch",
"sun.arch.data.model"
};
for(String prop:props) {
p.put(prop,System.getProperty(prop));
}
//info from System.env
String envs[] = new String[] {
"PROCESSOR_IDENTIFIER",
"NUMBER_OF_PROCESSORS",
"OS",
};
for(String env:envs) {
p.put(env,System.getenv(env));
}
return p;
}
/**
* @param tsp - the application (result)
* @return map of result properties
*/
protected static Map<String,String> getResultInfo(TSP tsp) {
Map<String,String> p=new LinkedHashMap<String,String>();
p.put(Report.PARAM_COMPUTATION_TIME,new SimpleDateFormat("yyyy/MM/dd HH:mm").format(Calendar.getInstance().getTime()));
p.put("Program version",TSP.getAppVersion());
p.put("TSPEngine",tsp.engineName);
p.put("Map file",tsp.mapFile);
p.put("Map file hash",getMapHash(tsp.mapFile));
p.put("Number of cities",Report.numberFormatter.format(tsp.cities.length));
p.put("Init. population size",Report.numberFormatter.format(tsp.configuration.initialPopulationSize));
p.put("Population growth",Report.numberFormatter.format(tsp.configuration.populationGrow));
if(tsp.engine!=null)
p.put("Final population size",Report.numberFormatter.format(tsp.engine.getPopulationSize()));
p.put("Mutation ratio",Report.numberFormatter.format(tsp.configuration.mutationRatio));
p.put("RMS cost",tsp.configuration.rmsCost ? "true" : "false");
p.put("Generation",Report.numberFormatter.format(tsp.generation));
p.put("Time",Report.numberFormatter.format((int)(tsp.runTime/1000))+" s.");
p.put("Best cost age",Report.numberFormatter.format(tsp.bestCostAge));
if(tsp.bestChromosome!=null)
p.put("Best distance",Report.numberFormatter.format((int)tsp.bestChromosome.totalDistance));
return p;
}
/**
* Create hash for map file
* @param mapFile
* @return SHA hash of map file converted into BASE64 encoding
*/
protected static String getMapHash(String mapFile) {
InputStream i=new Object().getClass().getResourceAsStream("/org/saiko/ai/genetics/tsp/etc/"+mapFile+".csv");
if(i==null) return null;
try {
byte b[]=new byte[1024];
int size;
ByteArrayOutputStream buffer=new ByteArrayOutputStream();
while((size=i.read(b))>0) {
buffer.write(b,0,size);
}
i.close();
MessageDigest digest=MessageDigest.getInstance("SHA");
return new BASE64Encoder().encode(digest.digest(buffer.toByteArray()));
} catch(Throwable e) {
return null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -