-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHomework.java
79 lines (63 loc) · 3.13 KB
/
Homework.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
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.jena.ontology.Individual;
import org.apache.jena.ontology.OntModel;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.ResultSet;
import org.apache.jena.rdf.model.Property;
public class Homework extends Base
{
public static OntModel m;
public static void main( String[] args ) {
new Homework().setArgs( args ).run();
}
public void run() {
// Create model from owl ontology file
m = Functions.CreateModelFromOwlFile("ontology.owl");
// Get Dorna drivers from JSON file
ArrayList<HashMap<String, String>> dornaRidersValues = Functions.ParseJsonArrayFile("Dorna.json", "n", "sn", "na");
for(int i = 0; i < dornaRidersValues.size(); i++) {
try
{
// Get data
String name = dornaRidersValues.get(i).get("n");
String surname = dornaRidersValues.get(i).get("sn");
String riderNameFormatted = Functions.MapRiderNameForDbPedia(name, surname);
String query = String.format(Functions.GetSourceQuery("dbpedia", "rider"),
riderNameFormatted, riderNameFormatted, riderNameFormatted, riderNameFormatted);
// Execute query on dbpedia
ResultSet resultQuery = Functions.ExecuteSparqlQueryOnSource("dbpedia", query);
// Properties mapping
HashMap<String, String> propertiesMap = new HashMap<String, String>();
propertiesMap.put("name", "foaf:name");
propertiesMap.put("team", "http://www.semanticweb.org/andre/ontologies/2019/11/homework-swt#corp");
propertiesMap.put("bikeNumber", "http://www.semanticweb.org/andre/ontologies/2019/11/homework-swt#number");
// Retrieve query solution
QuerySolution solution = resultQuery.nextSolution();
// Create individual rider with properties mapped
Individual riderIndividual = Functions.CreateModelIndividual
(m, solution,
"http://www.semanticweb.org/andre/resources/2019/11/" + Functions.MapRiderNameForDbPedia(name, surname),
"http://www.semanticweb.org/andre/ontologies/2019/11/homework-swt#rider",
propertiesMap);
// Add nationality property
Property nationalityProp = m.getProperty("http://www.semanticweb.org/andre/ontologies/2019/11/homework-swt#nationality");
riderIndividual.addProperty(nationalityProp, dornaRidersValues.get(i).get("na"));
// Logging
System.out.println(String.format("Adding %s", riderNameFormatted));
// Add individual to fuseki
Functions.AddIndividualToFuseki("http://localhost:3030/ds/update",
"rider",
riderNameFormatted);
// Write model to file as RDF/XML
m.write(new FileOutputStream("model.rdf"), "RDF/XML");
Functions.PrintFusekiInvididuals("http://localhost:3030/ds/query");
}
catch(Exception e) {
// If we couldn't find some riders on dbpedia, skip it
//System.out.println(e.getMessage());
}
}
}
}