MATIH Platform is in active MVP development. Documentation reflects current implementation status.
14. Context Graph & Ontology
Overview

Chapter 14: Context Graph, Ontology, and Semantic Layer

The Context Graph, Ontology Service, and Semantic Layer form the knowledge infrastructure of the MATIH platform, providing the semantic understanding that enables intelligent data discovery, agent reasoning capture, and business-aware query generation. This chapter covers three interconnected services: the Context Graph within the AI Service for knowledge graph construction and agent thinking traces, the Ontology Service for entity relationship management, and the Semantic Layer for business metric definitions powered by WrenAI.


What You Will Learn

By the end of this chapter, you will understand:

  • The Context Graph architecture including Dgraph-backed knowledge storage, agent thinking trace capture, and integration with the agent orchestrators
  • The Ontology Service built on Python/FastAPI for entity relationship management, GraphQL schema modeling, and semantic query support
  • The Semantic Layer built on Java/Spring Boot with WrenAI integration for business metric definitions, dimensions, facts, and semantic SQL generation
  • Knowledge graph construction from agent reasoning traces, entity extraction, and relationship discovery
  • Embedding generation including structural, trajectory, node2vec, similarity, and thinking embeddings
  • The Agent Thinking Service for capturing and querying agent reasoning patterns with tenant-scoped embeddings

Chapter Structure

SectionDescriptionAudience
Context Graph ArchitectureDgraph storage, tenant isolation, thinking models, and API surfaceKnowledge engineers, backend developers
Ontology ServiceEntity relationships, GraphQL schema, semantic queriesData architects, knowledge engineers
Semantic LayerWrenAI integration, business metrics, dimensions, factsData analysts, business analysts
Knowledge Graph ConstructionGraph building from agent traces, entity extraction, syncAI/ML engineers, knowledge engineers
Embedding GenerationStructural, trajectory, node2vec, similarity, model providersAI/ML engineers
Agent Thinking ServiceThinking trace capture, reasoning analysis, pattern detectionAI/ML engineers
API ReferenceComplete API documentation for all three servicesAll developers

Services Overview

The knowledge infrastructure comprises three services:

+---------------------------------------------------+
|                AI Service (Port 8000)              |
|                                                    |
|  +--------------------------------------------+   |
|  |           Context Graph Module              |   |
|  |                                              |  |
|  |  +------------------+  +------------------+  |  |
|  |  | Agent Thinking   |  | Orchestrator     |  |  |
|  |  | Service          |  | Hooks            |  |  |
|  |  +--------+---------+  +--------+---------+  |  |
|  |           |                      |            |  |
|  |  +--------v---------+  +--------v---------+  |  |
|  |  | Dgraph Context   |  | Metrics Bridge   |  |  |
|  |  | Store             |  |                  |  |  |
|  |  +--------+---------+  +--------+---------+  |  |
|  |           |                      |            |  |
|  |  +--------v----------------------v---------+  |  |
|  |  |         Embedding Services               |  |  |
|  |  | (structural, trajectory, thinking)       |  |  |
|  |  +------------------------------------------+  |  |
|  +--------------------------------------------+   |
+---------------------------------------------------+
         |                    |
         v                    v
+----------------+   +-------------------+
| Dgraph         |   | Qdrant / Pinecone |
| (Graph DB)     |   | (Vector Store)    |
+----------------+   +-------------------+

+---------------------------------------------------+
|          Ontology Service (Port 8101)              |
|  Entity relationships, GraphQL schema,             |
|  semantic queries                                  |
+---------------------------------------------------+
         |
         v
+----------------+
| Dgraph         |
| (Shared)       |
+----------------+

+---------------------------------------------------+
|          Semantic Layer (Port 8086)                |
|  WrenAI integration, business metrics,             |
|  dimensions, facts                                 |
+---------------------------------------------------+

Key Numbers

ServiceTechnologyPortStorage
Context GraphPython (within AI Service)8000Dgraph, Qdrant/Pinecone
Ontology ServicePython/FastAPI8101Dgraph
Semantic LayerJava/Spring Boot8086PostgreSQL, WrenAI

Key Source Files

Context Graph (within AI Service)

PathPurpose
context_graph/services/agent_thinking_service.pyAgentThinkingCaptureService for trace lifecycle
context_graph/services/agent_trace_service.pyTrace persistence and retrieval
context_graph/storage/dgraph_context_store.pyDgraphContextStore with tenant-isolated queries
context_graph/embeddings/thinking_embeddings.pyThinkingEmbeddingService for trace embeddings
context_graph/embeddings/structural.pyStructuralEmbedder using Node2Vec
context_graph/embeddings/trajectory.pyTrajectory embeddings
context_graph/embeddings/node2vec.pyNode2Vec implementation
context_graph/embeddings/similarity.pySimilarity computation
context_graph/embeddings/model_providers.pyEmbedding model abstraction
context_graph/integration/orchestrator_hooks.pyContextGraphOrchestratorHooks
context_graph/integration/metrics_bridge.pyMetricsBridge for observability linkage
context_graph/integration/kafka_producer.pyKafka event streaming
context_graph/integration/kafka_consumer.pyKafka event consumption
context_graph/security/authorization.pyGraph access authorization
context_graph/security/permissions.pyPermission management
context_graph/config/tenant_config.pyPer-tenant configuration
context_graph/causal/Causal chain analysis
context_graph/simulation/Graph simulation

Ontology Service

PathPurpose
ontology-service/src/api/routes/API route definitions
ontology-service/src/api/graphql/GraphQL schema and resolvers
ontology-service/src/models/object_type.pyEntity type definitions
ontology-service/src/models/enums.pyEnumeration types
ontology-service/src/models/semantic/Semantic model definitions
ontology-service/src/storage/Storage backends
ontology-service/src/schema_mapping/Schema mapping
ontology-service/src/templates/Ontology templates

Semantic Layer

PathPurpose
semantic-layer/src/Java/Spring Boot source
WrenAI integrationBusiness metric definitions

Integration Points

SourceTargetProtocolPurpose
AI Service OrchestratorContext GraphIn-processThinking trace capture
Context GraphDgraphHTTP/GraphQLKnowledge graph persistence
Context GraphQdrant/PineconegRPC/HTTPEmbedding storage and search
Context GraphKafkaKafkaEvent streaming
Ontology ServiceDgraphHTTP/GraphQLEntity relationship storage
Semantic LayerPostgreSQLJDBCMetric definitions
Semantic LayerWrenAIHTTPSemantic SQL generation
AI ServiceOntology ServiceHTTPEntity resolution
AI ServiceSemantic LayerHTTPMetric definitions for SQL