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="Machine-1",
    ktype_id=dsms.ktypes.TestingMachine,
    custom_properties={"Producer": "TestingLab GmBH",
                       "Location": "A404",
                       "Model Number" : "Bending Test Machine No 777"
                       },
)

item
[3]:
KItem(

        name = Machine-1,

        id = 6a5fd4a4-4dc0-4643-84eb-1e35513974ba,

        ktype_id = KTypes.TestingMachine,

        in_backend = False,

        slug = machine-1-6a5fd4a4,

        annotations = [],

        attachments = [],

        linked_kitems = [],

        affiliations = [],

        authors = [],

        avatar_exists = False,

        contacts = [],

        created_at = None,

        updated_at = None,

        external_links = [],

        kitem_apps = [],

        summary = None,

        user_groups = [],

        custom_properties = {
                Producer: TestingLab GmBH,
                Location: A404,
                Model Number: Bending Test Machine No 777
        },

        dataframe = None,

        rdf_exists = False
)

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

[4]:
dsms.commit()
item.url
[4]:
'https://stahldigital.materials-data.space/knowledge/testing-machine/machine-1-6a5fd4a4'

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 = Machine-1,

        id = 6a5fd4a4-4dc0-4643-84eb-1e35513974ba,

        ktype_id = testing-machine,

        in_backend = True,

        slug = machine-1-6a5fd4a4,

        annotations = [],

        attachments = [],

        linked_kitems = [],

        affiliations = [],

        authors = [
                {
                        user_id: aa97bc4c-939e-4142-8f22-c6be8c0df228
                }
        ],

        avatar_exists = False,

        contacts = [],

        created_at = 2024-08-23 18:16:24.190604,

        updated_at = 2024-08-23 18:16:24.190604,

        external_links = [],

        kitem_apps = [],

        summary = None,

        user_groups = [],

        custom_properties = {
                Producer: TestingLab GmBH,
                Location: A404,
                Model Number: Bending Test Machine No 777
        },

        dataframe = None,

        rdf_exists = False
)

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

[6]:
item.name
[6]:
'Machine-1'

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

[7]:
item.id
[7]:
UUID('6a5fd4a4-4dc0-4643-84eb-1e35513974ba')

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

[8]:
item.ktype
[8]:
KType(id='testing-machine', name='Testing Machine', webform=<class 'dsms.knowledge.utils.CustomPropertiesModel'>, json_schema=None)

… and also check the KType:

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

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.