Queries and Solutions
This page documents the query object model — the Python classes that the
parser builds from the ?-suffixed forms of the FDL language, what each query
computes, and how to read the Solution returned by solving it. For the FDL
syntax of each query see Grammatics; this page describes the
runtime objects.
The query lifecycle
Every query is a subclass of Query
(fuzzy_dl_owl2.fuzzydl.query.query.Query). When DLParser/DLParserFast
parses an FDL file, each query form is turned into a query instance and appended
to DLParser.queries_list. The typical flow is:
from fuzzy_dl_owl2.fuzzydl.parser import DLParserFast as DLParser
from fuzzy_dl_owl2.fuzzydl.milp.solution import Solution
kb, queries = DLParser.get_kb("./example.fdl")
kb.solve_kb() # solve the TBox once, up front
for query in queries:
result: Solution = query.solve(kb)
if result.is_consistent_kb():
print(f"{query}{result}")
print(f"Time (s): {query.get_total_time()}")
Query exposes three members used by the loop above:
solve(kb) -> Solution— runs the query against a solvedKnowledgeBaseand returns aSolution.get_total_time() -> float— wall-clock time of the lastsolve, in seconds (the internal counter is nanoseconds; this accessor divides by1e9).__str__()— a human-readable description of the query (used in the log output, e.g.Is audi instance of SportCar ?).
Query classes
The table maps each FDL query keyword to the class the parser instantiates and
the value the resulting Solution carries.
FDL keyword |
Query class |
Arguments |
Result ( |
|---|---|---|---|
|
|
— |
|
|
|
individual, concept |
|
|
|
individual, concept |
|
|
|
concept |
per-individual minimum membership |
|
|
a, b, role |
|
|
|
a, b, role |
|
|
|
concept, concept |
|
|
|
concept, concept |
|
|
|
concept, concept |
|
|
|
concept, concept |
|
|
|
concept, optional individual |
|
|
|
linear expression |
|
|
|
concept, individual, feature |
|
|
|
concept, individual, feature |
|
|
|
concept, individual, feature |
|
|
|
fuzzy number |
|
Inheritance
The classes are organised by what they compute, not by max/min:
InstanceQuery— base ofMaxInstanceQuery/MinInstanceQuery(concept + individual).SubsumptionQuery— base ofMaxSubsumesQuery/MinSubsumesQuery. The fuzzy implication variant is selected with aLogicOperatorType(LUKASIEWICZfor Łukasiewicz,GOEDELfor Gödel,KLEENE_DIENES,ZADEH), which is why a single pair of classes covers the plain,g-,l-andkd-subsumption keywords.SatisfiableQuery— base ofMaxSatisfiableQuery/MinSatisfiableQuery; the individual argument is optional (overloaded constructor).RelatedQuery— base ofMaxRelatedQuery/MinRelatedQuery.DefuzzifyQuery— base ofLomDefuzzifyQuery/MomDefuzzifyQuery/SomDefuzzifyQuery(concept + individual + concrete feature name).MaxQuery/MinQuery— optimise a linearExpression(themax-var?/min-var?keywords) subject to KB consistency.KbSatisfiableQuery,AllInstancesQuery,BnpQueryderive directly fromQuery.
ClassificationQueryalso exists in the package but is not produced by any FDL keyword; it is used internally for concept classification.
The Solution object
solve returns a fuzzy_dl_owl2.fuzzydl.milp.solution.Solution. Read it before
acting on the value:
Member |
Returns |
Meaning |
|---|---|---|
|
|
Whether the KB was consistent. If |
|
|
The query answer — a boolean for |
|
|
Variable values requested via |
|
|
Records a shown variable (used by the solver). |
str(solution) is the solved value when the KB is consistent, or the literal
"Inconsistent KB" otherwise. The class constants Solution.CONSISTENT_KB
(True) and Solution.INCONSISTENT_KB (False) name the two consistency
states.
Reading a result safely
result = query.solve(kb)
if not result.is_consistent_kb():
# Inconsistent KB: every query trivially answers 1.0
print("KB inconsistent")
else:
answer = result.get_solution() # bool for sat?, float otherwise
print(answer)
Notes
Solve the knowledge base once with
kb.solve_kb()before iterating the queries; eachquery.solve(kb)then reuses the prepared KB.all-instances?over a KB with no individuals short-circuits —DLParserFast.mainemits “There are no individuals in the fuzzy KB” instead of solving.For the mathematical definition of each query (the
sup/infit computes), see the Queries section of Grammatics.