|
| 1 | +//! Plan-based RPQ evaluation using `LAGraph_RPQMatrix`. |
| 2 | +
|
| 3 | +use std::ptr::null_mut; |
| 4 | + |
| 5 | +use egg::{Id, RecExpr, define_language}; |
| 6 | +use spargebra::algebra::PropertyPathExpression; |
| 7 | +use spargebra::term::TermPattern; |
| 8 | + |
| 9 | +use crate::graph::{GraphDecomposition, GraphblasVector, ensure_grb_init}; |
| 10 | +use crate::lagraph_sys::*; |
| 11 | +use crate::rpq::{RpqError, RpqEvaluator, RpqResult}; |
| 12 | +use crate::{grb_ok, la_ok}; |
| 13 | + |
| 14 | +unsafe impl Send for RPQMatrixPlan {} |
| 15 | + |
| 16 | +define_language! { |
| 17 | + pub enum RpqPlan { |
| 18 | + Label(String), |
| 19 | + "/" = Seq([Id; 2]), |
| 20 | + "|" = Alt([Id; 2]), |
| 21 | + "*" = Star([Id; 1]), |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +/// Compile a [`PropertyPathExpression`] into [`RecExpr<RpqPlan>`]. |
| 26 | +pub fn to_expr(path: &PropertyPathExpression) -> Result<RecExpr<RpqPlan>, RpqError> { |
| 27 | + let mut expr = RecExpr::default(); |
| 28 | + to_expr_aux(path, &mut expr)?; |
| 29 | + Ok(expr) |
| 30 | +} |
| 31 | + |
| 32 | +fn to_expr_aux( |
| 33 | + path: &PropertyPathExpression, |
| 34 | + expr: &mut RecExpr<RpqPlan>, |
| 35 | +) -> Result<Id, RpqError> { |
| 36 | + match path { |
| 37 | + PropertyPathExpression::NamedNode(nn) => { |
| 38 | + Ok(expr.add(RpqPlan::Label(nn.as_str().to_owned()))) |
| 39 | + } |
| 40 | + |
| 41 | + PropertyPathExpression::Sequence(lhs, rhs) => { |
| 42 | + let l = to_expr_aux(lhs, expr)?; |
| 43 | + let r = to_expr_aux(rhs, expr)?; |
| 44 | + Ok(expr.add(RpqPlan::Seq([l, r]))) |
| 45 | + } |
| 46 | + |
| 47 | + PropertyPathExpression::Alternative(lhs, rhs) => { |
| 48 | + let l = to_expr_aux(lhs, expr)?; |
| 49 | + let r = to_expr_aux(rhs, expr)?; |
| 50 | + Ok(expr.add(RpqPlan::Alt([l, r]))) |
| 51 | + } |
| 52 | + |
| 53 | + PropertyPathExpression::ZeroOrMore(inner) => { |
| 54 | + let i = to_expr_aux(inner, expr)?; |
| 55 | + Ok(expr.add(RpqPlan::Star([i]))) |
| 56 | + } |
| 57 | + |
| 58 | + PropertyPathExpression::OneOrMore(inner) => { |
| 59 | + let e = to_expr_aux(inner, expr)?; |
| 60 | + let s = expr.add(RpqPlan::Star([e])); |
| 61 | + Ok(expr.add(RpqPlan::Seq([e, s]))) |
| 62 | + } |
| 63 | + |
| 64 | + PropertyPathExpression::ZeroOrOne(_) => Err(RpqError::UnsupportedPath( |
| 65 | + "ZeroOrOne (?) is not supported by RPQMatrix".into(), |
| 66 | + )), |
| 67 | + |
| 68 | + PropertyPathExpression::Reverse(_) => Err(RpqError::UnsupportedPath( |
| 69 | + "Reverse paths are not supported".into(), |
| 70 | + )), |
| 71 | + |
| 72 | + PropertyPathExpression::NegatedPropertySet(_) => Err(RpqError::UnsupportedPath( |
| 73 | + "NegatedPropertySet paths are not supported".into(), |
| 74 | + )), |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +/// Convert a [`RecExpr<RpqPlan>`] into the flat [`RPQMatrixPlan`] array that |
| 79 | +/// `LAGraph_RPQMatrix` expects. |
| 80 | +pub fn materialize<G: GraphDecomposition>( |
| 81 | + expr: &RecExpr<RpqPlan>, |
| 82 | + graph: &G, |
| 83 | +) -> Result<Vec<RPQMatrixPlan>, RpqError> { |
| 84 | + let null_plan = RPQMatrixPlan { |
| 85 | + op: RPQMatrixOp::RPQ_MATRIX_OP_LABEL, |
| 86 | + lhs: null_mut(), |
| 87 | + rhs: null_mut(), |
| 88 | + mat: null_mut(), |
| 89 | + res_mat: null_mut(), |
| 90 | + }; |
| 91 | + let mut plans = vec![null_plan; expr.len()]; |
| 92 | + |
| 93 | + for (id, node) in expr.as_ref().iter().enumerate() { |
| 94 | + plans[id] = match node { |
| 95 | + RpqPlan::Label(label) => { |
| 96 | + let lg = graph |
| 97 | + .get_graph(label) |
| 98 | + .map_err(|_| RpqError::LabelNotFound(label.clone()))?; |
| 99 | + let mat = unsafe { (*lg.inner).A }; |
| 100 | + RPQMatrixPlan { |
| 101 | + op: RPQMatrixOp::RPQ_MATRIX_OP_LABEL, |
| 102 | + lhs: null_mut(), |
| 103 | + rhs: null_mut(), |
| 104 | + mat, |
| 105 | + res_mat: null_mut(), |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + RpqPlan::Seq([l, r]) => RPQMatrixPlan { |
| 110 | + op: RPQMatrixOp::RPQ_MATRIX_OP_CONCAT, |
| 111 | + lhs: unsafe { plans.as_mut_ptr().add(usize::from(*l)) }, |
| 112 | + rhs: unsafe { plans.as_mut_ptr().add(usize::from(*r)) }, |
| 113 | + mat: null_mut(), |
| 114 | + res_mat: null_mut(), |
| 115 | + }, |
| 116 | + |
| 117 | + RpqPlan::Alt([l, r]) => RPQMatrixPlan { |
| 118 | + op: RPQMatrixOp::RPQ_MATRIX_OP_LOR, |
| 119 | + lhs: unsafe { plans.as_mut_ptr().add(usize::from(*l)) }, |
| 120 | + rhs: unsafe { plans.as_mut_ptr().add(usize::from(*r)) }, |
| 121 | + mat: null_mut(), |
| 122 | + res_mat: null_mut(), |
| 123 | + }, |
| 124 | + |
| 125 | + RpqPlan::Star([i]) => RPQMatrixPlan { |
| 126 | + op: RPQMatrixOp::RPQ_MATRIX_OP_KLEENE, |
| 127 | + lhs: null_mut(), |
| 128 | + rhs: unsafe { plans.as_mut_ptr().add(usize::from(*i)) }, |
| 129 | + mat: null_mut(), |
| 130 | + res_mat: null_mut(), |
| 131 | + }, |
| 132 | + }; |
| 133 | + } |
| 134 | + |
| 135 | + Ok(plans) |
| 136 | +} |
| 137 | + |
| 138 | +/// RPQ evaluator backed by `LAGraph_RPQMatrix`. |
| 139 | +pub struct RpqMatrixEvaluator; |
| 140 | + |
| 141 | +impl RpqEvaluator for RpqMatrixEvaluator { |
| 142 | + fn evaluate<G: GraphDecomposition>( |
| 143 | + &self, |
| 144 | + subject: &TermPattern, |
| 145 | + path: &PropertyPathExpression, |
| 146 | + object: &TermPattern, |
| 147 | + graph: &G, |
| 148 | + ) -> Result<RpqResult, RpqError> { |
| 149 | + if !matches!(object, TermPattern::Variable(_)) { |
| 150 | + return Err(RpqError::UnsupportedPath( |
| 151 | + "bound object term is not yet supported by RpqMatrixEvaluator".into(), |
| 152 | + )); |
| 153 | + } |
| 154 | + |
| 155 | + ensure_grb_init().map_err(|e| RpqError::GraphBlas(e.to_string()))?; |
| 156 | + |
| 157 | + let n = graph.num_nodes() as GrB_Index; |
| 158 | + |
| 159 | + let expr = to_expr(path)?; |
| 160 | + |
| 161 | + let mut plans = materialize(&expr, graph)?; |
| 162 | + let root_ptr = unsafe { plans.as_mut_ptr().add(plans.len() - 1) }; |
| 163 | + |
| 164 | + let mut nnz: GrB_Index = 0; |
| 165 | + la_ok!(LAGraph_RPQMatrix(&mut nnz, root_ptr)) |
| 166 | + .map_err(|e| RpqError::GraphBlas(e.to_string()))?; |
| 167 | + |
| 168 | + let res_mat = unsafe { (*root_ptr).res_mat }; |
| 169 | + |
| 170 | + let src = unsafe { |
| 171 | + GraphblasVector::new_bool(n).map_err(|e| RpqError::GraphBlas(e.to_string()))? |
| 172 | + }; |
| 173 | + match subject { |
| 174 | + TermPattern::NamedNode(nn) => { |
| 175 | + let id = graph |
| 176 | + .get_node_id(nn.as_str()) |
| 177 | + .ok_or_else(|| RpqError::VertexNotFound(nn.as_str().to_owned()))? |
| 178 | + as GrB_Index; |
| 179 | + grb_ok!(GrB_Vector_setElement_BOOL(src.inner, true, id)) |
| 180 | + .map_err(|e| RpqError::GraphBlas(e.to_string()))?; |
| 181 | + } |
| 182 | + TermPattern::Variable(_) => { |
| 183 | + for i in 0..n { |
| 184 | + grb_ok!(GrB_Vector_setElement_BOOL(src.inner, true, i)) |
| 185 | + .map_err(|e| RpqError::GraphBlas(e.to_string()))?; |
| 186 | + } |
| 187 | + } |
| 188 | + _ => { |
| 189 | + return Err(RpqError::UnsupportedPath( |
| 190 | + "subject must be a variable or named node".into(), |
| 191 | + )); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + let result = unsafe { |
| 196 | + GraphblasVector::new_bool(n).map_err(|e| RpqError::GraphBlas(e.to_string()))? |
| 197 | + }; |
| 198 | + grb_ok!(GrB_vxm( |
| 199 | + result.inner, |
| 200 | + null_mut(), |
| 201 | + null_mut(), |
| 202 | + GrB_LOR_LAND_SEMIRING_BOOL, |
| 203 | + src.inner, |
| 204 | + res_mat, |
| 205 | + null_mut(), |
| 206 | + )) |
| 207 | + .map_err(|e| RpqError::GraphBlas(e.to_string()))?; |
| 208 | + |
| 209 | + grb_ok!(LAGraph_DestroyRpqMatrixPlan(root_ptr)) |
| 210 | + .map_err(|e| RpqError::GraphBlas(e.to_string()))?; |
| 211 | + |
| 212 | + Ok(RpqResult { reachable: result }) |
| 213 | + } |
| 214 | +} |
0 commit comments