Skip to content

Commit

Permalink
Refactoring of HTML annotations
Browse files Browse the repository at this point in the history
If rest of annotations has larger number then maxNumber it is split
  • Loading branch information
buma committed Aug 3, 2015
1 parent fad472f commit 3c9cc69
Showing 1 changed file with 52 additions and 48 deletions.
100 changes: 52 additions & 48 deletions src/main/java/org/opentripplanner/graph_builder/AnnotationsToHTML.java
Expand Up @@ -16,6 +16,7 @@ the License, or (at your option) any later version.
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
Expand All @@ -26,6 +27,7 @@ the License, or (at your option) any later version.
import java.util.*; import java.util.*;
import java.util.logging.Level; import java.util.logging.Level;


import com.google.common.collect.Multiset;
import com.google.common.primitives.Ints; import com.google.common.primitives.Ints;
import org.opentripplanner.common.model.T2; import org.opentripplanner.common.model.T2;
import org.opentripplanner.graph_builder.annotation.GraphBuilderAnnotation; import org.opentripplanner.graph_builder.annotation.GraphBuilderAnnotation;
Expand All @@ -49,6 +51,12 @@ public class AnnotationsToHTML implements GraphBuilderModule {


private int maxNumberOfAnnotationsPerFile; private int maxNumberOfAnnotationsPerFile;


Set<String> classes;

Multiset<String> classOccurences;

List<HTMLWriter> writers;

//Key is classname, value is annotation message //Key is classname, value is annotation message
//Multimap because there are multiple annotations for each classname //Multimap because there are multiple annotations for each classname
private Multimap<String, String> annotations; private Multimap<String, String> annotations;
Expand All @@ -57,6 +65,9 @@ public AnnotationsToHTML (File outpath, int maxNumberOfAnnotationsPerFile) {
this.outPath = outpath; this.outPath = outpath;
annotations = ArrayListMultimap.create(); annotations = ArrayListMultimap.create();
this.maxNumberOfAnnotationsPerFile = maxNumberOfAnnotationsPerFile; this.maxNumberOfAnnotationsPerFile = maxNumberOfAnnotationsPerFile;
this.classes = new TreeSet<>();
this.writers = new ArrayList<>();
this.classOccurences = HashMultiset.create();
} }




Expand All @@ -77,10 +88,6 @@ public void buildGraph(Graph graph, HashMap<Class<?>, Object> extra) {
} }
LOG.info("Creating Annotations log"); LOG.info("Creating Annotations log");


Set<String> classes = new TreeSet<>();



Map<String, Collection<String>> annotationsMap = annotations.asMap(); Map<String, Collection<String>> annotationsMap = annotations.asMap();
//saves list of annotation classes and counts //saves list of annotation classes and counts
List<T2<String, Integer>> counts = new ArrayList<>(annotationsMap.size()); List<T2<String, Integer>> counts = new ArrayList<>(annotationsMap.size());
Expand All @@ -96,65 +103,38 @@ public void buildGraph(Graph graph, HashMap<Class<?>, Object> extra) {
Multimap<String, String> current_map = ArrayListMultimap.create(); Multimap<String, String> current_map = ArrayListMultimap.create();
String last_added_key = null; String last_added_key = null;


//Annotations are grouped if the count of annotations is less then maxNumberOfAnnotationsPerFile //Annotations are grouped until the count of annotations is less then maxNumberOfAnnotationsPerFile
//otherwise each class of annotations is different file. //otherwise each class of annotations is different file.
List<HTMLWriter> writers = new ArrayList<>(annotationsMap.size());
for (T2<String, Integer> count : counts) { for (T2<String, Integer> count : counts) {
LOG.info("Key: {} ({})", count.first, count.second); LOG.info("Key: {} ({})", count.first, count.second);


if (currentNumberOfAnnotationsPerFile > maxNumberOfAnnotationsPerFile) { if ((currentNumberOfAnnotationsPerFile + count.second) <= maxNumberOfAnnotationsPerFile) {
LOG.info("Increasing count: {}+{}={}", currentNumberOfAnnotationsPerFile, count.second, currentNumberOfAnnotationsPerFile+count.second);
currentNumberOfAnnotationsPerFile+=count.second;
current_map.putAll(count.first, annotationsMap.get(count.first));
last_added_key = count.first;
} else {
LOG.info("Flush count:{}", currentNumberOfAnnotationsPerFile); LOG.info("Flush count:{}", currentNumberOfAnnotationsPerFile);
try { if (currentNumberOfAnnotationsPerFile > 0) {
HTMLWriter file_writer; addAnnotations(last_added_key, current_map);
if (current_map.keySet().size() == 1) {
file_writer =new HTMLWriter(last_added_key, current_map);
classes.add(last_added_key);
} else {
file_writer = new HTMLWriter("rest", current_map);
classes.add("rest");
}
writers.add(file_writer);
//file_writer.writeFile(classes);

} catch (FileNotFoundException ex) {
LOG.error("Output folder not found:{} {}", outPath, ex);
return;
} }

current_map = ArrayListMultimap.create(); current_map = ArrayListMultimap.create();
current_map.putAll(count.first, annotationsMap.get(count.first)); current_map.putAll(count.first, annotationsMap.get(count.first));
last_added_key = count.first; last_added_key = count.first;
currentNumberOfAnnotationsPerFile = count.second; currentNumberOfAnnotationsPerFile = count.second;
} else {
LOG.info("Increasing count: {}+{}={}", currentNumberOfAnnotationsPerFile, count.second, currentNumberOfAnnotationsPerFile+count.second);
currentNumberOfAnnotationsPerFile+=count.second;
current_map.putAll(count.first, annotationsMap.get(count.first));
last_added_key = count.first;
} }


} }


LOG.info("Flush last count:{}", currentNumberOfAnnotationsPerFile); LOG.info("Flush last count:{}", currentNumberOfAnnotationsPerFile);
try { addAnnotations(last_added_key, current_map);
HTMLWriter file_writer;
if (current_map.keySet().size() == 1) {
file_writer =new HTMLWriter(last_added_key, current_map);
classes.add(last_added_key);
} else {
file_writer = new HTMLWriter("rest", current_map);
classes.add("rest");
}
writers.add(file_writer);
//file_writer.writeFile(classes);


} catch (FileNotFoundException ex) {
LOG.error("Output folder not found:{} {}", outPath, ex);
return;
}


//Actual writing to the file is made here since //Actual writing to the file is made here since
// this is the first place where actual number of files is known (because it depends on annotations count) // this is the first place where actual number of files is known (because it depends on annotations count)
for (HTMLWriter writer : writers) { for (HTMLWriter writer : writers) {
writer.writeFile(classes); writer.writeFile(classOccurences);
} }




Expand All @@ -163,6 +143,27 @@ public void buildGraph(Graph graph, HashMap<Class<?>, Object> extra) {


} }


