7. Interact with KTypes through the SDK

7. Interact with KTypes through the SDK#

7.1. Setting up#

Before you run this tutorial: make sure to have access to a DSMS-instance of your interest, alongwith with installation of this package and have establised access to the DSMS through DSMS-SDK (refer to Connecting to DSMS)

Import the needed classes and functions.

[1]:
from dsms import DSMS, KType

Now source the environmental variables from an .env file and start the DSMS-session.

[2]:
dsms = DSMS(env=".env")

7.2. Create KTypes#

A Ktype can have a webform schema - for e.g. data properties or a process schema for clustering KItems into a context.

The webform and process schema of a ktype may look as follows:

[3]:
ktype = {
  "id": "characterization-process",
  "name": "Characterization Process",
  "webform_schema_id": "69e58442-5837-4bba-9326-3083bccc7c86",
  "webform_schema": {
    "id": "69e58442-5837-4bba-9326-3083bccc7c86",
    "name": "Characterization Process",
    "spec": {
      "semantics_enabled": True,
      "sections_enabled": False,
      "class_mapping": [
        "https://w3id.org/emmo/domain/characterisation-methodology/chameo#CharacterisationProcedure"
      ],
      "sections": [
        {
          "id": "id57f43923129f28",
          "name": "Untitled Section",
          "inputs": [
            {
              "id": "id02760f0b0cd56",
              "label": "Start time",
              "widget": "Text",
              "relation_mapping": {
                "iri": "http://www.w3.org/ns/dcat#startDate",
                "label": "start date",
                "type": "data_property",
              },
            },
            {
              "id": "id3ff5961015588",
              "label": "End time ",
              "widget": "Text",

              "relation_mapping": {
                "iri": "http://www.w3.org/ns/dcat#endDate",
                "label": "end date",
                "type": "data_property",
              },
            }
          ],
        }
      ]
    },
  },
  "process_schema_id": "ee815110-ee41-44cf-a049-5873942440d6",
  "process_schema": {
    "id": "ee815110-ee41-44cf-a049-5873942440d6",
    "name": "Characterization Process",
    "spec": [
      {
        "id": "specimen",
        "label": "Specimen",
      },
      {
        "id": "testingmachine",
        "label": "TestingMachine",
      },
      {
        "id": "expert",
        "label": "expert",
        "mappings": [
          {
            "dst_ktype_id": "organization",
            "relation_iri": "http://www.w3.org/ns/prov#wasAssociatedWith",
            "relation_name": "wasAssociatedWith"
          },
          {
            "dst_ktype_id": "testingmachine",
            "relation_iri": "http://www.w3.org/ns/prov#wasAssociatedWith",
            "relation_name": "wasAssociatedWith"
          },
          {
            "dst_ktype_id": "specimen",
            "relation_iri": "http://www.w3.org/ns/prov#wasAssociatedWith",
            "relation_name": "wasAssociatedWith"
          }
        ],
      },
      {
        "id": "organization",
        "label": "organization",
      }
    ],
  },
}

We can see, that the ktype is describing a characterization process. The webfrom describes two fields: * start date of the characterization (with dcat mapping) * end data of the characterization (with dcat mapping)

The process schema describes the following entities:

  • the expert involed

  • the testing machine involved

  • the specimen involved

  • the organization involed

  • the semantic relations between the expert and the organization/specimen/testing machine

The Ktype can be instanciated with simple dictionary expansion:

[4]:
ktype = KType(**ktype)

ktype
[4]:
ktype:
  id: characterization-process
  name: Characterization Process
  webform_schema_id: 69e58442-5837-4bba-9326-3083bccc7c86
  webform_schema:
    id: 69e58442-5837-4bba-9326-3083bccc7c86
    name: Characterization Process
    spec:
      semantics_enabled: true
      sections_enabled: false
      class_mapping:
      - https://w3id.org/emmo/domain/characterisation-methodology/chameo#CharacterisationProcedure
      sections:
      - id: id57f43923129f28
        name: Untitled Section
        inputs:
        - id: id02760f0b0cd56
          label: Start time
          widget: Text
          relation_mapping:
            iri: http://www.w3.org/ns/dcat#startDate
            label: start date
            type: data_property
        - id: id3ff5961015588
          label: 'End time '
          widget: Text
          relation_mapping:
            iri: http://www.w3.org/ns/dcat#endDate
            label: end date
            type: data_property
  process_schema_id: ee815110-ee41-44cf-a049-5873942440d6
  process_schema:
    id: ee815110-ee41-44cf-a049-5873942440d6
    name: Characterization Process
    spec:
    - id: specimen
      label: Specimen
    - id: testingmachine
      label: TestingMachine
    - id: expert
      label: expert
      mappings:
      - dst_ktype_id: organization
        relation_iri: http://www.w3.org/ns/prov#wasAssociatedWith
        relation_name: wasAssociatedWith
      - dst_ktype_id: testingmachine
        relation_iri: http://www.w3.org/ns/prov#wasAssociatedWith
        relation_name: wasAssociatedWith
      - dst_ktype_id: specimen
        relation_iri: http://www.w3.org/ns/prov#wasAssociatedWith
        relation_name: wasAssociatedWith
    - id: organization
      label: organization

