Torque API Reference

Complete reference for Torque, CogDB's graph traversal API.

Torque is CogDB's graph traversal language implemented as a Python API.

Graph

Graph(graph_name, cog_home="cog_home", cog_path_prefix=None, enable_caching=True, flush_interval=1)

Creates a Graph object. Parameters:

  • cog_home - Home directory name
  • cog_path_prefix - Parent directory
  • enable_caching - Enable caching (default: True)
  • flush_interval - Controls write flushing (default: 1)

flush_interval Options

ValueBehaviorUse Case
1Flush every writeInteractive use
> 1Async flush every N writesBulk inserts
0Manual flush via sync()Maximum speed

Understanding Traversal State

Each traversal method moves the cursor. The v() method resets the cursor.

g.v("alice").out("knows").all()   # {'result': [{'id': 'bob'}]}
g.v("bob").out("likes").all()     # {'result': [{'id': 'tacos'}]}

Terminal methods (all(), count()) do NOT reset the traversal. Only v() resets the cursor.


put

put(vertex1, predicate, vertex2)

Adds an edge connecting two vertices.


put_batch

put_batch(triples)

Adds multiple edges at once. Faster than repeated put() calls.


sync

sync()

Force pending writes to disk.


drop

drop(vertex1, predicate, vertex2)

Removes an edge.


v

v(vertex=None)

Start traversal at a vertex. If no vertex given, starts from all vertices.


out

out(predicates=None)

Traverse forward through outgoing edges.


inc

inc(predicates=None)

Traverse backward through incoming edges.


both

both(predicates=None)

Traverse in both directions.


bfs

bfs(predicates=None, max_depth=None, min_depth=0, direction="out", until=None, unique=True)

Breadth-first search traversal.

ParameterDescriptionDefault
predicatesEdge types to followNone
max_depthMaximum depthNone
min_depthMinimum depth0
direction"out", "inc", or "both""out"
untilStop condition lambdaNone
uniqueVisit each vertex onceTrue

dfs

dfs(predicates=None, max_depth=None, min_depth=0, direction="out", until=None, unique=True)

Depth-first search traversal. Same parameters as bfs().


is_

is_(*vertices)

Filter to specific vertices.


unique

unique()

Remove duplicate vertices.


limit

limit(n)

Limit to first n vertices.


skip

skip(n)

Skip first n vertices.


back

back(tag_name)

Navigate to a previously tagged position.


filter

filter(func=None)

Filter vertices using a lambda.


scan

scan(limit=10, scan_type='v')

Scan nodes ('v') or edges ('e').


tag

tag(tag_name)

Capture vertex IDs at this point. Used by view().


view

view(view_name)

Create HTML visualization. Saves to {cog_home}/views/{view_name}.html.


render

render(height=400, width=600)

Display visualization in Jupyter notebooks.


has

has(predicates, vertex)

Filter vertices with matching outgoing edges.


count

count()

Count vertices. Terminal function.


all

all()

Return all vertices.


lsv

lsv()

List saved view names.


getv

getv(view_name)

Load a saved view.


load_csv

load_csv(csv_path, id_column_name, graph_name)

load_triples

g.load_triples("/path/to/triples.nt", graph_name)

load_edgelist

load_edgelist(edgelist_path, graph_name)

On this page