Source code for knowledgespaces.query.types

"""
Core types for the QUERY algorithm.

Defines the Query object and the result containers used across
all blocks of the algorithm.
"""

from __future__ import annotations

from dataclasses import dataclass
from enum import Enum, auto


[docs] class InferenceSource(Enum): """How a query answer was determined.""" EXPERT = auto() TRANSITIVITY = auto() ANTISYMMETRY = auto() MONOTONICITY_NEGATIVE = auto() MONOTONICITY_POSITIVE = auto() STRUCTURAL = auto()
[docs] @dataclass(frozen=True) class Query: """A single query: 'Does failing all items in A imply failing q?' For pair queries (Block 1): antecedent has 1 item. For group queries (Block 2+): antecedent has 2+ items. For Qmax minimality test: antecedent = Q without {q}. Parameters ---------- antecedent : frozenset[str] The set A of items. consequent : str The target item q. """ antecedent: frozenset[str] consequent: str
[docs] @classmethod def pair(cls, a: str, q: str) -> Query: """Create a pair query: a -> q.""" return cls(frozenset({a}), q)
[docs] @classmethod def group(cls, A: frozenset[str], q: str) -> Query: """Create a group query: A -> q.""" return cls(A, q)
def __repr__(self) -> str: if len(self.antecedent) == 1: a = next(iter(self.antecedent)) return f"Query({a} -> {self.consequent})" return f"Query({set(self.antecedent)} -> {self.consequent})"
[docs] @dataclass(frozen=True) class QueryAnswer: """A resolved query with its answer and source.""" query: Query answer: bool source: InferenceSource @property def is_positive(self) -> bool: return self.answer @property def was_asked(self) -> bool: """True if this answer came from an expert, not inference.""" return self.source is InferenceSource.EXPERT