Skip to content

Commit

Permalink
Merge pull request #1 from geronimo-iia/develop
Browse files Browse the repository at this point in the history
prepare v1.0.1
  • Loading branch information
geronimo-iia authored May 4, 2020
2 parents d5269b2 + 7c554b4 commit 1795362
Show file tree
Hide file tree
Showing 29 changed files with 1,021 additions and 164 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# Change Log

## 1.0.1

- fix documentation syntax
- add search_direct_relationships function
- rewrote search_edges and search_nodes (avoid extra filter step)

## 1.0.0 (2020-05-02)

- complete documentation
- add litlle example
- add little example
- add search_edges, search_nodes for quick and eazy usage
- complete coverage

Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ GIT_DIR = .git

poetry.lock: pyproject.toml
poetry lock
@touch $@

.cache:
@mkdir -p .cache
Expand Down
123 changes: 93 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ $ poetry add networkx-query

## Usage

Searching node:
### Searching nodes

```python
import networkx as nx
Expand All @@ -49,36 +49,95 @@ for node_id in search_nodes(g, {"==": [("product",), "chocolate"]}):
print(node_id)

>> 1
```

### Searching edges

```python
for edge_id in search_edges(g, {"eq": [("action",), "produce"]}):
print(edge_id)

>> (3, 2)
```

You could do the same with edges using ```search_edges```.
### Searching relation ship

With ```search_direct_relationships``` you can made a query which filter edges on their :
- source node attributes
- edge attributes
- target node attributes

With this graph:

```python
import networkx as nx
from networkx_query import search_direct_relationships

g = nx.DiGraph()
for i in range(30):
g.add_node(i, data=i)

for i in range(10, 30):
g.add_edge(i - 10, i, data=i)
```

We can filtering all edges with source node with data < 3:

```python
list(search_direct_relationships(graph=g, source={"lt": ["data", 3]}))

[(0, 10), (1, 11), (2, 12)]
```


We can filtering all edges with:
- source node with data < 8
- edge with data > 15

```python
list(search_direct_relationships(graph=g, source={"lt": ["data", 8]}, edge={"gt": ["data", 15]}))

>> [(6, 16), (7, 17)]
```

We can filtering all edges with:
- source node with data > 9
- edge with data > 15
- target node with data < 22

```python
search_direct_relationships(
graph=g, source={"gt": ["data", 9]}, edge={"gt": ["data", 15]}, target={'lt': ["data", 22]}
)
)

>> [(10, 20), (11, 21)]
```

## API

