Skip to content

kg_creation_utils

add_and_attach_data_entity(kg, data, top_level_kg, top_level_schema_namespace, data_entity, relation, task_entity)

Adds data entity instance to kg with the necessary relations, and attaches it to the given task

Parameters:

Name Type Description Default
kg Graph

Graph object to add to

required
data Entity

object representing top-level DataEntity class in KG

required
top_level_kg Graph

KG corresponding to the top-level KG schema

required
top_level_schema_namespace Namespace

namespace of the top-level KG schema

required
data_entity DataEntity

data entity to add

required
relation URIRef

IRI of relation to add

required
task_entity Task

task to attach the data entity to

required
Source code in exe_kg_lib/utils/kg_creation_utils.py
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
def add_and_attach_data_entity(
    kg: Graph,
    data: Entity,
    top_level_kg: Graph,
    top_level_schema_namespace: Namespace,
    data_entity: DataEntity,
    relation: URIRef,
    task_entity: Task,
) -> None:
    """
    Adds data entity instance to kg with the necessary relations, and attaches it to the given task
    Args:
        kg: Graph object to add to
        data: object representing top-level DataEntity class in KG
        top_level_kg: KG corresponding to the top-level KG schema
        top_level_schema_namespace: namespace of the top-level KG schema
        data_entity: data entity to add
        relation: IRI of relation to add
        task_entity: task to attach the data entity to
    """
    add_data_entity_instance(kg, data, top_level_kg, top_level_schema_namespace, data_entity)
    add_relation(kg, task_entity, relation, data_entity)

add_data_entity_instance(kg, data, top_level_kg, top_level_schema_namespace, data_entity)

Adds data entity instance to kg with the necessary relations

Parameters:

Name Type Description Default
kg Graph

Graph object to add to

required
data Entity

object representing top-level DataEntity class in KG

required
top_level_kg Graph

KG corresponding to the top-level KG schema

required
top_level_schema_namespace Namespace

namespace of the top-level KG schema

required
data_entity DataEntity

data entity to add

required
Source code in exe_kg_lib/utils/kg_creation_utils.py
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
def add_data_entity_instance(
    kg: Graph,
    data: Entity,
    top_level_kg: Graph,
    top_level_schema_namespace: Namespace,
    data_entity: DataEntity,
) -> None:
    """
    Adds data entity instance to kg with the necessary relations
    Args:
        kg: Graph object to add to
        data: object representing top-level DataEntity class in KG
        top_level_kg: KG corresponding to the top-level KG schema
        top_level_schema_namespace: namespace of the top-level KG schema
        data_entity: data entity to add
    """
    add_instance(kg, data_entity)

    if data_entity.has_source:
        has_source_iri, range_iri = get_first_query_result_if_exists(
            get_data_properties_by_entity_iri, data.iri, top_level_kg
        )

        source_literal = Literal(
            lexical_or_value=data_entity.has_source,
            datatype=range_iri,
        )

        add_literal(kg, data_entity, has_source_iri, source_literal)

    if data_entity.has_data_structure:
        add_relation(
            kg,
            data_entity,
            top_level_schema_namespace.hasDataStructure,
            Entity(data_entity.has_data_structure),
        )

    if data_entity.has_data_semantics:
        add_relation(
            kg,
            data_entity,
            top_level_schema_namespace.hasDataSemantics,
            Entity(data_entity.has_data_semantics),
        )

    if data_entity.has_reference:
        add_relation(
            kg,
            data_entity,
            top_level_schema_namespace.hasReference,
            Entity(data_entity.has_reference),
        )

add_instance(kg, entity_instance)

Adds entity instance to KG only if its parent entity exists and there is no instance with the same IRI

Parameters:

Name Type Description Default
kg Graph

Graph object to add to

required
entity_instance Entity

the entity instance to create

required
Source code in exe_kg_lib/utils/kg_creation_utils.py
15
16
17
18
19
20
21
22
23
def add_instance(kg: Graph, entity_instance: Entity) -> None:
    """
    Adds entity instance to KG only if its parent entity exists and there is no instance with the same IRI
    Args:
        kg: Graph object to add to
        entity_instance: the entity instance to create
    """
    if entity_instance.parent_entity and (entity_instance.iri, None, None) not in kg:
        kg.add((entity_instance.iri, RDF.type, entity_instance.parent_entity.iri))

add_instance_from_parent_with_relation(namespace, kg, parent_entity, relation_iri, related_entity, instance_name)

Creates an entity object based on the arguments and calls add_instance() and add_relation() to create a new entity instance and relation

Parameters:

Name Type Description Default
namespace Namespace

namespace for the new instance

required
kg Graph

Graph object to add to

required
parent_entity Entity

parent entity for the new instance

required
relation_iri str

IRI that connects the given related_entity with the new instance

required
related_entity Entity

relation source

required
instance_name str

name for the new instance

required

Returns:

Name Type Description
Entity Entity

object containing the new entity instance's basic info

