Is it possible to create a user-defined rule with a sparql inside?

If yes, could you please provide an example? because I prefer to use a rule rather than a construct (if possible)

I'm trying to create a rule based to identify the tipical PrecededBy/followedBy task in a process (class):

SELECT ?process ?order (IF(?preceded < ?min, ?null, ?preceded) AS ?_preceded) (IF(?followed > ?max, ?null, ?followed) AS ?_followed)
{
{
SELECT ?process ?order ?preceded ?followed ?min ?max
{
?x rdf:type Hiring:Task .
?x Hiring:_Process ?process .
?x Hiring:Order ?order .
BIND(?order -1 AS ?preceded) .
BIND(?order +1 AS ?followed) .
{
SELECT ?process (MIN(?order) as ?min) (MAX(?order) as ?max)
{
?x rdf:type Hiring:Task .
?x Hiring:_Process ?process .
?x Hiring:Order ?order .
} GROUP BY ?process
}
}
}
}

Exit should be samething like below:

|Process|Order |PrecededBy|FollowedBy|
|1 |1 | | |
|2 |1 | |2 |
|2 |2 |1 | |
|3 |1 | |2 |
|3 |2 |1 |3 |
|3 |3 |2 | |
|4 |1 | |2 |
|4 |2 |1 | |

thank you and regards