-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainBeastChart.java
193 lines (144 loc) · 6.36 KB
/
MainBeastChart.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package org.aksw.beast.cli;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Map.Entry;
import java.util.Objects;
import org.aksw.beast.chart.ChartTransform;
import org.aksw.beast.chart.model.StatisticalBarChart;
import org.aksw.beast.viz.xchart.ChartModelConfigurerXChart;
import org.apache.jena.atlas.lib.IRILib;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.riot.RDFDataMgr;
import org.apache.xerces.util.URI;
import org.apache.xerces.util.URI.MalformedURIException;
import org.knowm.xchart.BitmapEncoder;
import org.knowm.xchart.BitmapEncoder.BitmapFormat;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.VectorGraphicsEncoder;
import org.knowm.xchart.VectorGraphicsEncoder.VectorGraphicsFormat;
import org.knowm.xchart.internal.chartpart.Chart;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
/**
* Command line frontend for the ldcharts tool.
*
* TODO Add option whether to place output files relative to input file or working directory
* TODO Create a bean class to hold the cli configuration
*
*
* @author raven Feb 22, 2018
*
*/
public class MainBeastChart {
private static final Logger logger = LoggerFactory.getLogger(MainBeastChart.class);
protected OptionParser parser = new OptionParser();
protected OptionSpec<?> pngOs;
protected OptionSpec<?> svgOs;
protected OptionSpec<?> jgpOs;
protected OptionSpec<?> guiOs;
protected OptionSpec<String> outputFolderOs;
protected OptionSpec<?> helpOs;
// protected OptionSpec<File> outputOs;
// protected OptionSpec<String> logFormatOs;
// protected OptionSpec<String> outFormatOs;
// protected OptionSpec<String> rdfizerOs;
public MainBeastChart() {
parser.allowsUnrecognizedOptions();
pngOs = parser.acceptsAll(Arrays.asList("png"), "Output charts in png format (Default if no other format is given)");
svgOs = parser.acceptsAll(Arrays.asList("svg"), "Output charts in svg format");
jgpOs = parser.acceptsAll(Arrays.asList("jgp"), "Output charts in jpg format");
guiOs = parser.acceptsAll(Arrays.asList("gui"), "Display charts in a window");
outputFolderOs = parser.acceptsAll(Arrays.asList("o", "output"), "Output folder").withRequiredArg().ofType(String.class);
}
public static String fileNameWithoutExtension(String fileName) {
int dotIndex = fileName.lastIndexOf(".");
String result = dotIndex >= 0 ? fileName.substring(0, dotIndex) : fileName;
return result;
}
public static String deriveFileName(String fileOrUri) throws MalformedURIException {
URI uri;
try {
uri = new URI(fileOrUri);
} catch(Exception e) {
String str = IRILib.filenameToIRI(fileOrUri);
uri = new URI(str);
}
String path = uri.getPath();
int offset = path.lastIndexOf('/') + 1;
String result = offset > 0 ? path.substring(offset) : null;
return result;
//System.out.println("Derived folder name: " + folderName);
}
public void run(String[] args) throws Exception {
OptionSet options = parser.parse(args);
List<?> inputResources = options.nonOptionArguments();
if(inputResources.isEmpty()) {
logger.error("No input resources (files or URLs) provided");
}
if(options.has(helpOs) || inputResources.isEmpty()) {
parser.printHelpOn(System.err);
return;
}
// TODO We could manage output formats in a list of output specification objects
boolean isPngOutputEnabled = options.has(pngOs);
boolean isSvgOutputEnabled = options.has(svgOs);
boolean isJgpOutputEnabled = options.has(jgpOs);
boolean isGuiOutputEnabled = options.has(guiOs);
boolean isAnyOutputEnabled = isSvgOutputEnabled || isPngOutputEnabled || isJgpOutputEnabled || isGuiOutputEnabled;
if(!isAnyOutputEnabled) {
isPngOutputEnabled = true;
}
boolean isOutputFolderNeeded = isSvgOutputEnabled || isPngOutputEnabled;
Model model = ModelFactory.createDefaultModel();
for(Object inputResource : inputResources) {
RDFDataMgr.read(model, Objects.toString(inputResource));
}
String outputFolderName = null;
File outputFolder = null;
if(isOutputFolderNeeded) {
String fileName;
outputFolderName = outputFolderOs.value(options);
if(outputFolderName == null) {
String tmpName = inputResources.stream().map(Objects::toString).findFirst().orElse(null);
fileName = tmpName != null ? deriveFileName(tmpName) : null;
if(fileName == null) {
throw new RuntimeException("Could not derive a folder name from " + tmpName + ". Please specify one via -o Option");
}
outputFolderName = fileNameWithoutExtension(fileName);
outputFolderName += "-charts";
}
outputFolder = new File(outputFolderName);
logger.info("Writing output to " + outputFolder.getAbsolutePath());
outputFolder.mkdir();
}
List<Entry<StatisticalBarChart, Model>> chartSpecs = ChartTransform.transform(model);
for(Entry<StatisticalBarChart, Model> chartSpec : chartSpecs) {
Chart<?, ?> xChart = ChartModelConfigurerXChart.toChart(chartSpec.getValue(), chartSpec.getKey());
String baseFileName = xChart.getTitle();
// TODO Check filenames more thoroughly
if(isPngOutputEnabled) {
File outFile = new File(outputFolder, baseFileName);
BitmapEncoder.saveBitmap(xChart, outFile.getPath(), BitmapFormat.PNG);
}
if(isSvgOutputEnabled) {
File outFile = new File(outputFolder, baseFileName);
VectorGraphicsEncoder.saveVectorGraphic(xChart, outFile.getPath(), VectorGraphicsFormat.SVG);
}
if(isJgpOutputEnabled) {
File outFile = new File(outputFolder, baseFileName);
BitmapEncoder.saveBitmap(xChart, outFile.getPath(), BitmapFormat.JPG);
}
if(isGuiOutputEnabled) {
new SwingWrapper<>(xChart).displayChart();
}
}
}
public static void main(String[] args) throws Exception {
new MainBeastChart().run(args);
}
}