Source code in exe_kg_lib/utils/kg_creation_utils.py
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
def add_instance_from_parent_with_relation(
    namespace: Namespace,
    kg: Graph,
    parent_entity: Entity,
    relation_iri: str,
    related_entity: Entity,
    instance_name: str,
) -> Entity:
    """
    Creates an entity object based on the arguments and calls add_instance() and add_relation() to create a new entity instance and relation
    Args:
        namespace: namespace for the new instance
        kg: Graph object to add to
        parent_entity: parent entity for the new instance
        relation_iri: IRI that connects the given related_entity with the new instance
        related_entity: relation source
        instance_name: name for the new instance

    Returns:
        Entity: object containing the new entity instance's basic info
    """
    entity_iri = namespace + instance_name
    instance = Entity(entity_iri, parent_entity)

    add_instance(kg, instance)
    add_relation(kg, related_entity, relation_iri, instance)

    return instance

add_literal(kg, from_entity, relation_iri, literal)

Adds relation between a given entity and a given literal to KG

Parameters:

Name Type Description Default
kg Graph

Graph object to add to

required
from_entity Entity

relation source

required
relation_iri str

IRI that connects the given entity with the given literal

required
literal Literal

literal to add to Graph object

required
Source code in exe_kg_lib/utils/kg_creation_utils.py
44
45
46
47
48
49
50
51
52
53
def add_literal(kg: Graph, from_entity: Entity, relation_iri: str, literal: Literal) -> None:
    """
    Adds relation between a given entity and a given literal to KG
    Args:
        kg: Graph object to add to
        from_entity: relation source
        relation_iri: IRI that connects the given entity with the given literal
        literal: literal to add to Graph object
    """
    kg.add((from_entity.iri, URIRef(relation_iri), literal))

add_relation(kg, from_entity, relation_iri, to_entity)

Adds relation between 2 given entities to KG

Parameters:

Name Type Description Default
kg Graph

Graph object to add to

required
from_entity Entity

relation source

required
relation_iri str

IRI that connects the 2 given entities

required
to_entity Entity

relation destination

required
Source code in exe_kg_lib/utils/kg_creation_utils.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
def add_relation(kg: Graph, from_entity: Entity, relation_iri: str, to_entity: Entity) -> None:
    """
    Adds relation between 2 given entities to KG
    Args:
        kg: Graph object to add to
        from_entity: relation source
        relation_iri: IRI that connects the 2 given entities
        to_entity: relation destination
    """
    kg.add(
        (
            from_entity.iri,
            URIRef(relation_iri),
            to_entity.iri,
        )
    )

create_pipeline_task(top_level_schema_namespace, parent_entity, kg, pipeline_name, input_data_path)

Adds instance of pipeline task to kg

Parameters:

Name Type Description Default
top_level_schema_namespace Namespace

namespace of the top-level KG schema

required
parent_entity Entity

parent entity of pipeline instance

required
kg Graph

Graph object to add to

required
pipeline_name str

name for the pipeline

required
input_data_path str

path for the input data to be used by the pipeline's tasks

required

Returns:

Name Type Description
Task Task

created pipeline task

Source code in exe_kg_lib/utils/kg_creation_utils.py
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
def create_pipeline_task(
    top_level_schema_namespace: Namespace,
    parent_entity: Entity,
    kg: Graph,
    pipeline_name: str,
    input_data_path: str,
) -> Task:
    """
    Adds instance of pipeline task to kg
    Args:
        top_level_schema_namespace: namespace of the top-level KG schema
        parent_entity: parent entity of pipeline instance
        kg: Graph object to add to
        pipeline_name: name for the pipeline
        input_data_path: path for the input data to be used by the pipeline's tasks

    Returns:
        Task: created pipeline task
    """
    pipeline = Task(top_level_schema_namespace + pipeline_name, parent_entity)
    add_instance(kg, pipeline)

    input_data_path_literal = Literal(lexical_or_value=input_data_path, datatype=XSD.string)
    add_literal(kg, pipeline, top_level_schema_namespace.hasInputDataPath, input_data_path_literal)

    return pipeline

name_instance(task_type_dict, method_type_dict, parent_entity)

Creates a unique name for a new instance by concatenating the parent entity's name (which is the instance type) with a number Also increments the relevant number of the corresponding dict

Parameters:

Name Type Description Default
task_type_dict Dict[str, int]

contains pairs of task types and numbers

required
method_type_dict Dict[str, int]

contains pairs of method types and numbers

required
parent_entity Entity

instance's parent entity

required

Returns:

Name Type Description
str Union[None, str]

name to be given to the new instance

None Union[None, str]

if the type of the given parent entity is not equal with "AtomicTask" or "AtomicMethod"

Source code in exe_kg_lib/utils/kg_creation_utils.py
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
def name_instance(
    task_type_dict: Dict[str, int],
    method_type_dict: Dict[str, int],
    parent_entity: Entity,
) -> Union[None, str]:
    """
    Creates a unique name for a new instance by concatenating the parent entity's name (which is the instance type) with a number
    Also increments the relevant number of the corresponding dict
    Args:
        task_type_dict: contains pairs of task types and numbers
        method_type_dict: contains pairs of method types and numbers
        parent_entity: instance's parent entity

    Returns:
        str: name to be given to the new instance
        None: if the type of the given parent entity is not equal with "AtomicTask" or "AtomicMethod"
    """
    if parent_entity.type == "AtomicTask":
        entity_type_dict = task_type_dict
    elif parent_entity.type == "AtomicMethod":
        entity_type_dict = method_type_dict
    else:
        print("Error: Invalid parent entity type")
        return None

    instance_name = parent_entity.name + str(entity_type_dict[parent_entity.name])
    entity_type_dict[parent_entity.name] += 1
    return instance_name

Last update: October 20, 2023