2. Create KItems with the SDK

2. Create KItems with the SDK#

In this tutorial we see how to create new Kitems.

2.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)

Now let us import the needed classes and functions for this tutorial.

[1]:
from dsms import DSMS, KItem

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

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

2.2: Create KItems#

We can make new KItems by simple class-initiation: (Make sure existing KItems are not given as input). #

[3]:
item = KItem(
    name="Specimen123",
    ktype_id=dsms.ktypes.Specimen,
    custom_properties = {
        "Width": 0.5,
        "Length": [0.1, 0.2],
    }
)

item
/app/dsms/knowledge/kitem.py:477: UserWarning: A flat dictionary was provided for custom properties.
                    Will be transformed into `KItemCustomPropertiesModel`.
  warnings.warn(
[3]:
kitem:
  name: Specimen123
  ktype_id: specimen
  custom_properties:
    content:
      sections:
      - id: id0595fb5f1853a8
        name: Untitled Section
        entries:
        - id: idba92210e193128
          type: Number
          label: Width
          value: 0.5
          measurementUnit:
            iri: http://qudt.org/vocab/unit/MilliM
            label: Millimetre
            symbol: null
            namespace: http://qudt.org/vocab/unit
          relationMapping: null
          required: false
        - id: ida08bcc9977e388
          type: Slider
          label: Length
          value:
          - 0.1
          - 0.2
          measurementUnit:
            iri: http://qudt.org/vocab/unit/MilliM
            label: Millimetre
            symbol: null
            namespace: http://qudt.org/vocab/unit
          relationMapping: null
          required: false

Remember: changes are only syncronized with the DSMS when you call the commit-method:

[4]:
dsms.add(item)
dsms.commit()
item.url
[4]:
'https://bue.materials-data.space/knowledge/specimen/specimen123-8a7f158b'

As we can see, the object we created before running the commit-method has automatically been updated, e.g. with the creation- and update-timestamp. We can check this with the below command:

[5]:
item
[5]:
kitem:
  name: Specimen123
  id: 8a7f158b-b1c5-4508-84a5-e20fe7191fd0
  ktype_id: specimen
  slug: specimen123-8a7f158b
  annotations: []
  attachments:
  - name: subgraph.ttl
  linked_kitems: []
  affiliations: []
  authors:
  - user_id: 7f0e5a37-353b-4bbc-b1f1-b6ad575f562d
  avatar_exists: false
  contacts: []
  created_at: 2025-04-14 12:34:01.852332
  updated_at: 2025-04-14 12:34:01.852332
  external_links: []
  apps: []
  user_groups: []
  custom_properties:
    content:
      sections:
      - id: id0595fb5f1853a8
        name: Untitled Section
        entries:
        - id: idba92210e193128
          type: Number
          label: Width
          value: 0.5
          measurementUnit:
            iri: http://qudt.org/vocab/unit/MilliM
            label: Millimetre
            symbol: null
            namespace: http://qudt.org/vocab/unit
          relationMapping: null
          required: false
        - id: ida08bcc9977e388
          type: Slider
          label: Length
          value:
          - 0.1
          - 0.2
          measurementUnit:
            iri: http://qudt.org/vocab/unit/MilliM
            label: Millimetre
            symbol: null
            namespace: http://qudt.org/vocab/unit
          relationMapping: null
          required: false
  rdf_exists: true

To just get the name of the item, we can do it as follows:

[6]:
item.name
[6]:
'Specimen123'

As well as the id of the kitem we can do it as follows:

[7]:
item.id
[7]:
UUID('8a7f158b-b1c5-4508-84a5-e20fe7191fd0')

To check the KType of the item newly created we can use the following:

[8]:
item.ktype
[8]:
ktype:
  id: specimen
  name: Specimen
  webform:
    semanticsEnabled: true
    sectionsEnabled: false
    classMapping:
    - https://w3id.org/steel/ProcessOntology/TestPiece
    sections:
    - id: id0595fb5f1853a8
      name: Untitled Section
      inputs:
      - id: idba92210e193128
        label: Width
        widget: Number
        required: false
        value: null
        hint: null
        hidden: false
        ignore: false
        selectOptions: []
        measurementUnit:
          label: Millimetre
          iri: http://qudt.org/vocab/unit/MilliM
          symbol: null
          namespace: http://qudt.org/vocab/unit
        relationMapping:
          iri: https://w3id.org/steel/ProcessOntology/hasWidth_Object
          type: object_property
          classIri: https://w3id.org/emmo#EMMO_e4de48b1_dabb_4490_ac2b_040f926c64f0
        relationMappingExtra: null
        multipleSelection: false
        knowledgeType:
        - null
        rangeOptions:
          min: 0
          max: 1
          step: 0.1
          range: false
        placeholder: null
      - id: ida08bcc9977e388
        label: Length
        widget: Slider
        required: false
        value: null
        hint: null
        hidden: false
        ignore: false
        selectOptions: []
        measurementUnit:
          label: Millimetre
          iri: http://qudt.org/vocab/unit/MilliM
          symbol: null
          namespace: http://qudt.org/vocab/unit
        relationMapping:
          iri: https://w3id.org/steel/ProcessOntology/hasTotalLength_Object
          type: object_property
          classIri: https://w3id.org/emmo#EMMO_cd2cd0de_e0cc_4ef1_b27e_2e88db027bac
        relationMappingExtra:
          iri: https://w3id.org/steel/ProcessOntology/hasTotalLength_Object
          type: object_property
          classIri: https://w3id.org/emmo#EMMO_cd2cd0de_e0cc_4ef1_b27e_2e88db027bac
        multipleSelection: false
        knowledgeType: null
        rangeOptions:
          min: 0
          max: 1
          step: 0.1
          range: true
        placeholder: null
      hidden: false
  json_schema:
    iri:
    - https://w3id.org/steel/ProcessOntology/TestPiece
    suffix: slug
    source: specimen[*]
    suffix_from_location: true
    custom_relations:
    - object_location: width
      relation: https://w3id.org/steel/ProcessOntology/hasWidth_Object
      relation_type: object_property
      object_type:
        iri: https://w3id.org/emmo#EMMO_e4de48b1_dabb_4490_ac2b_040f926c64f0
        unit: http://qudt.org/vocab/unit/MilliM
    - object_location: length[0]
      relation: https://w3id.org/steel/ProcessOntology/hasTotalLength_Object
      relation_type: object_property
      object_type:
        suffix: min
        iri: https://w3id.org/emmo#EMMO_cd2cd0de_e0cc_4ef1_b27e_2e88db027bac
        unit: http://qudt.org/vocab/unit/MilliM
    - object_location: length[1]
      relation: https://w3id.org/steel/ProcessOntology/hasTotalLength_Object
      relation_type: object_property
      object_type:
        suffix: max
        iri: https://w3id.org/emmo#EMMO_cd2cd0de_e0cc_4ef1_b27e_2e88db027bac
        unit: http://qudt.org/vocab/unit/MilliM
    - object_location: Name
      relation: http://www.w3.org/2000/01/rdf-schema#label
      relation_type: annotation_property
      object_data_type: string
  created_at: '2025-01-22T10:25:24.336663'
  updated_at: '2025-04-09T09:15:36.822065'

… and also check the KType:

[9]:
item.is_a(dsms.ktypes.Specimen)
[9]:
True

And we can convert the units:

[10]:
item.custom_properties.Width.convert_to("m")
[10]:
0.0005
[11]:
item.custom_properties.Length.convert_to("m")
[11]:
[0.0001, 0.0002]

You can also convert the custom_properties to a flat dict by passing the flat-parameter to the model_dump-method of the pydantic model:

[12]:
item.custom_properties.model_dump(flat=True)
[12]:
{'Width': 0.5, 'Length': [0.1, 0.2]}

Now you can check if the particular kitem is in the list of KItems. This can be done either by using the command: dsms.kitems or by logging into the frontend dsms instance.