Struct pregel_rs::pregel::PregelBuilder
source · pub struct PregelBuilder<'a> { /* private fields */ }
Expand description
The PregelBuilder
struct represents a builder for configuring the Pregel
algorithm to be executed on a given graph.
Properties:
-
graph
: Thegraph
property is aGraphFrame
struct that represents the graph data structure used in the Pregel algorithm. It contains information about the vertices and edges of the graph. -
max_iterations
: The maximum number of iterations that the Pregel algorithm will run for. -
vertex_column
:vertex_column
is a property of thePregelBuilder
struct that represents the name of the column in the graph’s vertex DataFrame that contains the vertex IDs. This column is used to identify and locate a column where we apply some of the provided operations during the Pregel computation. -
initial_message
:initial_message
is an expression that defines the initial message that each vertex in the graph will receive before the computation starts. This message can be used to initialize the state of each vertex or to provide some initial information to the computation. -
send_messages
:send_messages
is a tuple containing two expressions. The first expression represents the message sending function that determines whether the message will go from Src to Dst or vice-versa. The second expression represents the message sending function that determines which messages to send from a vertex to its neighbors. -
aggregate_messages
:aggregate_messages
is an expression that defines how messages sent to a vertex should be aggregated. In Pregel, messages are sent from one vertex to another and can be aggregated before being processed by the receiving vertex. Theaggregate_messages
expression specifies how these messages should be combined. -
v_prog
:v_prog
is an expression that defines the vertex program for the Pregel algorithm. It specifies the computation that each vertex performs during each iteration of the algorithm. The vertex program can take as input the current state of the vertex, the messages received from its neighbors or and any other relevant information.
Implementations§
source§impl<'a> PregelBuilder<'a>
impl<'a> PregelBuilder<'a>
sourcepub fn new(graph: GraphFrame) -> Self
pub fn new(graph: GraphFrame) -> Self
This function creates a new instance of a PregelBuilder with default values.
Arguments:
graph
: The graph parameter is of type GraphFrame and represents the graph on which the Pregel algorithm will be executed.
Returns:
A new instance of the PregelBuilder
struct.
Examples found in repository?
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
fn main() -> Result<(), Box<dyn Error>> {
let edges = df![
Subject.as_ref() => [0, 1, 1, 2, 2, 3],
Object.as_ref() => [1, 0, 3, 1, 3, 2],
]?;
let vertices = df![
VertexId.as_ref() => [0, 1, 2, 3],
Custom("value").as_ref() => [3, 6, 2, 1],
]?;
let pregel = PregelBuilder::new(GraphFrame::new(vertices, edges)?)
.max_iterations(4)
.with_vertex_column(Custom("max_value"))
.initial_message(col(Custom("value").as_ref()))
.send_messages(
MessageReceiver::Object,
Column::subject(Custom("max_value")),
)
.aggregate_messages(Column::msg(None).max())
.v_prog(max_exprs([
col(Custom("max_value").as_ref()),
Column::msg(None),
]))
.build();
Ok(println!("{}", pregel.run()?))
}
More examples
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
fn main() -> Result<(), Box<dyn Error>> {
let edges = df![
Subject.as_ref() => [0, 0, 1, 2, 3, 4, 4, 4],
Object.as_ref() => [1, 2, 2, 3, 3, 1, 2, 3],
]?;
let vertices = GraphFrame::from_edges(edges.clone())?.out_degrees()?;
let damping_factor = 0.85;
let num_vertices: f64 = vertices.column(VertexId.as_ref())?.len() as f64;
let pregel = PregelBuilder::new(GraphFrame::new(vertices, edges)?)
.max_iterations(4)
.with_vertex_column(Custom("rank"))
.initial_message(lit(1.0 / num_vertices))
.send_messages(
MessageReceiver::Subject,
Column::subject(Column::Custom("rank")) / Column::subject(Column::Custom("out_degree")),
)
.send_messages(
MessageReceiver::Object,
Column::subject(Custom("rank")) / Column::subject(Custom("out_degree")),
)
.aggregate_messages(Column::msg(None).sum())
.v_prog(
Column::msg(None) * lit(damping_factor) + lit((1.0 - damping_factor) / num_vertices),
)
.build();
Ok(println!("{}", pregel.run()?))
}
sourcepub fn max_iterations(self, max_iterations: u8) -> Self
pub fn max_iterations(self, max_iterations: u8) -> Self
This function sets the maximum number of iterations for the Pregel algorithm and returns the modified PregelBuilder.
Arguments:
max_iterations
: Themax_iterations
parameter is an unsigned 8-bit integer that represents the maximum number of iterations that the Pregel algorithm or process can perform. This method sets themax_iterations
field of a struct to the provided value and returns the modified struct instance.
Returns:
The max_iterations
method returns Self
, which refers to the same struct
instance that the method was called on. This allows for method chaining, where
multiple methods can be called on the same struct instance in a single
expression.
Examples found in repository?
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
fn main() -> Result<(), Box<dyn Error>> {
let edges = df![
Subject.as_ref() => [0, 1, 1, 2, 2, 3],
Object.as_ref() => [1, 0, 3, 1, 3, 2],
]?;
let vertices = df![
VertexId.as_ref() => [0, 1, 2, 3],
Custom("value").as_ref() => [3, 6, 2, 1],
]?;
let pregel = PregelBuilder::new(GraphFrame::new(vertices, edges)?)
.max_iterations(4)
.with_vertex_column(Custom("max_value"))
.initial_message(col(Custom("value").as_ref()))
.send_messages(
MessageReceiver::Object,
Column::subject(Custom("max_value")),
)
.aggregate_messages(Column::msg(None).max())
.v_prog(max_exprs([
col(Custom("max_value").as_ref()),
Column::msg(None),
]))
.build();
Ok(println!("{}", pregel.run()?))
}
More examples
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
fn main() -> Result<(), Box<dyn Error>> {
let edges = df![
Subject.as_ref() => [0, 0, 1, 2, 3, 4, 4, 4],
Object.as_ref() => [1, 2, 2, 3, 3, 1, 2, 3],
]?;
let vertices = GraphFrame::from_edges(edges.clone())?.out_degrees()?;
let damping_factor = 0.85;
let num_vertices: f64 = vertices.column(VertexId.as_ref())?.len() as f64;
let pregel = PregelBuilder::new(GraphFrame::new(vertices, edges)?)
.max_iterations(4)
.with_vertex_column(Custom("rank"))
.initial_message(lit(1.0 / num_vertices))
.send_messages(
MessageReceiver::Subject,
Column::subject(Column::Custom("rank")) / Column::subject(Column::Custom("out_degree")),
)
.send_messages(
MessageReceiver::Object,
Column::subject(Custom("rank")) / Column::subject(Custom("out_degree")),
)
.aggregate_messages(Column::msg(None).sum())
.v_prog(
Column::msg(None) * lit(damping_factor) + lit((1.0 - damping_factor) / num_vertices),
)
.build();
Ok(println!("{}", pregel.run()?))
}
sourcepub fn with_vertex_column(self, vertex_column: Column) -> Self
pub fn with_vertex_column(self, vertex_column: Column) -> Self
This function sets the vertex column identifier for a struct and returns the struct.
Arguments:
vertex_column
:vertex_column
is a parameter of typeColumnIdentifier
which represents the column identifier for the vertex column in a graph database. Thewith_vertex_column
method takes in aColumnIdentifier
value and sets it as thevertex_column
property of the struct instance. It then returns
Returns:
The with_vertex_column
method returns Self
, which refers to the same struct
instance that the method was called on. This allows for method chaining, where
multiple methods can be called on the same struct instance in a single
expression.
Examples found in repository?
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
fn main() -> Result<(), Box<dyn Error>> {
let edges = df![
Subject.as_ref() => [0, 1, 1, 2, 2, 3],
Object.as_ref() => [1, 0, 3, 1, 3, 2],
]?;
let vertices = df![
VertexId.as_ref() => [0, 1, 2, 3],
Custom("value").as_ref() => [3, 6, 2, 1],
]?;
let pregel = PregelBuilder::new(GraphFrame::new(vertices, edges)?)
.max_iterations(4)
.with_vertex_column(Custom("max_value"))
.initial_message(col(Custom("value").as_ref()))
.send_messages(
MessageReceiver::Object,
Column::subject(Custom("max_value")),
)
.aggregate_messages(Column::msg(None).max())
.v_prog(max_exprs([
col(Custom("max_value").as_ref()),
Column::msg(None),
]))
.build();
Ok(println!("{}", pregel.run()?))
}
More examples
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
fn main() -> Result<(), Box<dyn Error>> {
let edges = df![
Subject.as_ref() => [0, 0, 1, 2, 3, 4, 4, 4],
Object.as_ref() => [1, 2, 2, 3, 3, 1, 2, 3],
]?;
let vertices = GraphFrame::from_edges(edges.clone())?.out_degrees()?;
let damping_factor = 0.85;
let num_vertices: f64 = vertices.column(VertexId.as_ref())?.len() as f64;
let pregel = PregelBuilder::new(GraphFrame::new(vertices, edges)?)
.max_iterations(4)
.with_vertex_column(Custom("rank"))
.initial_message(lit(1.0 / num_vertices))
.send_messages(
MessageReceiver::Subject,
Column::subject(Column::Custom("rank")) / Column::subject(Column::Custom("out_degree")),
)
.send_messages(
MessageReceiver::Object,
Column::subject(Custom("rank")) / Column::subject(Custom("out_degree")),
)
.aggregate_messages(Column::msg(None).sum())
.v_prog(
Column::msg(None) * lit(damping_factor) + lit((1.0 - damping_factor) / num_vertices),
)
.build();
Ok(println!("{}", pregel.run()?))
}
sourcepub fn initial_message(self, initial_message: Expr) -> Self
pub fn initial_message(self, initial_message: Expr) -> Self
This function sets the initial message for a Rust struct and returns the struct.
Arguments:
initial_message
:initial_message
is a parameter of typeExpr
that is used in a method of a struct. The method takes ownership ofself
and returns it after setting theinitial_message
field of the struct to the value of theinitial_message
parameter. This method can be used
Returns:
The initial_message
method returns Self
, which refers to the same struct
instance that the method was called on. This allows for method chaining, where
multiple methods can be called on the same struct instance in a single
expression.
Examples found in repository?
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
fn main() -> Result<(), Box<dyn Error>> {
let edges = df![
Subject.as_ref() => [0, 1, 1, 2, 2, 3],
Object.as_ref() => [1, 0, 3, 1, 3, 2],
]?;
let vertices = df![
VertexId.as_ref() => [0, 1, 2, 3],
Custom("value").as_ref() => [3, 6, 2, 1],
]?;
let pregel = PregelBuilder::new(GraphFrame::new(vertices, edges)?)
.max_iterations(4)
.with_vertex_column(Custom("max_value"))
.initial_message(col(Custom("value").as_ref()))
.send_messages(
MessageReceiver::Object,
Column::subject(Custom("max_value")),
)
.aggregate_messages(Column::msg(None).max())
.v_prog(max_exprs([
col(Custom("max_value").as_ref()),
Column::msg(None),
]))
.build();
Ok(println!("{}", pregel.run()?))
}
More examples
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
fn main() -> Result<(), Box<dyn Error>> {
let edges = df![
Subject.as_ref() => [0, 0, 1, 2, 3, 4, 4, 4],
Object.as_ref() => [1, 2, 2, 3, 3, 1, 2, 3],
]?;
let vertices = GraphFrame::from_edges(edges.clone())?.out_degrees()?;
let damping_factor = 0.85;
let num_vertices: f64 = vertices.column(VertexId.as_ref())?.len() as f64;
let pregel = PregelBuilder::new(GraphFrame::new(vertices, edges)?)
.max_iterations(4)
.with_vertex_column(Custom("rank"))
.initial_message(lit(1.0 / num_vertices))
.send_messages(
MessageReceiver::Subject,
Column::subject(Column::Custom("rank")) / Column::subject(Column::Custom("out_degree")),
)
.send_messages(
MessageReceiver::Object,
Column::subject(Custom("rank")) / Column::subject(Custom("out_degree")),
)
.aggregate_messages(Column::msg(None).sum())
.v_prog(
Column::msg(None) * lit(damping_factor) + lit((1.0 - damping_factor) / num_vertices),
)
.build();
Ok(println!("{}", pregel.run()?))
}
sourcepub fn send_messages(self, to: MessageReceiver, send_messages: Expr) -> Self
pub fn send_messages(self, to: MessageReceiver, send_messages: Expr) -> Self
This function sets the message sending behavior for a Pregel computation in Rust. Chaining this method allows for multiple message sending behaviors to be specified for a single Pregel computation.
Examples
use polars::prelude::*;
use pregel_rs::graph_frame::GraphFrame;
use pregel_rs::pregel::Column;
use pregel_rs::pregel::Column::{Custom, Object, VertexId, Subject};
use pregel_rs::pregel::{MessageReceiver, Pregel, PregelBuilder};
use std::error::Error;
// Simple example of a Pregel algorithm where we chain several `send_messages` calls. In
// this example, we send a message to the source of an edge and then to the destination of
// the same edge. It has no real use case, but it demonstrates how to chain multiple calls.
fn main() -> Result<(), Box<dyn Error>> {
let edges = df![
Subject.as_ref() => [0, 1, 1, 2, 2, 3],
Object.as_ref() => [1, 0, 3, 1, 3, 2],
]?;
let vertices = df![
VertexId.as_ref() => [0, 1, 2, 3],
Custom("value").as_ref() => [3, 6, 2, 1],
]?;
let pregel = PregelBuilder::new(GraphFrame::new(vertices, edges)?)
.max_iterations(4)
.with_vertex_column(Custom("aux"))
.initial_message(lit(0))
.send_messages(MessageReceiver::Subject, lit(1))
.send_messages(MessageReceiver::Object, lit(-1))
.aggregate_messages(Column::msg(None).sum())
.v_prog(Column::msg(None) + lit(1))
.build();
Ok(println!("{:?}", pregel.run()))
}
Arguments:
to
:to
is a parameter of typeMessageReceiver
. It represents the destination vertex or vertices to which messages will be sent during the computation.send_messages
:send_messages
is a parameter of typeExpr
. It is used to specify the function that will be applied to each vertex in the graph to send messages to its neighboring vertices.
Returns:
The send_messages
method returns Self
, which refers to the same struct
instance that the method was called on. This allows for method chaining, where
multiple methods can be called on the same struct instance in a single
expression.
Examples found in repository?
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
fn main() -> Result<(), Box<dyn Error>> {
let edges = df![
Subject.as_ref() => [0, 1, 1, 2, 2, 3],
Object.as_ref() => [1, 0, 3, 1, 3, 2],
]?;
let vertices = df![
VertexId.as_ref() => [0, 1, 2, 3],
Custom("value").as_ref() => [3, 6, 2, 1],
]?;
let pregel = PregelBuilder::new(GraphFrame::new(vertices, edges)?)
.max_iterations(4)
.with_vertex_column(Custom("max_value"))
.initial_message(col(Custom("value").as_ref()))
.send_messages(
MessageReceiver::Object,
Column::subject(Custom("max_value")),
)
.aggregate_messages(Column::msg(None).max())
.v_prog(max_exprs([
col(Custom("max_value").as_ref()),
Column::msg(None),
]))
.build();
Ok(println!("{}", pregel.run()?))
}
More examples
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
fn main() -> Result<(), Box<dyn Error>> {
let edges = df![
Subject.as_ref() => [0, 0, 1, 2, 3, 4, 4, 4],
Object.as_ref() => [1, 2, 2, 3, 3, 1, 2, 3],
]?;
let vertices = GraphFrame::from_edges(edges.clone())?.out_degrees()?;
let damping_factor = 0.85;
let num_vertices: f64 = vertices.column(VertexId.as_ref())?.len() as f64;
let pregel = PregelBuilder::new(GraphFrame::new(vertices, edges)?)
.max_iterations(4)
.with_vertex_column(Custom("rank"))
.initial_message(lit(1.0 / num_vertices))
.send_messages(
MessageReceiver::Subject,
Column::subject(Column::Custom("rank")) / Column::subject(Column::Custom("out_degree")),
)
.send_messages(
MessageReceiver::Object,
Column::subject(Custom("rank")) / Column::subject(Custom("out_degree")),
)
.aggregate_messages(Column::msg(None).sum())
.v_prog(
Column::msg(None) * lit(damping_factor) + lit((1.0 - damping_factor) / num_vertices),
)
.build();
Ok(println!("{}", pregel.run()?))
}
sourcepub fn send_messages_function(
self,
to: MessageReceiver,
send_messages: impl FnMut() -> Expr + 'a
) -> Self
pub fn send_messages_function( self, to: MessageReceiver, send_messages: impl FnMut() -> Expr + 'a ) -> Self
This is a Rust function that adds a new message to be sent to a message receiver using a closure that returns an expression.
Arguments:
-
to
:to
is a parameter of typeMessageReceiver
which represents the recipient of the message being sent. It could be an email address, phone number, or any other means of communication. -
send_messages
:send_messages
is a closure that takes no arguments and returns anExpr
. It is used to generate the messages that will be sent to theto
message receiver. The closure is passed as an argument to thesend_messages_function
method and is stored in aSendMessage
struct
Returns:
The Self
object is being returned, which allows for method chaining.
sourcepub fn aggregate_messages(self, aggregate_messages: Expr) -> Self
pub fn aggregate_messages(self, aggregate_messages: Expr) -> Self
This function sets the aggregate_messages field of a struct to a given expression and returns the struct.
Arguments:
aggregate_messages
:aggregate_messages
is a parameter of typeExpr
that is used in a method of a struct. The method takes ownership of the struct instance (self
) and assigns the value ofaggregate_messages
to the corresponding field of the struct. The method then returns the modified struct instance. This
Returns:
The aggregate_messages
method returns Self
, which refers to the same struct
instance that the method was called on. This allows for method chaining, where
multiple methods can be called on the same struct instance in a single
expression.
Examples found in repository?
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
fn main() -> Result<(), Box<dyn Error>> {
let edges = df![
Subject.as_ref() => [0, 1, 1, 2, 2, 3],
Object.as_ref() => [1, 0, 3, 1, 3, 2],
]?;
let vertices = df![
VertexId.as_ref() => [0, 1, 2, 3],
Custom("value").as_ref() => [3, 6, 2, 1],
]?;
let pregel = PregelBuilder::new(GraphFrame::new(vertices, edges)?)
.max_iterations(4)
.with_vertex_column(Custom("max_value"))
.initial_message(col(Custom("value").as_ref()))
.send_messages(
MessageReceiver::Object,
Column::subject(Custom("max_value")),
)
.aggregate_messages(Column::msg(None).max())
.v_prog(max_exprs([
col(Custom("max_value").as_ref()),
Column::msg(None),
]))
.build();
Ok(println!("{}", pregel.run()?))
}
More examples
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
fn main() -> Result<(), Box<dyn Error>> {
let edges = df![
Subject.as_ref() => [0, 0, 1, 2, 3, 4, 4, 4],
Object.as_ref() => [1, 2, 2, 3, 3, 1, 2, 3],
]?;
let vertices = GraphFrame::from_edges(edges.clone())?.out_degrees()?;
let damping_factor = 0.85;
let num_vertices: f64 = vertices.column(VertexId.as_ref())?.len() as f64;
let pregel = PregelBuilder::new(GraphFrame::new(vertices, edges)?)
.max_iterations(4)
.with_vertex_column(Custom("rank"))
.initial_message(lit(1.0 / num_vertices))
.send_messages(
MessageReceiver::Subject,
Column::subject(Column::Custom("rank")) / Column::subject(Column::Custom("out_degree")),
)
.send_messages(
MessageReceiver::Object,
Column::subject(Custom("rank")) / Column::subject(Custom("out_degree")),
)
.aggregate_messages(Column::msg(None).sum())
.v_prog(
Column::msg(None) * lit(damping_factor) + lit((1.0 - damping_factor) / num_vertices),
)
.build();
Ok(println!("{}", pregel.run()?))
}
sourcepub fn aggregate_messages_function(
self,
aggregate_messages: impl FnMut() -> Expr + 'a
) -> Self
pub fn aggregate_messages_function( self, aggregate_messages: impl FnMut() -> Expr + 'a ) -> Self
This function sets the aggregate_messages field of a struct to a closure that returns an Expr.
Arguments:
aggregate_messages
:aggregate_messages
is a closure that takes no arguments and returns anExpr
object. The closure is passed as an argument to theaggregate_messages_function
method and is stored in theself.aggregate_messages
field. The closure is expected to be implemented by the caller and will be used
Returns:
The Self
object is being returned. This allows for method chaining, where
multiple methods can be called on the same object in a single expression.
sourcepub fn v_prog(self, v_prog: Expr) -> Self
pub fn v_prog(self, v_prog: Expr) -> Self
This function sets the value of a field in a struct and returns the struct itself.
Arguments:
v_prog
:v_prog
is a parameter of typeExpr
that is being passed to a method calledv_prog
. The method takes ownership ofself
(which is of the same type as the struct or object that the method belongs to) and assigns the value ofv_prog
to
Returns:
The v_prog
method returns Self
, which refers to the same struct
instance that the method was called on. This allows for method chaining, where
multiple methods can be called on the same struct instance in a single
expression.
Examples found in repository?
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
fn main() -> Result<(), Box<dyn Error>> {
let edges = df![
Subject.as_ref() => [0, 1, 1, 2, 2, 3],
Object.as_ref() => [1, 0, 3, 1, 3, 2],
]?;
let vertices = df![
VertexId.as_ref() => [0, 1, 2, 3],
Custom("value").as_ref() => [3, 6, 2, 1],
]?;
let pregel = PregelBuilder::new(GraphFrame::new(vertices, edges)?)
.max_iterations(4)
.with_vertex_column(Custom("max_value"))
.initial_message(col(Custom("value").as_ref()))
.send_messages(
MessageReceiver::Object,
Column::subject(Custom("max_value")),
)
.aggregate_messages(Column::msg(None).max())
.v_prog(max_exprs([
col(Custom("max_value").as_ref()),
Column::msg(None),
]))
.build();
Ok(println!("{}", pregel.run()?))
}
More examples
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
fn main() -> Result<(), Box<dyn Error>> {
let edges = df![
Subject.as_ref() => [0, 0, 1, 2, 3, 4, 4, 4],
Object.as_ref() => [1, 2, 2, 3, 3, 1, 2, 3],
]?;
let vertices = GraphFrame::from_edges(edges.clone())?.out_degrees()?;
let damping_factor = 0.85;
let num_vertices: f64 = vertices.column(VertexId.as_ref())?.len() as f64;
let pregel = PregelBuilder::new(GraphFrame::new(vertices, edges)?)
.max_iterations(4)
.with_vertex_column(Custom("rank"))
.initial_message(lit(1.0 / num_vertices))
.send_messages(
MessageReceiver::Subject,
Column::subject(Column::Custom("rank")) / Column::subject(Column::Custom("out_degree")),
)
.send_messages(
MessageReceiver::Object,
Column::subject(Custom("rank")) / Column::subject(Custom("out_degree")),
)
.aggregate_messages(Column::msg(None).sum())
.v_prog(
Column::msg(None) * lit(damping_factor) + lit((1.0 - damping_factor) / num_vertices),
)
.build();
Ok(println!("{}", pregel.run()?))
}
sourcepub fn v_prog_function(self, v_prog: impl FnMut() -> Expr + 'a) -> Self
pub fn v_prog_function(self, v_prog: impl FnMut() -> Expr + 'a) -> Self
This is a Rust function that takes a closure that returns an expression and sets it as a field in a struct.
Arguments:
v_prog
:v_prog
is a closure that takes no arguments and returns anExpr
object. The closure is passed as an argument to thev_prog_function
method. Theimpl FnMut() -> Expr + 'a
syntax specifies that the closure must be mutable (FnMut
) and that it must not capture any variables from the enclosing scope ('a
). The closure is stored in theself.v_prog
field of the struct.
Returns:
The Self
object is being returned. This allows for method chaining, where
multiple methods can be called on the same object in a single expression.
sourcepub fn build(self) -> Pregel<'a>
pub fn build(self) -> Pregel<'a>
The function returns a Pregel struct with the specified properties. This is,
Pregel structs are to be created using the Builder Pattern
, a creational
design pattern that provides a way to construct complex structs in a
step-by-step manner, allowing for the creation of several different configurations
or variations of the same struct without directly exposing the underlying
complexity. It allows for flexibility in creating different variations of
objects while keeping the construction process modular and manageable.
Examples
use polars::prelude::*;
use pregel_rs::graph_frame::GraphFrame;
use pregel_rs::pregel::Column;
use pregel_rs::pregel::Column::{Custom, Object, VertexId, Subject};
use pregel_rs::pregel::{MessageReceiver, Pregel, PregelBuilder};
use std::error::Error;
// Simple example of a Pregel algorithm that finds the maximum value in a graph.
fn main() -> Result<(), Box<dyn Error>> {
let edges = df![
Subject.as_ref() => [0, 1, 1, 2, 2, 3],
Object.as_ref() => [1, 0, 3, 1, 3, 2],
]?;
let vertices = df![
VertexId.as_ref() => [0, 1, 2, 3],
Custom("value").as_ref() => [3, 6, 2, 1],
]?;
let pregel = PregelBuilder::new(GraphFrame::new(vertices, edges)?)
.max_iterations(4)
.with_vertex_column(Custom("max_value"))
.initial_message(col(Custom("value").as_ref()))
.send_messages(MessageReceiver::Object, Column::subject(Custom("max_value")))
.aggregate_messages(Column::msg(None).max())
.v_prog(max_exprs([col(Custom("max_value").as_ref()), Column::msg(None)]))
.build();
Ok(println!("{}", pregel.run()?))
}
Returns:
The build
method is returning an instance of the Pregel
struct with the
values of the fields set to the values of the corresponding fields in the
Builder
struct.
Examples found in repository?
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
fn main() -> Result<(), Box<dyn Error>> {
let edges = df![
Subject.as_ref() => [0, 1, 1, 2, 2, 3],
Object.as_ref() => [1, 0, 3, 1, 3, 2],
]?;
let vertices = df![
VertexId.as_ref() => [0, 1, 2, 3],
Custom("value").as_ref() => [3, 6, 2, 1],
]?;
let pregel = PregelBuilder::new(GraphFrame::new(vertices, edges)?)
.max_iterations(4)
.with_vertex_column(Custom("max_value"))
.initial_message(col(Custom("value").as_ref()))
.send_messages(
MessageReceiver::Object,
Column::subject(Custom("max_value")),
)
.aggregate_messages(Column::msg(None).max())
.v_prog(max_exprs([
col(Custom("max_value").as_ref()),
Column::msg(None),
]))
.build();
Ok(println!("{}", pregel.run()?))
}
More examples
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
fn main() -> Result<(), Box<dyn Error>> {
let edges = df![
Subject.as_ref() => [0, 0, 1, 2, 3, 4, 4, 4],
Object.as_ref() => [1, 2, 2, 3, 3, 1, 2, 3],
]?;
let vertices = GraphFrame::from_edges(edges.clone())?.out_degrees()?;
let damping_factor = 0.85;
let num_vertices: f64 = vertices.column(VertexId.as_ref())?.len() as f64;
let pregel = PregelBuilder::new(GraphFrame::new(vertices, edges)?)
.max_iterations(4)
.with_vertex_column(Custom("rank"))
.initial_message(lit(1.0 / num_vertices))
.send_messages(
MessageReceiver::Subject,
Column::subject(Column::Custom("rank")) / Column::subject(Column::Custom("out_degree")),
)
.send_messages(
MessageReceiver::Object,
Column::subject(Custom("rank")) / Column::subject(Custom("out_degree")),
)
.aggregate_messages(Column::msg(None).sum())
.v_prog(
Column::msg(None) * lit(damping_factor) + lit((1.0 - damping_factor) / num_vertices),
)
.build();
Ok(println!("{}", pregel.run()?))
}