public static void loadFromFile(GraknTx graph, String file) { File graql = new File(GraknSystemProperty.PROJECT_RELATIVE_DIR.value() + "/grakn-test-tools/src/main/graql/" + file); List<String> queries; try { queries = Files.readLines(graql, StandardCharsets.UTF_8); } catch (IOException e) { throw new RuntimeException(e); } graph.graql().parser().parseList(queries.stream().collect(Collectors.joining("\n"))).forEach(Query::execute); } }
/** * @param queryString a string representing a query * @return a query, the type will depend on the type of query. */ @Override public <T extends Query<?>> T parse(String queryString) { return queryParser.parseQuery(queryString); }
/** * @param template a string representing a templated graql query * @param data data used in the template * @return an insert query */ protected Stream<Query> template(String template, Map<String, Object> data, boolean failFast) { try (Context c = parseTemplate.time()){ return queryParser.parseTemplate(template, data); } catch (Exception e) { System.out.println("Query not sent to server: " + e.getMessage()); if (failFast) { throw e; } } return Stream.empty(); }
public static Pattern convert(GrpcConcept.Pattern pattern ) { return Graql.parser().parsePattern(pattern.getValue()); }
@Override protected void buildRules(GraknTx tx) { // These rules are totally made up for testing purposes and don't work! Pattern when = tx.graql().parser().parsePattern("$x has name 'expectation-when'"); Pattern then = tx.graql().parser().parsePattern("$x has name 'expectation-then'"); tx.putRule("expectation-rule", when, then); when = tx.graql().parser().parsePattern("$x has name 'materialize-when'"); then = tx.graql().parser().parsePattern("$x has name 'materialize-then'"); tx.putRule("materialize-rule", when, then); }
@Override public void buildRules(GraknTx tx) { Pattern transitivity_LHS = tx.graql().parser().parsePattern("{(geo-entity: $x, entity-location: $y) isa is-located-in;" + "(geo-entity: $y, entity-location: $z) isa is-located-in;}"); Pattern transitivity_RHS = tx.graql().parser().parsePattern("{(geo-entity: $x, entity-location: $z) isa is-located-in;}"); tx.putRule("Geo Rule", transitivity_LHS, transitivity_RHS); } }
@Override protected Rule newSchemaConcept(Label label) { // TODO: generate more complicated rules QueryBuilder graql = this.tx().graql(); return tx().putRule(label, graql.parser().parsePattern("$x"), graql.parser().parsePattern("$x")); }
@Override protected BiFunction<GraknTx, Label, SchemaConcept> generate() { return random.choose(ImmutableList.of( GraknTx::putEntityType, (graph, label) -> graph.putAttributeType(label, gen(AttributeType.DataType.class)), GraknTx::putRelationshipType, GraknTx::putRole, //TODO: Make smarter rules (graph, label) -> graph.putRule(label, graph.graql().parser().parsePattern("$x"), graph.graql().parser().parsePattern("$x")) )); } }
private Pattern parsePattern(String value){ if(value == null) { return null; } else { return vertex().tx().graql().parser().parsePattern(value); } }
Pattern R1_LHS = tx.graql().parser().parsePattern("{" + "$x isa person;$x has nationality 'American';" + "$y isa weapon;" + Pattern R1_RHS = tx.graql().parser().parsePattern("{$x isa criminal;}"); tx.putRule("R1: It is a crime for an American to sell weapons to hostile nations" , R1_LHS, R1_RHS); Pattern R2_LHS = tx.graql().parser().parsePattern("{$x isa missile;}"); Pattern R2_RHS = tx.graql().parser().parsePattern("{$x isa weapon;}"); tx.putRule("R2: Missiles are a kind of a weapon\"" , R2_LHS, R2_RHS); Pattern R3_LHS = tx.graql().parser().parsePattern("{$x isa country;" + "($x, $y) isa is-enemy-of;" + "$y isa country;$y has name 'America';}"); Pattern R3_RHS = tx.graql().parser().parsePattern("{$x has alignment 'hostile';}"); tx.putRule("R3: If a country is an enemy of America then it is hostile" , R3_LHS, R3_RHS); Pattern R4_LHS = tx.graql().parser().parsePattern("{$x isa rocket;$x has propulsion 'gsp';}"); Pattern R4_RHS = tx.graql().parser().parsePattern("{$x isa missile;}"); tx.putRule("R4: If a rocket is self-propelled and guided, it is a missile" , R4_LHS, R4_RHS); Pattern R5_LHS = tx.graql().parser().parsePattern("{$x isa person;" + "$y isa country;" + "$z isa weapon;" + Pattern R5_RHS = tx.graql().parser().parsePattern("{(seller: $x, buyer: $y, transaction-item: $z) isa transaction;}"); tx.putRule("R5: If a country pays a person and that country now owns a weapon then the person has sold the country a weapon" , R5_LHS, R5_RHS);
@Nullable @Override public final Pattern then() { ConceptProto.Method.Req method = ConceptProto.Method.Req.newBuilder() .setRuleThenReq(ConceptProto.Rule.Then.Req.getDefaultInstance()).build(); ConceptProto.Rule.Then.Res response = runMethod(method).getRuleThenRes(); switch (response.getResCase()) { case NULL: return null; case PATTERN: return Graql.parser().parsePattern(response.getPattern()); default: throw CommonUtil.unreachableStatement("Unexpected response " + response); } }
@Nullable @Override public final Pattern when() { ConceptProto.Method.Req method = ConceptProto.Method.Req.newBuilder() .setRuleWhenReq(ConceptProto.Rule.When.Req.getDefaultInstance()).build(); ConceptProto.Rule.When.Res response = runMethod(method).getRuleWhenRes(); switch (response.getResCase()) { case NULL: return null; case PATTERN: return Graql.parser().parsePattern(response.getPattern()); default: throw CommonUtil.unreachableStatement("Unexpected response " + response); } }