from IPython.display import Image
from IPython.core.display import HTML 
from rsplib.processing import RSPSource, StreamReasoner

"""
Explanation: Part 2 :  Stream Reasoning with Jasper
In this part of the tutorial, we will see how to query  RDF Streams under an simple entailment regime
using a stream reasoner called Jasper.
The JASPER is an RSP engine that offers rule-based reasoning capabilities. It consumes queries
written in RSP-QL syntax and fully implements the RSP-QL reference model.
RDFLib
RSPLib is the python library that we are going to use in our tutorial.
It offers abstrations to manipulate RSPSources, 
e.g. RDF Streams and to interact with RSP Engines, e.g.
registering streams and queries.
RSPLib APIs are still under development. In this tutorial we are going to use version 0.3.4 which
is available at https://pypi.python.org/pypi/rsplib/.
to upgrade it just type !pip instal rsplib --upgrade in a cell
source code available at https://github.com/streamreasoning/rsplib
End of explanation
"""


jasper = StreamReasoner("http://jasper", 8183);
jasper.status()

"""
Explanation: Now one we assume that RDF Stream are up and running from Part 1. If you did not followed part one please follow the link below and complete the part about TripleWave and RDF Streams.
http://localhost:8888/notebooks/work/streamapp/StreamApp%20-%20Part%201%20-%20%20RDF%20Stream%20Processing%20with%20the%20CSPARQL%20engine.ipynb
Stream Reasoner: Jasper
Now that we have our RDF Stream running, we can start thinking about how to query them.
To this extent, we are going to use jasper.
Did you started csparql in the consumer folder?
RSPLib offers a facade to communicate with Stream Reasoners via the SR Services (a RESTful interface for SRs).
End of explanation
"""


jasper.register_stream("AarhusTrafficData158505", "http://aarhustrafficdata158505:4001/sgraph")

jasper.register_stream("AarhusTrafficData182955", "http://aarhustrafficdata182955:4000/sgraph")

"""
Explanation: Registering Streams
End of explanation
"""


Image(url= "https://raw.githubusercontent.com/streamreasoning/rsplab/tutorial/collector/lab/streamapp/images/tbox.png")

"""
Explanation: Adding domain knowledge: TBOX
In order to perform some reasoning, we require to capture some knowledge about our domain.
In this example the ontology available at here
contains a simple hierarchi
CongestionLevelA and CongenstionLevelB are subclasses of CongestionLevelT.
End of explanation
"""


tbox = "https://raw.githubusercontent.com/streamreasoning/rsplab/tutorial/collector/lab/streamapp/tbox.rdf"

"""
Explanation: If we have a look to our streams again: 
- Locally: http://localhost:8888/notebooks/work/streamapp/Observing%20The%20Stream.ipynb
- On GitHub: https://github.com/streamreasoning/rsplab/blob/tutorial/collector/lab/streamapp/Observing%20The%20Output%20-%20Part%202.ipynb
we will observer that either CongestionLevelA or CongenstionLevelB are present.
We are now going to show how to look for CongestionLevelT adding some reasoning.
End of explanation
"""


with open('rdfs.rules.txt', 'r') as rule_file:
    rdfs=rule_file.read()
rdfs

"""
Explanation: Load Some rules from our file, e.g. RDFS entailment
End of explanation
"""


jasper.register_rules("rdfs", rdfs)

jasper.rules()

"""
Explanation: Register RuleSet
End of explanation
"""


with open('q4.rspql.txt', 'r') as rspql_query:
   body = rspql_query.read()
   print(body)

"""
Explanation: Query RDF Streams Under Graph Level Entailment
RSP-QL Syntax
the syntax we are now using is called RSP-QL syntax.
The rsp w3c working group is currently working on its specification.
Jasper is an early adopter for this syntax.
End of explanation
"""


jasper.register_query("ct", "STREAM", body, "rdfs", tbox)

"""
Explanation: Registering the Query
Similarly to what we did for the CSPARQL engine, also for jasper it is necessary to register the query.
However, this time we have to specify which rule set and which tbox use during the query answering.
End of explanation
"""


jasper.register_observer("ct", "default", {"host":"jasper","type":"ws","port":8283,"name":"default"})

"""
Explanation: Registering the Observer
Also in this case, we want to observe the results of our query answering on the web.
End of explanation
"""


Image(url= "https://raw.githubusercontent.com/streamreasoning/rsplab/tutorial/collector/lab/streamapp/images/csparq_grafana.png")

"""
Explanation: Are you courious how the output stream looks like?
http://localhost:8888/notebooks/work/streamapp/Observing%20The%20Output%20-%20Part%202.ipynb
Measuring the Performance
RSPLab offers assisted realt-time performance monitoring using cAdvisor and Grafana.
In order to observe the current status of the engine you can visit
http://localhost:3000/dashboard/db/jasper?orgId=1
in case you didn't access yet, username:admin password:admin
End of explanation
"""


jasper.unregister_observer("ct", "default")

jasper.unregister_query("ct")

jasper.unregister_stream("AarhusTrafficData182955")

jasper.unregister_stream("AarhusTrafficData158505")

jasper.status()

"""
Explanation: Clean Up
End of explanation
"""


