Title: | A Simple Graph Database |
---|---|
Description: | This is a graph database in 'SQLite'. It is inspired by Denis Papathanasiou's Python simple-graph project on 'GitHub'. |
Authors: | Michael Silva [aut, cre] |
Maintainer: | Michael Silva <[email protected]> |
License: | MIT + file LICENSE |
Version: | 2021.03.10 |
Built: | 2024-11-08 03:11:25 UTC |
Source: | https://github.com/mikeasilva/simplegraphdb |
Generates the SQL to add a node to the database
add_node(data, identifier = NA)
add_node(data, identifier = NA)
data |
Data to be added to the node in a list format |
identifier |
The identifier for the node |
A SQL statement to add a node to a database
## Not run: library(simplegraphdb) apple <- "apple_test.sqlite" initialize(apple) # Add nodes with data atomic(apple, add_node(list( "name" = "Apple Computer Company", "type" = c("company", "start-up"), "founded" = "April 1, 1976"), 1)) atomic(apple, add_node(list( "name" = "Steve Wozniak", "type" = c("person", "engineer", "founder")), 2)) atomic(apple, add_node(list( "name" = "Steve Jobs", "type" = c("person", "designer", "founder")), 3)) atomic(apple, add_node(list( "name" = "Ronald Wayne", "type" = c("person", "administrator", "founder")), 4)) atomic(apple, add_node(list( "name" = "Mike Markkula", "type" = c("person", "investor")), 5)) ## End(Not run)
## Not run: library(simplegraphdb) apple <- "apple_test.sqlite" initialize(apple) # Add nodes with data atomic(apple, add_node(list( "name" = "Apple Computer Company", "type" = c("company", "start-up"), "founded" = "April 1, 1976"), 1)) atomic(apple, add_node(list( "name" = "Steve Wozniak", "type" = c("person", "engineer", "founder")), 2)) atomic(apple, add_node(list( "name" = "Steve Jobs", "type" = c("person", "designer", "founder")), 3)) atomic(apple, add_node(list( "name" = "Ronald Wayne", "type" = c("person", "administrator", "founder")), 4)) atomic(apple, add_node(list( "name" = "Mike Markkula", "type" = c("person", "investor")), 5)) ## End(Not run)
An atomic transaction wrapper function
atomic(db_file, sql_statement)
atomic(db_file, sql_statement)
db_file |
The name of the SQLite database |
sql_statement |
The SQL statement to execute |
Either the query results or NA for executed SQL statements
Add an edge to the database
connect_nodes(source_id, target_id, properties = list())
connect_nodes(source_id, target_id, properties = list())
source_id |
Source node's id |
target_id |
Target node's id |
properties |
Edge properties (optional) |
A SQL statement to insert an edge into the database
## Not run: library(simplegraphdb) apple <- "apple_test.sqlite" initialize(apple) atomic(apple, add_node(list( "name" = "Apple Computer Company", "type" = c("company", "start-up"), "founded" = "April 1, 1976"), 1)) atomic(apple, add_node(list( "name" = "Steve Wozniak", "type" = c("person", "engineer", "founder")), 2)) atomic(apple, add_node(list( "name" = "Steve Jobs", "type" = c("person", "designer", "founder")), 3)) atomic(apple, add_node(list( "name" = "Ronald Wayne", "type" = c("person", "administrator", "founder")), 4)) atomic(apple, add_node(list( "name" = "Mike Markkula", "type" = c("person", "investor")), 5)) # Add in some edges to the graph atomic(apple, connect_nodes(2, 1, list("action" = "founded"))) atomic(apple, connect_nodes(3, 1, list("action" = "founded"))) atomic(apple, connect_nodes(4, 1, list("action" = "founded"))) atomic(apple, connect_nodes(5, 1, list( "action" = "invested", "equity" = 80000, "debt" = 170000))) atomic(apple, connect_nodes(1, 4, list( "action" = "divested", "amount" = 800, "date" = "April 12, 1976"))) atomic(apple, connect_nodes(2, 3)) ## End(Not run)
## Not run: library(simplegraphdb) apple <- "apple_test.sqlite" initialize(apple) atomic(apple, add_node(list( "name" = "Apple Computer Company", "type" = c("company", "start-up"), "founded" = "April 1, 1976"), 1)) atomic(apple, add_node(list( "name" = "Steve Wozniak", "type" = c("person", "engineer", "founder")), 2)) atomic(apple, add_node(list( "name" = "Steve Jobs", "type" = c("person", "designer", "founder")), 3)) atomic(apple, add_node(list( "name" = "Ronald Wayne", "type" = c("person", "administrator", "founder")), 4)) atomic(apple, add_node(list( "name" = "Mike Markkula", "type" = c("person", "investor")), 5)) # Add in some edges to the graph atomic(apple, connect_nodes(2, 1, list("action" = "founded"))) atomic(apple, connect_nodes(3, 1, list("action" = "founded"))) atomic(apple, connect_nodes(4, 1, list("action" = "founded"))) atomic(apple, connect_nodes(5, 1, list( "action" = "invested", "equity" = 80000, "debt" = 170000))) atomic(apple, connect_nodes(1, 4, list( "action" = "divested", "amount" = 800, "date" = "April 12, 1976"))) atomic(apple, connect_nodes(2, 3)) ## End(Not run)
Generates the SQL to find the inbound neighbors for a node in the database
find_inbound_neighbors(identifier)
find_inbound_neighbors(identifier)
identifier |
The identifier for the node |
A SQL statement to find the inbound neighbors
Generates the SQL to find the neighbors for a node in the database
find_neighbors(identifier)
find_neighbors(identifier)
identifier |
The identifier for the node |
A SQL statement to find the neighbors
Generates the SQL to find a node from the database
find_node(identifier)
find_node(identifier)
identifier |
The identifier for the node |
A SQL statement to find a node
Generate SQL to find nodes matching a criteria
find_nodes(data, where_fn = "search_where", search_fn = "search_equals")
find_nodes(data, where_fn = "search_where", search_fn = "search_equals")
data |
A list of data that are the search criteria |
where_fn |
The function to use in the SQL WHERE clause. Valid values are: search_where (default) or search_like |
search_fn |
The function to use in the search. Valid values are: search_equals (default), search_starts_with, or search_contains |
A SQL statement to find nodes matching a criteria
Generates the SQL to find the outbound neighbors for a node in the database
find_outbound_neighbors(identifier)
find_outbound_neighbors(identifier)
identifier |
The identifier for the node |
A SQL statement to find outbound neighbors
Generates the SQL to find the connections for a node in the database
get_connections(source_id, target_id)
get_connections(source_id, target_id)
source_id |
The identifier for the source node |
target_id |
The identifier for the target node |
A SQL statement to find the edge connecting two nodes
Initialize a new graph database
initialize(db_file, schema_file = "./tests/schema.sql")
initialize(db_file, schema_file = "./tests/schema.sql")
db_file |
The name of the SQLite database |
schema_file |
The SQL schema file (optional) |
No return value. It creates the database.
## Not run: library(simplegraphdb) initialize("network.sqlite") ## End(Not run)
## Not run: library(simplegraphdb) initialize("network.sqlite") ## End(Not run)
Generates the SQL to remove a node from the database
remove_node(identifier)
remove_node(identifier)
identifier |
The identifier for the node |
A SQL statement to delete a node
## Not run: library(simplegraphdb) apple <- "apple_test.sqlite" initialize(apple) atomic(apple, add_node(list( "name" = "Apple Computer Company", "type" = c("company", "start-up"), "founded" = "April 1, 1976"), 1)) atomic(apple, add_node(list( "name" = "Steve Wozniak", "type" = c("person", "engineer", "founder")), 2)) atomic(apple, add_node(list( "name" = "Steve Jobs", "type" = c("person", "designer", "founder")), 3)) atomic(apple, add_node(list( "name" = "Ronald Wayne", "type" = c("person", "administrator", "founder")), 4)) atomic(apple, add_node(list( "name" = "Mike Markkula", "type" = c("person", "investor")), 5)) atomic(apple, connect_nodes(2, 1, list("action" = "founded"))) atomic(apple, connect_nodes(3, 1, list("action" = "founded"))) atomic(apple, connect_nodes(4, 1, list("action" = "founded"))) atomic(apple, connect_nodes(5, 1, list( "action" = "invested", "equity" = 80000, "debt" = 170000))) atomic(apple, connect_nodes(1, 4, list( "action" = "divested", "amount" = 800, "date" = "April 12, 1976"))) atomic(apple, connect_nodes(2, 3)) atomic(apple, upsert_node(2, list("nickname" = "Woz"), apple)) # Remove node 1 from the data atomic(apple, remove_node(1)) ## End(Not run)
## Not run: library(simplegraphdb) apple <- "apple_test.sqlite" initialize(apple) atomic(apple, add_node(list( "name" = "Apple Computer Company", "type" = c("company", "start-up"), "founded" = "April 1, 1976"), 1)) atomic(apple, add_node(list( "name" = "Steve Wozniak", "type" = c("person", "engineer", "founder")), 2)) atomic(apple, add_node(list( "name" = "Steve Jobs", "type" = c("person", "designer", "founder")), 3)) atomic(apple, add_node(list( "name" = "Ronald Wayne", "type" = c("person", "administrator", "founder")), 4)) atomic(apple, add_node(list( "name" = "Mike Markkula", "type" = c("person", "investor")), 5)) atomic(apple, connect_nodes(2, 1, list("action" = "founded"))) atomic(apple, connect_nodes(3, 1, list("action" = "founded"))) atomic(apple, connect_nodes(4, 1, list("action" = "founded"))) atomic(apple, connect_nodes(5, 1, list( "action" = "invested", "equity" = 80000, "debt" = 170000))) atomic(apple, connect_nodes(1, 4, list( "action" = "divested", "amount" = 800, "date" = "April 12, 1976"))) atomic(apple, connect_nodes(2, 3)) atomic(apple, upsert_node(2, list("nickname" = "Woz"), apple)) # Remove node 1 from the data atomic(apple, remove_node(1)) ## End(Not run)
Sets the id attribute in JSON data
set_id(identifier = NA, data)
set_id(identifier = NA, data)
identifier |
The id |
data |
The JSON data |
JSON ecoded data
Finds the path as you traverse the graph
traverse(db_file, src, tgt = NA, neighbors_fn = "find_neighbors")
traverse(db_file, src, tgt = NA, neighbors_fn = "find_neighbors")
db_file |
The name of the SQLite database |
src |
The id of the source node |
tgt |
The id of the target node (optional) |
neighbors_fn |
The neighbor function to employ. Valid options are find_neighbors, find_inbound_neighbors or find_outbound_neighbors (optional) |
A JSON object containing the id of the nodes in the path
## Not run: library(simplegraphdb) apple <- "apple_test.sqlite" initialize(apple) atomic(apple, add_node(list( "name" = "Apple Computer Company", "type" = c("company", "start-up"), "founded" = "April 1, 1976"), 1)) atomic(apple, add_node(list( "name" = "Steve Wozniak", "type" = c("person", "engineer", "founder")), 2)) atomic(apple, add_node(list( "name" = "Steve Jobs", "type" = c("person", "designer", "founder")), 3)) atomic(apple, add_node(list( "name" = "Ronald Wayne", "type" = c("person", "administrator", "founder")), 4)) atomic(apple, add_node(list( "name" = "Mike Markkula", "type" = c("person", "investor")), 5)) atomic(apple, connect_nodes(2, 1, list("action" = "founded"))) atomic(apple, connect_nodes(3, 1, list("action" = "founded"))) atomic(apple, connect_nodes(4, 1, list("action" = "founded"))) atomic(apple, connect_nodes(5, 1, list( "action" = "invested", "equity" = 80000, "debt" = 170000))) atomic(apple, connect_nodes(1, 4, list( "action" = "divested", "amount" = 800, "date" = "April 12, 1976"))) atomic(apple, connect_nodes(2, 3)) atomic(apple, upsert_node(2, list("nickname" = "Woz"), apple)) # Traverse the data traverse(apple, 4, 5) # Get the inbound neighbors traverse(apple, 5, "find_inbound_neighbors") # Get the outbound neighbors traverse(apple, 5, "find_outbound_neighbors") ## End(Not run)
## Not run: library(simplegraphdb) apple <- "apple_test.sqlite" initialize(apple) atomic(apple, add_node(list( "name" = "Apple Computer Company", "type" = c("company", "start-up"), "founded" = "April 1, 1976"), 1)) atomic(apple, add_node(list( "name" = "Steve Wozniak", "type" = c("person", "engineer", "founder")), 2)) atomic(apple, add_node(list( "name" = "Steve Jobs", "type" = c("person", "designer", "founder")), 3)) atomic(apple, add_node(list( "name" = "Ronald Wayne", "type" = c("person", "administrator", "founder")), 4)) atomic(apple, add_node(list( "name" = "Mike Markkula", "type" = c("person", "investor")), 5)) atomic(apple, connect_nodes(2, 1, list("action" = "founded"))) atomic(apple, connect_nodes(3, 1, list("action" = "founded"))) atomic(apple, connect_nodes(4, 1, list("action" = "founded"))) atomic(apple, connect_nodes(5, 1, list( "action" = "invested", "equity" = 80000, "debt" = 170000))) atomic(apple, connect_nodes(1, 4, list( "action" = "divested", "amount" = 800, "date" = "April 12, 1976"))) atomic(apple, connect_nodes(2, 3)) atomic(apple, upsert_node(2, list("nickname" = "Woz"), apple)) # Traverse the data traverse(apple, 4, 5) # Get the inbound neighbors traverse(apple, 5, "find_inbound_neighbors") # Get the outbound neighbors traverse(apple, 5, "find_outbound_neighbors") ## End(Not run)
Generates the SQL to upsert a node in the database
upsert_node(identifier, data, db_file)
upsert_node(identifier, data, db_file)
identifier |
The identifier for the node |
data |
Data to be added to the node in a list format |
db_file |
The name of the 'SQLite' database |
A SQL statement to upsert a node
## Not run: library(simplegraphdb) apple <- "apple_test.sqlite" initialize(apple) atomic(apple, add_node(list( "name" = "Apple Computer Company", "type" = c("company", "start-up"), "founded" = "April 1, 1976"), 1)) atomic(apple, add_node(list( "name" = "Steve Wozniak", "type" = c("person", "engineer", "founder")), 2)) atomic(apple, add_node(list( "name" = "Steve Jobs", "type" = c("person", "designer", "founder")), 3)) atomic(apple, add_node(list( "name" = "Ronald Wayne", "type" = c("person", "administrator", "founder")), 4)) atomic(apple, add_node(list( "name" = "Mike Markkula", "type" = c("person", "investor")), 5)) atomic(apple, connect_nodes(2, 1, list("action" = "founded"))) atomic(apple, connect_nodes(3, 1, list("action" = "founded"))) atomic(apple, connect_nodes(4, 1, list("action" = "founded"))) atomic(apple, connect_nodes(5, 1, list( "action" = "invested", "equity" = 80000, "debt" = 170000))) atomic(apple, connect_nodes(1, 4, list( "action" = "divested", "amount" = 800, "date" = "April 12, 1976"))) atomic(apple, connect_nodes(2, 3)) #Upsert some data atomic(apple, upsert_node(2, list("nickname" = "Woz"), apple)) ## End(Not run)
## Not run: library(simplegraphdb) apple <- "apple_test.sqlite" initialize(apple) atomic(apple, add_node(list( "name" = "Apple Computer Company", "type" = c("company", "start-up"), "founded" = "April 1, 1976"), 1)) atomic(apple, add_node(list( "name" = "Steve Wozniak", "type" = c("person", "engineer", "founder")), 2)) atomic(apple, add_node(list( "name" = "Steve Jobs", "type" = c("person", "designer", "founder")), 3)) atomic(apple, add_node(list( "name" = "Ronald Wayne", "type" = c("person", "administrator", "founder")), 4)) atomic(apple, add_node(list( "name" = "Mike Markkula", "type" = c("person", "investor")), 5)) atomic(apple, connect_nodes(2, 1, list("action" = "founded"))) atomic(apple, connect_nodes(3, 1, list("action" = "founded"))) atomic(apple, connect_nodes(4, 1, list("action" = "founded"))) atomic(apple, connect_nodes(5, 1, list( "action" = "invested", "equity" = 80000, "debt" = 170000))) atomic(apple, connect_nodes(1, 4, list( "action" = "divested", "amount" = 800, "date" = "April 12, 1976"))) atomic(apple, connect_nodes(2, 3)) #Upsert some data atomic(apple, upsert_node(2, list("nickname" = "Woz"), apple)) ## End(Not run)
Generates dot files for visualization of the graph
visualize( db_file, dot_file = "file.dot", path = c(), exclude_node_keys = c(), hide_node_key = FALSE, node_kv = " ", exclude_edge_keys = c(), hide_edge_key = FALSE, edge_kv = " " )
visualize( db_file, dot_file = "file.dot", path = c(), exclude_node_keys = c(), hide_node_key = FALSE, node_kv = " ", exclude_edge_keys = c(), hide_edge_key = FALSE, edge_kv = " " )
db_file |
The name of the SQLite database |
dot_file |
The name of the file |
path |
The path to include in the visualization |
exclude_node_keys |
The node keys to exclude from the visualization |
hide_node_key |
Boolean if the node key is hidden |
node_kv |
The node key values |
exclude_edge_keys |
The key of edges to exclude |
hide_edge_key |
Boolean if the edge key is hidden |
edge_kv |
The edge key values |
No return value. It creates a file.
## Not run: library(simplegraphdb) library(simplegraphdb) apple <- "apple_test.sqlite" initialize(apple) atomic(apple, add_node(list( "name" = "Apple Computer Company", "type" = c("company", "start-up"), "founded" = "April 1, 1976"), 1)) atomic(apple, add_node(list( "name" = "Steve Wozniak", "type" = c("person", "engineer", "founder")), 2)) atomic(apple, add_node(list( "name" = "Steve Jobs", "type" = c("person", "designer", "founder")), 3)) atomic(apple, add_node(list( "name" = "Ronald Wayne", "type" = c("person", "administrator", "founder")), 4)) atomic(apple, add_node(list( "name" = "Mike Markkula", "type" = c("person", "investor")), 5)) atomic(apple, connect_nodes(2, 1, list("action" = "founded"))) atomic(apple, connect_nodes(3, 1, list("action" = "founded"))) atomic(apple, connect_nodes(4, 1, list("action" = "founded"))) atomic(apple, connect_nodes(5, 1, list( "action" = "invested", "equity" = 80000, "debt" = 170000))) atomic(apple, connect_nodes(1, 4, list( "action" = "divested", "amount" = 800, "date" = "April 12, 1976"))) atomic(apple, connect_nodes(2, 3)) atomic(apple, upsert_node(2, list("nickname" = "Woz"), apple)) # Visualize the data visualize(apple, dot_file = "apple.dot", path = c(4, 1, 5)) ## End(Not run)
## Not run: library(simplegraphdb) library(simplegraphdb) apple <- "apple_test.sqlite" initialize(apple) atomic(apple, add_node(list( "name" = "Apple Computer Company", "type" = c("company", "start-up"), "founded" = "April 1, 1976"), 1)) atomic(apple, add_node(list( "name" = "Steve Wozniak", "type" = c("person", "engineer", "founder")), 2)) atomic(apple, add_node(list( "name" = "Steve Jobs", "type" = c("person", "designer", "founder")), 3)) atomic(apple, add_node(list( "name" = "Ronald Wayne", "type" = c("person", "administrator", "founder")), 4)) atomic(apple, add_node(list( "name" = "Mike Markkula", "type" = c("person", "investor")), 5)) atomic(apple, connect_nodes(2, 1, list("action" = "founded"))) atomic(apple, connect_nodes(3, 1, list("action" = "founded"))) atomic(apple, connect_nodes(4, 1, list("action" = "founded"))) atomic(apple, connect_nodes(5, 1, list( "action" = "invested", "equity" = 80000, "debt" = 170000))) atomic(apple, connect_nodes(1, 4, list( "action" = "divested", "amount" = 800, "date" = "April 12, 1976"))) atomic(apple, connect_nodes(2, 3)) atomic(apple, upsert_node(2, list("nickname" = "Woz"), apple)) # Visualize the data visualize(apple, dot_file = "apple.dot", path = c(4, 1, 5)) ## End(Not run)