-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path162.java
More file actions
271 lines (227 loc) · 12.9 KB
/
Copy path162.java
File metadata and controls
271 lines (227 loc) · 12.9 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package com.github.tkurz.sparqlmm.doc;
import com.github.tkurz.sparqlmm.Constants;
import com.github.tkurz.sparqlmm.vocabularies.SCHEMA;
import com.google.common.base.Joiner;
import com.google.common.collect.FluentIterable;
import com.google.common.io.Files;
import org.openrdf.model.Model;
import org.openrdf.model.URI;
import org.openrdf.model.ValueFactory;
import org.openrdf.model.impl.LinkedHashModel;
import org.openrdf.model.impl.ValueFactoryImpl;
import org.openrdf.model.vocabulary.RDF;
import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.RDFHandlerException;
import org.openrdf.rio.Rio;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.*;
/**
* ...
* <p/>
* Author: Thomas Kurz (tkurz@apache.org)
*/
public class FunctionSet {
private static Logger logger = LoggerFactory.getLogger(FunctionSet.class);
public static String URL = Constants.NAMESPACE.substring(0, Constants.NAMESPACE.length()-1);
public static String TITLE = "SPARQL-MM Function Library for Media Fragments";
public static String DESCRIPTION = "SPARQL Function Library for handling Media Fragments URI like they are recommended from W3C at http://www.w3.org/TR/media-frags/ Media Fragments URI 1.0 (basic) recommendation.";
public static String CREATOR = "http://github.com/tkurz";
public static String PUBLISHER = "http://mico-project.eu";
public static String LICENSE = "http://www.apache.org/licenses/LICENSE-2.0";
public static String HOMEPAGE = "http://github.com/tkurz/sparql-mm";
public static String LINK_TURTLE = "index.ttl";
public static String LINK_XML = "index.rdf";
public static String STYLE_URL = "style.css";
private List<Function> functions = new ArrayList<>();
private Model model = new LinkedHashModel();
public FunctionSet(List<String> functionNames) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
for(String functionName : functionNames) {
functions.add(new Function(functionName));
}
Collections.sort(functions, new Comparator<Function>() {
@Override
public int compare(Function o1, Function o2) {
return o1.title.compareTo(o2.title);
}
});
//build sesame model
ValueFactory f = ValueFactoryImpl.getInstance();
URI vocabulary = f.createURI(URL);
model.add(vocabulary, RDF.TYPE, ValueFactoryImpl.getInstance().createURI("http://purl.org/net/schemas/sparql-extension-description#FunctionLibrary"));
model.add(vocabulary, SCHEMA.name, f.createLiteral(TITLE));
model.add(vocabulary, SCHEMA.description, f.createLiteral(DESCRIPTION));
model.add(vocabulary, SCHEMA.version, f.createLiteral(Constants.VERSION));
model.add(vocabulary, SCHEMA.creator, f.createURI(CREATOR));
model.add(vocabulary, SCHEMA.provider, f.createURI(PUBLISHER));
model.add(vocabulary, f.createURI(SCHEMA.NAMESPACE + "license"), f.createURI(LICENSE));
model.add(vocabulary, SCHEMA.url, f.createURI(HOMEPAGE));
for(Function function: functions) {
model.add(function.uri, RDF.TYPE, function.type);
model.add(function.uri, SCHEMA.name, f.createLiteral(function.title));
model.add(function.uri, SCHEMA.description, f.createLiteral(function.description));
model.add(function.uri, ValueFactoryImpl.getInstance().createURI("http://purl.org/net/schemas/sparql-extension-description#includedIn"), vocabulary);
model.add(vocabulary, ValueFactoryImpl.getInstance().createURI("http://purl.org/net/schemas/sparql-extension-description#includes"), function.uri);
}
}
public void toRDF(File file, RDFFormat format) throws RDFHandlerException, IOException {
Writer writer = Files.newWriter(file, Charset.forName("UTF-8"));
Rio.write(model, writer, format);
writer.flush();
writer.close();
}
public void toMD(File file) throws IOException {
PrintWriter out = new PrintWriter(file);
out.printf("# %s%n", TITLE);
out.println();
out.println("The URI for this vocabulary is");
out.println();
out.println("```");
out.println(Constants.NAMESPACE);
out.println("```");
out.println();
out.printf("When abbreviating terms the suggested prefix is `%s`%n", Constants.ABBREVIATION);
out.println();
out.println("Each function in this function set has a URI constructed by appending a term name to the vocabulary URI. For example:");
out.println();
out.println("```");
out.println(functions.get(0).uri.stringValue());
out.println("```");
out.println();
out.print("There are machine readable function description using [SPARQL Extension Description Vocabulary](http://www.ldodds.com/schemas/sparql-extension-description/) in ");
out.printf("[RDF/XML](%s) and [TURTLE](%s).%n", LINK_XML, LINK_TURTLE);
out.println();
for(FunctionDoc.Reference reference : FunctionDoc.Reference.values()) {
for(FunctionDoc.Type type : FunctionDoc.Type.values()) {
boolean show = false;
//test if it should be displayed
for (Function function : functions) {
if (function.reference == reference && function.typeName.equals(type.getName()) && function.type.equals(type.getUri())) {
show = true; break;
}
}
if(!show) continue;
out.printf("## %s %ss", reference.getName(), type.getName());
out.println();
out.println("| Relation name | Description |");
out.println("| :------------ |:------------|");
for(Function function: functions) {
if(function.reference == reference && function.typeName.equals(type.getName()) && function.type.equals(type.getUri())) out.printf("| %s:%s( %s ) | %s |%n", Constants.ABBREVIATION, function.uri.stringValue().substring(Constants.NAMESPACE.length()), function.getPropertyString(", ","*","*"), function.description);
}
out.println();
}
}
out.flush();
out.close();
}
public void toLatex(File file) throws IOException {
PrintWriter out = new PrintWriter(file);
out.printf("The id number follow the scheme described in D4.2.1-SPEC (Table 3)");
out.println("The base URI for the SPARQL-MM vocabulary is");
out.print("\\texttt{");
out.print(Constants.NAMESPACE);
out.println("}.");
out.printf("When abbreviating terms the suggested prefix is \\texttt{%s}.%n", Constants.ABBREVIATION);
out.println("Each function in this function set has a URI constructed by appending a term name to the vocabulary URI. For example:");
out.print("\\texttt{");
out.print(functions.get(0).uri.stringValue());
out.println("}.");
out.print("There are machine readable function description using \\textit{SPARQL Extension Description Vocabulary}\\footurl{http://www.ldodds.com/schemas/sparql-extension-description/} e.g. in ");
out.printf("RDF/XML\\footurl{https://raw.githubusercontent.com/tkurz/sparql-mm/master/ns/1.0.0/function/%s}.%n", LINK_XML);
out.println("%");
for(FunctionDoc.Reference reference : FunctionDoc.Reference.values()) {
for (FunctionDoc.Type type : FunctionDoc.Type.values()) {
boolean show = false;
//test if it should be displayed
for (Function function : functions) {
if (function.reference == reference && function.typeName.equals(type.getName()) && function.type.equals(type.getUri())) {
show = true; break;
}
}
if(!show) continue;
out.printf("\\subsubsection*{%s %ss}\n", reference.getName(), type.getName());
for (Function function : functions) {
if (function.reference == reference && function.typeName.equals(type.getName()) && function.type.equals(type.getUri())) {
out.println("\\begin{tabular}{|p{3cm}|p{10cm}|}");
out.printf("\\hline Name & %s:%s\\\\\n\\hline Properties & %s \\\\\n\\hline Description & %s\\\\\n\\hline\n", Constants.ABBREVIATION, function.uri.stringValue().substring(Constants.NAMESPACE.length()), function.getPropertyString(", ", "\\textit{", "}"), function.description);
out.println("\\end{tabular}");
out.println("\\vspace{0.3cm}");
out.println("\\newline");
}
}
}
}
out.flush();
out.close();
}
public void toHTML(File file) throws IOException {
PrintWriter out = new PrintWriter(file);
out.printf("<html><head><link rel=\"stylesheet\" type=\"text/css\" href=\"%s\"></head><body>\n", STYLE_URL);
out.printf("<h1>%s</h1>%n", TITLE);
out.println();
out.println("<p>");
out.printf("The URI for this vocabulary is <code>%s</code>. ", Constants.NAMESPACE);
out.printf("When abbreviating terms the suggested prefix is <code>%s</code>. ", Constants.ABBREVIATION);
out.printf("Each function in this function set has a URI constructed by appending a term name to the vocabulary URI. For example <code>%s</code>. ", functions.get(0).uri.stringValue());
out.print("There are machine readable function description using <a href=\"http://www.ldodds.com/schemas/sparql-extension-description/\">SPARQL Extension Description Vocabulary]</a> in ");
out.printf("<a href=\"%s\">RDF/XML</a> and <a href=\"%s\">TURTLE</a>.", LINK_XML, LINK_TURTLE);
out.println("</p>");
for(FunctionDoc.Reference reference : FunctionDoc.Reference.values()) {
for (FunctionDoc.Type type : FunctionDoc.Type.values()) {
boolean show = false;
//test if it should be displayed
for (Function function : functions) {
if (function.reference == reference && function.typeName.equals(type.getName()) && function.type.equals(type.getUri())) {
show = true; break;
}
}
if(!show) continue;
out.printf("<h2>%s %ss</h2>", reference.getName(), type.getName());
out.println("<table>");
out.println("<tr><th>Relation name</th><th>Description</th>");
for (Function function : functions) {
if (function.reference == reference && function.typeName.equals(type.getName()) && function.type.equals(type.getUri()))
out.printf("<tr><td>%s:%s( %s )</td><td>%s</td>\n", Constants.ABBREVIATION, function.uri.stringValue().substring(Constants.NAMESPACE.length()), function.getPropertyString(", ","<i>","</i>"), function.description);
}
out.println("</table>");
}
}
out.flush();
out.close();
}
class Function {
String title, description;
URI type;
String typeName;
URI uri;
FunctionDoc.Reference reference;
Class[] properties;
public Function(String className) throws ClassNotFoundException, IOException, IllegalAccessException, InstantiationException {
Class clazz = Class.forName(className);
if(clazz.getAnnotation(FunctionDoc.class) == null) throw new IOException("Class " + className + " is not properly annotated for documentation.");
FunctionDoc doc = (FunctionDoc) clazz.getAnnotation(FunctionDoc.class);
org.openrdf.query.algebra.evaluation.function.Function function = (org.openrdf.query.algebra.evaluation.function.Function) clazz.newInstance();
title = doc.title();
description = doc.description();
type = doc.type().getUri();
uri = ValueFactoryImpl.getInstance().createURI(function.getURI());
typeName = doc.type().getName();
reference = doc.reference();
properties = doc.properties();
logger.debug("read doc for {}: uri:{}, title:{}, description:{}", className, uri.stringValue(), title, description);
}
public String getPropertyString(String join, final String wrapStart, final String wrapEnd) {
return Joiner.on(join).join(FluentIterable.from(Arrays.asList(properties)).transform(new com.google.common.base.Function<Class, String>() {
@Override
public String apply(Class aClass) {
return wrapStart + aClass.getSimpleName() + wrapEnd;
}
}));
}
}
}