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
Implement explanations #213
Merged
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0e54620
Implement explanations
b000fc4
Fix bugs in explanation implementation
9d5aeb0
Add simple explanations test
2c31a87
Include type in Thing.__str__
2756d2f
Update explanations protocol
5414cc2
Update Grakn Cluster artifact
3dd3d88
Document what test_debug is for
aa651db
Fix build
de260b4
Update grakn-protocol and common
1f247f1
Rename ResponseIterator to ResponsePartIterator
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 then_answer(self) -> ConceptMap: | ||
pass | ||
|
||
@abstractmethod | ||
def when_answer(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
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.VarsList]): | ||
mapping = {} | ||
for from_ in var_mapping: | ||
tos = var_mapping[from_] | ||
mapping[from_] = set(tos.vars_list) | ||
return mapping | ||
|
||
|
||
class _Explanation(Explanation): | ||
|
||
def __init__(self, rule: Rule, variable_mapping: Mapping[str, Set[str]], then_answer: ConceptMap, when_answer: ConceptMap): | ||
self._rule = rule | ||
self._variable_mapping = variable_mapping | ||
self._then_answer = then_answer | ||
self._when_answer = when_answer | ||
|
||
@staticmethod | ||
def of(explanation: logic_proto.Explanation): | ||
return _Explanation(_Rule.of(explanation.rule), _var_mapping_of(explanation.var_mapping), | ||
_ConceptMap.of(explanation.then_answer), _ConceptMap.of(explanation.when_answer)) | ||
|
||
def rule(self) -> Rule: | ||
return self._rule | ||
|
||
def variable_mapping(self) -> Mapping[str, Set[str]]: | ||
return self._variable_mapping | ||
|
||
def then_answer(self) -> ConceptMap: | ||
return self._then_answer | ||
|
||
def when_answer(self) -> ConceptMap: | ||
return self._when_answer | ||
|
||
def __str__(self): | ||
return "Explanation[rule: %s, variable_mapping: %s, then_answer: %s, when_answer: %s]" % (self._rule, self._variable_mapping, self._then_answer, self._when_answer) | ||
|
||
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._then_answer == other._then_answer and self._when_answer == other._when_answer | ||
|
||
def __hash__(self): | ||
return hash((self._rule, self._variable_mapping, self._then_answer, self._when_answer)) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this typing cracks me up