Beispiel #1
0
 def test_create_node(self):
     """Test we can build a query and create a node"""
     g = nx.MultiDiGraph()
     query = 'CREATE (n) RETURN n'
     test_parser = python_cypher.CypherToNetworkx()
     for i in test_parser.query(g, query):
         pass
     self.assertEqual(len(g.node), 1)
Beispiel #2
0
 def test_return_attribute(self):
     """Test we can return attribute from matching node"""
     g = nx.MultiDiGraph()
     create_query = 'CREATE (n:SOMECLASS {foo: "bar"}) RETURN n'
     match_query = 'MATCH (n) RETURN n.foo'
     test_parser = python_cypher.CypherToNetworkx()
     list(test_parser.query(g, create_query))
     out = list(test_parser.query(g, match_query))
Beispiel #3
0
 def test_create_node_and_edge(self):
     """Test we can build a query and create two nodes and an edge"""
     g = nx.MultiDiGraph()
     query = 'CREATE (n)-->(m) RETURN n, m'
     test_parser = python_cypher.CypherToNetworkx()
     for i in test_parser.query(g, query):
         pass
     self.assertEqual(len(g.node), 2)
     self.assertEqual(len(g.edge), 2)
Beispiel #4
0
 def test_upper(self):
     """Test we can parse a CREATE... RETURN query."""
     g = nx.MultiDiGraph()
     query = 'CREATE (n:SOMECLASS) RETURN n'
     test_parser = python_cypher.CypherToNetworkx()
     test_parser.query(g, query)
Beispiel #5
0
from collections import defaultdict
import logging

import networkx as nx
from networkx.classes.function import add_path
from python_cypher import python_cypher
from xpark import settings
from xpark.plan.base import BaseOptimizedPlan
from xpark.utils.iter import take_pairs

_LOG = logging.getLogger(__name__)
PARSER = python_cypher.CypherToNetworkx()


class OptimizationRule(object):
    rule_str = None

    def __init__(self):
        self.rule = PARSER.parse(' '.join(self.rule_str.split()))

    def __repr__(self):
        return '<rule: %s\n%s>' % (self.__class__.__name__,
                                   self.rule_str.strip())

    def transform_path(self, path, g):
        return path, {}

    def replace_path(self, g, path, new_path):
        g = g.copy()
        paths = list(take_pairs(path))
        g.remove_edges_from(paths)