aplbrain / grandlite

A SQLite-like tool for querying graphs from the command-line using graph query languages in in-memory Python.
Apache License 2.0
5 stars 0 forks source link

Match nodes by label #8

Open spranger opened 5 months ago

spranger commented 5 months ago

Is their a way to match nodes by labels. eg. MATCH (p:Person) RETURN p.name?

j6k4m8 commented 5 months ago

@spranger: Yes!

import networkx as nx
from grandcypher import GrandCypher

host = nx.DiGraph()
host.add_edge("spranger", "grandlite")
host.add_node("spranger", __labels__={"Person"})
host.add_node("grandlite", __labels__={"Repository"})

GrandCypher(host).run("MATCH (n:Person) RETURN n")

See https://github.com/aplbrain/grand-cypher/issues/22 for more details :)

spranger commented 5 months ago

Thanks a lot. But I meant using a .graphml-file with grandlite, where the nodes have various attributes, including a type or a label

Regards Steffen

spranger commented 5 months ago

the cypher ""MATCH (n:Person) RETURN n"" query fails:

    result = GrandCypher(host).run("MATCH (n:Person) RETURN n")
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\SSR\PycharmProjects\simple_graph_sqlite_example\.venv3\Lib\site-packages\grandcypher\__init__.py", line 466, in run
    self._transformer.transform(_GrandCypherGrammar.parse(cypher))
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\SSR\PycharmProjects\simple_graph_sqlite_example\.venv3\Lib\site-packages\lark\lark.py", line 581, in parse
    return self.parser.parse(text, start=start, on_error=on_error)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\SSR\PycharmProjects\simple_graph_sqlite_example\.venv3\Lib\site-packages\lark\parser_frontends.py", line 106, in parse
    return self.parser.parse(stream, chosen_start, **kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\SSR\PycharmProjects\simple_graph_sqlite_example\.venv3\Lib\site-packages\lark\parsers\earley.py", line 297, in parse
    to_scan = self._parse(lexer, columns, to_scan, start_symbol)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\SSR\PycharmProjects\simple_graph_sqlite_example\.venv3\Lib\site-packages\lark\parsers\xearley.py", line 144, in _parse
    to_scan = scan(i, to_scan)
              ^^^^^^^^^^^^^^^^
  File "C:\Users\SSR\PycharmProjects\simple_graph_sqlite_example\.venv3\Lib\site-packages\lark\parsers\xearley.py", line 118, in scan
    raise UnexpectedCharacters(stream, i, text_line, text_column, {item.expect.name for item in to_scan},
lark.exceptions.UnexpectedCharacters: No terminal matches ':' in the current parser context, at line 1 col 9

MATCH (n:Person) RETURN n
        ^
Expected one of: 
    * LBRACE
    * RPAR

my environment: grand-cypher 0.3.0 grand-graph 0.4.2 grandiso 2.2.0 grandlite 0.1.0

python 3.12.1 windows 10

j6k4m8 commented 5 months ago

Try updating grand-cypherwe added support for __labels__ in v0.4.0!

Right now we only support entity labels using the __labels__ "magic" attribute (this is to avoid colliding with users' vertex attributes). So if the GraphML file has __labels__ specified, we'll use them — otherwise you can still use Cypher query clauses to search for other attributes. If you're populating the type attribute on your nodes, for example, you could do this:

MATCH (n) 
WHERE n.type = "Person"
RETURN n