Skip to content

Commit 04bdeec

Browse files
author
Alex Walker
authored
Add ability to retrieve all rules (#238)
## What is the goal of this PR? Since Rule was refactored in Grakn 2.0 to no longer be a subtype of Concept, we lost the ability to retrieve all rules using `match $x sub rule; get;`. So, to allow the user to retrieve all rules, we now add `getRules` to the `LogicManager`, enabling use of `tx().logic().getRules()`. ## What are the changes implemented in this PR? Add getRules to LogicManager
1 parent 05b0470 commit 04bdeec

File tree

4 files changed

+34
-12
lines changed

4 files changed

+34
-12
lines changed

dependencies/graknlabs/repositories.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def graknlabs_protocol():
5151
git_repository(
5252
name = "graknlabs_protocol",
5353
remote = "https://github.com/graknlabs/protocol",
54-
commit = "a763a1ce558d47bcfb93b18165126403e1472912", # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_protocol
54+
commit = "3b8f0d6dc7cd176e59b13e447c64ba41a2ecff7e", # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_protocol
5555
)
5656

5757
def graknlabs_behaviour():

logic/LogicManager.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,21 @@
1919

2020
package grakn.client.logic;
2121

22+
import grakn.client.concept.type.impl.TypeImpl;
2223
import grakn.client.logic.impl.RuleImpl;
2324
import grakn.client.rpc.RPCTransaction;
25+
import grakn.protocol.ConceptProto;
2426
import grakn.protocol.LogicProto;
2527
import grakn.protocol.TransactionProto;
2628
import graql.lang.pattern.Pattern;
2729

2830
import javax.annotation.CheckReturnValue;
2931
import javax.annotation.Nullable;
3032

33+
import java.util.List;
34+
import java.util.function.Function;
35+
import java.util.stream.Stream;
36+
3137
import static grakn.client.common.tracing.TracingProtoBuilder.tracingData;
3238

3339
public final class LogicManager {
@@ -64,10 +70,24 @@ public Rule getRule(String label) {
6470
}
6571
}
6672

73+
@CheckReturnValue
74+
public Stream<RuleImpl> getRules() {
75+
final LogicProto.LogicManager.Req.Builder method = LogicProto.LogicManager.Req.newBuilder()
76+
.setGetRulesReq(LogicProto.LogicManager.GetRules.Req.getDefaultInstance());
77+
return ruleStream(method, res -> res.getGetRulesRes().getRulesList());
78+
}
79+
6780
private LogicProto.LogicManager.Res execute(LogicProto.LogicManager.Req request) {
6881
final TransactionProto.Transaction.Req.Builder req = TransactionProto.Transaction.Req.newBuilder()
6982
.putAllMetadata(tracingData())
7083
.setLogicManagerReq(request);
7184
return rpcTransaction.execute(req).getLogicManagerRes();
7285
}
86+
87+
private Stream<RuleImpl> ruleStream(LogicProto.LogicManager.Req.Builder method, Function<LogicProto.LogicManager.Res, List<LogicProto.Rule>> ruleListGetter) {
88+
final TransactionProto.Transaction.Req.Builder request = TransactionProto.Transaction.Req.newBuilder()
89+
.putAllMetadata(tracingData())
90+
.setLogicManagerReq(method);
91+
return rpcTransaction.stream(request, res -> ruleListGetter.apply(res.getLogicManagerRes()).stream().map(RuleImpl::of));
92+
}
7393
}

logic/impl/RuleImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class RuleImpl implements Rule {
5252
}
5353

5454
public static RuleImpl of(LogicProto.Rule ruleProto) {
55-
return new RuleImpl(ruleProto.getLabel(), Graql.and(Graql.parsePatterns(ruleProto.getWhen())), Graql.parseVariable(ruleProto.getThen()).asThing());
55+
return new RuleImpl(ruleProto.getLabel(), Graql.parsePattern(ruleProto.getWhen()).asConjunction(), Graql.parseVariable(ruleProto.getThen()).asThing());
5656
}
5757

5858
@Override

test/integration/ClientQueryTest.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
import java.util.function.Consumer;
4040

4141
import static grakn.client.Grakn.Transaction.Type.WRITE;
42+
import static graql.lang.Graql.and;
43+
import static graql.lang.Graql.rel;
44+
import static graql.lang.Graql.rule;
4245
import static graql.lang.Graql.type;
4346
import static graql.lang.Graql.var;
4447

@@ -53,6 +56,7 @@ public static void setUpClass() throws InterruptedException, IOException, Timeou
5356
grakn = new GraknCoreRunner();
5457
grakn.start();
5558
graknClient = GraknClient.core(grakn.address());
59+
if (graknClient.databases().contains("grakn")) graknClient.databases().delete("grakn");
5660
graknClient.databases().create("grakn");
5761
}
5862

@@ -76,19 +80,17 @@ public void applicationTest() {
7680
type("lion").sub("entity").owns("name").plays("mating", "male-partner").plays("mating", "female-partner").plays("child-bearing", "offspring").plays("parentship", "parent").plays("parentship", "child")
7781
);
7882

79-
// TODO: re-enable when defining rules is supported
80-
// final GraqlDefine ruleQuery = Graql.define(type("infer-parentship-from-mating-and-child-bearing").sub("rule")
81-
// .when(and(
82-
// rel("male-partner", var("male")).rel("female-partner", var("female")).isa("mating"),
83-
// var("childbearing").rel("child-bearer").rel("offspring", var("offspring")).isa("child-bearing")
84-
// ))
85-
// .then(and(
86-
// rel("parent", var("male")).rel("parent", var("female")).rel("child", var("offspring")).isa("parentship")
87-
// )));
83+
final GraqlDefine ruleQuery = Graql.define(rule("infer-parentship-from-mating-and-child-bearing")
84+
.when(and(
85+
rel("male-partner", var("male")).rel("female-partner", var("female")).isa("mating"),
86+
var("childbearing").rel("child-bearer").rel("offspring", var("offspring")).isa("child-bearing")))
87+
.then(rel("parent", var("male"))
88+
.rel("parent", var("female"))
89+
.rel("child", var("offspring")).isa("parentship")));
8890
LOG.info("clientJavaE2E() - define a schema...");
8991
LOG.info("clientJavaE2E() - '" + defineQuery + "'");
9092
tx.query().define(defineQuery);
91-
// tx.query().define(ruleQuery);
93+
tx.query().define(ruleQuery);
9294
tx.commit();
9395
LOG.info("clientJavaE2E() - done.");
9496
}, Session.Type.SCHEMA);

0 commit comments

Comments
 (0)