|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Example: Business Ontology Layer with Microsoft CDM Support |
| 4 | +
|
| 5 | +This example demonstrates how to use the business ontology layer to map |
| 6 | +a semantic model to Microsoft Common Data Model entities. |
| 7 | +""" |
| 8 | + |
| 9 | +import sys |
| 10 | +import io |
| 11 | +import pandas as pd |
| 12 | + |
| 13 | +# Fix Windows console encoding |
| 14 | +if sys.platform == 'win32': |
| 15 | + sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace') |
| 16 | + sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8', errors='replace') |
| 17 | + |
| 18 | +from intugle import ( |
| 19 | + BusinessOntology, |
| 20 | + CDMCatalog, |
| 21 | + OntologyMapper, |
| 22 | + SemanticModel, |
| 23 | +) |
| 24 | + |
| 25 | + |
| 26 | +def main(): |
| 27 | + # Sample data for demonstration |
| 28 | + customer_data = pd.DataFrame({ |
| 29 | + "customer_id": [1, 2, 3, 4, 5], |
| 30 | + "email": ["alice@example.com", "bob@example.com", "charlie@example.com", |
| 31 | + "diana@example.com", "eve@example.com"], |
| 32 | + "full_name": ["Alice Smith", "Bob Jones", "Charlie Brown", "Diana Prince", "Eve Wilson"], |
| 33 | + "phone": ["555-0001", "555-0002", "555-0003", "555-0004", "555-0005"] |
| 34 | + }) |
| 35 | + |
| 36 | + account_data = pd.DataFrame({ |
| 37 | + "account_id": [101, 102, 103], |
| 38 | + "account_name": ["Acme Corp", "TechStart Inc", "Global Solutions"], |
| 39 | + "account_balance": [50000.00, 75000.00, 120000.00] |
| 40 | + }) |
| 41 | + |
| 42 | + sales_order_data = pd.DataFrame({ |
| 43 | + "order_id": [1001, 1002, 1003, 1004], |
| 44 | + "order_date": pd.to_datetime(["2024-01-15", "2024-01-16", "2024-01-17", "2024-01-18"]), |
| 45 | + "customer_id": [1, 2, 1, 3], |
| 46 | + "total_amount": [1500.00, 2300.00, 890.00, 3200.00] |
| 47 | + }) |
| 48 | + |
| 49 | + data_dict = { |
| 50 | + "customer": customer_data, |
| 51 | + "account": account_data, |
| 52 | + "sales_order_header": sales_order_data |
| 53 | + } |
| 54 | + |
| 55 | + print("=" * 80) |
| 56 | + print("Business Ontology Layer Example - Microsoft CDM Integration") |
| 57 | + print("=" * 80) |
| 58 | + print() |
| 59 | + |
| 60 | + # 1. Load the existing semantic data model |
| 61 | + print("Step 1: Creating semantic model from data...") |
| 62 | + semantic_model = SemanticModel(data_dict, domain="E-commerce") |
| 63 | + print(f"✓ Created semantic model with {len(semantic_model.datasets)} datasets") |
| 64 | + print(f" Datasets: {', '.join(semantic_model.datasets.keys())}") |
| 65 | + print() |
| 66 | + |
| 67 | + # 2. Load or initialize the Microsoft CDM catalog |
| 68 | + print("Step 2: Loading Microsoft CDM catalog...") |
| 69 | + cdm_catalog = CDMCatalog.load_builtin("cdm_core") |
| 70 | + print(f"✓ Loaded CDM catalog: {cdm_catalog.name}") |
| 71 | + print(f" Available entities: {', '.join(cdm_catalog.list_entities()[:5])}...") |
| 72 | + print() |
| 73 | + |
| 74 | + # Also load sales catalog |
| 75 | + sales_catalog = CDMCatalog.load_builtin("cdm_sales") |
| 76 | + print(f"✓ Loaded sales catalog: {sales_catalog.name}") |
| 77 | + print(f" Available entities: {', '.join(sales_catalog.list_entities())}") |
| 78 | + print() |
| 79 | + |
| 80 | + # 3. Create / load a business ontology |
| 81 | + print("Step 3: Creating business ontology...") |
| 82 | + business_ontology = BusinessOntology(name="Enterprise Business Ontology (CDM)") |
| 83 | + print(f"✓ Created business ontology: {business_ontology.name}") |
| 84 | + print() |
| 85 | + |
| 86 | + # Define domains |
| 87 | + print("Step 4: Defining business domains...") |
| 88 | + business_ontology.add_domain( |
| 89 | + name="CustomerDomain", |
| 90 | + description="All customer and account-related concepts" |
| 91 | + ) |
| 92 | + business_ontology.add_domain( |
| 93 | + name="SalesDomain", |
| 94 | + description="Sales orders, invoices, and related concepts" |
| 95 | + ) |
| 96 | + print(f"✓ Added {len(business_ontology.domains)} domains") |
| 97 | + for domain_name in business_ontology.list_domains(): |
| 98 | + domain = business_ontology.get_domain(domain_name) |
| 99 | + print(f" - {domain_name}: {domain.description}") |
| 100 | + print() |
| 101 | + |
| 102 | + # Define business concepts linked to CDM entities |
| 103 | + print("Step 5: Defining business concepts linked to CDM entities...") |
| 104 | + |
| 105 | + customer_concept = business_ontology.add_concept( |
| 106 | + name="Customer", |
| 107 | + domain="CustomerDomain", |
| 108 | + cdm_entity=cdm_catalog.get_entity("Contact"), |
| 109 | + ) |
| 110 | + print(f"✓ Added concept: {customer_concept.name} -> CDM:{customer_concept.cdm_entity_name}") |
| 111 | + |
| 112 | + account_concept = business_ontology.add_concept( |
| 113 | + name="Account", |
| 114 | + domain="CustomerDomain", |
| 115 | + cdm_entity=cdm_catalog.get_entity("Account"), |
| 116 | + ) |
| 117 | + print(f"✓ Added concept: {account_concept.name} -> CDM:{account_concept.cdm_entity_name}") |
| 118 | + |
| 119 | + sales_order_concept = business_ontology.add_concept( |
| 120 | + name="SalesOrder", |
| 121 | + domain="SalesDomain", |
| 122 | + cdm_entity=sales_catalog.get_entity("SalesOrder"), |
| 123 | + ) |
| 124 | + print(f"✓ Added concept: {sales_order_concept.name} -> CDM:{sales_order_concept.cdm_entity_name}") |
| 125 | + print() |
| 126 | + |
| 127 | + # 4. Map semantic entities to business concepts / CDM |
| 128 | + print("Step 6: Mapping semantic entities to business concepts and CDM...") |
| 129 | + mapper = OntologyMapper(semantic_model, business_ontology, cdm_catalog) |
| 130 | + print(f"✓ Created ontology mapper") |
| 131 | + print() |
| 132 | + |
| 133 | + print("Mapping: semantic.customer -> Business Concept: Customer -> CDM: Contact") |
| 134 | + mapper.map_entity( |
| 135 | + semantic_entity="customer", |
| 136 | + concept="Customer", |
| 137 | + attribute_map={ |
| 138 | + "customer_id": "Contact.ContactId", |
| 139 | + "email": "Contact.Email", |
| 140 | + "full_name": "Contact.FullName", |
| 141 | + "phone": "Contact.PhoneNumber", |
| 142 | + }, |
| 143 | + ) |
| 144 | + print("✓ Mapped customer entity with 4 attributes") |
| 145 | + print() |
| 146 | + |
| 147 | + print("Mapping: semantic.account -> Business Concept: Account -> CDM: Account") |
| 148 | + mapper.map_entity( |
| 149 | + semantic_entity="account", |
| 150 | + concept="Account", |
| 151 | + attribute_map={ |
| 152 | + "account_id": "Account.AccountId", |
| 153 | + "account_name": "Account.Name", |
| 154 | + "account_balance": "Account.Balance", |
| 155 | + }, |
| 156 | + ) |
| 157 | + print("✓ Mapped account entity with 3 attributes") |
| 158 | + print() |
| 159 | + |
| 160 | + print("Mapping: semantic.sales_order_header -> Business Concept: SalesOrder -> CDM: SalesOrder") |
| 161 | + mapper.map_entity( |
| 162 | + semantic_entity="sales_order_header", |
| 163 | + concept="SalesOrder", |
| 164 | + attribute_map={ |
| 165 | + "order_id": "SalesOrder.SalesOrderId", |
| 166 | + "order_date": "SalesOrder.OrderDate", |
| 167 | + "customer_id": "SalesOrder.CustomerId", |
| 168 | + "total_amount": "SalesOrder.TotalAmount", |
| 169 | + }, |
| 170 | + ) |
| 171 | + print("✓ Mapped sales_order_header entity with 4 attributes") |
| 172 | + print() |
| 173 | + |
| 174 | + # 5. Query and analyze mappings |
| 175 | + print("=" * 80) |
| 176 | + print("Mapping Analysis") |
| 177 | + print("=" * 80) |
| 178 | + print() |
| 179 | + |
| 180 | + summary = mapper.get_mapping_summary() |
| 181 | + print(f"Total mappings: {summary['total_mappings']}") |
| 182 | + print(f"Unmapped semantic entities: {summary['unmapped_semantic_entities']}") |
| 183 | + print(f"Unmapped CDM entities: {summary['unmapped_cdm_entities']}") |
| 184 | + print() |
| 185 | + |
| 186 | + print("Mappings by status:") |
| 187 | + for status, count in summary['mappings_by_status'].items(): |
| 188 | + print(f" - {status}: {count}") |
| 189 | + print() |
| 190 | + |
| 191 | + print("Mappings by type:") |
| 192 | + for map_type, count in summary['mappings_by_type'].items(): |
| 193 | + print(f" - {map_type}: {count}") |
| 194 | + print() |
| 195 | + |
| 196 | + # Query specific mappings |
| 197 | + print("Query: Which semantic entities map to CDM Contact?") |
| 198 | + contact_mappings = mapper.get_mappings_by_cdm_entity("Contact") |
| 199 | + for mapping in contact_mappings: |
| 200 | + print(f" - {', '.join(mapping.semantic_entities)} -> {mapping.concept_name}") |
| 201 | + print() |
| 202 | + |
| 203 | + print("Query: Which CDM entities are in CustomerDomain?") |
| 204 | + customer_concepts = business_ontology.get_concepts_by_domain("CustomerDomain") |
| 205 | + for concept in customer_concepts: |
| 206 | + print(f" - {concept.name} -> CDM:{concept.cdm_entity_name}") |
| 207 | + print() |
| 208 | + |
| 209 | + # 6. Save ontology + mappings |
| 210 | + print("=" * 80) |
| 211 | + print("Saving Ontology and Mappings") |
| 212 | + print("=" * 80) |
| 213 | + print() |
| 214 | + |
| 215 | + print("Saving business ontology...") |
| 216 | + business_ontology.save("business_ontology_cdm.json") |
| 217 | + print("✓ Saved to: business_ontology_cdm.json") |
| 218 | + print() |
| 219 | + |
| 220 | + print("Saving semantic-to-CDM mappings...") |
| 221 | + mapper.export_mappings("semantic_to_cdm_mappings.json") |
| 222 | + print("✓ Saved to: semantic_to_cdm_mappings.json") |
| 223 | + print() |
| 224 | + |
| 225 | + print("=" * 80) |
| 226 | + print("Example Complete!") |
| 227 | + print("=" * 80) |
| 228 | + print() |
| 229 | + print("Key capabilities demonstrated:") |
| 230 | + print(" ✓ Business domain organization (CustomerDomain, SalesDomain)") |
| 231 | + print(" ✓ Business concepts aligned with CDM entities") |
| 232 | + print(" ✓ Semantic model to CDM mappings at entity and attribute level") |
| 233 | + print(" ✓ Query and analysis of mappings") |
| 234 | + print(" ✓ Persistence of ontology and mappings") |
| 235 | + print() |
| 236 | + print("Next steps:") |
| 237 | + print(" - Extend to other CDM catalogs (FIBO, custom ontologies)") |
| 238 | + print(" - Use mappings for data product generation") |
| 239 | + print(" - Integrate with governance and cataloging tools") |
| 240 | + print() |
| 241 | + |
| 242 | + |
| 243 | +if __name__ == "__main__": |
| 244 | + main() |
0 commit comments