diff --git a/.github/workflows/publish-pypi.yml b/.github/workflows/publish-pypi.yml index 7c7e9055..0bc9051b 100644 --- a/.github/workflows/publish-pypi.yml +++ b/.github/workflows/publish-pypi.yml @@ -26,4 +26,5 @@ jobs: - name: Build and publish package run: | - poetry publish --build -u __token__ -p ${{ secrets.PYPI_API_TOKEN }} \ No newline at end of file + poetry build + poetry publish --username __token__ --password ${{ secrets.PYPI_API_TOKEN }} \ No newline at end of file diff --git a/README.md b/README.md index d45c3fb1..e5e11290 100644 --- a/README.md +++ b/README.md @@ -102,25 +102,72 @@ This way you don't need anything just instantiate the class as shown below: ```python from hyperon_das import DistributedAtomSpace from hyperon_das.pattern_matcher import And, Variable, Link + from hyperon_das.utils import QueryOutputFormat api = DistributedAtomSpace('ram_only') - V1 = Variable("V1") - V2 = Variable("V2") - V3 = Variable("V3") + api.add_link({ + 'type': 'Evaluation', + 'targets': [ + {'type': 'Predicate', 'name': 'Predicate:has_name'}, + { + 'type': 'Evaluation', + 'targets': [ + {'type': 'Predicate', 'name': 'Predicate:has_name'}, + { + 'type': 'Set', + 'targets': [ + {'type': 'Reactome', 'name': 'Reactome:R-HSA-164843'}, + {'type': 'Concept', 'name': 'Concept:2-LTR circle formation'}, + ] + }, + ], + }, + ], + }) - expression = And([ - Link("Inheritance", ordered=True, targets=[V1, V2]), - Link("Inheritance", ordered=True, targets=[V2, V3]) - ]) + expression = Link("Evaluation", ordered=True, targets=[Variable("V1"), Variable("V2")]) - resp = api.query(expression) + resp = api.query(expression, {'return_type': QueryOutputFormat.JSON, 'toplevel_only': True}) print(resp) ``` ```bash - {{'V1': 'a1fb3a4de5c459bfa4bd87dc423019c3', 'V2': '98870929d76a80c618e70a0393055b31', 'V3': '81ec21b0f1b03e18c55e056a56179fef'}, {'V1': 'bd497eb24420dd50fed5f3d2e6cdd7c1', 'V2': '98870929d76a80c618e70a0393055b31', 'V3': '81ec21b0f1b03e18c55e056a56179fef'}, {'V1': 'e2d9b15ab3461228d75502e754137caa', 'V2': 'c90242e2dbece101813762cc2a83d726', 'V3': '81ec21b0f1b03e18c55e056a56179fef'}, {'V1': 'd1ec11ec366a1deb24a079dc39863c68', 'V2': 'c90242e2dbece101813762cc2a83d726', 'V3': '81ec21b0f1b03e18c55e056a56179fef'}, {'V1': 'fa77994f6835fad256902605a506c59c', 'V2': '98870929d76a80c618e70a0393055b31', 'V3': '81ec21b0f1b03e18c55e056a56179fef'}, {'V1': 'c77b519f8ab36dfea8e2a532a7603d9a', 'V2': 'd1ec11ec366a1deb24a079dc39863c68', 'V3': 'c90242e2dbece101813762cc2a83d726'}, {'V1': '305e7d502a0ce80b94374ff0d79a6464', 'V2': '98870929d76a80c618e70a0393055b31', 'V3': '81ec21b0f1b03e18c55e056a56179fef'}} + [ + { + "V1": { + "type": "Predicate", + "name": "Predicate:has_name", + "is_link": false, + "is_node": true + }, + "V2": { + "type": "Evaluation", + "targets": [ + { + "type": "Predicate", + "name": "Predicate:has_name" + }, + { + "type": "Set", + "targets": [ + { + "type": "Reactome", + "name": "Reactome:R-HSA-164843" + }, + { + "type": "Concept", + "name": "Concept:2-LTR circle formation" + } + ] + } + ], + "is_link": true, + "is_node": false + } + } + ] ``` 2. Add Node and And Link (It's possible only using [Ram Only](#in-memory)) diff --git a/hyperon_das/api.py b/hyperon_das/api.py index 87bb92bf..6dc4cfb6 100644 --- a/hyperon_das/api.py +++ b/hyperon_das/api.py @@ -77,17 +77,21 @@ def _to_json(self, db_answer: Union[List[str], List[Dict]]) -> List[Dict]: return json.dumps(answer, sort_keys=False, indent=4) def _turn_into_deep_representation(self, assignments) -> list: - objs = [] + results = [] for assignment in assignments: - obj = {} - for var, handle in assignment.mapping.items(): - obj[var] = self.db.get_atom_as_deep_representation(handle) - if obj[var].get('targets'): - obj[var].update({'is_link': True, 'is_node': False}) - else: - obj[var].update({'is_link': False, 'is_node': True}) - objs.append(obj) - return objs + result = {} + for variable, handle in assignment.mapping.items(): + deep_representation = self.db.get_atom_as_deep_representation( + handle + ) + is_link = 'targets' in deep_representation + result[variable] = { + **deep_representation, + 'is_link': is_link, + 'is_node': not is_link, + } + results.append(result) + return results def clear_database(self) -> None: """Clear all data""" @@ -593,7 +597,7 @@ def query( Link("Inheritance", ordered=True, targets=[V2, V3]) ]) - >>> result = obj.query(query=logical_expression) + >>> result = obj.query(query=logical_expression, {'return_type': QueryOutputFormat.HANDLE}) >>> print(result) { @@ -670,3 +674,48 @@ def add_link(self, link_params: Dict[str, Any]) -> Dict[str, Any]: message='This method is permited only in memory database', details='Instantiate the class sent the database type as `ram_only`', ) + + +if __name__ == '__main__': + from hyperon_das.pattern_matcher import And, Link, Variable + from hyperon_das.utils import QueryOutputFormat + + api = DistributedAtomSpace('ram_only') + api.add_link( + { + 'type': 'Evaluation', + 'targets': [ + {'type': 'Predicate', 'name': 'Predicate:has_name'}, + { + 'type': 'Evaluation', + 'targets': [ + {'type': 'Predicate', 'name': 'Predicate:has_name'}, + { + 'type': 'Set', + 'targets': [ + { + 'type': 'Reactome', + 'name': 'Reactome:R-HSA-164843', + }, + { + 'type': 'Concept', + 'name': 'Concept:2-LTR circle formation', + }, + ], + }, + ], + }, + ], + } + ) + + expression = Link( + "Evaluation", ordered=True, targets=[Variable("V1"), Variable("V2")] + ) + + resp = api.query( + expression, + {'return_type': QueryOutputFormat.JSON, 'toplevel_only': True}, + ) + + print(resp) diff --git a/poetry.lock b/poetry.lock index 9bf47848..e8315642 100644 --- a/poetry.lock +++ b/poetry.lock @@ -13,29 +13,29 @@ files = [ [[package]] name = "black" -version = "23.10.0" +version = "23.10.1" description = "The uncompromising code formatter." optional = false python-versions = ">=3.8" files = [ - {file = "black-23.10.0-cp310-cp310-macosx_10_16_arm64.whl", hash = "sha256:f8dc7d50d94063cdfd13c82368afd8588bac4ce360e4224ac399e769d6704e98"}, - {file = "black-23.10.0-cp310-cp310-macosx_10_16_x86_64.whl", hash = "sha256:f20ff03f3fdd2fd4460b4f631663813e57dc277e37fb216463f3b907aa5a9bdd"}, - {file = "black-23.10.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:d3d9129ce05b0829730323bdcb00f928a448a124af5acf90aa94d9aba6969604"}, - {file = "black-23.10.0-cp310-cp310-win_amd64.whl", hash = "sha256:960c21555be135c4b37b7018d63d6248bdae8514e5c55b71e994ad37407f45b8"}, - {file = "black-23.10.0-cp311-cp311-macosx_10_16_arm64.whl", hash = "sha256:30b78ac9b54cf87bcb9910ee3d499d2bc893afd52495066c49d9ee6b21eee06e"}, - {file = "black-23.10.0-cp311-cp311-macosx_10_16_x86_64.whl", hash = "sha256:0e232f24a337fed7a82c1185ae46c56c4a6167fb0fe37411b43e876892c76699"}, - {file = "black-23.10.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:31946ec6f9c54ed7ba431c38bc81d758970dd734b96b8e8c2b17a367d7908171"}, - {file = "black-23.10.0-cp311-cp311-win_amd64.whl", hash = "sha256:c870bee76ad5f7a5ea7bd01dc646028d05568d33b0b09b7ecfc8ec0da3f3f39c"}, - {file = "black-23.10.0-cp38-cp38-macosx_10_16_arm64.whl", hash = "sha256:6901631b937acbee93c75537e74f69463adaf34379a04eef32425b88aca88a23"}, - {file = "black-23.10.0-cp38-cp38-macosx_10_16_x86_64.whl", hash = "sha256:481167c60cd3e6b1cb8ef2aac0f76165843a374346aeeaa9d86765fe0dd0318b"}, - {file = "black-23.10.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:f74892b4b836e5162aa0452393112a574dac85e13902c57dfbaaf388e4eda37c"}, - {file = "black-23.10.0-cp38-cp38-win_amd64.whl", hash = "sha256:47c4510f70ec2e8f9135ba490811c071419c115e46f143e4dce2ac45afdcf4c9"}, - {file = "black-23.10.0-cp39-cp39-macosx_10_16_arm64.whl", hash = "sha256:76baba9281e5e5b230c9b7f83a96daf67a95e919c2dfc240d9e6295eab7b9204"}, - {file = "black-23.10.0-cp39-cp39-macosx_10_16_x86_64.whl", hash = "sha256:a3c2ddb35f71976a4cfeca558848c2f2f89abc86b06e8dd89b5a65c1e6c0f22a"}, - {file = "black-23.10.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:db451a3363b1e765c172c3fd86213a4ce63fb8524c938ebd82919bf2a6e28c6a"}, - {file = "black-23.10.0-cp39-cp39-win_amd64.whl", hash = "sha256:7fb5fc36bb65160df21498d5a3dd330af8b6401be3f25af60c6ebfe23753f747"}, - {file = "black-23.10.0-py3-none-any.whl", hash = "sha256:e223b731a0e025f8ef427dd79d8cd69c167da807f5710add30cdf131f13dd62e"}, - {file = "black-23.10.0.tar.gz", hash = "sha256:31b9f87b277a68d0e99d2905edae08807c007973eaa609da5f0c62def6b7c0bd"}, + {file = "black-23.10.1-cp310-cp310-macosx_10_16_arm64.whl", hash = "sha256:ec3f8e6234c4e46ff9e16d9ae96f4ef69fa328bb4ad08198c8cee45bb1f08c69"}, + {file = "black-23.10.1-cp310-cp310-macosx_10_16_x86_64.whl", hash = "sha256:1b917a2aa020ca600483a7b340c165970b26e9029067f019e3755b56e8dd5916"}, + {file = "black-23.10.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:9c74de4c77b849e6359c6f01987e94873c707098322b91490d24296f66d067dc"}, + {file = "black-23.10.1-cp310-cp310-win_amd64.whl", hash = "sha256:7b4d10b0f016616a0d93d24a448100adf1699712fb7a4efd0e2c32bbb219b173"}, + {file = "black-23.10.1-cp311-cp311-macosx_10_16_arm64.whl", hash = "sha256:b15b75fc53a2fbcac8a87d3e20f69874d161beef13954747e053bca7a1ce53a0"}, + {file = "black-23.10.1-cp311-cp311-macosx_10_16_x86_64.whl", hash = "sha256:e293e4c2f4a992b980032bbd62df07c1bcff82d6964d6c9496f2cd726e246ace"}, + {file = "black-23.10.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:7d56124b7a61d092cb52cce34182a5280e160e6aff3137172a68c2c2c4b76bcb"}, + {file = "black-23.10.1-cp311-cp311-win_amd64.whl", hash = "sha256:3f157a8945a7b2d424da3335f7ace89c14a3b0625e6593d21139c2d8214d55ce"}, + {file = "black-23.10.1-cp38-cp38-macosx_10_16_arm64.whl", hash = "sha256:cfcce6f0a384d0da692119f2d72d79ed07c7159879d0bb1bb32d2e443382bf3a"}, + {file = "black-23.10.1-cp38-cp38-macosx_10_16_x86_64.whl", hash = "sha256:33d40f5b06be80c1bbce17b173cda17994fbad096ce60eb22054da021bf933d1"}, + {file = "black-23.10.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:840015166dbdfbc47992871325799fd2dc0dcf9395e401ada6d88fe11498abad"}, + {file = "black-23.10.1-cp38-cp38-win_amd64.whl", hash = "sha256:037e9b4664cafda5f025a1728c50a9e9aedb99a759c89f760bd83730e76ba884"}, + {file = "black-23.10.1-cp39-cp39-macosx_10_16_arm64.whl", hash = "sha256:7cb5936e686e782fddb1c73f8aa6f459e1ad38a6a7b0e54b403f1f05a1507ee9"}, + {file = "black-23.10.1-cp39-cp39-macosx_10_16_x86_64.whl", hash = "sha256:7670242e90dc129c539e9ca17665e39a146a761e681805c54fbd86015c7c84f7"}, + {file = "black-23.10.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:5ed45ac9a613fb52dad3b61c8dea2ec9510bf3108d4db88422bacc7d1ba1243d"}, + {file = "black-23.10.1-cp39-cp39-win_amd64.whl", hash = "sha256:6d23d7822140e3fef190734216cefb262521789367fbdc0b3f22af6744058982"}, + {file = "black-23.10.1-py3-none-any.whl", hash = "sha256:d431e6739f727bb2e0495df64a6c7a5310758e87505f5f8cde9ff6c0f2d7e4fe"}, + {file = "black-23.10.1.tar.gz", hash = "sha256:1f8ce316753428ff68749c65a5f7844631aa18c8679dfd3ca9dc1a289979c258"}, ] [package.dependencies] @@ -517,4 +517,4 @@ files = [ [metadata] lock-version = "2.0" python-versions = "^3.8.5" -content-hash = "86aeded12d4265c09239687187839b0959ddee0adf44dbb045788fc72b3b31f6" +content-hash = "cda0c3cce7b1160c25ec4351dc8ec15c57f7391a742bd3856c3f2856690490fd" diff --git a/pyproject.toml b/pyproject.toml index 9b61b70d..ebb3b247 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "hyperon-das" -version = "0.1.5" +version = "0.1.6" description = "Query Engine API for Distributed AtomSpace" authors = ["marcocapozzoli "] readme = "README.md" @@ -8,7 +8,7 @@ packages = [{include = "hyperon_das"}] [tool.poetry.dependencies] python = "^3.8.5" -hyperon-das-atomdb = "^0.1.6" +hyperon-das-atomdb = "^0.1.7" [tool.poetry.group.dev.dependencies]