Skip to content

Commit

Permalink
Add annotations to output file
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Mendez committed May 8, 2014
1 parent 88486a8 commit 077335e
Showing 1 changed file with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@
*/
public class PropertyConstraintDumpProcessor {

public static final String DEFAULT_FILE_NAME = "constraints.owl";
public static final String COMMENT_A = "AnnotationAssertion( rdfs:comment ";
public static final String COMMENT_B = "\"";
public static final String COMMENT_C = "\" )";

public static final String DEFAULT_FILE_NAME = "constraints.owl";
public static final String OWL_END = "\n\n)\n\n";

public static final String OWL_START = ""
+ "Prefix(:=<http://www.wikidata.org/owl/constraints/>)"
+ "\nPrefix(xsd:=<http://www.w3.org/2001/XMLSchema#>)"
Expand All @@ -66,6 +68,24 @@ void printLines(List<String> lines, BufferedWriter output)
output.flush();
}

public String escapeChars(String str) {
return str.replaceAll("&", "&amp;").replaceAll("\"", "&quot;")
.replaceAll("<", "&lt;").replaceAll("'", "&apos;");
}

public void printTemplates(Map<String, List<Template>> templateMap,
BufferedWriter output) throws IOException {
for (String key : templateMap.keySet()) {
List<Template> templates = templateMap.get(key);
output.write(COMMENT_A);
output.write(":" + key);
output.write(COMMENT_B);
output.write(escapeChars(templates.toString()));
output.write(COMMENT_C);
output.newLine();
}
}

public void processDumps(BufferedWriter output) throws IOException {
DumpProcessingController controller = new DumpProcessingController(
WIKIDATAWIKI);
Expand All @@ -75,6 +95,7 @@ public void processDumps(BufferedWriter output) throws IOException {
controller.processAllRecentRevisionDumps();

output.write(OWL_START);
printTemplates(propertyTalkTemplateProcessor.getMap(), output);
processTemplates(propertyTalkTemplateProcessor.getMap(), output);
output.write(OWL_END);
}
Expand All @@ -85,16 +106,31 @@ public void processTemplates(Map<String, List<Template>> templateMap,
ConstraintMainRenderer renderer = new ConstraintMainRenderer();
TemplateExpander expander = new TemplateExpander();
for (String key : templateMap.keySet()) {
output.newLine();
List<Template> templates = expander.expand(key,
templateMap.get(key));
for (Template template : templates) {
Constraint constraint = parser.parse(template);

Constraint constraint = null;
try {
constraint = parser.parse(template);
} catch (Exception e) {
System.out.println("Exception while parsing " + key);
System.out.println("Template: " + template.toString());
e.printStackTrace();
}

List<String> owlLines = null;
try {
owlLines = constraint.accept(renderer);
if (constraint != null) {
owlLines = constraint.accept(renderer);
}
} catch (Exception e) {
System.out.println("Exception while rendering " + key);
System.out.println("Constraint: " + constraint.toString());
e.printStackTrace();
}

printLines(owlLines, output);
}
}
Expand Down

0 comments on commit 077335e

Please sign in to comment.