-
Notifications
You must be signed in to change notification settings - Fork 0
/
Util.java
148 lines (132 loc) · 6.53 KB
/
Util.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
package org.deri.rdfagg.sparql.junitbenchmarkers;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;
import org.deri.rdfagg.sparql.algebra.HypergraphBuilderOp.DimensionDef;
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.NodeFactory;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.sparql.core.BasicPattern;
import com.hp.hpl.jena.sparql.core.Var;
import com.hp.hpl.jena.sparql.syntax.Template;
public class Util {
public static Model readTurtleModel(String rdfFilename) throws IOException {
Model m = ModelFactory.createDefaultModel();
InputStream in = rdfFilename.getClass().getResourceAsStream(rdfFilename);
m.read(in, "", "TTL");
in.close();
return m;
}
public static Model execConstruct(Model m, Query query){
QueryExecution qExec = QueryExecutionFactory.create(query, m);
Model res = qExec.execConstruct();
return res;
}
public static Model execConstruct(Model m, String query){
QueryExecution qExec = QueryExecutionFactory.create(query, m);
Model res = qExec.execConstruct();
return res;
}
static public Model readModel(String rdfFilename) throws IOException {
Model m = ModelFactory.createDefaultModel();
InputStream in = "".getClass().getResourceAsStream(rdfFilename);
in = new GZIPInputStream(in);
m.read(in, "", "TTL");
in.close();
return m;
}
static public Template buildTemplate(DimensionDef[] subDims, DimensionDef[] objDims) {
BasicPattern ptrn = new BasicPattern();
Node n1 = NodeFactory.createAnon();
Node n2 = NodeFactory.createAnon();
Node rel = NodeFactory.createAnon();
ptrn.add(new Triple(n1, NodeFactory.createURI("http://example.org/count"), Var.alloc("subD_count")));
for (DimensionDef subD : subDims) {
ptrn.add(new Triple(n1, NodeFactory.createURI(subD.uri), Var.alloc(subD.outVarname)));
}
ptrn.add(new Triple(n2, NodeFactory.createURI("http://example.org/count"), Var.alloc("objD_count")));
for (DimensionDef objD : objDims) {
ptrn.add(new Triple(n2, NodeFactory.createURI(objD.uri), Var.alloc(objD.outVarname)));
}
ptrn.add(new Triple(rel, NodeFactory.createURI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
NodeFactory.createURI("http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement")));
ptrn.add(new Triple(rel, NodeFactory.createURI("http://www.w3.org/1999/02/22-rdf-syntax-ns#subject"), n1));
ptrn.add(new Triple(rel, NodeFactory.createURI("http://www.w3.org/1999/02/22-rdf-syntax-ns#object"), n2));
ptrn.add(new Triple(rel, NodeFactory.createURI("http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate"), Var.alloc("prop")));
ptrn.add(new Triple(rel, NodeFactory.createURI("http://example.org/count"), Var.alloc("prop_count")));
Template tmplt = new Template(ptrn);
return tmplt;
}
public static final String SPARQL_COUNT_PROPS = "PREFIX :<http://example.org/> " +
"PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> " +
"CONSTRUCT{ " +
"[ a rdf:Statement; rdf:subject ?t1x; rdf:object ?t2x; rdf:predicate ?p; :count ?cnt ] . " +
"?t1x a ?t1 . " +
"?t2x a ?t2 . " +
"} " +
"WHERE{ " +
"SELECT ?p ?t1 (IRI(CONCAT (STR(?t1), \"_sss\")) AS ?t1x) ?t2 (IRI(CONCAT (STR(?t2), \"_ooo\")) AS ?t2x) ?cnt " +
"WHERE{ " +
"SELECT ?p ?t1 ?t2 (COUNT(*) AS ?cnt) " +
"WHERE{ " +
"?s ?p ?o; a ?t1 . " +
"?o a ?t2 . " +
"} GROUP BY ?t1 ?t2 ?p " +
"} " +
"}";
public static final String SPARQL_COUNT_SUBS = "PREFIX :<http://example.org/> " +
"CONSTRUCT{ " +
"?t1x :count ?cnt . " +
"} " +
"WHERE{ " +
"SELECT ?t1 (IRI(CONCAT (STR(?t1), \"_sss\")) AS ?t1x) ?cnt " +
"WHERE{ " +
"SELECT ?t1 (COUNT(DISTINCT ?s) AS ?cnt) " +
"WHERE{ " +
"?s ?p ?o; a ?t1 . " +
"?o a ?t2 . " +
"} GROUP BY ?t1 " +
"} " +
"}";
public static final String SPARQL_COUNT_OBS = "PREFIX :<http://example.org/> " +
"CONSTRUCT{ " +
"?t2x :count ?cnt . " +
"} " +
"WHERE{ " +
"SELECT ?t2 (IRI(CONCAT (STR(?t2), \"_ooo\")) AS ?t2x) ?cnt " +
"WHERE{ " +
"SELECT ?t2 (COUNT(DISTINCT ?o) AS ?cnt) " +
"WHERE{ " +
"?s ?p ?o; a ?t1 . " +
"?o a ?t2 . " +
"} GROUP BY ?t2 " +
"} " +
"}";
public static final String SPARQL_AGG_QUERY = "prefix : <http://example.org/> CONSTRUCT { _:b0 <http://example.org/count> ?subD_count . "
+ "_:b0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?t1 . "
+ "_:b1 <http://example.org/count> ?objD_count . "
+ "_:b1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?t2 . "
+ "_:b2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement> . "
+ "_:b2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#subject> _:b0 . "
+ "_:b2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#object> _:b1 . "
+ "_:b2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate> ?prop . "
+ "_:b2 <http://example.org/count> ?prop_count . "
+ "} WHERE {SELECT ?t1 ?subD_count ?t2 ?objD_count ?prop (COUNT(?prop) AS ?prop_count) { ?p1 a ?t1 . ?p2 a ?t2 . ?p1 ?prop ?p2 . { SELECT ?t1 (COUNT(DISTINCT ?s1) AS ?subD_count) WHERE { ?s1 a ?t1 . ?s2 a ?t2 . ?s1 ?prop ?s2 . } GROUP BY ?t1 } { SELECT ?t2 (COUNT(DISTINCT ?v2) AS ?objD_count) WHERE { ?v1 a ?t1 . ?v2 a ?t2 . ?v1 ?prop ?v2 . } GROUP BY ?t2 } } GROUP BY ?t1 ?t2 ?subD_count ?objD_count ?prop}";
public static final String AGG_SSE = "(prefix ((: <http://example.org/>)) "
+ "(bgp (triple ?p1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?t1) "
+ "(triple ?p2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?t2) (triple ?p1 ?prop ?p2) ))";
public static final String SIMPLE_SPARQL_AGG_QUERY = "prefix : <http://example.org/> CONSTRUCT { "
+ "_:b0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?t1 . "
+ "_:b1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?t2 . "
+ "_:b2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement> . "
+ "_:b2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#subject> _:b0 . "
+ "_:b2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#object> _:b1 . "
+ "_:b2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate> ?prop . "
+ "_:b2 <http://example.org/count> ?prop_count . "
+ "} WHERE {SELECT ?t1 ?t2 ?prop (COUNT(?prop) AS ?prop_count) { ?p1 a ?t1 . ?p2 a ?t2 . ?p1 ?prop ?p2 . } GROUP BY ?t1 ?t2 ?prop}";
}