private void addAnnotations(String last_added_key, Multimap<String, String> current_map) {
try {
HTMLWriter file_writer;
if (current_map.keySet().size() == 1) {
classOccurences.add(last_added_key);
int labelCount = classOccurences.count(last_added_key);
file_writer =new HTMLWriter(last_added_key+Integer.toString(labelCount), current_map);

} else {
classOccurences.add("rest");
int labelCount = classOccurences.count("rest");
file_writer = new HTMLWriter("rest" + labelCount, current_map);
}
writers.add(file_writer);

} catch (FileNotFoundException ex) {
LOG.error("Output folder not found:{} {}", outPath, ex);
return;
}
}

@Override @Override
public void checkInputs() { public void checkInputs() {


Expand All @@ -182,6 +183,7 @@ class HTMLWriter {
private String current_class; private String current_class;


public HTMLWriter(String key, Collection<String> annotations) throws FileNotFoundException { public HTMLWriter(String key, Collection<String> annotations) throws FileNotFoundException {
LOG.info("Making file: {}", key);
File newFile = new File(outPath, key +".html"); File newFile = new File(outPath, key +".html");
FileOutputStream fileOutputStream = new FileOutputStream(newFile); FileOutputStream fileOutputStream = new FileOutputStream(newFile);
this.out = new PrintStream(fileOutputStream); this.out = new PrintStream(fileOutputStream);
Expand All @@ -192,14 +194,15 @@ public HTMLWriter(String key, Collection<String> annotations) throws FileNotFoun


public HTMLWriter(String filename, Multimap<String, String> curMap) public HTMLWriter(String filename, Multimap<String, String> curMap)
throws FileNotFoundException { throws FileNotFoundException {
LOG.info("Making file: {}", filename);
File newFile = new File(outPath, filename +".html"); File newFile = new File(outPath, filename +".html");
FileOutputStream fileOutputStream = new FileOutputStream(newFile); FileOutputStream fileOutputStream = new FileOutputStream(newFile);
this.out = new PrintStream(fileOutputStream); this.out = new PrintStream(fileOutputStream);
lannotations = curMap; lannotations = curMap;
current_class = filename; current_class = filename;
} }


private void writeFile(Collection<String> classes) { private void writeFile(Multiset<String> classes) {
println("<html><head><title>Graph report for " + outPath.getParentFile() println("<html><head><title>Graph report for " + outPath.getParentFile()
+ "Graph.obj</title>"); + "Graph.obj</title>");
println("\t<meta charset=\"utf-8\">"); println("\t<meta charset=\"utf-8\">");
Expand Down Expand Up @@ -249,11 +252,12 @@ private void writeFile(Collection<String> classes) {
println("<h2>Graph report for " + outPath.getParentFile() + "Graph.obj</h2>"); println("<h2>Graph report for " + outPath.getParentFile() + "Graph.obj</h2>");
println("<p>"); println("<p>");
//adds links to the other HTML files //adds links to the other HTML files
for (String htmlAnnotationClass: classes) { for (Multiset.Entry<String> htmlAnnotationClass: classes.entrySet()) {
if (htmlAnnotationClass.equals(current_class)) { String label = htmlAnnotationClass.getElement() + htmlAnnotationClass.getCount();
println("<span>" + htmlAnnotationClass + "</span><br />"); if (label.equals(current_class)) {
println("<span>" + label + "</span><br />");
} else { } else {
println("<a href=\"" + htmlAnnotationClass + ".html\">" + htmlAnnotationClass + "</a><br />"); println("<a href=\"" + label + ".html\">" + label + "</a><br />");
} }
} }
println("</p>"); println("</p>");
Expand Down

0 comments on commit 3c9cc69

Please sign in to comment.