-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstPhase.py
More file actions
71 lines (52 loc) · 2.34 KB
/
FirstPhase.py
File metadata and controls
71 lines (52 loc) · 2.34 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
from flask import Flask, render_template, request
from rdflib import Graph, Namespace
from rdflib.namespace import RDF, OWL, RDFS, XSD
app = Flask(__name__)
g = Graph()
g.parse("json-ontology.jsonld", format="json-ld") # OWL file path and format
# Namespace definition
ns = Namespace("http://www.semanticweb.org/satilmis/ontologies/2024/10/ontologygame#")
g.bind(":", ns)
g.bind("rdf", RDF)
g.bind("owl", OWL)
g.bind("rdfs", RDFS)
g.bind("xsd", XSD)
@app.route("/", methods=["GET", "POST"])
def home():
platforms = request.form.getlist("platform")
genres = request.form.getlist("genre")
difficulties = request.form.getlist("difficulty")
sparql_query = """
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX : <http://www.semanticweb.org/satilmis/ontologies/2024/10/ontologygame#>
SELECT DISTINCT ?game
WHERE {
?game rdfs:subClassOf :Game .
"""
filters = []
# Add platform filter
if platforms and "any" not in platforms:
platform_filters = " UNION ".join([f"{{ ?game rdfs:subClassOf [ owl:onProperty :hasPlatform ; owl:someValuesFrom :{p} ] }}" for p in platforms])
filters.append(f"({platform_filters})")
# Add genre filter
if genres and "any" not in genres:
genre_filters = " UNION ".join([f"{{ ?game rdfs:subClassOf [ owl:onProperty :hasGenre ; owl:someValuesFrom :{g} ] }}" for g in genres])
filters.append(f"({genre_filters})")
# Add difficulty filter
if difficulties and "any" not in difficulties:
difficulty_filters = " UNION ".join([f"{{ ?game rdfs:subClassOf [ owl:onProperty :hasDifficulty ; owl:someValuesFrom :{d} ] }}" for d in difficulties])
filters.append(f"({difficulty_filters})")
# Combine filters into the query
if filters:
sparql_query += " FILTER (" + " && ".join(filters) + ")"
sparql_query += "\n}"
# Execute the SPARQL query
results = g.query(sparql_query)
# Extract results
games = [str(row.game).split("#")[-1] for row in results]
return render_template("index.html", games=games)
if __name__ == "__main__":
app.run(debug=True,port=5051)