Search in sources :

Example 1 with ScenarioDefinitionException

use of com.vaticle.typedb.core.test.behaviour.exception.ScenarioDefinitionException in project grakn by graknlabs.

the class TypeQLSteps method applyQueryTemplate.

private String applyQueryTemplate(String template, ConceptMap templateFiller) {
    // find shortest matching strings between <>
    Pattern pattern = Pattern.compile("<.+?>");
    Matcher matcher = pattern.matcher(template);
    StringBuilder builder = new StringBuilder();
    int i = 0;
    while (matcher.find()) {
        String matched = matcher.group(0);
        String requiredVariable = variableFromTemplatePlaceholder(matched.substring(1, matched.length() - 1));
        builder.append(template, i, matcher.start());
        if (templateFiller.contains(Reference.name(requiredVariable))) {
            Concept concept = templateFiller.get(requiredVariable);
            if (!concept.isThing())
                throw new ScenarioDefinitionException("Cannot apply IID templating to Type concepts");
            String conceptId = concept.asThing().getIID().toHexString();
            builder.append(conceptId);
        } else {
            throw new ScenarioDefinitionException(String.format("No IID available for template placeholder: %s.", matched));
        }
        i = matcher.end();
    }
    builder.append(template.substring(i));
    return builder.toString();
}
Also used : Concept(com.vaticle.typedb.core.concept.Concept) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ScenarioDefinitionException(com.vaticle.typedb.core.test.behaviour.exception.ScenarioDefinitionException)

Aggregations

Concept (com.vaticle.typedb.core.concept.Concept)1 ScenarioDefinitionException (com.vaticle.typedb.core.test.behaviour.exception.ScenarioDefinitionException)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1