forked from MarieluiseOden/FiLiP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
relationship_example.py
178 lines (156 loc) · 6.37 KB
/
relationship_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
84
85
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
114
115
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
169
170
171
172
173
174
175
176
177
178
import logging
from filip.clients.ngsi_v2 import ContextBrokerClient
from filip.models.ngsi_v2.context import ContextEntity
from filip.models.base import FiwareHeader
# Setting up logging
logging.basicConfig(
level='INFO',
format='%(asctime)s %(name)s %(levelname)s: %(message)s')
logger = logging.getLogger(__name__)
def create_store_entities(fiware_header: FiwareHeader):
with ContextBrokerClient(fiware_header=fiware_header) as cb_client:
store_dict = [{"type": "Store",
"id": "urn:ngsi-ld:Store:001",
"address": {
"type": "Text",
"value": "Bornholmer Straße 65"
},
"location": {
"type": "Text",
"value": "[13.3986, 52.5547]"
},
"name": {
"type": "Text",
"value": "Bösebrücke Einkauf"
}},
{
"type": "Store",
"id": "urn:ngsi-ld:Store:002",
"address": {
"type": "Text",
"value": "Friedrichstraße 44"
},
"location": {
"type": "Text",
"value": "[13.3903, 52.5075]"
},
"name": {
"type": "Text",
"value": "Checkpoint Markt"
}
}]
store_entities = []
for store in store_dict:
cb_client.post_entity(ContextEntity(**store))
store_entities.append(ContextEntity(**store))
return store_entities
def create_product_entities(fiware_header: FiwareHeader):
with ContextBrokerClient(fiware_header=fiware_header) as cb_client:
product_dict = [
{
"id": "urn:ngsi-ld:Product:001", "type": "Product",
"name": {
"type": "Text", "value": "Beer"
},
"size": {
"type": "Text", "value": "S"
},
"price": {
"type": "Integer", "value": 99
}
},
{
"id": "urn:ngsi-ld:Product:002", "type": "Product",
"name": {
"type": "Text", "value": "Red Wine"
},
"size": {
"type": "Text", "value": "M"
},
"price": {
"type": "Integer", "value": 1099
}
},
{
"id": "urn:ngsi-ld:Product:003", "type": "Product",
"name": {
"type": "Text", "value": "White Wine"
},
"size": {
"type": "Text", "value": "M"
},
"price": {
"type": "Integer", "value": 1499
}
},
{
"id": "urn:ngsi-ld:Product:004", "type": "Product",
"name": {
"type": "Text", "value": "Vodka"
},
"size": {
"type": "Text", "value": "XL"
},
"price": {
"type": "Integer", "value": 5000
}
}
]
product_entities = []
for product_entity in product_dict:
cb_client.post_entity(ContextEntity(**product_entity))
product_entities.append(ContextEntity(**product_entity))
return product_entities
def create_inventory(fiware_header: FiwareHeader):
with ContextBrokerClient(fiware_header=fiware_header) as cb_client:
inventory_dict = {
"id": "urn:ngsi-ld:InventoryItem:001", "type": "InventoryItem",
"refStore": {
"type": "Relationship",
"value": "urn:ngsi-ld:Store:001"
},
"refProduct": {
"type": "Relationship",
"value": "urn:ngsi-ld:Product:001"
},
"stockCount": {
"type": "Integer", "value": 10000
}}
inventory_entity = ContextEntity(**inventory_dict)
cb_client.post_entity(inventory_entity)
return inventory_entity
def delete_entities(fiware_header: FiwareHeader, entities_to_delete):
with ContextBrokerClient(fiware_header=fiware_header) as cb_client:
for entity in entities_to_delete:
cb_client.delete_entity(entity_id=entity.id)
return
def get_entities(fiware_header: FiwareHeader, query: str):
with ContextBrokerClient(fiware_header=fiware_header) as cb_client:
logger.info(cb_client.get_entity_list(q=query))
if __name__ == "__main__":
fiware_header = FiwareHeader(service='filip', service_path='/testing')
example_store_entities = create_store_entities(
fiware_header=fiware_header)
example_product_entities = create_product_entities(
fiware_header=fiware_header)
example_inventory_entity = create_inventory(
fiware_header=fiware_header)
logger.info("------Inventory relationship with Store and Product------")
logger.info(example_inventory_entity.get_relationships())
logger.info("------It should return the inventory item according to the "
"relationship------")
get_entities(fiware_header=fiware_header,
query="refProduct==urn:ngsi-ld:Product:001")
get_entities(fiware_header=fiware_header,
query="refStore==urn:ngsi-ld:Store:001")
logger.info("------It should not return the inventory item according to the "
"relationship------")
get_entities(fiware_header=fiware_header,
query="refStore==urn:ngsi-ld:Store:002")
logger.info("------Deleting all test entities------")
delete_entities(fiware_header=fiware_header,
entities_to_delete=example_store_entities)
delete_entities(fiware_header=fiware_header,
entities_to_delete=example_product_entities)
delete_entities(fiware_header=fiware_header,
entities_to_delete=[example_inventory_entity])