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.

[24]:
from dsms import DSMS, KItem

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

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

[26]:
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
[26]:
KItem(

        name = Machine-1,

        id = dd091666-a7c9-4b3b-8832-910bdec5c63c,

        ktype_id = KTypes.TestingMachine,

        in_backend = False,

        slug = machine-1-dd091666,

        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:

[27]:
dsms.commit()
item.url
[27]:
'https://bue.materials-data.space/knowledge/testing-machine/machine-1-dd091666'

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:

[28]:
item
[28]:
KItem(

        name = Machine-1,

        id = dd091666-a7c9-4b3b-8832-910bdec5c63c,

        ktype_id = testing-machine,

        in_backend = True,

        slug = machine-1-dd091666,

        annotations = [],

        attachments = [],

        linked_kitems = [],

        affiliations = [],

        authors = [
                {
                        user_id: 7f0e5a37-353b-4bbc-b1f1-b6ad575f562d
                }
        ],

        avatar_exists = False,

        contacts = [],

        created_at = 2024-08-19 18:12:11.338394,

        updated_at = 2024-08-19 18:12:11.338394,

        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:

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

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

[30]:
type(item)
[30]:
dsms.knowledge.kitem.KItem

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.