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.

[12]:
from dsms import DSMS, KItem

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

[13]:
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). #

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

item
[14]:
kitem:
  name: Specimen123
  ktype_id: specimen
  custom_properties:
    sections:
    - id: ida3c5c42685526
      name: Untitled Section
      entries:
      - id: id175f885f8dda58
        type: Slider
        label: Width
        value: 0.5
        measurement_unit:
          iri: http://qudt.org/vocab/unit/MilliM
          label: Millimetre
          namespace: http://qudt.org/vocab/unit
      - id: idef5a37328789f
        type: Slider
        label: Length
        value:
        - 0.1
        - 0.2
        measurement_unit:
          iri: http://qudt.org/vocab/unit/MilliM
          label: Millimetre
          namespace: http://qudt.org/vocab/unit

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

[15]:
dsms.commit()
item.url
[15]:
'https://bue.materials-data.space/knowledge/specimen/specimen123-73d648b4'

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:

[16]:
item
[16]:
kitem:
  name: Specimen123
  id: 73d648b4-4619-4a02-a0da-64818df3851e
  ktype_id: specimen
  in_backend: true
  slug: specimen123-73d648b4
  annotations: []
  attachments:
  - name: subgraph.ttl
    content: null
  linked_kitems: []
  affiliations: []
  authors:
  - user_id: 7f0e5a37-353b-4bbc-b1f1-b6ad575f562d
  avatar_exists: false
  contacts: []
  created_at: 2025-01-17 10:56:08.681238
  updated_at: 2025-01-17 10:56:08.681238
  external_links: []
  kitem_apps: []
  user_groups: []
  custom_properties:
    sections:
    - id: ida3c5c42685526
      name: Untitled Section
      entries:
      - id: id175f885f8dda58
        type: Slider
        label: Width
        value: 0.5
        measurement_unit:
          iri: http://qudt.org/vocab/unit/MilliM
          label: Millimetre
          namespace: http://qudt.org/vocab/unit
      - id: idef5a37328789f
        type: Slider
        label: Length
        value:
        - 0.1
        - 0.2
        measurement_unit:
          iri: http://qudt.org/vocab/unit/MilliM
          label: Millimetre
          namespace: http://qudt.org/vocab/unit
  rdf_exists: true

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

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

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

[18]:
item.id
[18]:
UUID('73d648b4-4619-4a02-a0da-64818df3851e')

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

[19]:
item.ktype
[19]:
ktype:
  id: specimen
  name: Specimen
  context: false
  context_schema: []
  webform:
    semantics_enabled: true
    sections_enabled: false
    class_mapping:
    - https://w3id.org/pmd/co/Specimen
    sections:
    - id: ida3c5c42685526
      name: Untitled Section
      inputs:
      - id: id175f885f8dda58
        label: Width
        widget: Slider
        required: false
        value: null
        hint: null
        hidden: false
        ignore: false
        select_options: []
        measurement_unit:
          label: Millimetre
          iri: http://qudt.org/vocab/unit/MilliM
          symbol: null
          namespace: http://qudt.org/vocab/unit
        relation_mapping:
          iri: https://w3id.org/steel/ProcessOntology/hasWidth_Object
          type: object_property
          class_iri: https://w3id.org/emmo#EMMO_e4de48b1_dabb_4490_ac2b_040f926c64f0
        relation_mapping_extra: null
        multiple_selection: false
        knowledge_type: null
        range_options:
          min: 0
          max: 1
          step: 0.1
          range: false
        placeholder: null
      - id: idef5a37328789f
        label: Length
        widget: Slider
        required: false
        value: null
        hint: null
        hidden: false
        ignore: false
        select_options: []
        measurement_unit:
          label: Millimetre
          iri: http://qudt.org/vocab/unit/MilliM
          symbol: null
          namespace: http://qudt.org/vocab/unit
        relation_mapping:
          iri: https://w3id.org/steel/ProcessOntology/hasTotalLength_Object
          type: object_property
          class_iri: https://w3id.org/steel/ProcessOntology/TotalLength
        relation_mapping_extra:
          iri: https://w3id.org/steel/ProcessOntology/hasTotalLength_Object
          type: object_property
          class_iri: https://w3id.org/steel/ProcessOntology/TotalLength
        multiple_selection: false
        knowledge_type: null
        range_options:
          min: 0
          max: 1
          step: 0.1
          range: true
        placeholder: null
      - id: id54decb9641f9a
        label: Radius
        widget: Number
        required: false
        value: null
        hint: null
        hidden: false
        ignore: false
        select_options: []
        measurement_unit:
          label: Millimetre
          iri: http://qudt.org/vocab/unit/MilliM
          symbol: null
          namespace: http://qudt.org/vocab/unit
        relation_mapping:
          iri: https://w3id.org/steel/ProcessOntology/hasRadius_Object
          type: object_property
          class_iri: https://w3id.org/emmo#EMMO_32dcd601_47c7_4028_b7fa_5e972ae57f12
        relation_mapping_extra: null
        multiple_selection: false
        knowledge_type: null
        range_options:
          min: 0
          max: 1
          step: 0.1
          range: false
        placeholder: null
      hidden: false
  json_schema:
    iri:
    - https://w3id.org/pmd/co/Specimen
    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:
        iri: https://w3id.org/steel/ProcessOntology/TotalLength
        unit: http://qudt.org/vocab/unit/MilliM
        suffix: min
    - object_location: length[1]
      relation: https://w3id.org/steel/ProcessOntology/hasTotalLength_Object
      relation_type: object_property
      object_type:
        suffix: max
        iri: https://w3id.org/steel/ProcessOntology/TotalLength
        unit: http://qudt.org/vocab/unit/MilliM
    - object_location: radius
      relation: https://w3id.org/steel/ProcessOntology/hasRadius_Object
      relation_type: object_property
      object_type:
        iri: https://w3id.org/emmo#EMMO_32dcd601_47c7_4028_b7fa_5e972ae57f12
        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: '2024-12-17T11:00:34.924035'
  updated_at: '2025-01-16T18:25:33.054442'

… and also check the KType:

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

And we can convert the units:

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

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.