Giter Site home page Giter Site logo

nom-gdl's Introduction

Graph Definition Language (GDL)

Inspired by the Neo4j Cypher query language, GDL allows the simple definition of property graphs. GDL contains a parser and simple structs that represent the property graph and its elements. The Rust implementation is inspired by my Java implementation.

Property graph data model

A property graph consists of nodes and relationships. Nodes have zero or more labels, relationships have zero or one relationship type. Both, nodes and relationships have properties, organized as key-value-pairs. Relationships are directed, starting at a source node and pointing at a target node.

Quickstart example

use gdl::{CypherValue, Graph};
use std::rc::Rc;

let gdl_string = "(alice:Person { name: 'Alice', age: 23 }),
                  (bob:Person { name: 'Bob', age: 42 }),
                  (alice)-[r:KNOWS { since: 1984 }]->(bob)";

let graph = gdl_string.parse::<gdl::Graph>().unwrap();

assert_eq!(graph.node_count(), 2);
assert_eq!(graph.relationship_count(), 1);

let alice = graph.get_node("alice").unwrap();
assert_eq!(alice.property_value("age"), Some(&CypherValue::from(23)));
assert_eq!(alice.property_value("name"), Some(&CypherValue::from("Alice")));

let relationship = graph.get_relationship("r").unwrap();
assert_eq!(relationship.rel_type(), Some("KNOWS"));

More GDL language examples

Define a node:

let g = "()".parse::<gdl::Graph>().unwrap();

assert_eq!(g.node_count(), 1);

Define a node and assign it to variable alice:

let g = "(alice)".parse::<gdl::Graph>().unwrap();

assert!(g.get_node("alice").is_some());

Define a node with label User and multiple properties:

let g = "(alice:User { name: 'Alice', age : 23 })".parse::<gdl::Graph>().unwrap();

assert_eq!(g.get_node("alice").unwrap().labels().collect::<Vec<_>>(), vec!["User"]);
assert!(g.get_node("alice").unwrap().property_value("name").is_some());
assert!(g.get_node("alice").unwrap().property_value("age").is_some());

Define an outgoing relationship:

let g = "(alice)-->()".parse::<gdl::Graph>().unwrap();

assert_eq!(g.relationship_count(), 1);

Define an incoming relationship:

let g = "(alice)<--()".parse::<gdl::Graph>().unwrap();

assert_eq!(g.relationship_count(), 1);

Define a relationship with type KNOWS, assign it to variable r1 and add a property:

use std::rc::Rc;

let g = "(alice)-[r1:KNOWS { since : 2014 }]->(bob)".parse::<gdl::Graph>().unwrap();

assert!(g.get_relationship("r1").is_some());
assert_eq!(g.get_relationship("r1").unwrap().rel_type(), Some("KNOWS"));

Define multiple outgoing relationships from the same source node (i.e. alice):

let g = "
    (alice)-[r1:KNOWS { since : 2014 }]->(bob)
    (alice)-[r2:KNOWS { since : 2013 }]->(eve)
".parse::<gdl::Graph>().unwrap();

assert_eq!(g.node_count(), 3);
assert_eq!(g.relationship_count(), 2);

Define paths (four nodes and three relationships are created):

let g = "()-->()<--()-->()".parse::<gdl::Graph>().unwrap();

assert_eq!(g.node_count(), 4);
assert_eq!(g.relationship_count(), 3);

Paths can be comma separated to express arbitrary complex patterns:

let g = "
    ()-->()<--()-->(),
    ()<--()-->()-->(),
    ()-->()<--()-->()
".parse::<gdl::Graph>().unwrap();

assert_eq!(g.node_count(), 12);
assert_eq!(g.relationship_count(), 9);

License

Apache 2.0 or MIT

nom-gdl's People

Contributors

knutwalker avatar s1ck avatar x87-va avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.