{ "cells": [ { "cell_type": "markdown", "id": "985a87a9", "metadata": {}, "source": [ "# Query DAS after loading a knowledge base" ] }, { "cell_type": "markdown", "id": "2455b0e8", "metadata": {}, "source": [ "This notebook shows how to instantiate a DAS with some knowledge base already loaded and query it.\n", "\n", "The first cell just imports the relevant class and instantiates relevant objects." ] }, { "cell_type": "code", "execution_count": null, "id": "0caf9543", "metadata": {}, "outputs": [], "source": [ "from das.distributed_atom_space import DistributedAtomSpace, QueryOutputFormat\$n", "from das.pattern_matcher.pattern_matcher import PatternMatchingAnswer, OrderedAssignment, UnorderedAssignment, CompositeAssignment, Node, Link, Variable, Not, And, Or\$n", "import warnings\$n", "warnings.filterwarnings('ignore')\n", "das = DistributedAtomSpace()\n", "db = das.db\$n", "db.prefetch()" ] }, { "cell_type": "markdown", "id": "1c9d9bf7", "metadata": {}, "source": [ "Follows two utilitary functions which are an example of how to iterate and manipulate a query answer.\n", "\n", "Query answers may have two types of assignments. `Ordered` and `unordered`. The former is a regular assignment defining a value for each variable in the query. For instance:\n", "\n", "```\n", "V1 = H1\$n", "V2 = H2\$n", "V3 = H3\$n", "```\n", "\n", "The later assigns a set of possible values to a set of variables. For instance:\n", "\n", "```\n", "V1, V2, V3 = H1, H2, H3\$n", "```\n", "\n", "It means that any of these assignments are a valid answer to the query:\n", "\n", "```\n", "V1 = H1\$n", "V2 = H2\$n", "V3 = H3\$n", "\n", "V1 = H1\$n", "V2 = H3\$n", "V3 = H2\$n", "\n", "V1 = H2\$n", "V2 = H1\$n", "V3 = H3\$n", "\n", "V1 = H2\$n", "V2 = H3\$n", "V3 = H1\$n", "\n", "V1 = H3\$n", "V2 = H1\$n", "V3 = H2\$n", "\n", "V1 = H3\$n", "V2 = H2\$n", "V3 = H1\$n", "```" ] }, { "cell_type": "code", "execution_count": null, "id": "ece77aa9", "metadata": {}, "outputs": [], "source": [ "def print_ordered_assignment(assignment):\n", " if assignment is not None:\n", " for key, value in assignment.mapping.items():\n", " print(f\"{key}: {db.get_node_name(value)}\")\n", "\n", "def print_unordered_assignment(assignment):\n", " if assignment is not None:\n", " symbols = []\n", " for key in assignment.symbols:\n", " for i in range(assignment.symbols[key]):\n", " symbols.append(key)\n", " values = []\n", " for key in assignment.values:\n", " for i in range(assignment.values[key]):\n", " values.append(key)\n", " mapping_keys = []\n", " mapping_values = []\n", " for symbol, value in zip(symbols, values):\n", " mapping_keys.append(symbol)\n", " mapping_values.append(db.get_node_name(value))\n", " print(f\"{mapping_keys} = {mapping_values}\")" ] }, { "cell_type": "markdown", "id": "e3724d08", "metadata": {}, "source": [ "Before proceeding, print the atom count to make sure the knowledge base is correct." ] }, { "cell_type": "code", "execution_count": null, "id": "4a0ffae0", "metadata": {}, "outputs": [], "source": [ "das.count_atoms()" ] }, { "cell_type": "markdown", "id": "c4550119", "metadata": {}, "source": [ "Now we define 4 example queries to use later. We are just building the queries here, not executing them.\n", "\n", "Each query is an object built using its constructor and possibly nesting other constructors in its arguments. We can use logical operators (`And`, `Or` and `Not`), nodes (`Node`), links (`Link`) and variables (`Variable` or `TypedVariable`)\n", "\n", "As constructor's parameters, `And` and `Or` expect a list of other logical operators or `Links`. `Not` expects a single logical operator or a `Link`.\n", "\n", "`Links` expect a type, a flag to indicated if targets ar ordered or not and a list of targets.\n", "\n", "The list of targets of a link may contain `Nodes` or `Variables`.\n", "\n", "In `my_query_1` we define a query which is a simple `AND` operation on two links whose targets are variables. We are looking for two inheritance links which links 3 nodes in such a way that V1 -> V2 -> V3." ] }, { "cell_type": "code", "execution_count": null, "id": "abd3455c", "metadata": {}, "outputs": [], "source": [ "V1 = Variable(\"V1\")\n", "V2 = Variable(\"V2\")\n", "V3 = Variable(\"V3\")\n", "my_query_1 = And([\n", " Link(\"Inheritance\", ordered=True, targets=[V1, V2]),\n", " Link(\"Inheritance\", ordered=True, targets=[V2, V3])\n", "])" ] }, { "cell_type": "markdown", "id": "cf02772f", "metadata": {}, "source": [ "In `my_quary_2` we are doing a similar query but adding an extra constraint that it shaw not exist an inheritance link \"human\" -> V2" ] }, { "cell_type": "code", "execution_count": null, "id": "b7a53cbf", "metadata": {}, "outputs": [], "source": [ "V1 = Variable(\"V1\")\n", "V2 = Variable(\"V2\")\n", "V3 = Variable(\"V3\")\n", "N1 = Node(\"Concept\", \"human\")\n", "my_query_2 = And([\n", " Link(\"Inheritance\", ordered=True, targets=[V1, V2]),\n", " Link(\"Inheritance\", ordered=True, targets=[V2, V3]),\n", " Not(Link(\"Inheritance\", ordered=True, targets=[N1, V2]))\n", "])" ] }, { "cell_type": "markdown", "id": "ada0cf9a", "metadata": {}, "source": [ "In `my_query_3` we do a similar query but now there shaw not exist neither an inheritance link \"human\" -> V2 nor \"snake\" -> V2." ] }, { "cell_type": "code", "execution_count": null, "id": "8560414c", "metadata": {}, "outputs": [], "source": [ "V1 = Variable(\"V1\")\n", "V2 = Variable(\"V2\")\n", "V3 = Variable(\"V3\")\n", "N1 = Node(\"Concept\", \"human\")\n", "N2 = Node(\"Concept\", \"snake\")\n", "my_query_3 = And([\n", " Link(\"Inheritance\", ordered=True, targets=[V1, V2]),\n", " Link(\"Inheritance\", ordered=True, targets=[V2, V3]),\n", " Not(Or([\n", " Link(\"Inheritance\", ordered=True, targets=[N1, V2]),\n", " Link(\"Inheritance\", ordered=True, targets=[N2, V2])\n", " ]))\n", "])" ] }, { "cell_type": "markdown", "id": "93ead66f", "metadata": {}, "source": [ "In `my_query_4` we show an example of query using an unordered link. We are looking for nodes V1 and V2 which have a similarity link between them but which don't have inheritance links fromk any of them to \"mammal\"." ] }, { "cell_type": "code", "execution_count": null, "id": "0073c09e", "metadata": {}, "outputs": [], "source": [ "V1 = Variable(\"V1\")\n", "V2 = Variable(\"V2\")\n", "V3 = Variable(\"V3\")\n", "N1 = Node(\"Concept\", \"mammal\")\n", "my_query_4 = And([\n", " Link(\"Similarity\", ordered=False, targets=[V1, V2]),\n", " Not(Or([\n", " Link(\"Inheritance\", ordered=True, targets=[V1, N1]),\n", " Link(\"Inheritance\", ordered=True, targets=[V2, N1]),\n", " ]))\n", "])" ] }, { "cell_type": "markdown", "id": "2e91a757", "metadata": {}, "source": [ "Now select which query you want to execute and see the results. You can re-execute the cells below just changing the query (no need to re-execute the previous cells)." ] }, { "cell_type": "code", "execution_count": null, "id": "6f88c891", "metadata": {}, "outputs": [], "source": [ "my_query = my_query_1" ] }, { "cell_type": "code", "execution_count": null, "id": "846b71c2", "metadata": {}, "outputs": [], "source": [ "query_answer = PatternMatchingAnswer()\n", "matched = my_query.matched(db, query_answer)\n", "print(matched)" ] }, { "cell_type": "code", "execution_count": null, "id": "ee6f1b8b", "metadata": {}, "outputs": [], "source": [ "query_answer.assignments" ] }, { "cell_type": "code", "execution_count": null, "id": "9fc93321", "metadata": {}, "outputs": [], "source": [ "for assignment in query_answer.assignments:\n", " if type(assignment) is OrderedAssignment:\n", " print_ordered_assignment(assignment)\n", " elif type(assignment) is UnorderedAssignment:\n", " print_unordered_assignment(assignment)\n", " elif type(assignment) is CompositeAssignment:\n", " print_ordered_assignment(assignment.ordered_mapping)\n", " for unordered_assignment in assignment.unordered_mappings:\n", " print_unordered_assignment(unordered_assignment)\n", " print(\"\")" ] } ], "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.5" } }, "nbformat": 4, "nbformat_minor": 5 }