The commit method should be executed to synchronize the changes with the DSMS SDK.

[5]:
dsms.add(ktype)
dsms.commit()

The ktype object will automatically get updated with after the commit.

[6]:
ktype
/usr/local/lib/python3.11/site-packages/pydantic/main.py:463: UserWarning: Pydantic serializer warnings:
  PydanticSerializationUnexpectedValue(Expected `WebformSchema` - serialized value may not be as expected [input_value={'id': '69e58442-5837-4bb...-08-13T15:19:53.355557'}, input_type=dict])
  PydanticSerializationUnexpectedValue(Expected `ProcessSchema` - serialized value may not be as expected [input_value={'id': 'ee815110-ee41-44c...-08-13T15:16:24.873119'}, input_type=dict])
  return self.__pydantic_serializer__.to_python(
[6]:
ktype:
  id: characterization-process
  name: Characterization Process
  webform_schema_id: 69e58442-5837-4bba-9326-3083bccc7c86
  webform_schema:
    id: 69e58442-5837-4bba-9326-3083bccc7c86
    name: Characterization Process
    spec:
      semanticsEnabled: true
      sectionsEnabled: false
      classMapping:
      - https://w3id.org/emmo/domain/characterisation-methodology/chameo#CharacterisationProcedure
      sections:
      - id: id57f43923129f28
        name: Untitled Section
        inputs:
        - id: id02760f0b0cd56
          label: Start time
          widget: Text
          required: false
          value: null
          hint: null
          hidden: false
          ignore: false
          selectOptions: []
          measurementUnit: null
          relationMapping:
            iri: http://www.w3.org/ns/dcat#startDate
            label: start date
            type: data_property
            classIri: null
          relationMappingExtra: null
          multipleSelection: false
          knowledgeType: null
          rangeOptions: null
          placeholder: null
        - id: id3ff5961015588
          label: 'End time '
          widget: Text
          required: false
          value: null
          hint: null
          hidden: false
          ignore: false
          selectOptions: []
          measurementUnit: null
          relationMapping:
            iri: http://www.w3.org/ns/dcat#endDate
            label: end date
            type: data_property
            classIri: null
          relationMappingExtra: null
          multipleSelection: false
          knowledgeType: null
          rangeOptions: null
          placeholder: null
        hidden: false
    created_at: '2025-08-13T15:16:24.730491'
    updated_at: '2025-08-13T15:19:53.355557'
  process_schema_id: ee815110-ee41-44cf-a049-5873942440d6
  process_schema:
    id: ee815110-ee41-44cf-a049-5873942440d6
    name: Characterization Process
    spec:
    - id: specimen
      label: Specimen
      isChild: false
      mappings: []
      children: []
    - id: testingmachine
      label: TestingMachine
      isChild: false
      mappings: []
      children: []
    - id: expert
      label: expert
      isChild: false
      mappings:
      - dstKtypeId: organization
        relationIri: http://www.w3.org/ns/prov#wasAssociatedWith
        relationName: wasAssociatedWith
      - dstKtypeId: testingmachine
        relationIri: http://www.w3.org/ns/prov#wasAssociatedWith
        relationName: wasAssociatedWith
      - dstKtypeId: specimen
        relationIri: http://www.w3.org/ns/prov#wasAssociatedWith
        relationName: wasAssociatedWith
      children: []
    - id: organization
      label: organization
      isChild: false
      mappings: []
      children: []
    created_at: '2025-08-13T15:16:24.873119'
    updated_at: '2025-08-13T15:16:24.873119'
  created_at: '2025-08-13T15:34:31.201502'
  updated_at: '2025-08-13T15:34:31.268380'

7.3. Update KTypes#

The ktype object can be fetched using its id.

[7]:
ktype = dsms.ktypes.CharacterizationProcess
ktype
[7]:
ktype:
  id: characterization-process
  name: Characterization Process
  webform_schema_id: 69e58442-5837-4bba-9326-3083bccc7c86
  webform_schema:
    id: 69e58442-5837-4bba-9326-3083bccc7c86
    name: Characterization Process
    spec:
      semantics_enabled: true
      sections_enabled: false
      class_mapping:
      - https://w3id.org/emmo/domain/characterisation-methodology/chameo#CharacterisationProcedure
      sections:
      - id: id57f43923129f28
        name: Untitled Section
        inputs:
        - id: id02760f0b0cd56
          label: Start time
          widget: Text
          required: false
          hidden: false
          ignore: false
          select_options: []
          relation_mapping:
            iri: http://www.w3.org/ns/dcat#startDate
            label: start date
            type: data_property
          multiple_selection: false
        - id: id3ff5961015588
          label: 'End time '
          widget: Text
          required: false
          hidden: false
          ignore: false
          select_options: []
          relation_mapping:
            iri: http://www.w3.org/ns/dcat#endDate
            label: end date
            type: data_property
          multiple_selection: false
        hidden: false
    created_at: '2025-08-13T15:16:24.730491'
    updated_at: '2025-08-13T15:19:53.355557'
  process_schema_id: ee815110-ee41-44cf-a049-5873942440d6
  process_schema:
    id: ee815110-ee41-44cf-a049-5873942440d6
    name: Characterization Process
    spec:
    - id: specimen
      label: Specimen
      is_child: false
      mappings: []
      children: []
    - id: testingmachine
      label: TestingMachine
      is_child: false
      mappings: []
      children: []
    - id: expert
      label: expert
      is_child: false
      mappings:
      - dst_ktype_id: organization
        relation_iri: http://www.w3.org/ns/prov#wasAssociatedWith
        relation_name: wasAssociatedWith
      - dst_ktype_id: testingmachine
        relation_iri: http://www.w3.org/ns/prov#wasAssociatedWith
        relation_name: wasAssociatedWith
      - dst_ktype_id: specimen
        relation_iri: http://www.w3.org/ns/prov#wasAssociatedWith
        relation_name: wasAssociatedWith
      children: []
    - id: organization
      label: organization
      is_child: false
      mappings: []
      children: []
    created_at: 2025-08-13 15:16:24.873119
    updated_at: 2025-08-13 15:16:24.873119
  created_at: '2025-08-13T15:34:31.201502'
  updated_at: '2025-08-13T15:34:31.268380'

We can change e.g. the name of the ktype

[8]:
ktype.name = 'Characterization Procedure'

dsms.add(ktype)
dsms.commit()

ktype
[8]:
ktype:
  id: characterization-process
  name: Characterization Procedure
  webform_schema_id: 69e58442-5837-4bba-9326-3083bccc7c86
  webform_schema:
    id: 69e58442-5837-4bba-9326-3083bccc7c86
    name: Characterization Process
    spec:
      semanticsEnabled: true
      sectionsEnabled: false
      classMapping:
      - https://w3id.org/emmo/domain/characterisation-methodology/chameo#CharacterisationProcedure
      sections:
      - id: id57f43923129f28
        name: Untitled Section
        inputs:
        - id: id02760f0b0cd56
          label: Start time
          widget: Text
          required: false
          value: null
          hint: null
          hidden: false
          ignore: false
          selectOptions: []
          measurementUnit: null
          relationMapping:
            iri: http://www.w3.org/ns/dcat#startDate
            label: start date
            type: data_property
            classIri: null
          relationMappingExtra: null
          multipleSelection: false
          knowledgeType: null
          rangeOptions: null
          placeholder: null
        - id: id3ff5961015588
          label: 'End time '
          widget: Text
          required: false
          value: null
          hint: null
          hidden: false
          ignore: false
          selectOptions: []
          measurementUnit: null
          relationMapping:
            iri: http://www.w3.org/ns/dcat#endDate
            label: end date
            type: data_property
            classIri: null
          relationMappingExtra: null
          multipleSelection: false
          knowledgeType: null
          rangeOptions: null
          placeholder: null
        hidden: false
    created_at: '2025-08-13T15:16:24.730491'
    updated_at: '2025-08-13T15:19:53.355557'
  process_schema_id: ee815110-ee41-44cf-a049-5873942440d6
  process_schema:
    id: ee815110-ee41-44cf-a049-5873942440d6
    name: Characterization Process
    spec:
    - id: specimen
      label: Specimen
      isChild: false
      mappings: []
      children: []
    - id: testingmachine
      label: TestingMachine
      isChild: false
      mappings: []
      children: []
    - id: expert
      label: expert
      isChild: false
      mappings:
      - dstKtypeId: organization
        relationIri: http://www.w3.org/ns/prov#wasAssociatedWith
        relationName: wasAssociatedWith
      - dstKtypeId: testingmachine
        relationIri: http://www.w3.org/ns/prov#wasAssociatedWith
        relationName: wasAssociatedWith
      - dstKtypeId: specimen
        relationIri: http://www.w3.org/ns/prov#wasAssociatedWith
        relationName: wasAssociatedWith
      children: []
    - id: organization
      label: organization
      isChild: false
      mappings: []
      children: []
    created_at: '2025-08-13T15:16:24.873119'
    updated_at: '2025-08-13T15:16:24.873119'
  created_at: '2025-08-13T15:34:31.201502'
  updated_at: '2025-08-13T15:34:31.904250'

7.4. Delete KTypes#

The fetched ktype can be deleted by applying the del-operator to the dsms object with the individual KType object.

[9]:
del dsms[ktype]

As always, commit the changes.

[10]:
dsms.commit()

The available KTypes in the SDK can be fetched from an enum list.