diff --git a/docs/dsms_sdk/tutorials/2_creation.ipynb b/docs/dsms_sdk/tutorials/2_creation.ipynb index b075b7c..6eeaaa3 100644 --- a/docs/dsms_sdk/tutorials/2_creation.ipynb +++ b/docs/dsms_sdk/tutorials/2_creation.ipynb @@ -21,7 +21,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -37,7 +37,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -62,71 +62,26 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "KItem(\n", - "\n", - "\tname = Machine-1, \n", - "\n", - "\tid = c4128bec-3ecd-4c92-a307-7015bc9f2e86, \n", - "\n", - "\tktype_id = KTypes.TestingMachine, \n", - "\n", - "\tin_backend = False, \n", - "\n", - "\tslug = machine-1-c4128bec, \n", - "\n", - "\tannotations = [], \n", - "\n", - "\tattachments = [], \n", - "\n", - "\tlinked_kitems = [], \n", - "\n", - "\taffiliations = [], \n", - "\n", - "\tauthors = [], \n", - "\n", - "\tavatar_exists = False, \n", - "\n", - "\tcontacts = [], \n", - "\n", - "\tcreated_at = None, \n", - "\n", - "\tupdated_at = None, \n", - "\n", - "\texternal_links = [], \n", - "\n", - "\tkitem_apps = [], \n", - "\n", - "\tsummary = None, \n", - "\n", - "\tuser_groups = [], \n", - "\n", - "\tcustom_properties = {\n", - "\t\tProducer: TestingLab GmBH, \n", - "\t\tLocation: A404, \n", - "\t\tModel Number: Bending Test Machine No 777\n", - "\t}, \n", - "\n", - "\tdataframe = None, \n", - "\n", - "\trdf_exists = False\n", - ")" - ] - }, - "execution_count": 14, - "metadata": {}, - "output_type": "execute_result" + "ename": "AttributeError", + "evalue": "TestingMachine", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[3], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m item \u001b[38;5;241m=\u001b[39m KItem(\n\u001b[1;32m 2\u001b[0m name\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mMachine-1\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m----> 3\u001b[0m ktype_id\u001b[38;5;241m=\u001b[39m\u001b[43mdsms\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mktypes\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mTestingMachine\u001b[49m,\n\u001b[1;32m 4\u001b[0m custom_properties\u001b[38;5;241m=\u001b[39m{\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mProducer\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mTestingLab GmBH\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 5\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mLocation\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mA404\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 6\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mModel Number\u001b[39m\u001b[38;5;124m\"\u001b[39m : \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mBending Test Machine No 777\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 7\u001b[0m },\n\u001b[1;32m 8\u001b[0m )\n\u001b[1;32m 10\u001b[0m item\n", + "File \u001b[0;32m/usr/local/lib/python3.10/enum.py:437\u001b[0m, in \u001b[0;36mEnumMeta.__getattr__\u001b[0;34m(cls, name)\u001b[0m\n\u001b[1;32m 435\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39m_member_map_[name]\n\u001b[1;32m 436\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m:\n\u001b[0;32m--> 437\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mAttributeError\u001b[39;00m(name) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n", + "\u001b[0;31mAttributeError\u001b[0m: TestingMachine" + ] } ], "source": [ "item = KItem(\n", " name=\"Machine-1\",\n", - " ktype_id=dsms.ktypes.TestingMachine,\n", + " ktype_id=dsms.ktypes.Specimen,\n", " custom_properties={\"Producer\": \"TestingLab GmBH\",\n", " \"Location\": \"A404\",\n", " \"Model Number\" : \"Bending Test Machine No 777\"\n", @@ -365,7 +320,7 @@ ], "metadata": { "kernelspec": { - "display_name": "sdk", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -379,7 +334,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.2" + "version": "3.10.15" } }, "nbformat": 4, diff --git a/docs/dsms_sdk/tutorials/3_updation.ipynb b/docs/dsms_sdk/tutorials/3_updation.ipynb index cfa9245..4a5d7ba 100644 --- a/docs/dsms_sdk/tutorials/3_updation.ipynb +++ b/docs/dsms_sdk/tutorials/3_updation.ipynb @@ -23,7 +23,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -39,7 +39,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -55,20 +55,76 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Machine-1\n" + "81\n" ] } ], "source": [ - "item = dsms.kitems[-1]\n", - "print(item.name)" + "items = dsms.get_kitems(limit=200)[-1]" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "kitem:\n", + " name: Machine-1\n", + " id: 73046897-4569-4b7b-9d2f-2d8a72002432\n", + " ktype_id: specimen\n", + " slug: machine-1-73046897\n", + " annotations: []\n", + " attachments:\n", + " - name: subgraph.ttl\n", + " content: null\n", + " linked_kitems: []\n", + " affiliations: []\n", + " authors:\n", + " - user_id: 7f0e5a37-353b-4bbc-b1f1-b6ad575f562d\n", + " avatar_exists: false\n", + " contacts: []\n", + " created_at: 2025-01-08 14:46:22.262147\n", + " updated_at: 2025-01-08 14:46:22.262147\n", + " external_links: []\n", + " kitem_apps: []\n", + " user_groups: []\n", + " custom_properties:\n", + " sections:\n", + " - id: id17363475782033gesrh\n", + " name: Misc\n", + " entries:\n", + " - id: id1736347578203ff9n04\n", + " type: Text\n", + " label: Producer\n", + " value: TestingLab GmBH\n", + " - id: id1736347578203kpxtjv\n", + " type: Text\n", + " label: Location\n", + " value: A404\n", + " - id: id1736347578203a9211r\n", + " type: Text\n", + " label: Model Number\n", + " value: Bending Test Machine No 777\n", + " rdf_exists: true" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "item" ] }, { @@ -91,14 +147,14 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "item.name = \"Machine-1\"\n", "item.custom_properties.Producer = \"Machinery GmBH\"\n", "item.attachments.append(\"testfile.txt\")\n", - "item.annotations.append(\"www.machinery.org/\")\n", + "item.annotations.append(\"www.machinery.org/testingmachine\")\n", "item.external_links.append(\n", " {\"url\": \"http://machine.org\", \"label\": \"machine-link\"}\n", ")\n", @@ -109,7 +165,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -132,98 +188,66 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "KItem(\n", - "\n", - "\tname = Machine-1, \n", - "\n", - "\tid = dd091666-a7c9-4b3b-8832-910bdec5c63c, \n", - "\n", - "\tktype_id = testing-machine, \n", - "\n", - "\tin_backend = True, \n", - "\n", - "\tslug = machine-1-dd091666, \n", - "\n", - "\tannotations = [\n", - "\t\t{\n", - "\t\t\tiri: www.machinery.org/,\n", - "\t\t\tname: ,\n", - "\t\t\tnamespace: www.machinery.org,\n", - "\t\t\tdescription: None\n", - "\t\t}\n", - "\t], \n", - "\n", - "\tattachments = [\n", - "\t\t{\n", - "\t\t\tname: testfile.txt\n", - "\t\t}\n", - "\t], \n", - "\n", - "\tlinked_kitems = [], \n", - "\n", - "\taffiliations = [\n", - "\t\t{\n", - "\t\t\tname: machine-team\n", - "\t\t}\n", - "\t], \n", - "\n", - "\tauthors = [\n", - "\t\t{\n", - "\t\t\tuser_id: 7f0e5a37-353b-4bbc-b1f1-b6ad575f562d\n", - "\t\t}\n", - "\t], \n", - "\n", - "\tavatar_exists = False, \n", - "\n", - "\tcontacts = [\n", - "\t\t{\n", - "\t\t\tname: machinesupport,\n", - "\t\t\temail: machinesupport@group.mail,\n", - "\t\t\tuser_id: None\n", - "\t\t}\n", - "\t], \n", - "\n", - "\tcreated_at = 2024-08-19 18:12:11.338394, \n", - "\n", - "\tupdated_at = 2024-08-19 18:12:11.338394, \n", - "\n", - "\texternal_links = [\n", - "\t\t{\n", - "\t\t\tlabel: machine-link,\n", - "\t\t\turl: http://machine.org/\n", - "\t\t}\n", - "\t], \n", - "\n", - "\tkitem_apps = [], \n", - "\n", - "\tsummary = None, \n", - "\n", - "\tuser_groups = [\n", - "\t\t{\n", - "\t\t\tname: machinegroup,\n", - "\t\t\tgroup_id: 123\n", - "\t\t}\n", - "\t], \n", - "\n", - "\tcustom_properties = {\n", - "\t\tProducer: Machinery GmBH, \n", - "\t\tLocation: A404, \n", - "\t\tModel Number: Bending Test Machine No 777\n", - "\t}, \n", - "\n", - "\tdataframe = None, \n", - "\n", - "\trdf_exists = False\n", - ")" + "kitem:\n", + " name: Machine-1\n", + " id: 73046897-4569-4b7b-9d2f-2d8a72002432\n", + " ktype_id: specimen\n", + " slug: machine-1-73046897\n", + " annotations:\n", + " - iri: www.machinery.org/testingmachine\n", + " label: testingmachine\n", + " namespace: www.machinery.org\n", + " attachments:\n", + " - name: subgraph.ttl\n", + " content: null\n", + " - name: testfile.txt\n", + " content: null\n", + " linked_kitems: []\n", + " affiliations:\n", + " - name: machine-team\n", + " authors:\n", + " - user_id: 7f0e5a37-353b-4bbc-b1f1-b6ad575f562d\n", + " avatar_exists: false\n", + " contacts:\n", + " - name: machinesupport\n", + " email: machinesupport@group.mail\n", + " user_id: null\n", + " created_at: 2025-01-08 14:46:22.262147\n", + " updated_at: 2025-01-08 14:46:22.262147\n", + " external_links:\n", + " - label: machine-link\n", + " url: http://machine.org\n", + " kitem_apps: []\n", + " user_groups:\n", + " - name: machinegroup\n", + " group_id: '123'\n", + " custom_properties:\n", + " sections:\n", + " - id: id17363475782033gesrh\n", + " name: Misc\n", + " entries:\n", + " - id: id1736347578203ff9n04\n", + " type: Text\n", + " label: Producer\n", + " value: Machinery GmBH\n", + " - id: id1736347578203kpxtjv\n", + " type: Text\n", + " label: Location\n", + " value: A404\n", + " - id: id1736347578203a9211r\n", + " type: Text\n", + " label: Model Number\n", + " value: Bending Test Machine No 777\n", + " rdf_exists: true" ] }, - "execution_count": 19, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } @@ -241,18 +265,20 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "\t\t\t Downloaded file: {\n", - "\t\t\tname: testfile.txt\n", - "\t\t}\n", + "\t\t\t Downloaded file: attachment:\n", + " name: testfile.txt\n", + " content: null\n", + "\n", "|------------------------------------Beginning of file------------------------------------|\n", "This is a calibration protocol!\n", + "\n", "|---------------------------------------End of file---------------------------------------|\n" ] } @@ -270,7 +296,7 @@ ], "metadata": { "kernelspec": { - "display_name": "sdk", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -284,7 +310,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.12" + "version": "3.10.15" } }, "nbformat": 4, diff --git a/docs/dsms_sdk/tutorials/4_deletion.ipynb b/docs/dsms_sdk/tutorials/4_deletion.ipynb index fe44b17..d95039e 100644 --- a/docs/dsms_sdk/tutorials/4_deletion.ipynb +++ b/docs/dsms_sdk/tutorials/4_deletion.ipynb @@ -148,8 +148,7 @@ } ], "source": [ - "item = dsms.kitems[-1]\n", - "print(item)" + "items = dsms.get_kitems(limit=200)[-1]" ] }, { @@ -386,7 +385,7 @@ ], "metadata": { "kernelspec": { - "display_name": "sdk", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -400,7 +399,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.12" + "version": "3.10.15" } }, "nbformat": 4, diff --git a/docs/dsms_sdk/tutorials/5_search.ipynb b/docs/dsms_sdk/tutorials/5_search.ipynb index f72fac7..954e881 100644 --- a/docs/dsms_sdk/tutorials/5_search.ipynb +++ b/docs/dsms_sdk/tutorials/5_search.ipynb @@ -67,13 +67,23 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/app/dsms/knowledge/kitem.py:575: UserWarning: A flat dictionary was provided for custom properties.\n", + " Will be transformed into `KItemCustomPropertiesModel`.\n", + " warnings.warn(\n" + ] + } + ], "source": [ "item1 = KItem(\n", " name=\"Machine-1\",\n", - " ktype_id=dsms.ktypes.TestingMachine,\n", + " ktype_id=dsms.ktypes.Specimen,\n", " annotations=[\"https://w3id.org/steel/ProcessOntology/TestingMachine\"],\n", " custom_properties={\"Producer\": \"TestingLab GmBH\",\n", " \"Room Number\": \"A404\",\n", @@ -83,7 +93,7 @@ "\n", "item2 = KItem(\n", " name=\"Machine-2\",\n", - " ktype_id=dsms.ktypes.TestingMachine,\n", + " ktype_id=dsms.ktypes.Specimen,\n", " annotations=[\"https://w3id.org/steel/ProcessOntology/TestingMachine\"],\n", " custom_properties={\"Producer\": \"StressStrain GmBH\",\n", " \"Room Number\": \"B500\",\n", @@ -136,126 +146,192 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "KItem(\n", - "\n", - "\tname = Machine-1, \n", - "\n", - "\tid = d1524272-5f34-454b-bb61-7b0ab3a825a7, \n", - "\n", - "\tktype_id = testing-machine, \n", - "\n", - "\tin_backend = True, \n", - "\n", - "\tslug = machine-1-d1524272, \n", - "\n", - "\tannotations = [], \n", - "\n", - "\tattachments = [], \n", - "\n", - "\tlinked_kitems = [\n", - "\t\t\n", - "\t\t\tid: ba1a7871-ff75-465f-8625-a7a0202edca4\n", - "\t\t\tname: Specimen-1\n", - "\t\t\tslug: specimen-1-ba1a7871\n", - "\t\t\tktype_id: specimen\n", - "\t\t\tsummary: None\n", - "\t\t\tavatar_exists: False\n", - "\t\t\tannotations: []\n", - "\t\t\tlinked_kitems: [{\n", - "\t\t\tid: d1524272-5f34-454b-bb61-7b0ab3a825a7\n", - "\t\t}]\n", - "\t\t\texternal_links: []\n", - "\t\t\tcontacts: []\n", - "\t\t\tauthors: [{\n", - "\t\t\tuser_id: 7f0e5a37-353b-4bbc-b1f1-b6ad575f562d\n", - "\t\t}]\n", - "\t\t\tlinked_affiliations: []\n", - "\t\t\tattachments: []\n", - "\t\t\tuser_groups: []\n", - "\t\t\tcustom_properties: {'sampletype': None, 'sampleinformation': None, 'sampleproductionprocess': None, 'Geometry': 'Cylindrical 150mm x 20mm x 40mm', 'Material': 'Concrete', 'Project ID': 'ConstructionProject2024'}\n", - "\t\t\tcreated_at: 2024-08-23T17:15:46.876917\n", - "\t\t\tupdated_at: 2024-08-23T17:15:46.876917\n", - "\t\t, \n", - "\t\t\n", - "\t\t\tid: 2335f57a-efc2-47ce-83b3-866cdeb6d30d\n", - "\t\t\tname: Research Institute ABC\n", - "\t\t\tslug: researchinstituteabc-2335f57a\n", - "\t\t\tktype_id: organization\n", - "\t\t\tsummary: None\n", - "\t\t\tavatar_exists: False\n", - "\t\t\tannotations: [{\n", - "\t\t\tiri: www.researchBACiri.org/foo,\n", - "\t\t\tname: research ABC Institute,\n", - "\t\t\tnamespace: research,\n", - "\t\t\tdescription: None\n", - "\t\t}]\n", - "\t\t\tlinked_kitems: [{\n", - "\t\t\tid: d1524272-5f34-454b-bb61-7b0ab3a825a7\n", - "\t\t}]\n", - "\t\t\texternal_links: []\n", - "\t\t\tcontacts: []\n", - "\t\t\tauthors: [{\n", - "\t\t\tuser_id: 7f0e5a37-353b-4bbc-b1f1-b6ad575f562d\n", - "\t\t}]\n", - "\t\t\tlinked_affiliations: []\n", - "\t\t\tattachments: []\n", - "\t\t\tuser_groups: []\n", - "\t\t\tcustom_properties: None\n", - "\t\t\tcreated_at: 2024-08-23T17:15:47.998202\n", - "\t\t\tupdated_at: 2024-08-23T17:15:47.998202\n", - "\t\t\n", - "\t], \n", - "\n", - "\taffiliations = [], \n", - "\n", - "\tauthors = [\n", - "\t\t{\n", - "\t\t\tuser_id: 7f0e5a37-353b-4bbc-b1f1-b6ad575f562d\n", - "\t\t}\n", - "\t], \n", - "\n", - "\tavatar_exists = False, \n", - "\n", - "\tcontacts = [], \n", - "\n", - "\tcreated_at = 2024-08-23 17:15:45.431622, \n", - "\n", - "\tupdated_at = 2024-08-23 17:15:45.431622, \n", - "\n", - "\texternal_links = [], \n", - "\n", - "\tkitem_apps = [], \n", - "\n", - "\tsummary = None, \n", - "\n", - "\tuser_groups = [], \n", - "\n", - "\tcustom_properties = {\n", - "\t\tProducer: TestingLab GmBH, \n", - "\t\tRoom Number: A404, \n", - "\t\tDescription: Bending Test Machine\n", - "\t}, \n", - "\n", - "\tdataframe = None, \n", - "\n", - "\trdf_exists = False\n", - ")\n", - "\n", - "\n" + "Results:\n", + "hits:\n", + "- kitem:\n", + " name: Specimen-2\n", + " id: 8cb51ac4-4d05-4bc1-b9f0-9d54333f7d50\n", + " ktype_id: specimen\n", + " slug: specimen-2-8cb51ac4\n", + " annotations:\n", + " - iri: https://w3id.org/steel/ProcessOntology/TestPiece\n", + " label: TestPiece\n", + " namespace: https://w3id.org/steel/ProcessOntology\n", + " attachments: []\n", + " linked_kitems:\n", + " - id: bdeebac2-a14c-44fb-9277-637e9c3d4036\n", + " name: Machine-2\n", + " slug: machine-2-bdeebac2\n", + " ktype_id: material\n", + " summary: null\n", + " avatar_exists: false\n", + " annotations:\n", + " - iri: https://w3id.org/steel/ProcessOntology/TestingMachine\n", + " label: TestingMachine\n", + " namespace: https://w3id.org/steel/ProcessOntology\n", + " linked_kitems:\n", + " - id: 8cb51ac4-4d05-4bc1-b9f0-9d54333f7d50\n", + " - id: 2b769463-8f9b-4479-a525-6819abd22124\n", + " external_links: []\n", + " contacts: []\n", + " authors:\n", + " - user_id: 7f0e5a37-353b-4bbc-b1f1-b6ad575f562d\n", + " linked_affiliations: []\n", + " attachments: []\n", + " user_groups: []\n", + " custom_properties:\n", + " sections:\n", + " - id: id1734433260058gxhvl1\n", + " name: Misc\n", + " entries:\n", + " - id: id1734433260058dgkepy\n", + " type: Text\n", + " label: Producer\n", + " value: StressStrain GmBH\n", + " measurementUnit: null\n", + " relationMapping: null\n", + " - id: id1734433260058hbxapc\n", + " type: Text\n", + " label: Room Number\n", + " value: B500\n", + " measurementUnit: null\n", + " relationMapping: null\n", + " - id: id1734433260058sd3zvi\n", + " type: Text\n", + " label: Description\n", + " value: Compression Test Machine\n", + " measurementUnit: null\n", + " relationMapping: null\n", + " created_at: '2024-12-17T11:01:00.817233'\n", + " updated_at: '2024-12-17T11:01:00.817233'\n", + " affiliations: []\n", + " authors:\n", + " - user_id: 7f0e5a37-353b-4bbc-b1f1-b6ad575f562d\n", + " avatar_exists: false\n", + " contacts: []\n", + " created_at: 2024-12-17 11:01:01.118746\n", + " updated_at: 2024-12-17 11:01:01.118746\n", + " external_links: []\n", + " kitem_apps: []\n", + " user_groups: []\n", + " custom_properties:\n", + " sections:\n", + " - id: id1734433260403g4wr5h\n", + " name: Misc\n", + " entries:\n", + " - id: id1734433260403a84ajz\n", + " type: Text\n", + " label: Geometry\n", + " value: Rectangular 200mm x 30mm x 20mm\n", + " - id: id1734433260403x81qse\n", + " type: Text\n", + " label: Material\n", + " value: Metal\n", + " - id: id1734433260403c0jaxc\n", + " type: Text\n", + " label: Project ID\n", + " value: MetalBlenders2024\n", + " rdf_exists: false\n", + " fuzzy: false\n", + "- kitem:\n", + " name: asdf\n", + " id: 5b2c234e-9532-4d09-8970-43c1fd645a61\n", + " ktype_id: specimen\n", + " slug: asdf\n", + " annotations: []\n", + " attachments:\n", + " - name: subgraph.ttl\n", + " content: null\n", + " linked_kitems:\n", + " - id: a065f1cb-58a4-4c53-98e2-c2ff4b7c3b98\n", + " name: data345\n", + " slug: data345\n", + " ktype_id: material\n", + " summary: null\n", + " avatar_exists: false\n", + " annotations: []\n", + " linked_kitems:\n", + " - id: 5b2c234e-9532-4d09-8970-43c1fd645a61\n", + " external_links: []\n", + " contacts: []\n", + " authors:\n", + " - user_id: 7f0e5a37-353b-4bbc-b1f1-b6ad575f562d\n", + " linked_affiliations: []\n", + " attachments:\n", + " - name: subgraph.ttl\n", + " content: null\n", + " user_groups: []\n", + " custom_properties:\n", + " sections:\n", + " - id: id37bef167f3118\n", + " name: Material spec\n", + " entries:\n", + " - id: id052c20aaf2f2d\n", + " type: Number\n", + " label: Minimum Tensile Strength\n", + " value: 40.0\n", + " measurementUnit:\n", + " iri: http://qudt.org/vocab/unit/MegaPA\n", + " label: Megapascal\n", + " symbol: null\n", + " namespace: http://qudt.org/vocab/unit\n", + " relationMapping:\n", + " iri: https://w3id.org/steel/ProcessOntology/hasTensileStrength_Object\n", + " type: object_property\n", + " class_iri: https://w3id.org/steel/ProcessOntology/TensileStrength\n", + " created_at: '2024-12-10T13:37:00.345483'\n", + " updated_at: '2024-12-10T13:37:00.345483'\n", + " affiliations: []\n", + " authors:\n", + " - user_id: 7f0e5a37-353b-4bbc-b1f1-b6ad575f562d\n", + " avatar_exists: false\n", + " contacts: []\n", + " created_at: 2024-12-20 09:41:10.103478\n", + " updated_at: 2024-12-20 09:41:10.103478\n", + " external_links: []\n", + " kitem_apps: []\n", + " user_groups: []\n", + " custom_properties:\n", + " sections:\n", + " - id: idae07df653eaee8\n", + " name: Untitled Section\n", + " entries:\n", + " - id: idc5302183f13488\n", + " type: Slider\n", + " label: Width\n", + " value: 45.2\n", + " - id: idd82012b5d5b3f\n", + " type: Knowledge item\n", + " label: material\n", + " value:\n", + " - id: a065f1cb-58a4-4c53-98e2-c2ff4b7c3b98\n", + " ktype_id: material\n", + " slug: data345\n", + " name: data345\n", + " - id: id1fd8f15fea9698\n", + " type: File\n", + " label: asdf\n", + " rdf_exists: true\n", + " fuzzy: false\n", + "total_count: 18\n", + "\n", + "Name of the first kitem\n", + "Specimen-2\n" ] } ], "source": [ - "for result in dsms.search(ktypes=[dsms.ktypes.TestingMachine], limit=1):\n", - " print(result.hit)\n", - " print(\"\\n\")" + "result = dsms.search(ktypes=[dsms.ktypes.Specimen], limit=2)\n", + "print(result)\n", + "print(\"Name of the first kitem:\")\n", + "print(result.hits[0].kitem.name)" ] }, { @@ -267,94 +343,143 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "KItem(\n", - "\n", - "\tname = Research Institute ABC, \n", - "\n", - "\tid = 2335f57a-efc2-47ce-83b3-866cdeb6d30d, \n", - "\n", - "\tktype_id = organization, \n", - "\n", - "\tin_backend = True, \n", - "\n", - "\tslug = researchinstituteabc-2335f57a, \n", - "\n", - "\tannotations = [\n", - "\t\t{\n", - "\t\t\tiri: www.researchBACiri.org/foo,\n", - "\t\t\tname: research ABC Institute,\n", - "\t\t\tnamespace: research,\n", - "\t\t\tdescription: None\n", - "\t\t}\n", - "\t], \n", - "\n", - "\tattachments = [], \n", - "\n", - "\tlinked_kitems = [\n", - "\t\t\n", - "\t\t\tid: d1524272-5f34-454b-bb61-7b0ab3a825a7\n", - "\t\t\tname: Machine-1\n", - "\t\t\tslug: machine-1-d1524272\n", - "\t\t\tktype_id: testing-machine\n", - "\t\t\tsummary: None\n", - "\t\t\tavatar_exists: False\n", - "\t\t\tannotations: []\n", - "\t\t\tlinked_kitems: [{\n", - "\t\t\tid: ba1a7871-ff75-465f-8625-a7a0202edca4\n", - "\t\t}, {\n", - "\t\t\tid: 2335f57a-efc2-47ce-83b3-866cdeb6d30d\n", - "\t\t}]\n", - "\t\t\texternal_links: []\n", - "\t\t\tcontacts: []\n", - "\t\t\tauthors: [{\n", - "\t\t\tuser_id: 7f0e5a37-353b-4bbc-b1f1-b6ad575f562d\n", - "\t\t}]\n", - "\t\t\tlinked_affiliations: []\n", - "\t\t\tattachments: []\n", - "\t\t\tuser_groups: []\n", - "\t\t\tcustom_properties: {'Producer': 'TestingLab GmBH', 'Room Number': 'A404', 'Description': 'Bending Test Machine'}\n", - "\t\t\tcreated_at: 2024-08-23T17:15:45.431622\n", - "\t\t\tupdated_at: 2024-08-23T17:15:45.431622\n", - "\t\t\n", - "\t], \n", - "\n", - "\taffiliations = [], \n", - "\n", - "\tauthors = [\n", - "\t\t{\n", - "\t\t\tuser_id: 7f0e5a37-353b-4bbc-b1f1-b6ad575f562d\n", - "\t\t}\n", - "\t], \n", - "\n", - "\tavatar_exists = False, \n", - "\n", - "\tcontacts = [], \n", - "\n", - "\tcreated_at = 2024-08-23 17:15:47.998202, \n", - "\n", - "\tupdated_at = 2024-08-23 17:15:47.998202, \n", - "\n", - "\texternal_links = [], \n", - "\n", - "\tkitem_apps = [], \n", + "kitem:\n", + " name: Research Institute ABC\n", + " id: 109e910b-e8dd-42ac-8b41-91f85e4b1be4\n", + " ktype_id: organization\n", + " slug: researchinstituteabc-109e910b\n", + " annotations:\n", + " - iri: www.researchBACiri.org/foo\n", + " label: foo\n", + " namespace: www.researchBACiri.org\n", + " attachments: []\n", + " linked_kitems:\n", + " - id: 67f59cdb-38ae-4a22-996e-2bb33348e7c9\n", + " name: Machine-1\n", + " slug: machine-1-67f59cdb\n", + " ktype_id: material\n", + " summary: null\n", + " avatar_exists: false\n", + " annotations:\n", + " - iri: https://w3id.org/steel/ProcessOntology/TestingMachine\n", + " label: TestingMachine\n", + " namespace: https://w3id.org/steel/ProcessOntology\n", + " linked_kitems:\n", + " - id: 79e87341-4714-46dc-a0ee-7a3f3ab9d160\n", + " - id: 109e910b-e8dd-42ac-8b41-91f85e4b1be4\n", + " external_links: []\n", + " contacts: []\n", + " authors:\n", + " - user_id: 7f0e5a37-353b-4bbc-b1f1-b6ad575f562d\n", + " linked_affiliations: []\n", + " attachments:\n", + " - name: subgraph.ttl\n", + " content: null\n", + " user_groups: []\n", + " custom_properties:\n", + " sections:\n", + " - id: id1733827827537wke3oc\n", + " name: Misc\n", + " entries:\n", + " - id: id17338278275379iwghc\n", + " type: Text\n", + " label: Producer\n", + " value: TestingLab GmBH\n", + " measurementUnit: null\n", + " relationMapping: null\n", + " - id: id1733827827537juf4dg\n", + " type: Text\n", + " label: Room Number\n", + " value: A404\n", + " measurementUnit: null\n", + " relationMapping: null\n", + " - id: id1733827827537q70q0q\n", + " type: Text\n", + " label: Description\n", + " value: Bending Test Machine\n", + " measurementUnit: null\n", + " relationMapping: null\n", + " created_at: '2024-12-10T10:50:28.246384'\n", + " updated_at: '2024-12-10T10:50:28.246384'\n", + " - id: 2c43e877-2f7c-45ef-822c-5523a6d8bead\n", + " name: Machine-1\n", + " slug: machine-2-2c43e877\n", + " ktype_id: expert\n", + " summary: null\n", + " avatar_exists: false\n", + " annotations:\n", + " - iri: https://w3id.org/steel/ProcessOntology/TestingMachine\n", + " label: TestingMachine\n", + " namespace: https://w3id.org/steel/ProcessOntology\n", + " - iri: www.machinery.org/\n", + " label: ''\n", + " namespace: www.machinery.org\n", + " - iri: www.machinery.org/testingmachine\n", + " label: testingmachine\n", + " namespace: www.machinery.org\n", + " linked_kitems:\n", + " - id: a3a4ec06-d488-4582-b5ac-14ad978dd2d4\n", + " - id: 109e910b-e8dd-42ac-8b41-91f85e4b1be4\n", + " external_links:\n", + " - label: machine-link\n", + " url: http://machine.org\n", + " contacts:\n", + " - name: machinesupport\n", + " email: machinesupport@group.mail\n", + " user_id: null\n", + " authors:\n", + " - user_id: 7f0e5a37-353b-4bbc-b1f1-b6ad575f562d\n", + " linked_affiliations: []\n", + " attachments:\n", + " - name: testfile.txt\n", + " content: null\n", + " user_groups:\n", + " - name: machinegroup\n", + " group_id: '123'\n", + " custom_properties:\n", + " sections:\n", + " - id: id1733827827683ze7b2n\n", + " name: Misc\n", + " entries:\n", + " - id: id1733827827683vp194x\n", + " type: Text\n", + " label: Producer\n", + " value: Machinery GmBH\n", + " measurementUnit: null\n", + " relationMapping: null\n", + " - id: id1733827827683as7zer\n", + " type: Text\n", + " label: Room Number\n", + " value: B500\n", + " measurementUnit: null\n", + " relationMapping: null\n", + " - id: id1733827827683lc3tnw\n", + " type: Text\n", + " label: Description\n", + " value: Compression Test Machine\n", + " measurementUnit: null\n", + " relationMapping: null\n", + " created_at: '2024-12-10T10:50:28.406930'\n", + " updated_at: '2025-01-08T14:29:00.545742'\n", + " affiliations: []\n", + " authors:\n", + " - user_id: 7f0e5a37-353b-4bbc-b1f1-b6ad575f562d\n", + " avatar_exists: false\n", + " contacts: []\n", + " created_at: 2024-12-10 10:50:28.798372\n", + " updated_at: 2024-12-10 10:50:28.798372\n", + " external_links: []\n", + " kitem_apps: []\n", + " user_groups: []\n", + " rdf_exists: false\n", "\n", - "\tsummary = None, \n", - "\n", - "\tuser_groups = [], \n", - "\n", - "\tcustom_properties = None, \n", - "\n", - "\tdataframe = None, \n", - "\n", - "\trdf_exists = False\n", - ")\n", "fuzziness: False\n", "\n", "\n" @@ -363,7 +488,7 @@ ], "source": [ "for result in dsms.search(ktypes=[dsms.ktypes.Organization, dsms.ktypes.DatasetCatalog], limit=1):\n", - " print(result.hit)\n", + " print(result.kitem)\n", " print(\"fuzziness: \", result.fuzzy)\n", " print(\"\\n\")\n", " " @@ -1339,7 +1464,7 @@ ], "metadata": { "kernelspec": { - "display_name": "sdk", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -1353,7 +1478,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.2" + "version": "3.10.15" } }, "nbformat": 4, diff --git a/dsms/core/dsms.py b/dsms/core/dsms.py index a5bf18f..5d5f938 100644 --- a/dsms/core/dsms.py +++ b/dsms/core/dsms.py @@ -30,6 +30,8 @@ from dsms.knowledge.ktype import KType from dsms.knowledge.search import KItemListModel, SearchResult +warnings.simplefilter("always", DeprecationWarning) + class DSMS: """ @@ -187,7 +189,7 @@ def headers(cls) -> Dict[str, Any]: return header @property - def kitems(cls) -> "KItemListModel": + def kitems(self) -> "KItemListModel": """ **DEPRECATED** @@ -202,10 +204,10 @@ def kitems(cls) -> "KItemListModel": """ message = """`kitems`-property is deprecated and only returns the 10 first kitems. Please use the `get_kitems`-method instead.""" - warnings.warn(DeprecationWarning, message) + warnings.warn(message, DeprecationWarning) return _get_kitem_list() - def get_kitems(limit=10, offset=0) -> "KItemListModel": + def get_kitems(self, limit=10, offset=0) -> "KItemListModel": """ Get all available KItems from the remote backend. @@ -214,7 +216,7 @@ def get_kitems(limit=10, offset=0) -> "KItemListModel": offset (int): The offset in the list of KItems. Defaults to 0. """ - return _get_kitem_list(limit, offset) + return _get_kitem_list(limit=limit, offset=offset) def app_configs(cls) -> "List[AppConfig]": """Return available app configs in the DSMS""" diff --git a/dsms/knowledge/kitem.py b/dsms/knowledge/kitem.py index 7f261e4..6536cd2 100644 --- a/dsms/knowledge/kitem.py +++ b/dsms/knowledge/kitem.py @@ -134,7 +134,7 @@ class KItem(BaseModel): ) slug: Optional[str] = Field( None, - description="Slug of the KContext.dsms", + description="Slug of the KItem", min_length=4, max_length=1000, ) diff --git a/dsms/knowledge/ktype.py b/dsms/knowledge/ktype.py index ba9300e..440c070 100644 --- a/dsms/knowledge/ktype.py +++ b/dsms/knowledge/ktype.py @@ -133,9 +133,7 @@ def serialize(self): """Serialize ktype.""" return { key: ( - value.dict( # pylint: disable=no-member - exclude_none=False, by_alias=False - ) + value.model_dump(exclude_none=False, by_alias=False) if key == "webform" and value is not None and not isinstance(value, dict) diff --git a/dsms/knowledge/search.py b/dsms/knowledge/search.py index 2c92ef4..c72f148 100644 --- a/dsms/knowledge/search.py +++ b/dsms/knowledge/search.py @@ -1,9 +1,12 @@ """DSMS search model""" -from typing import TYPE_CHECKING, List, Union +from typing import TYPE_CHECKING, Generator, List, Union +import oyaml as yaml from pydantic import BaseModel, Field +from dsms.core.session import Session + if TYPE_CHECKING: from dsms import KItem @@ -18,6 +21,24 @@ class KItemSearchResult(BaseModel): If not a bool, a float indicates the distance from search term""", ) + def __str__(self): + """Pretty print the KItemSearchResult""" + from dsms.knowledge.utils import dump_model + + return yaml.dump( + { + "kitem": dump_model( + self.kitem, + exclude_extra=Session.dsms.config.hide_properties, + ), + "fuzzy": self.fuzzy, + } + ) + + def __repr__(self): + """Pretty print the KItemSearchResult""" + return str(self) + class SearchResult(BaseModel): """DSMS search result""" @@ -27,6 +48,45 @@ class SearchResult(BaseModel): ) total_count: int = Field(..., description="Total number of hits") + def __getitem__(self, key: str) -> "KItemSearchResult": + """Retrieve a KItemSearchResult from the search result by its index. + + Args: + key (str): The index of the KItemSearchResult to retrieve. + + Returns: + KItemSearchResult: The KItemSearchResult at the given index. + """ + return self.hits[key] + + def __iter__(self) -> "Generator[KItemSearchResult]": + """Iterate over the hits in the search result.""" + yield from self.hits + + def __str__(self): + """Pretty print the KItemSearchResult""" + from dsms.knowledge.utils import dump_model + + return yaml.dump( + { + "hits": [ + { + "kitem": dump_model( + hit.kitem, + exclude_extra=Session.dsms.config.hide_properties, + ), + "fuzzy": hit.fuzzy, + } + for hit in self.hits + ], + "total_count": self.total_count, + } + ) + + def __repr__(self): + """Pretty print the KItemSearchResult""" + return str(self) + class KItemListModel(BaseModel): """KItem list model returned by used id""" @@ -36,11 +96,35 @@ class KItemListModel(BaseModel): ) total_count: int = Field(..., description="Total number of hits") + def __getitem__(self, key: str) -> "KItem": + """Retrieve a KItem from the list by its index. + + Args: + key (str): The index of the KItem to retrieve. + + Returns: + KItem: The KItem at the given index. + + Raises: + IndexError: If the index is out of range. + """ + return self.kitems[key] + def __str__(self): """Pretty print the KItemList""" - return "\n".join( - [str(item) for item in self.kitems] - + [f"Total count: {self.total_count}"] + from dsms.knowledge.utils import dump_model + + return yaml.dump( + { + "kitems": [ + dump_model( + kitem, + exclude_extra=Session.dsms.config.hide_properties, + ) + for kitem in self.kitems + ], + "total_count": self.total_count, + } ) def __repr__(self): diff --git a/dsms/knowledge/utils.py b/dsms/knowledge/utils.py index 4d798ad..8349160 100644 --- a/dsms/knowledge/utils.py +++ b/dsms/knowledge/utils.py @@ -44,17 +44,34 @@ def _is_number(value): def print_model(self, key, exclude_extra: set = set()) -> str: """Pretty print the ktype fields""" + dumped = dump_model(self, exclude_extra) + return yaml.dump({key: dumped}) + + +def dump_model(self, exclude_extra: set = set()) -> Dict[str, Any]: + """ + Dump the model fields into a dictionary format with optional exclusions. + + This method converts the model's fields into a dictionary while allowing + specific fields to be excluded. UUID fields are converted to string + representation. + + Args: + exclude_extra (set): Additional fields to exclude from the dump. + + Returns: + Dict[str, Any]: A dictionary of the model fields with specified exclusions. + """ exclude = self.model_config.get("exclude", set()) | exclude_extra dumped = self.model_dump( exclude_none=True, exclude_unset=True, exclude=exclude, ) - dumped = { + return { key: (str(value) if isinstance(value, UUID) else value) for key, value in dumped.items() } - return yaml.dump({key: dumped}) def print_ktype(self) -> str: @@ -721,10 +738,13 @@ def _search( raise RuntimeError( f"""Something went wrong while searching for KItems: {response.text}""" ) from excep - return [ - SearchResult(hit=KItem(**item.get("hit")), fuzzy=item.get("fuzzy")) - for item in dumped - ] + return SearchResult( + hits=[ + {"kitem": KItem(**item.get("kitem")), "fuzzy": item.get("fuzzy")} + for item in dumped.get("hits") + ], + total_count=dumped.get("total_count"), + ) def _slugify(input_string: str, replacement: str = ""): diff --git a/dsms/knowledge/webform.py b/dsms/knowledge/webform.py index c488cdb..977d070 100644 --- a/dsms/knowledge/webform.py +++ b/dsms/knowledge/webform.py @@ -558,12 +558,12 @@ def _validate_inputs(cls, self: "Entry") -> "Entry": Widget.MULTI_SELECT.value, ): if self.type == Widget.MULTI_SELECT.value: - dtype = str + dtype = list else: dtype = str choices = [choice.value for choice in select_options] elif self.type == Widget.KNOWLEDGE_ITEM.value: - dtype = (KnowledgeItemReference, type(self.kitem)) + dtype = (KnowledgeItemReference, type(self.kitem), list) else: raise ValueError( f"Widget type is not mapped to a data type: {self.type}"