|
| 1 | +import time |
| 2 | +from SPARQLWrapper import SPARQLWrapper, JSON |
| 3 | + |
| 4 | +# ------------------------- |
| 5 | +# SPARQL endpoint variable |
| 6 | +# ------------------------- |
| 7 | +SPARQL_API = "http://sorin-MS-7E06:7200/repositories/15files" # change to your endpoint |
| 8 | + |
| 9 | + |
| 10 | +def run_query(query, query_name): |
| 11 | + """Run a SPARQL query and print results with timing.""" |
| 12 | + print(f"\n=== Running {query_name} ===") |
| 13 | + |
| 14 | + sparql = SPARQLWrapper(SPARQL_API) |
| 15 | + sparql.setQuery(query) |
| 16 | + sparql.setReturnFormat(JSON) |
| 17 | + |
| 18 | + start_time = time.time() |
| 19 | + results = sparql.query().convert() |
| 20 | + end_time = time.time() |
| 21 | + |
| 22 | + elapsed = end_time - start_time |
| 23 | + print(f"⏱️ {query_name} completed in {elapsed:.3f} seconds") |
| 24 | + |
| 25 | + # Print each result row (compactly) |
| 26 | + for result in results["results"]["bindings"]: |
| 27 | + print({k: v["value"] for k, v in result.items()}) |
| 28 | + |
| 29 | + return elapsed |
| 30 | + |
| 31 | + |
| 32 | +# --------------------------------------------------- |
| 33 | +# Individual Query Functions |
| 34 | +# --------------------------------------------------- |
| 35 | + |
| 36 | +def query1_graduate_students(): |
| 37 | + query = """ |
| 38 | +PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> |
| 39 | +PREFIX ub: <http://swat.cse.lehigh.edu/onto/univ-bench.owl#> |
| 40 | +SELECT ?X |
| 41 | +WHERE { |
| 42 | + ?X rdf:type ub:GraduateStudent . |
| 43 | + ?X ub:takesCourse <http://www.Department0.University0.edu/GraduateCourse0> |
| 44 | +} |
| 45 | +""" |
| 46 | + return run_query(query, "Query 1 - Graduate Students taking GraduateCourse0") |
| 47 | + |
| 48 | + |
| 49 | +def query2_students_universities_departments(): |
| 50 | + query = """ |
| 51 | +PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> |
| 52 | +PREFIX ub: <http://swat.cse.lehigh.edu/onto/univ-bench.owl#> |
| 53 | +SELECT ?X ?Y ?Z |
| 54 | +WHERE { |
| 55 | + ?X rdf:type ub:GraduateStudent . |
| 56 | + ?Y rdf:type ub:University . |
| 57 | + ?Z rdf:type ub:Department . |
| 58 | + ?X ub:memberOf ?Z . |
| 59 | + ?Z ub:subOrganizationOf ?Y . |
| 60 | + ?X ub:undergraduateDegreeFrom ?Y |
| 61 | +} |
| 62 | +""" |
| 63 | + return run_query(query, "Query 2 - Students/Universities/Departments") |
| 64 | + |
| 65 | + |
| 66 | +def query3_publications_by_assistant_professor(): |
| 67 | + query = """ |
| 68 | +PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> |
| 69 | +PREFIX ub: <http://swat.cse.lehigh.edu/onto/univ-bench.owl#> |
| 70 | +SELECT ?X |
| 71 | +WHERE { |
| 72 | + ?X rdf:type ub:Publication . |
| 73 | + ?X ub:publicationAuthor <http://www.Department0.University0.edu/AssistantProfessor0> |
| 74 | +} |
| 75 | +""" |
| 76 | + return run_query(query, "Query 3 - Publications by AssistantProfessor0") |
| 77 | + |
| 78 | + |
| 79 | +def query4_professor_info(): |
| 80 | + query = """ |
| 81 | +PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> |
| 82 | +PREFIX ub: <http://swat.cse.lehigh.edu/onto/univ-bench.owl#> |
| 83 | +SELECT ?X ?Y1 ?Y2 ?Y3 |
| 84 | +WHERE { |
| 85 | + ?X rdf:type ub:Professor . |
| 86 | + ?X ub:worksFor <http://www.Department0.University0.edu> . |
| 87 | + ?X ub:name ?Y1 . |
| 88 | + ?X ub:emailAddress ?Y2 . |
| 89 | + ?X ub:telephone ?Y3 |
| 90 | +} |
| 91 | +""" |
| 92 | + return run_query(query, "Query 4 - Professor contact info") |
| 93 | + |
| 94 | + |
| 95 | +def query5_persons_in_department0(): |
| 96 | + query = """ |
| 97 | +PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> |
| 98 | +PREFIX ub: <http://swat.cse.lehigh.edu/onto/univ-bench.owl#> |
| 99 | +SELECT ?X |
| 100 | +WHERE { |
| 101 | + ?X rdf:type ub:Person . |
| 102 | + ?X ub:memberOf <http://www.Department0.University0.edu> |
| 103 | +} |
| 104 | +""" |
| 105 | + return run_query(query, "Query 5 - Persons in Department0") |
| 106 | + |
| 107 | + |
| 108 | +def query6_all_students(): |
| 109 | + query = """ |
| 110 | +PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> |
| 111 | +PREFIX ub: <http://swat.cse.lehigh.edu/onto/univ-bench.owl#> |
| 112 | +SELECT ?X WHERE { ?X rdf:type ub:Student } |
| 113 | +""" |
| 114 | + return run_query(query, "Query 6 - All Students") |
| 115 | + |
| 116 | + |
| 117 | +def query7_students_taught_by_associate_professor0(): |
| 118 | + query = """ |
| 119 | +PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> |
| 120 | +PREFIX ub: <http://swat.cse.lehigh.edu/onto/univ-bench.owl#> |
| 121 | +SELECT ?X ?Y |
| 122 | +WHERE { |
| 123 | + ?X rdf:type ub:Student . |
| 124 | + ?Y rdf:type ub:Course . |
| 125 | + ?X ub:takesCourse ?Y . |
| 126 | + <http://www.Department0.University0.edu/AssociateProfessor0> ub:teacherOf ?Y |
| 127 | +} |
| 128 | +""" |
| 129 | + return run_query(query, "Query 7 - Students taught by AssociateProfessor0") |
| 130 | + |
| 131 | + |
| 132 | +def query8_students_email_in_university0(): |
| 133 | + query = """ |
| 134 | +PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> |
| 135 | +PREFIX ub: <http://swat.cse.lehigh.edu/onto/univ-bench.owl#> |
| 136 | +SELECT ?X ?Y ?Z |
| 137 | +WHERE { |
| 138 | + ?X rdf:type ub:Student . |
| 139 | + ?Y rdf:type ub:Department . |
| 140 | + ?X ub:memberOf ?Y . |
| 141 | + ?Y ub:subOrganizationOf <http://www.University0.edu> . |
| 142 | + ?X ub:emailAddress ?Z |
| 143 | +} |
| 144 | +""" |
| 145 | + return run_query(query, "Query 8 - Student emails in University0") |
| 146 | + |
| 147 | + |
| 148 | +def query9_students_with_advisors_and_courses(): |
| 149 | + query = """ |
| 150 | +PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> |
| 151 | +PREFIX ub: <http://swat.cse.lehigh.edu/onto/univ-bench.owl#> |
| 152 | +SELECT ?X ?Y ?Z |
| 153 | +WHERE { |
| 154 | + ?X rdf:type ub:Student . |
| 155 | + ?Y rdf:type ub:Faculty . |
| 156 | + ?Z rdf:type ub:Course . |
| 157 | + ?X ub:advisor ?Y . |
| 158 | + ?Y ub:teacherOf ?Z . |
| 159 | + ?X ub:takesCourse ?Z |
| 160 | +} |
| 161 | +""" |
| 162 | + return run_query(query, "Query 9 - Students with advisors & courses") |
| 163 | + |
| 164 | + |
| 165 | +def query10_students_in_graduatecourse0(): |
| 166 | + query = """ |
| 167 | +PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> |
| 168 | +PREFIX ub: <http://swat.cse.lehigh.edu/onto/univ-bench.owl#> |
| 169 | +SELECT ?X |
| 170 | +WHERE { |
| 171 | + ?X rdf:type ub:Student . |
| 172 | + ?X ub:takesCourse <http://www.Department0.University0.edu/GraduateCourse0> |
| 173 | +} |
| 174 | +""" |
| 175 | + return run_query(query, "Query 10 - Students in GraduateCourse0") |
| 176 | + |
| 177 | + |
| 178 | +def query11_research_groups_university0(): |
| 179 | + query = """ |
| 180 | +PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> |
| 181 | +PREFIX ub: <http://swat.cse.lehigh.edu/onto/univ-bench.owl#> |
| 182 | +SELECT ?X |
| 183 | +WHERE { |
| 184 | + ?X rdf:type ub:ResearchGroup . |
| 185 | + ?X ub:subOrganizationOf <http://www.University0.edu> |
| 186 | +} |
| 187 | +""" |
| 188 | + return run_query(query, "Query 11 - Research groups in University0") |
| 189 | + |
| 190 | + |
| 191 | +def query12_chairs_and_departments(): |
| 192 | + query = """ |
| 193 | +PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> |
| 194 | +PREFIX ub: <http://swat.cse.lehigh.edu/onto/univ-bench.owl#> |
| 195 | +SELECT ?X ?Y |
| 196 | +WHERE { |
| 197 | + ?X rdf:type ub:Chair . |
| 198 | + ?Y rdf:type ub:Department . |
| 199 | + ?X ub:worksFor ?Y . |
| 200 | + ?Y ub:subOrganizationOf <http://www.University0.edu> |
| 201 | +} |
| 202 | +""" |
| 203 | + return run_query(query, "Query 12 - Chairs and Departments") |
| 204 | + |
| 205 | + |
| 206 | +def query13_alumni_of_university0(): |
| 207 | + query = """ |
| 208 | +PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> |
| 209 | +PREFIX ub: <http://swat.cse.lehigh.edu/onto/univ-bench.owl#> |
| 210 | +SELECT ?X |
| 211 | +WHERE { |
| 212 | + ?X rdf:type ub:Person . |
| 213 | + <http://www.University0.edu> ub:hasAlumnus ?X |
| 214 | +} |
| 215 | +""" |
| 216 | + return run_query(query, "Query 13 - Alumni of University0") |
| 217 | + |
| 218 | + |
| 219 | +def query14_undergraduate_students(): |
| 220 | + query = """ |
| 221 | +PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> |
| 222 | +PREFIX ub: <http://swat.cse.lehigh.edu/onto/univ-bench.owl#> |
| 223 | +SELECT ?X |
| 224 | +WHERE { ?X rdf:type ub:UndergraduateStudent } |
| 225 | +""" |
| 226 | + return run_query(query, "Query 14 - Undergraduate Students") |
| 227 | + |
| 228 | + |
| 229 | +# --------------------------------------------------- |
| 230 | +# Pipeline Runner |
| 231 | +# --------------------------------------------------- |
| 232 | +def run_pipeline(): |
| 233 | + print("=== 🚀 Starting SPARQL Query Pipeline ===") |
| 234 | + total_start = time.time() |
| 235 | + |
| 236 | + total_time = 0 |
| 237 | + total_time += query1_graduate_students() |
| 238 | + total_time += query2_students_universities_departments() |
| 239 | + total_time += query3_publications_by_assistant_professor() |
| 240 | + total_time += query4_professor_info() |
| 241 | + total_time += query5_persons_in_department0() |
| 242 | + total_time += query6_all_students() |
| 243 | + total_time += query7_students_taught_by_associate_professor0() |
| 244 | + total_time += query8_students_email_in_university0() |
| 245 | + total_time += query9_students_with_advisors_and_courses() |
| 246 | + total_time += query10_students_in_graduatecourse0() |
| 247 | + total_time += query11_research_groups_university0() |
| 248 | + total_time += query12_chairs_and_departments() |
| 249 | + total_time += query13_alumni_of_university0() |
| 250 | + total_time += query14_undergraduate_students() |
| 251 | + |
| 252 | + total_end = time.time() |
| 253 | + print("\n=== ✅ Pipeline Complete ===") |
| 254 | + print(f"Total measured query time: {total_time:.3f} seconds") |
| 255 | + print(f"Total wall-clock time: {total_end - total_start:.3f} seconds") |
| 256 | + |
| 257 | + |
| 258 | +# --------------------------------------------------- |
| 259 | +# Main Entry |
| 260 | +# --------------------------------------------------- |
| 261 | +if __name__ == "__main__": |
| 262 | + run_pipeline() |
0 commit comments