logicalbotAndKG.ipynb

{
“cells”: [
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“# Intelligent Systems 2021: 4rd practical assignment \n”,
“## Logical Agents\n”,
“\n”,
“Your name: \n”,
“\n”,
“Your VUnetID: \n”,
“\n”,
“If you do not provide your name and VUnetID we will not accept your submission. \n”,
“\n”,
“### Learning objectives\n”,
“At the end of this exercise you should be able to work with logical agents on the Schnapsen platform\n”,
“\n”,
“1. Be able to apply a SAT solver to check for satisfiability and entailment (bots)\n”,
“2. Learn on how to use a knowledge graph \n”,
“3. Have a first idea on how to use a knowledge graph for playing Schnapsen.\n”,
“\n”,
“### Preliminaries\n”,
“\n”,
“In this worksheet we’ll get to know Propositional Logic and RDF.\n”,
“We start building a knowledge graph based agent for playing Schnapsen. The idea is to represent the game and the game states fully in RDF and use queries and logical entailments for getting information.\n”,
“We are not implementing any game play strategy yet.\n”,
“\n”,
“First things first, let’s deal with dependencies for using the kb bot, namely numpy and scipy. They should be installed fairly easily via \”pip install numpy\”, \”pip install scipy\”.\n”,
“Furthermore you need the packages rdflib and owlrl to work with knowledge graphs.\n”,
“rdflib is a in memory database for RDF.\n”,
“owlrl is a reasoning library for RDF-S and OWL which uses rdflib.\n”,
“\n”,
“\n”,
“### Practicalities\n”,
“\n”,
“Follow this Notebook step-by-step. \n”,
“\n”,
“Of course, you can do the exercises in any Programming Editor of your liking. But you do not have to. Feel free to simply write code in the Notebook. Please use your studentID+Assignment4.ipynb as the name of the Notebook. \n”,
“\n”,
“Note: unlike the courses dedicated to programming we will not evaluate the style of the programs. But we will, however, test your programs on other data that we provide, and your program should give the correct output to the test-data as well.\n”,
“\n”,
“As was mentioned, the assignment is graded as pass/fail. To pass you need to have either a full working code or an explanation of what you tried and what didn’t work for the tasks that you were unable to complete (you can use multi-line comments or a text cell).\n”,
“\n”,
“## Initialising \n”,
“\n”,
“First, let us make sure the necessary packages are installed, and imported. Run the following code:”
]
},
{
“cell_type”: “code”,
“execution_count”: 1,
“metadata”: {},
“outputs”: [],
“source”: [
“import sys, random\n”,
“from bots.kbbot.kb import KB, Boolean, Integer, Constant\n”,
“from api import State, engine, util”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“## SAT solving and Entailment checking \n”,
“\n”,
“We will start with some exercises to use an existing SAT solver to check for satisfiability and entailment. “
]
},
{
“cell_type”: “code”,
“execution_count”: 2,
“metadata”: {},
“outputs”: [],
“source”: [
“# Define our symbols\n”,
“A = Boolean(‘A’)\n”,
“B = Boolean(‘B’)\n”,
“C = Boolean(‘C’)\n”,
“\n”,
“# Create a new knowledge base\n”,
“kb = KB()\n”,
“\n”,
“# Add clauses\n”,
“kb.add_clause(A, B, C)\n”,
“kb.add_clause(~A, B)\n”,
“kb.add_clause(~B, C)\n”,
“kb.add_clause(B, ~C)”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“This file first defines the three boolean symbols we will use (A, B and C), creates an empty knowledge base, and then adds the four following clauses:\n”,
“> A or B or C
\n”,
“> (not A) or B
\n”,
“> (not B) or C
\n”,
“> B or not C\n”,
“\n”,
“### Task 1\n”,
“Are there any models (assignments of values to these variables that make all clauses true)? Write down all the models of the knolwedge base in the following cell. “
]
},
{
“cell_type”: “code”,
“execution_count”: 3,
“metadata”: {},
“outputs”: [],
“source”: [
“#Put your answers here\n”,
“\n”,
” # A-T, B-T, C-T\n”,
” # A-F, B-T, C-T”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“Now run the script and report the output in the cell after the code. Explain what it means. “
]
},
{
“cell_type”: “code”,
“execution_count”: 4,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“{A: False, B: True, C: True}\n”,
“{A: True, B: True, C: True}\n”,
“True\n”
]
}
],
“source”: [
“# Print all models of the knowledge base\n”,
“for model in kb.models():\n”,
” print(model)\n”,
“\n”,
“# Print out whether the KB is satisfiable (if there are no models, it is not satisfiable)\n”,
“print(kb.satisfiable())\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 5,
“metadata”: {},
“outputs”: [],
“source”: [
“#Put your answers here\n”,
“Report1=\”\”\”{A: False, B: True, C: True}\n”,
“{A: True, B: True, C: True}\n”,
“True\n”,
“\”\”\””
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“Now it is time to adapt your first knowledge-base, by adding stuff…. \n”,
“\n”,
“## Tasks 2: \n”,
“Add a clause to the knowledge base to that it becomes unsatisfiable. Report the line of code you added, and in a separate line, the result that you get from the SAT solver and your script. “
]
},
{
“cell_type”: “code”,
“execution_count”: 6,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“False\n”
]
}
],
“source”: [
“# A or not B\n”,
“kb.add_clause(~B,~C)\n”,
“for model in kb.models():\n”,
” print(model)\n”,
“\n”,
“print(kb.satisfiable())\n”,
“\n”,
“#Since there are no models printed as result of adding the above clause , we also get that knowledge base is unsatisfiable.\n”,
“# we already have clause with ‘A or B or C’ ,'(not A) or B’ ,'(not B) or C’ ,’B or not C’, and now we added ‘not B or (not C)’\n”,
“# which makes the models zero as all are nullified.”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“Now you can already to build your own knowledge base, and do reasoning/inferencing with it. For example, check satisfiability of one of the questions of the working group automatically. \n”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“### Task 3:\n”,
“Let KB be the knowledge base introduced in Exercise 7 of the Worksheet on Logical Agents: KB=\n”,
“> (P v Q) ^ (Q -> R) ^ (R -> -P)
\n”,
“> -(P <-> – Q)\n”,
“\n”,
“Write a new version of the script above to prove or disprove whether KB is satisfiable or not. You can put both the knowledge base construction (you will have to translate the axioms into CNF by hand before adding them to the KB), the printing of the model and the satisfiability check in the next cell. \n”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“PVQ, -QVR, -RV-P, -QVP, Qv-Q, -PVP, -PV-Q”
]
},
{
“cell_type”: “code”,
“execution_count”: 7,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“{P: False, Q: True, R: True}\n”,
“{P: True, Q: False, R: False}\n”,
“True\n”
]
}
],
“source”: [
“#Put your answers here\n”,
“# Define our symbols\n”,
“P = Boolean(‘P’)\n”,
“Q = Boolean(‘Q’)\n”,
“R = Boolean(‘R’)\n”,
“\n”,
“# Create a new knowledge base\n”,
“kb = KB()\n”,
“\n”,
“# Add clause\n”,
“kb.add_clause(P, Q)\n”,
“kb.add_clause(~Q, R)\n”,
“kb.add_clause(~R, ~P)\n”,
“kb.add_clause(~P, ~Q)\n”,
“kb.add_clause(Q, P)\n”,
“\n”,
“for model in kb.models():\n”,
” print(model)\n”,
“\n”,
“print(kb.satisfiable())”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“### Task 4: \n”,
“Exercise 9 of this week’s work session on logical agents contained the following knowledge base KB:\n”,
“\n”,
“>-A -> B
\n”,
“> B -> A
\n”,
“> A -> (C ^ D)\n”,
“\n”,
“Convert it to clause normal form, and write a script that creates this knowledge base as you did before. There are different subtasks: \n”,
“1) Print out its models and report them.
\n”,
“2) As seen in the exercise, the knowledge base entails A ^ C ^ D. Explain in your own words what this says about the possible models for the knowledge base?\n”,
“\n”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“AVB,-BVA,-AVC,-AVD, A,C,D”
]
},
{
“cell_type”: “code”,
“execution_count”: 8,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“{A: True, D: True, C: True, B: False}\n”,
“{A: True, D: True, C: True, B: True}\n”,
“True\n”
]
}
],
“source”: [
“# Define our symbols\n”,
“A = Boolean(‘A’)\n”,
“B = Boolean(‘B’)\n”,
“C = Boolean(‘C’)\n”,
“D = Boolean(‘D’)\n”,
“\n”,
“# Create a new knowledge base\n”,
“kb = KB()\n”,
“\n”,
“kb.add_clause(A, B)\n”,
“kb.add_clause(~B, A)\n”,
“kb.add_clause(~A, C)\n”,
“kb.add_clause(~A, D)\n”,
“\n”,
“# Add clause\n”,
“for model in kb.models():\n”,
” print(model)\n”,
“\n”,
“print(kb.satisfiable())\n”,
“#Models for knowledge base\n”,
“# {A: True, D: True, C: True, B: False}\n”,
“# {A: True, D: True, C: True, B: True}\n”,
“# we focus on models that evaluate KB to true, it is irrelevent to evaluate models that make KB false.”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“In the previous question, you have shown that A ^ C ^ D is entailed by KB semantically (following the definition of entailment), where you used the prover to give you all the models. Now let us see whether you can actually prove entailment using the SAT solver directly.\n”,
“\n”,
“### Task 5: \n”,
“Show entailment of A ^ C ^ D by a proof by refutation. Write the script like above with the knowledge base your create in the following executable cell, including the control statements. \n”
]
},
{
“cell_type”: “code”,
“execution_count”: 9,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“{A: False, D: False, C: False, B: False}\n”,
“{A: False, D: False, C: False, B: True}\n”,
“True\n”
]
}
],
“source”: [
“# Define our symbols\n”,
“A = Boolean(‘A’)\n”,
“B = Boolean(‘B’)\n”,
“C = Boolean(‘C’)\n”,
“D = Boolean(‘D’)\n”,
“\n”,
“# Create a new knowledge base\n”,
“kb = KB()\n”,
“\n”,
“# Add clause\n”,
“kb.add_clause(~A, ~B)\n”,
“kb.add_clause(B, ~A)\n”,
“kb.add_clause(A, ~C)\n”,
“kb.add_clause(A, ~D)\n”,
“\n”,
“for model in kb.models():\n”,
” print(model)\n”,
“\n”,
“print(kb.satisfiable())\n”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“Explain in your own words what you have done, and what you can conclude from the output of your script for Task 5.”
]
},
{
“cell_type”: “code”,
“execution_count”: 10,
“metadata”: {},
“outputs”: [],
“source”: [
“# we select two clauses that have conflict and then combine them and we cancel out the terms in them that are in conflict\n”,
“Report2=\”\”\”{A: False, D: False, C: False, B: False}\n”,
“{A: False, D: False, C: False, B: True}\n”,
“True\n”,
“\”\”\””
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“# Playing Schnapsen with a Knowledge Graph\n”,
“\n”,
“As demonstrated during the lecture, we can model Schnapsen also as a basic knowledge graph using RDF and RDF-S.\n”,
“In the following tasks, we will built a small knowledge graph which models a Schnapsen game using the two Python libraries rdflib and owl-rl.”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“First, you need to install the two Python libraries.”
]
},
{
“cell_type”: “code”,
“execution_count”: 11,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“Requirement already satisfied: rdflib in c:\users\indekaur\appdata\local\programs\python\python310\lib\site-packages (6.0.2)\n”,
“Requirement already satisfied: isodate in c:\users\indekaur\appdata\local\programs\python\python310\lib\site-packages (from rdflib) (0.6.0)\n”,
“Requirement already satisfied: setuptools in c:\users\indekaur\appdata\local\programs\python\python310\lib\site-packages (from rdflib) (57.4.0)\n”,
“Requirement already satisfied: pyparsing in c:\users\indekaur\appdata\local\programs\python\python310\lib\site-packages (from rdflib) (3.0.6)\n”,
“Requirement already satisfied: six in c:\users\indekaur\appdata\local\programs\python\python310\lib\site-packages (from isodate->rdflib) (1.16.0)\n”
]
},
{
“name”: “stderr”,
“output_type”: “stream”,
“text”: [
“WARNING: You are using pip version 21.2.3; however, version 21.3.1 is available.\n”,
“You should consider upgrading via the ‘C:\Users\indekaur\AppData\Local\Programs\Python\Python310\python.exe -m pip install –upgrade pip’ command.\n”
]
},
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“Requirement already satisfied: owlrl in c:\users\indekaur\appdata\local\programs\python\python310\lib\site-packages (6.0.2)\n”,
“Requirement already satisfied: rdflib>=6.0.2 in c:\users\indekaur\appdata\local\programs\python\python310\lib\site-packages (from owlrl) (6.0.2)\n”,
“Requirement already satisfied: pyparsing in c:\users\indekaur\appdata\local\programs\python\python310\lib\site-packages (from rdflib>=6.0.2->owlrl) (3.0.6)\n”,
“Requirement already satisfied: setuptools in c:\users\indekaur\appdata\local\programs\python\python310\lib\site-packages (from rdflib>=6.0.2->owlrl) (57.4.0)\n”,
“Requirement already satisfied: isodate in c:\users\indekaur\appdata\local\programs\python\python310\lib\site-packages (from rdflib>=6.0.2->owlrl) (0.6.0)\n”,
“Requirement already satisfied: six in c:\users\indekaur\appdata\local\programs\python\python310\lib\site-packages (from isodate->rdflib>=6.0.2->owlrl) (1.16.0)\n”
]
},
{
“name”: “stderr”,
“output_type”: “stream”,
“text”: [
“WARNING: You are using pip version 21.2.3; however, version 21.3.1 is available.\n”,
“You should consider upgrading via the ‘C:\Users\indekaur\AppData\Local\Programs\Python\Python310\python.exe -m pip install –upgrade pip’ command.\n”
]
}
],
“source”: [
“import sys\n”,
“!{sys.executable} -m pip install rdflib\n”,
“!{sys.executable} -m pip install owlrl”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“In this first code snippet, we will create an ontology modelling Schnapsen similar to the one we have introduced in the lecture.\n”,
“We will use the triple store library rdflib to model our knowledge graph.”
]
},
{
“cell_type”: “code”,
“execution_count”: 12,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“)>”
]
},
“execution_count”: 12,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“from rdflib import Graph, Literal, Namespace\n”,
“from rdflib.namespace import RDF, RDFS\n”,
“\n”,
“g = Graph()\n”,
“s = Namespace(\”http://schnapsen.org/\”)\n”,
“\n”,
“\n”,
“\n”,
“#suits\n”,
“g.add((s.Hearts, RDFS.subClassOf, s.Card))\n”,
“g.add((s.Spades, RDFS.subClassOf, s.Card))\n”,
“g.add((s.Diamonds, RDFS.subClassOf, s.Card))\n”,
“g.add((s.Clubs, RDFS.subClassOf, s.Card))\n”,
“\n”,
“#ranks\n”,
“g.add((s.Jack, RDFS.subClassOf, s.Card))\n”,
“g.add((s.Queen, RDFS.subClassOf, s.Card))\n”,
“g.add((s.King, RDFS.subClassOf, s.Card))\n”,
“g.add((s.Ten, RDFS.subClassOf, s.Card))\n”,
“g.add((s.Ace, RDFS.subClassOf, s.Card))\n”,
” \n”,
“\n”,
“#information about cards\n”,
“g.add((s.JackHearts, RDF.type, s.Heartss))\n”,
“g.add((s.JackHearts, RDF.type, s.Jack))\n”,
“g.add((s.JackHearts, s.value, Literal(‘2’)))\n”,
“\n”,
“g.add((s.JackSpades, RDF.type, s.Spades))\n”,
“g.add((s.JackSpades, RDF.type, s.Jack))\n”,
“g.add((s.JackSpades, s.value, Literal(‘2’)))\n”,
“\n”,
“g.add((s.JackDiamonds, RDF.type, s.Diamonds))\n”,
“g.add((s.JackDiamonds, RDF.type, s.Jack))\n”,
“g.add((s.JackDiamonds, s.value, Literal(‘2’)))\n”,
“\n”,
“g.add((s.JackClubs, RDF.type, s.Clubs))\n”,
“g.add((s.JackClubs, RDF.type, s.Jack))\n”,
“g.add((s.JackClubs, s.value, Literal(‘2’)))\n”,
“\n”,
“g.add((s.QueenHearts, RDF.type, s.Hearts))\n”,
“g.add((s.QueenHearts, RDF.type, s.Queen))\n”,
“g.add((s.QueenHearts, s.value, Literal(‘3’)))\n”,
“\n”,
“g.add((s.QueenSpades, RDF.type, s.Spades))\n”,
“g.add((s.QueenSpades, RDF.type, s.Queen))\n”,
“g.add((s.QueenSpades, s.value, Literal(‘3’)))\n”,
“\n”,
“g.add((s.QueenDiamonds, RDF.type, s.Diamonds))\n”,
“g.add((s.QueenDiamonds, RDF.type, s.Queen))\n”,
“g.add((s.QueenDiamonds, s.value, Literal(‘3’)))\n”,
“\n”,
“g.add((s.QueenClubs, RDF.type, s.Clubs))\n”,
“g.add((s.QueenClubs, RDF.type, s.Queen))\n”,
“g.add((s.QueenClubs, s.value, Literal(‘3’)))\n”,
“\n”,
“g.add((s.KingHearts, RDF.type, s.Hearts))\n”,
“g.add((s.KingHearts, RDF.type, s.King))\n”,
“g.add((s.KingHearts, s.value, Literal(‘4’)))\n”,
“\n”,
“g.add((s.KingSpades, RDF.type, s.Spades))\n”,
“g.add((s.KingSpades, RDF.type, s.King))\n”,
“g.add((s.KingSpades, s.value, Literal(‘4’)))\n”,
“\n”,
“g.add((s.KingDiamonds, RDF.type, s.Diamonds))\n”,
“g.add((s.KingDiamonds, RDF.type, s.King))\n”,
“g.add((s.KingDiamonds, s.value, Literal(‘4’)))\n”,
“\n”,
“g.add((s.KingClubs, RDF.type, s.Clubs))\n”,
“g.add((s.KingClubs, RDF.type, s.King))\n”,
“g.add((s.KingClubs, s.value, Literal(‘4’)))\n”,
“\n”,
“\n”,
“g.add((s.AceHearts, RDF.type, s.Hearts))\n”,
“g.add((s.AceHearts, RDF.type, s.Ace))\n”,
“g.add((s.AceHearts, s.value, Literal(’11’)))\n”,
“\n”,
“g.add((s.AceSpades, RDF.type, s.Spades))\n”,
“g.add((s.AceSpades, RDF.type, s.Ace))\n”,
“g.add((s.AceSpades, s.value, Literal(’11’)))\n”,
“\n”,
“g.add((s.AceDiamonds, RDF.type, s.Diamonds))\n”,
“g.add((s.AceDiamonds, RDF.type, s.Ace))\n”,
“g.add((s.AceDiamonds, s.value, Literal(’11’)))\n”,
“\n”,
“g.add((s.AceClubs, RDF.type, s.Clubs))\n”,
“g.add((s.AceClubs, RDF.type, s.Ace))\n”,
“g.add((s.AceClubs, s.value, Literal(’11’)))\n”,
“\n”,
“\n”,
“#additional information about the game \n”,
“\n”,
“g.add((s.Jack, RDFS.subClassOf, s.CheapCard))\n”,
“g.add((s.Ace, RDFS.subClassOf, s.ExpensiveCard))\n”,
” \n”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“### Task 6: \n”,
“\n”,
“The ontology that we have shown above is similar to the one that we have started modelling in the lecture. However, some cards of our Schnapsen game are not modelled yet.\n”,
“Please add knowledge about the missing cards to the knowledge graph analogously to above. \n”,
“\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 13,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“)>”
]
},
“execution_count”: 13,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“#put your additional information about the cards here\n”,
“g.add((s.Queen, RDFS.subClassOf, s.HighCard))\n”,
“g.add((s.King, RDFS.subClassOf, s.VeryHighCard))\n”,
“g.add((s.Ten, RDFS.subClassOf, s.AverageCard))”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“Additionally, we need information about the current game and the game state.\n”,
“This involves the following additional things that we model using classes in a knowledge graph. \n”,
“- current trump card\n”,
“- the tallon\n”,
“- both players hands\n”,
“- both players tricks\n”,
“\n”,
“(This list is not complete.)\n”,
“\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 14,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“)>”
]
},
“execution_count”: 14,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“from rdflib import Seq, Bag\n”,
“\n”,
“#additional information about the gameplay\n”,
“\n”,
“#trump\n”,
“g.add((s.Jack, RDFS.subClassOf, s.TrumpSuit))\n”,
“g.add((s.JackSpades, RDF.type, s.TrumpCard))\n”,
“\n”,
“#tallon\n”,
“tallon = Seq(g, s.tallon)\n”,
“tallon.append(s.AceClubs)\n”,
“tallon.append(s.TenHearts)\n”,
“tallon.append(s.JackDiamonds)\n”,
“tallon.append(s.AceDiamonds)\n”,
“tallon.append(s.JackClubs)\n”,
“tallon.append(s.QueenHearts)\n”,
“tallon.append(s.KingHearts)\n”,
“tallon.append(s.TenSpades)\n”,
“tallon.append(s.TenClubs)\n”,
“\n”,
“#Player 1\n”,
“g.add((s.JackHearts, RDF.type, s.player1))\n”,
“g.add((s.AceHearts, RDF.type, s.player1))\n”,
“g.add((s.QueenSpades, RDF.type, s.player1))\n”,
“\n”,
“\n”,
“#Player 2\n”,
“g.add((s.AceSpades, RDF.type, s.player2))\n”,
“g.add((s.KingSpades, RDF.type, s.player2))\n”,
“g.add((s.QueenDiamonds, RDF.type, s.player2))\n”,
“\n”,
“\n”,
“#Cards won by Player 1\n”,
“g.add((s.TenDiamonds, RDF.type, s.wonPlayer1))\n”,
“g.add((s.KingDiamonds, RDF.type, s.wonPlayer1))\n”,
“\n”,
“#Cards won by Player 2\n”,
“g.add((s.KingClubs, RDF.type, s.wonPlayer2))\n”,
“g.add((s.QueenClubs, RDF.type, s.wonPlayer2))\n”,
“\n”,
“\n”,
“\n”,
“\n”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“We are now able to perform a simple query on the knowledge graph to get information from the knowledge graph. In this example, we retrieve the cards from the hand of player 1 by using a SPARQL query.”
]
},
{
“cell_type”: “code”,
“execution_count”: 15,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“Result: (rdflib.term.URIRef(‘http://schnapsen.org/JackHearts’),)\n”,
“Result: (rdflib.term.URIRef(‘http://schnapsen.org/AceHearts’),)\n”,
“Result: (rdflib.term.URIRef(‘http://schnapsen.org/QueenSpades’),)\n”
]
}
],
“source”: [
“result = g.query(\”\”\”\n”,
“PREFIX s: http://schnapsen.org/\n”,
“PREFIX rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns#\n”,
“SELECT ?card \n”,
“WHERE {\n”,
” ?card rdf:type s:player1\n”,
“} \n”,
“\”\”\”)\n”,
“\n”,
“for row in result:\n”,
” print(‘Result: {}’.format(row))”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“### Task 7 \n”,
“As a next task, we want to get all cards of player 2 using a SPARQL query similar to before. Adapt the previous query.\n”,
“\n”,
Hint: You can copy the code from above and only need to change a single line.
]
},
{
“cell_type”: “code”,
“execution_count”: 16,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“Result: (rdflib.term.URIRef(‘http://schnapsen.org/AceSpades’),)\n”,
“Result: (rdflib.term.URIRef(‘http://schnapsen.org/KingSpades’),)\n”,
“Result: (rdflib.term.URIRef(‘http://schnapsen.org/QueenDiamonds’),)\n”
]
}
],
“source”: [
“#Put your answer here\n”,
“result = g.query(\”\”\”\n”,
“PREFIX s: http://schnapsen.org/\n”,
“PREFIX rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns#\n”,
“SELECT ?card \n”,
“WHERE {\n”,
” ?card rdf:type s:player2\n”,
“} \n”,
“\”\”\”)\n”,
“for row in result:\n”,
” print(‘Result: {}’.format(row))”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“### Task 8\n”,
“\n”,
“You are supposed to take the position of Player 1. Try to complete the following query such that all cards which are known to Player 1 are returned.\n”,
“\n”,
Hint: You need to change 2 lines in the query.
]
},
{
“cell_type”: “code”,
“execution_count”: 17,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“Result: (rdflib.term.URIRef(‘http://schnapsen.org/JackHearts’),)\n”,
“Result: (rdflib.term.URIRef(‘http://schnapsen.org/AceHearts’),)\n”,
“Result: (rdflib.term.URIRef(‘http://schnapsen.org/QueenSpades’),)\n”,
“Result: (rdflib.term.URIRef(‘http://schnapsen.org/TenDiamonds’),)\n”,
“Result: (rdflib.term.URIRef(‘http://schnapsen.org/KingDiamonds’),)\n”
]
}
],
“source”: [
“result = g.query(\”\”\”\n”,
“PREFIX s: http://schnapsen.org/\n”,
“PREFIX rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns#\n”,
“\n”,
“SELECT ?card \n”,
“WHERE {\n”,
” {?card rdf:type s:player1}\n”,
” UNION\n”,
” {?card rdf:type s:wonPlayer1}\n”,
“} \n”,
“\”\”\”)\n”,
“\n”,
“for row in result:\n”,
” print(‘Result: {}’.format(row))”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“## Entailment\n”,
“\n”,
“So far, we have only worked with simple SPARQL queries to get information about the current game state from our knowledge graph.\n”,
“We will extend this idea now to work with RDF-S entailments.\n”,
“\n”,
“Run the two following cells.”
]
},
{
“cell_type”: “code”,
“execution_count”: 18,
“metadata”: {},
“outputs”: [],
“source”: [
“result = g.query(\”\”\”\n”,
“PREFIX s: http://schnapsen.org/\n”,
“SELECT ?card\n”,
“WHERE{\n”,
” ?card rdf:type s:Card .\n”,
“} \n”,
“\”\”\”)\n”,
“\n”,
“for row in result:\n”,
” print(‘Result: {}’.format(row))”
]
},
{
“cell_type”: “code”,
“execution_count”: 19,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“Result: (rdflib.term.URIRef(‘http://schnapsen.org/QueenHearts’),)\n”,
“Result: (rdflib.term.URIRef(‘http://schnapsen.org/AceSpades’),)\n”,
“Result: (rdflib.term.URIRef(‘http://schnapsen.org/KingHearts’),)\n”,
“Result: (rdflib.term.URIRef(‘http://schnapsen.org/JackSpades’),)\n”,
“Result: (rdflib.term.URIRef(‘http://schnapsen.org/AceClubs’),)\n”,
“Result: (rdflib.term.URIRef(‘http://schnapsen.org/AceHearts’),)\n”,
“Result: (rdflib.term.URIRef(‘http://schnapsen.org/JackDiamonds’),)\n”,
“Result: (rdflib.term.URIRef(‘http://schnapsen.org/JackClubs’),)\n”,
“Result: (rdflib.term.URIRef(‘http://schnapsen.org/AceDiamonds’),)\n”,
“Result: (rdflib.term.URIRef(‘http://schnapsen.org/KingSpades’),)\n”,
“Result: (rdflib.term.URIRef(‘http://schnapsen.org/KingClubs’),)\n”,
“Result: (rdflib.term.URIRef(‘http://schnapsen.org/JackHearts’),)\n”,
“Result: (rdflib.term.URIRef(‘http://schnapsen.org/QueenDiamonds’),)\n”,
“Result: (rdflib.term.URIRef(‘http://schnapsen.org/QueenClubs’),)\n”,
“Result: (rdflib.term.URIRef(‘http://schnapsen.org/KingDiamonds’),)\n”,
“Result: (rdflib.term.URIRef(‘http://schnapsen.org/QueenSpades’),)\n”
]
}
],
“source”: [
“import owlrl\n”,
“\n”,
“rdfs = owlrl.RDFSClosure.RDFS_Semantics(g, False, False, False)\n”,
“rdfs.closure()\n”,
“\n”,
“result = g.query(\”\”\”\n”,
“PREFIX s: http://schnapsen.org/\n”,
“SELECT ?card\n”,
“WHERE{\n”,
” ?card rdf:type s:Card .\n”,
“} \n”,
“\”\”\”)\n”,
“\n”,
“for row in result:\n”,
” print(‘Result: {}’.format(row))\n”,
” \n”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“### Task 9:\n”,
“\n”,
“The last two code cells have shown you two identical queries. But one of them uses RDF-S entailments on the classes, the other one is not.\n”,
“\n”,
“Please explain in your own words, what RDF-S entailment is and why the result set is different between both queries.\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 20,
“metadata”: {},
“outputs”: [],
“source”: [
“#Put your explanation here\n”,
“Report3=\”\”\”\n”,
“owl is a brute force implemenatation of rdfs.\n”,
“Calling closure generate the closure of the graph. A closure of grapgh is the graph obtained \n”,
“from repeatedly adding edges between non-adjacent vertices whose degrees sum to at least number n,\n”,
“until this can no longer be done.\n”,
“A rdfs entails B when every rdfs interpretation which satisfies every member of A also satisfies B.\n”,
“In second implemantation , we are using the rdfs schema and the owl staandars to get the results, \n”,
“but in first one we cant get results as the semantics of graph are not defined properly.\n”,
“\”\”\””
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“## Final Task: Collect all the results\n”,
“\n”,
“Uncomment and run this cell (and all the cells above) to generate the text file that you have to hand in together with the notebook on canvas!\n”,
“\n”,
“### Please hand in only the text file which is generated by this method!”
]
},
{
“cell_type”: “code”,
“execution_count”: 21,
“metadata”: {},
“outputs”: [],
“source”: [
“from utils import *\n”,
“exportToText(\”assignment4.txt\”, Report1, Report2, Report3)”
]
},
{
“cell_type”: “code”,
“execution_count”: null,
“metadata”: {},
“outputs”: [],
“source”: []
}
],
“metadata”: {
“kernelspec”: {
“display_name”: “Python 3 (ipykernel)”,
“language”: “python”,
“name”: “python3”
},
“language_info”: {
“codemirror_mode”: {
“name”: “ipython”,
“version”: 3
},
“file_extension”: “.py”,
“mimetype”: “text/x-python”,
“name”: “python”,
“nbconvert_exporter”: “python”,
“pygments_lexer”: “ipython3”,
“version”: “3.10.0”
}
},
“nbformat”: 4,
“nbformat_minor”: 4
}

text file .txt

assignment4.txt
{A: False, B: True, C: True}
{A: True, B: True, C: True}
True

{A: False, D: False, C: False, B: False}
{A: False, D: False, C: False, B: True}
True

owl is a brute force implemenatation of rdfs.
Calling closure generate the closure of the graph. A closure of grapgh is the graph obtained
from repeatedly adding edges between non-adjacent vertices whose degrees sum to at least number n,
until this can no longer be done.
A rdfs entails B when every rdfs interpretation which satisfies every member of A also satisfies B.
In second implemantation , we are using the rdfs schema and the owl staandars to get the results,
but in first one we cant get results as the semantics of graph are not defined properly.

Give a Comment