[search_edges](https://geronimo-iia.github.io/networkx-query/api.html#networkx_query.search_edges) and [search_nodes](https://geronimo-iia.github.io/networkx-query/api.html#networkx_query.search_nodes) are based on [prepare_query](https://geronimo-iia.github.io/networkx-query/api.html#networkx_query.prepare_query) which return an Evaluator.
Actually, we have:

- [search_edges](https://geronimo-iia.github.io/networkx-query/api.html#networkx_query.search_edges)
- [search_nodes](https://geronimo-iia.github.io/networkx-query/api.html#networkx_query.search_nodes)
- [search_direct_relationships](https://geronimo-iia.github.io/networkx-query/api.html#networkx_query.search_direct_relationships)


All this function are based on [prepare_query](https://geronimo-iia.github.io/networkx-query/api.html#networkx_query.prepare_query) which return an Evaluator.

Evaluator are function with this signature: (context) -> bool
Quickly, ```Evaluator``` are function with this signature: (context) -> bool, and ```Context``` is a dictionary like structure (with in and [] methods, and support __contains__ or (__iter__ and __getitem__))
With networkX, node and edge attributes are dictionary like, so implementation of this three methods are very simple.

Context is a dictionnary like structure (with in and [] methods, and support __contains__ or (__iter__ and __getitem__))


## Query language

Define a json query language like [json-query-language](https://github.com/clue/json-query-language/blob/master/SYNTAX.md)
We define a little json query language like [json-query-language](https://github.com/clue/json-query-language/blob/master/SYNTAX.md)
against nodes or edges attributes.

A Path is a single string or a tuple of string which represente a path in a tree (here a dictionnary).


### Expressions

All those expression are evaluate against a context wich is a dictionnary like (as can be a NodeDataView or an EdgeDataView).

Main expression syntax turn around this:

```
Expand All @@ -104,32 +163,35 @@ Test if a node/edge has an attribute product : { "definition": { "name": xxx }}
}
```

The tuple ```("product", "definition", "name")``` is a path in attribut dictionnary.
A Path is a single string or a tuple of string which represente a path in a tree (here a dictionary).

We support this operators:

| Name | Alias | Parameters | Description |
| -------- | :---: | --------------- | --------------------------------------------------------------------------------------------- |
| has | | Path | Check if path exists in context. |
| contains | | Path, str | Check if an attribut (specifed with path) exists and contains specified value. |
| eq | `==` | Path, Any | Check if an attribut (specifed with path) exists and equals specified value. |
| neq | `!=` | Path, Any | Check if an attribut (specifed with path) did not exists or not equals specified value. |
| gt | `<` | Path, Any | Check if an attribut (specifed with path) exists and greather that specified value. |
| lt | `<` | Path, Any | Check if an attribut (specifed with path) exists and lower that specified value. |
| gte | `>=` | Path, Any | Check if an attribut (specifed with path) exists and greather or equals that specified value. |
| lte | `<=` | Path, Any | Check if an attribut (specifed with path) exists and lower or equals that specified value. |
| in | `:=` | Path, List[Any] | Check if an attribut (specifed with path) exists and attribut value in specified values. |
| Name | Alias | Parameters | Description |
| -------- | :---: | --------------- | ----------------------------------------------------------------------------- |
| has | | Path | Check if path exists in context. |
| contains | | Path, str | Check if an attribut path exists and contains specified value. |
| eq | `==` | Path, Any | Check if an attribut path exists and equals specified value. |
| neq | `!=` | Path, Any | Check if an attribut path did not exists or not equals specified value. |
| gt | `>` | Path, Any | Check if an attribut path exists and greather that specified value. |
| lt | `<` | Path, Any | Check if an attribut path exists and lower that specified value. |
| gte | `>=` | Path, Any | Check if an attribut path exists and greather or equals that specified value. |
| lte | `<=` | Path, Any | Check if an attribut path exists and lower or equals that specified value. |
| in | `:=` | Path, List[Any] | Check if an attribut path exists and attribut value in specified values. |


### Boolean composition of matching expression

We support this operators:

| Name | Alias | Parameters | Description |
| ---- | :---: | ------------- | --------------------- |
| and | `&&` | list of query | Define And operator. |
| or | \|\| | list of query | Define Or operator. |
| xor | | list of query | Define xor operator. |
| nxor | | list of query | Define nxor operator. |
| not | `!` | query | Define Not operator. |
| Name | Alias | Parameters | Description |
| ---- | :---: | ------------- | -------------- |
| and | `&&` | list of query | And operator. |
| or | \|\| | list of query | Or operator. |
| xor | | list of query | xor operator. |
| nxor | | list of query | nxor operator. |
| not | `!` | query | Not operator. |


By default, a list of expressions is equivalent of an "AND" of this expressions.
Expand Down Expand Up @@ -169,7 +231,8 @@ is equivalent to:

## Wished Features

- add match node, edges, path specification
- add set expression on node/edges with constraints
- add projection expression (a return like statement)
- add join relation ship
- add path condition between node


2 changes: 1 addition & 1 deletion docs/.buildinfo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Sphinx build info version 1
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done.
config: 338e0dc8e9233c8bf950dd6c371793f5
config: 8dfca22d60d3e1d5c8a532a06cef1707
tags: 645f666f9bcd5a90fca523b33c5a78b7
4 changes: 3 additions & 1 deletion docs/_modules/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Overview: module code &mdash; networkx_query 0.2.0 documentation</title>
<title>Overview: module code &mdash; networkx_query 1.0.1 documentation</title>



Expand Down Expand Up @@ -146,7 +146,9 @@

<h1>All modules for which code is available</h1>
<ul><li><a href="networkx_query/definition.html">networkx_query.definition</a></li>
<li><a href="networkx_query/parser.html">networkx_query.parser</a></li>
<li><a href="networkx_query/query.html">networkx_query.query</a></li>
<li><a href="networkx_query/relationship.html">networkx_query.relationship</a></li>
</ul>

</div>
Expand Down
2 changes: 1 addition & 1 deletion docs/_modules/networkx_query/definition.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>networkx_query.definition &mdash; networkx_query 0.2.0 documentation</title>
<title>networkx_query.definition &mdash; networkx_query 1.0.1 documentation</title>



Expand Down
Loading

0 comments on commit 1795362

Please sign in to comment.