Modelling context-aware systems
How to represent and reason about context in context-aware systems: definitions, common modelling styles (key-value, markup, graphical, logic, ontology, probabilistic), formulas (probability/Bayes), worked examples (Bayesian inference, RDF triples/SPARQL, designing an entity-attribute model), common
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Context-aware systems adapt behaviour based on the situation of people, devices, or environments. An engineer uses context modelling to choose how to represent that situation so software can acquire, store, and reason about it reliably and efficiently.
Key ideas
Start from first principles; define each term before use.
Context — the set of facts or information that characterises the situation of an entity relevant to an application's behaviour. Exact statement: "Given an application and an entity (person, device, or place), the context is the set of observable and inferred property-value pairs about that entity and its environment that can influence application behaviour." Assumptions: the application and entity are both explicitly in scope; only information that can be observed or inferred is part of context.
Context-aware system — a system that uses context to provide relevant services or information. Exact statement: "A context-aware system is a software/hardware system that (1) acquires context, (2) models context in a computable form, (3) reasons about context to infer higher-level facts or make decisions, and (4) adapts its behaviour based on those facts." Assumptions: the system has mechanisms for acquisition, representation, reasoning, and adaptation.
Context acquisition — the process of obtaining raw context data from sensors, user input, external services, or other systems. Acquisition may be active (device polls a sensor) or passive (sensor pushes events). Important assumption: raw sensor data may be noisy and incomplete. Also model Quality of Context (QoC) attributes (e.g., accuracy, precision, completeness, timeliness/freshness, confidence) and propagate them through reasoning.
Context representation / modelling — the method used to encode context so that software can store and process it. A model must balance expressiveness, efficiency, and support for uncertainty.
Context reasoning — deriving higher-level context or decisions from represented context using rules, logic, or probabilistic methods.
Context lifecycle — the stages: acquisition → preprocessing (filtering, cleaning) → representation (modelling) → reasoning/inference → storage/archival → dissemination → adaptation. Assumption: context ages; freshness is important.
Common modelling styles (each defined with assumptions and typical use):
Key–Value model
- Definition: a simple representation where each context item is a key and has a value (key: value). Example:
location: "Room 101". - Assumptions: suitable for small, simple context; no native relationships or semantics; easy to implement.
- When asked to draw: draw a table with two columns labelled "Key" and "Value" and rows for example keys (userID, location, batteryLevel).
- Definition: a simple representation where each context item is a key and has a value (key: value). Example:
Markup-based model
- Definition: uses tree-structured formats (XML, JSON) to represent nested context objects. Example: a JSON object with fields for user and location.
- Assumptions: supports nesting and typing; human-readable; needs schema for validation.
- Diagram: draw a tree with root
Context, childUser(with childrenid,name), childDevice(withid,battery).
Graphical model (ER/UML graphs)
- Definition: uses entities, attributes, and relationships (nodes and edges). Example: a
Usernode linked to aLocationnode by anisAtedge. - Assumptions: good for relational queries and visual design; needs mapping to storage.
- Diagram: draw circles for entities (User, Device, Place) and labelled edges (owns, isAt) with multiplicities.
- Definition: uses entities, attributes, and relationships (nodes and edges). Example: a
Logic-based model
- Definition: represents context as facts and rules in predicate logic (Prolog-style). Example facts:
at(user1, room101).Rule:in_meeting(U) :- at(U, room101), time(T), between('10:00','11:00', T). - Assumptions: good for explicit rule reasoning; deterministic unless extended with probabilities.
- If asked to derive: show modus ponens: from
p -> qandp, inferq.
- Definition: represents context as facts and rules in predicate logic (Prolog-style). Example facts:
Ontology-based model (RDF/OWL)
- Definition: uses RDF triples (subject, predicate, object) and ontologies (OWL) to define types, relationships, and inference rules. Supports semantic interoperability.
- Exact RDF triple statement: a triple is
<subject> <predicate> <object> .(subject and predicate are IRIs; object is IRI or literal). Assumptions: requires ontology design and reasoning engine; more expressive but heavier. - Diagram to draw: nodes for subjects/objects and labelled directed edges for predicates; annotate classes and subclass hierarchies.
Probabilistic models
- Definition: represent uncertain context using probability distributions or graphical models (Bayesian networks, HMMs). Example:
P(activity = walking | accel = high) = 0.8. - Assumptions: sensors are noisy; need training data or expert probabilities. Good for activity recognition and sensor fusion.
- When deriving inference, use Bayes' theorem (see Formulas).
- Definition: represent uncertain context using probability distributions or graphical models (Bayesian networks, HMMs). Example:
Design trade-offs to state exactly as an answer: expressiveness vs efficiency (more expressive models like OWL add reasoning cost); openness vs control (semantic web gives interoperability but needs agreement on ontologies); determinism vs uncertainty (logic vs probabilistic models).
Formulas
(We list the probability formulas and important syntax lines to remember for context modelling.)
P(A | B) = P(B | A) * P(A) / P(B)
- P(A | B): conditional probability of A given B. Unitless. Applies in Bayes inference for uncertain context.
P(A ∧ B) = P(A | B) * P(B) = P(B | A) * P(A)
- Joint probability of A and B. Unitless. Use when computing combined events.
P(A1, A2, ..., An) = Π_{i=1..n} P(Ai | A1..A(i-1)) (chain rule)
- Chain rule for joint distributions. Unitless. Use in probabilistic graphical models.
RDF triple syntax to remember (one line):
<subject> <predicate> <object> .
- subject: IRI or blank node; predicate: IRI; object: IRI, blank node, or literal. Use in ontology-based models.
OWL class declaration (Turtle-ish example):
ex:User rdf:type owl:Class .
- Shows how to declare a class in RDF/OWL serialisation; used when designing ontologies.
SPARQL SELECT pattern (simplified):
SELECT ?user WHERE { ?user ex:isAt ex:Room101 . }
- Selects users at Room101.
Note: these syntaxes are representative; specific serialisations (RDF/XML, Turtle, JSON-LD) vary. Verify serialisation details for your toolchain.
Worked examples
Example 1 — Bayesian activity recognition (numerical): Problem: A wearable accelerometer reports a feature "accel_high". From training we know: P(accel_high | walking) = 0.9, P(accel_high | running) = 0.6. Prior probabilities: P(walking) = 0.5, P(running) = 0.1, P(sitting) = 0.4. Compute P(walking | accel_high). Steps:
- Write Bayes:
P(walking | accel_high) = P(accel_high | walking) * P(walking) / P(accel_high). - Compute P(accel_high) = P(accel_high | walking)P(walking) + P(accel_high | running)P(running) + P(accel_high | sitting)P(sitting). We assume P(accel_high | sitting)=0.05 (sensor rarely high when sitting).
- Numbers: P(accel_high) = 0.90.5 + 0.60.1 + 0.05*0.4 = 0.45 + 0.06 + 0.02 = 0.53.
- Now posterior: (0.9*0.5)/0.53 = 0.45/0.53 = 0.8490566... ≈ 0.849. Answer: P(walking | accel_high) = 0.849 (≈84.9%).
Example 2 — RDF triples and SPARQL query (trace): Goal: model that user U1 is in Room101 at time 10:15 and query who is in Room101. Triples to create (in Turtle-like notation):
<http://example.org/U1> rdf:type ex:User .<http://example.org/Room101> rdf:type ex:Room .<http://example.org/U1> ex:isAt <http://example.org/Room101> .<http://example.org/obs1> ex:timestamp "2026-11-24T10:15:00"^^xsd:dateTime .<http://example.org/obs1> ex:about <http://example.org/assert1> .<http://example.org/assert1> rdf:type rdf:Statement ; rdf:subject <http://example.org/U1> ; rdf:predicate ex:isAt ; rdf:object <http://example.org/Room101> .SPARQL query:SELECT ?user WHERE { ?user ex:isAt <http://example.org/Room101> . }Query trace: engine matches triple 3, binds?user=<http://example.org/U1>. Result: http://example.org/U1. Final answer: http://example.org/U1.
Example 3 — Designing a context model for a smart office (algorithmic/model design): Problem: choose a representation for user presence and meeting state that allows apps to know if a user is in a meeting and whether to interrupt. Steps:
- Decide requirements: need user, device, room, calendar event, sensor readings (microphone, calendar service). Need to infer
inMeeting(user)with uncertainty. - Choose hybrid model: ontology for entities/relationships (User, Device, Room, CalendarEvent) and probabilistic model for inference (Bayesian network combining calendar and sensors).
- Create entity-attribute list: User(id, name), Device(id, owner), Room(id, capacity), CalendarEvent(id, participants, start, end).
- Diagram to draw: boxes for classes and arrows: User --owns--> Device; User --isAt--> Room; CalendarEvent --scheduledIn--> Room; annotate probabilistic nodes: calendar_conflict (boolean), mic_activity (probability), combine into inMeeting node with CPT (conditional probability table).
- Explain reasoning: if calendar shows event and mic detects conversation, posterior P(inMeeting=true) high; if calendar empty and mic silent, posterior low. Result: hybrid gives structured queries (from ontology) and robust inference (probabilistic).
Common mistakes
- Forgetting sensor uncertainty and temporal aspects: treating every sensor reading as ground truth and not modelling time/freshness.
- Overfitting probabilistic models without enough training data: bad CPTs make poor inferences.
- Omitting units or time zones in time stamps (leads to wrong temporal reasoning).
- Mixing up RDF triple serialisations (Turtle vs RDF/XML) and writing invalid syntax — state the serialisation you use.
- Ignoring privacy and access control for context data: failing to minimise collection, protect sensitive attributes, and enforce consent/policies.
For your exam
Types of questions you will see: definitions (define context, context-aware system), short notes (compare modelling styles), derivations (derive Bayes' theorem or show logic rule inference), numericals (compute posterior probability for activity recognition), design questions (choose and justify a model for a use case), and diagram tasks (draw an ontology fragment or ER diagram to represent context).
What a full-marks answer includes:
- Precise definitions with assumptions stated.
- For modelling comparisons: state trade-offs and example use-cases.
- For derivations: step-by-step mathematics (show chain rule, substitution), or for logic show rule application (modus ponens).
- For design questions: list requirements, chosen model, schema (entities and attributes), reasoning method, and a labelled diagram description.
- For numericals: show formulas, substitute numbers, carry units/tags (time stamps), show final numeric answer with percent if probability.
Quick check
- Define context in one sentence.
- Write Bayes' theorem and state what each term means.
- Give one advantage and one disadvantage of ontology-based context models.
Answers: 1) The set of facts or information that characterises the situation of an entity relevant to an application's behaviour. 2) P(A | B) = P(B | A) * P(A) / P(B) where P(A|B) is posterior, P(B|A) is likelihood, P(A) is prior, P(B) is evidence. 3) Advantage: semantic interoperability and reasoning; Disadvantage: higher complexity and computational cost.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?