-
Notifications
You must be signed in to change notification settings - Fork 14
/
e02_ngsi_v2_context_relationships.py
143 lines (129 loc) · 5.2 KB
/
e02_ngsi_v2_context_relationships.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
"""
# Examples for relationships in FIWARE ContextBroker
"""
# ## Import packages
import logging
from filip.config import settings
from filip.clients.ngsi_v2 import ContextBrokerClient
from filip.models.ngsi_v2.context import ContextEntity
from filip.models.base import FiwareHeader
from filip.utils.simple_ql import QueryString
# ## Parameters
#
# To run this example you need a working Fiware v2 setup with a context-broker
# You can set the address:
#
# Host address of Context Broker
CB_URL = settings.CB_URL
# You can also change the used Fiware service
# FIWARE-Service
SERVICE = "filip"
# FIWARE-Servicepath
SERVICE_PATH = "/example"
# Setting up logging
logging.basicConfig(
level="INFO",
format="%(asctime)s %(name)s %(levelname)s: %(message)s",
datefmt="%d-%m-%Y %H:%M:%S",
)
logger = logging.getLogger(__name__)
if __name__ == "__main__":
# # 1 Setup client and models
#
fiware_header = FiwareHeader(service=SERVICE, service_path=SERVICE_PATH)
# ## 1.1 Store entities
#
with ContextBrokerClient(fiware_header=fiware_header, url=CB_URL) as cb_client:
# make sure that the server is clean
cb_client.delete_entities(cb_client.get_entity_list())
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 = [ContextEntity(**store) for store in store_dict]
for entity in store_entities:
cb_client.post_entity(entity)
# ## 1.2 Product entities
#
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))
# ## 1.3 Inventory Entities
#
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)
# # 2 Read data from FIWARE
#
# ## 2.1 Inventory relationship with Store and Product
logger.info(inventory_entity.get_relationships())
# ## 2.2 Get entities
#
with ContextBrokerClient(fiware_header=fiware_header) as cb_client:
# It should return the inventory item according to the relationship
query = QueryString(qs=[("refProduct", "==", "urn:ngsi-ld:Product:001")])
logger.info(cb_client.get_entity_list(q=query))
query = QueryString(qs=[("refStore", "==", "urn:ngsi-ld:Store:001")])
logger.info(cb_client.get_entity_list(q=query))
# It should not return the inventory item according to the relationship
query = QueryString(qs=[("refStore", "==", "urn:ngsi-ld:Store:002")])
logger.info(cb_client.get_entity_list(q=query))
# # 3 Delete test entities
#
with ContextBrokerClient(fiware_header=fiware_header) as cb_client:
cb_client.delete_entities(store_entities)
cb_client.delete_entities(product_entities)
cb_client.delete_entity(
entity_id=inventory_entity.id, entity_type=inventory_entity.type
)