This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## What is the goal of this PR? Following typedb/typedb#6271 and the corresponding protocol change in typedb/typedb-protocol#131 we implement Explanations, Explainable concept maps, and the explain() query API, which allows users to stream Explanations on demand **note: explain query or transaction option must be set to `true`** ## What are the changes implemented in this PR? * Implement `Explanation` objects, and extend `ConceptMap` to contain `Explainables` * Add the `QueryManager.explain(Explainable)` API to retrieve all direct explanations (1-rule layer)
- Loading branch information
Alex Walker
authored
Mar 31, 2021
1 parent
f3111a3
commit eae5454
Showing
18 changed files
with
403 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
# | ||
from abc import ABC, abstractmethod | ||
from typing import Mapping, Set | ||
|
||
from grakn.api.answer.concept_map import ConceptMap | ||
from grakn.api.logic.rule import Rule | ||
|
||
|
||
class Explanation(ABC): | ||
|
||
@abstractmethod | ||
def rule(self) -> Rule: | ||
pass | ||
|
||
@abstractmethod | ||
def conclusion(self) -> ConceptMap: | ||
pass | ||
|
||
@abstractmethod | ||
def condition(self) -> ConceptMap: | ||
pass | ||
|
||
@abstractmethod | ||
def variable_mapping(self) -> Mapping[str, Set[str]]: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
# | ||
from typing import Mapping, Set | ||
|
||
import grakn_protocol.common.logic_pb2 as logic_proto | ||
|
||
from grakn.api.answer.concept_map import ConceptMap | ||
from grakn.api.logic.explanation import Explanation | ||
from grakn.api.logic.rule import Rule | ||
from grakn.concept.answer.concept_map import _ConceptMap | ||
from grakn.logic.rule import _Rule | ||
|
||
|
||
def _var_mapping_of(var_mapping: Mapping[str, logic_proto.Explanation.VarList]): | ||
mapping = {} | ||
for from_ in var_mapping: | ||
tos = var_mapping[from_] | ||
mapping[from_] = set(tos.vars) | ||
return mapping | ||
|
||
|
||
class _Explanation(Explanation): | ||
|
||
def __init__(self, rule: Rule, variable_mapping: Mapping[str, Set[str]], conclusion: ConceptMap, condition: ConceptMap): | ||
self._rule = rule | ||
self._variable_mapping = variable_mapping | ||
self._conclusion = conclusion | ||
self._condition = condition | ||
|
||
@staticmethod | ||
def of(explanation: logic_proto.Explanation): | ||
return _Explanation(_Rule.of(explanation.rule), _var_mapping_of(explanation.var_mapping), | ||
_ConceptMap.of(explanation.conclusion), _ConceptMap.of(explanation.condition)) | ||
|
||
def rule(self) -> Rule: | ||
return self._rule | ||
|
||
def variable_mapping(self) -> Mapping[str, Set[str]]: | ||
return self._variable_mapping | ||
|
||
def conclusion(self) -> ConceptMap: | ||
return self._conclusion | ||
|
||
def condition(self) -> ConceptMap: | ||
return self._condition | ||
|
||
def __str__(self): | ||
return "Explanation[rule: %s, variable_mapping: %s, then_answer: %s, when_answer: %s]" % (self._rule, self._variable_mapping, self._conclusion, self._condition) | ||
|
||
def __eq__(self, other): | ||
if other is self: | ||
return True | ||
if not other or type(self) != type(other): | ||
return False | ||
return self._rule == other._rule and self._variable_mapping == other._variable_mapping and self._conclusion == other._conclusion and self._condition == other._condition | ||
|
||
def __hash__(self): | ||
return hash((self._rule, self._variable_mapping, self._conclusion, self._condition)) |
Oops, something went wrong.