Skip to content

Commit

Permalink
Merge pull request #19 from singnet/improvement/tiny-refactor
Browse files Browse the repository at this point in the history
apply some adjustments
  • Loading branch information
marcocapozzoli authored Oct 24, 2023
2 parents aec2e6a + e0446da commit c7e5e6b
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 43 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/publish-pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ jobs:
- name: Build and publish package
run: |
poetry publish --build -u __token__ -p ${{ secrets.PYPI_API_TOKEN }}
poetry build
poetry publish --username __token__ --password ${{ secrets.PYPI_API_TOKEN }}
65 changes: 56 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
71 changes: 60 additions & 11 deletions hyperon_das/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
40 changes: 20 additions & 20 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[tool.poetry]
name = "hyperon-das"
version = "0.1.5"
version = "0.1.6"
description = "Query Engine API for Distributed AtomSpace"
authors = ["marcocapozzoli <[email protected]>"]
readme = "README.md"
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]
Expand Down

0 comments on commit c7e5e6b

Please sign in to comment.