Example usage for edu.stanford.nlp.semgraph.semgrex SemgrexPattern compile

List of usage examples for edu.stanford.nlp.semgraph.semgrex SemgrexPattern compile

Introduction

In this page you can find the example usage for edu.stanford.nlp.semgraph.semgrex SemgrexPattern compile.

Prototype

public static SemgrexPattern compile(String semgrex) 

Source Link

Usage

From source file:opendial.datastructs.Template.java

License:Open Source License

public SemgrexTemplate(String value) {
    this.rawString = value;
    slots = StringUtils.getSlots(value).keySet();
    String semgrex = StringUtils.constructSemgrex(value);
    pattern = SemgrexPattern.compile(semgrex);
}

From source file:org.wso2.extension.siddhi.gpl.execution.nlp.RelationshipByRegexStreamProcessor.java

License:Open Source License

@Override
protected List<Attribute> init(AbstractDefinition inputDefinition,
        ExpressionExecutor[] attributeExpressionExecutors, ConfigReader configReader,
        SiddhiAppContext siddhiAppContext) {
    if (logger.isDebugEnabled()) {
        logger.debug("Initializing Query ...");
    }/*from  ww  w.j  a va 2s .c o  m*/

    if (attributeExpressionLength < 2) {
        throw new SiddhiAppCreationException(
                "Query expects at least two parameters. Received only " + attributeExpressionLength
                        + ".\nUsage: #nlp.findRelationshipByRegex(regex:string, text:string-variable)");
    }

    String regex;
    try {
        if (attributeExpressionExecutors[0] instanceof ConstantExpressionExecutor) {
            regex = (String) attributeExpressionExecutors[0].execute(null);
        } else {
            throw new SiddhiAppCreationException("First parameter should be a constant."
                    + ".\nUsage: #nlp.findRelationshipByRegex(regex:string, text:string-variable)");
        }
    } catch (ClassCastException e) {
        throw new SiddhiAppCreationException("First parameter should be of type string. Found "
                + attributeExpressionExecutors[0].getReturnType()
                + ".\nUsage: #nlp.findRelationshipByRegex(regex:string, text:string-variable)");
    }

    try {
        regexPattern = SemgrexPattern.compile(regex);
    } catch (SemgrexParseException e) {
        throw new SiddhiAppCreationException("Cannot parse given regex: " + regex, e);
    }

    Set<String> namedNodeSet = new HashSet<String>();
    Pattern validationPattern = Pattern.compile(validationRegex);
    Matcher validationMatcher = validationPattern.matcher(regex);
    while (validationMatcher.find()) {
        //group 1 of the matcher gives the node name
        namedNodeSet.add(validationMatcher.group(1).trim());
    }

    if (!namedNodeSet.contains(Constants.SUBJECT)) {
        throw new SiddhiAppCreationException("Given regex " + regex
                + " does not contain a named node as subject. " + "Expect a node named as {}=subject");
    }

    if (!namedNodeSet.contains(Constants.OBJECT)) {
        throw new SiddhiAppCreationException("Given regex " + regex
                + " does not contain a named node as object. " + "Expect a node named as {}=object");
    }

    if (!namedNodeSet.contains(Constants.VERB)) {
        throw new SiddhiAppCreationException("Given regex " + regex
                + " does not contain a named node as verb. Expect" + " a node named as {}=verb");
    }

    if (!(attributeExpressionExecutors[1] instanceof VariableExpressionExecutor)) {
        throw new SiddhiAppCreationException("Second parameter should be a variable."
                + ".\nUsage: #nlp.findRelationshipByRegex(regex:string, text:string-variable)");
    }

    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Query parameters initialized. Regex: %s Stream Parameters: %s", regex,
                inputDefinition.getAttributeList()));
    }

    initPipeline();
    ArrayList<Attribute> attributes = new ArrayList<Attribute>(1);
    attributes.add(new Attribute(Constants.SUBJECT, Attribute.Type.STRING));
    attributes.add(new Attribute(Constants.OBJECT, Attribute.Type.STRING));
    attributes.add(new Attribute(Constants.VERB, Attribute.Type.STRING));
    return attributes;
}

From source file:org.wso2.extension.siddhi.gpl.execution.nlp.RelationshipByVerbStreamProcessor.java

License:Open Source License

@Override
protected List<Attribute> init(AbstractDefinition inputDefinition,
        ExpressionExecutor[] attributeExpressionExecutors, ConfigReader configReader,
        SiddhiAppContext siddhiAppContext) {
    if (logger.isDebugEnabled()) {
        logger.debug("Initializing Query ...");
    }/*  w  w  w.j  a va 2 s  .  com*/

    if (attributeExpressionLength < 2) {
        throw new SiddhiAppCreationException(
                "Query expects at least two parameters. Received only " + attributeExpressionLength
                        + ".\nUsage: #nlp.findRelationshipByVerb(verb:string, text:string-variable)");
    }

    String verb;
    try {
        if (attributeExpressionExecutors[0] instanceof ConstantExpressionExecutor) {
            verb = (String) attributeExpressionExecutors[0].execute(null);
        } else {
            throw new SiddhiAppCreationException("First parameter should be a constant."
                    + ".\nUsage: #nlp.findRelationshipByVerb(verb:string, text:string-variable)");
        }
    } catch (ClassCastException e) {
        throw new SiddhiAppCreationException("First parameter should be of type string. Found "
                + attributeExpressionExecutors[0].getReturnType()
                + ".\nUsage: #nlp.findRelationshipByVerb(verb:string, text:string-variable)");
    }

    try {
        verbOptSubPattern = SemgrexPattern.compile(String.format(verbOptSub, verb));
        verbOptObjPattern = SemgrexPattern.compile(String.format(verbOptObj, verb));
    } catch (SemgrexParseException e) {
        throw new SiddhiAppCreationException("First parameter is not a verb. Found " + verb
                + "\nUsage: #nlp.findRelationshipByVerb(verb:string, text:string-variable)");
    }

    if (!(attributeExpressionExecutors[1] instanceof VariableExpressionExecutor)) {
        throw new SiddhiAppCreationException("Second parameter should be a variable."
                + ".\nUsage: #nlp.findRelationshipByVerb(verb:string, text:string-variable)");
    }

    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Query parameters initialized. verb: %s Stream Parameters: %s", verb,
                inputDefinition.getAttributeList()));
    }

    initPipeline();
    ArrayList<Attribute> attributes = new ArrayList<Attribute>(1);
    attributes.add(new Attribute(Constants.SUBJECT, Attribute.Type.STRING));
    attributes.add(new Attribute(Constants.OBJECT, Attribute.Type.STRING));
    attributes.add(new Attribute(Constants.VERB, Attribute.Type.STRING));
    return attributes;
}

From source file:org.wso2.extension.siddhi.gpl.execution.nlp.SemgrexPatternStreamProcessor.java

License:Open Source License

@Override
protected List<Attribute> init(AbstractDefinition inputDefinition,
        ExpressionExecutor[] attributeExpressionExecutors, ConfigReader configReader,
        SiddhiAppContext siddhiAppContext) {
    if (logger.isDebugEnabled()) {
        logger.debug("Initializing Query ...");
    }/*from   w w w.  j av  a2  s.c  o m*/

    if (attributeExpressionLength < 2) {
        throw new SiddhiAppCreationException(
                "Query expects at least two parameters. Received only " + attributeExpressionLength
                        + ".\nUsage: #nlp.findSemgrexPattern(regex:string, text:string-variable)");
    }

    String regex;
    try {
        if (attributeExpressionExecutors[0] instanceof ConstantExpressionExecutor) {
            regex = (String) attributeExpressionExecutors[0].execute(null);
        } else {
            throw new SiddhiAppCreationException("First parameter should be a constant."
                    + ".\nUsage: #nlp.findSemgrexPattern(regex:string, text:string-variable)");
        }
    } catch (ClassCastException e) {
        throw new SiddhiAppCreationException("First parameter should be of type string. Found "
                + attributeExpressionExecutors[0].getReturnType()
                + ".\nUsage: #nlp.findSemgrexPattern(regex:string, text:string-variable)");
    }

    try {
        regexPattern = SemgrexPattern.compile(regex);
    } catch (SemgrexParseException e) {
        throw new SiddhiAppCreationException("Cannot parse given regex: " + regex, e);
    }

    if (!(attributeExpressionExecutors[1] instanceof VariableExpressionExecutor)) {
        throw new SiddhiAppCreationException("Second parameter should be a variable."
                + ".\nUsage: #nlp.findSemgrexPattern(regex:string, text:string-variable)");
    }

    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Query parameters initialized. Regex: %s Stream Parameters: %s", regex,
                inputDefinition.getAttributeList()));
    }

    initPipeline();

    ArrayList<Attribute> attributes = new ArrayList<Attribute>(1);
    attributes.add(new Attribute("match", Attribute.Type.STRING));

    // Find all named elements in the regular expression and add them to the output stream definition attributes
    Set<String> namedElementSet = new HashSet<String>();
    Pattern validationPattern = Pattern.compile(validationRegex);
    Matcher validationMatcher = validationPattern.matcher(regex);
    while (validationMatcher.find()) {
        //group 1 of the matcher gives the node name or the relation name
        namedElementSet.add(validationMatcher.group(1).trim());
    }

    attributeCount = 1;
    for (String namedElement : namedElementSet) {
        attributes.add(new Attribute(namedElement, Attribute.Type.STRING));
        namedElementParamPositions.put(namedElement, attributeCount++);
    }
    return attributes;
}

From source file:org.wso2.gpl.siddhi.extension.nlp.RelationshipByRegexStreamProcessor.java

License:Open Source License

@Override
protected List<Attribute> init(AbstractDefinition abstractDefinition,
        ExpressionExecutor[] attributeExpressionExecutors, ExecutionPlanContext executionPlanContext) {
    if (logger.isDebugEnabled()) {
        logger.debug("Initializing Query ...");
    }// www .j a  v  a 2 s. c  om

    if (attributeExpressionLength < 2) {
        throw new ExecutionPlanCreationException(
                "Query expects at least two parameters. Received only " + attributeExpressionLength
                        + ".\nUsage: #nlp.findRelationshipByRegex(regex:string, text:string-variable)");
    }

    String regex;
    try {
        if (attributeExpressionExecutors[0] instanceof ConstantExpressionExecutor) {
            regex = (String) attributeExpressionExecutors[0].execute(null);
        } else {
            throw new ExecutionPlanCreationException("First parameter should be a constant."
                    + ".\nUsage: #nlp.findRelationshipByRegex(regex:string, text:string-variable)");
        }
    } catch (ClassCastException e) {
        throw new ExecutionPlanCreationException("First parameter should be of type string. Found "
                + attributeExpressionExecutors[0].getReturnType()
                + ".\nUsage: #nlp.findRelationshipByRegex(regex:string, text:string-variable)");
    }

    try {
        regexPattern = SemgrexPattern.compile(regex);
    } catch (SemgrexParseException e) {
        throw new ExecutionPlanCreationException("Cannot parse given regex: " + regex, e);
    }

    Set<String> namedNodeSet = new HashSet<String>();
    Pattern validationPattern = Pattern.compile(validationRegex);
    Matcher validationMatcher = validationPattern.matcher(regex);
    while (validationMatcher.find()) {
        //group 1 of the matcher gives the node name
        namedNodeSet.add(validationMatcher.group(1).trim());
    }

    if (!namedNodeSet.contains(Constants.subject)) {
        throw new ExecutionPlanCreationException("Given regex " + regex
                + " does not contain a named node as subject. " + "Expect a node named as {}=subject");
    }

    if (!namedNodeSet.contains(Constants.object)) {
        throw new ExecutionPlanCreationException("Given regex " + regex
                + " does not contain a named node as object. " + "Expect a node named as {}=object");
    }

    if (!namedNodeSet.contains(Constants.verb)) {
        throw new ExecutionPlanCreationException("Given regex " + regex
                + " does not contain a named node as verb. Expect" + " a node named as {}=verb");
    }

    if (!(attributeExpressionExecutors[1] instanceof VariableExpressionExecutor)) {
        throw new ExecutionPlanCreationException("Second parameter should be a variable."
                + ".\nUsage: #nlp.findRelationshipByRegex(regex:string, text:string-variable)");
    }

    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Query parameters initialized. Regex: %s Stream Parameters: %s", regex,
                abstractDefinition.getAttributeList()));
    }

    initPipeline();
    ArrayList<Attribute> attributes = new ArrayList<Attribute>(1);
    attributes.add(new Attribute(Constants.subject, Attribute.Type.STRING));
    attributes.add(new Attribute(Constants.object, Attribute.Type.STRING));
    attributes.add(new Attribute(Constants.verb, Attribute.Type.STRING));
    return attributes;
}

From source file:org.wso2.gpl.siddhi.extension.nlp.RelationshipByVerbStreamProcessor.java

License:Open Source License

@Override
protected List<Attribute> init(AbstractDefinition abstractDefinition,
        ExpressionExecutor[] attributeExpressionExecutors, ExecutionPlanContext executionPlanContext) {
    if (logger.isDebugEnabled()) {
        logger.debug("Initializing Query ...");
    }/*from   w ww .  jav  a  2s. c o m*/

    if (attributeExpressionLength < 2) {
        throw new ExecutionPlanCreationException(
                "Query expects at least two parameters. Received only " + attributeExpressionLength
                        + ".\nUsage: #nlp.findRelationshipByVerb(verb:string, text:string-variable)");
    }

    String verb;
    try {
        if (attributeExpressionExecutors[0] instanceof ConstantExpressionExecutor) {
            verb = (String) attributeExpressionExecutors[0].execute(null);
        } else {
            throw new ExecutionPlanCreationException("First parameter should be a constant."
                    + ".\nUsage: #nlp.findRelationshipByVerb(verb:string, text:string-variable)");
        }
    } catch (ClassCastException e) {
        throw new ExecutionPlanCreationException("First parameter should be of type string. Found "
                + attributeExpressionExecutors[0].getReturnType()
                + ".\nUsage: #nlp.findRelationshipByVerb(verb:string, text:string-variable)");
    }

    try {
        verbOptSubPattern = SemgrexPattern.compile(String.format(verbOptSub, verb));
        verbOptObjPattern = SemgrexPattern.compile(String.format(verbOptObj, verb));
    } catch (SemgrexParseException e) {
        throw new ExecutionPlanCreationException("First parameter is not a verb. Found " + verb
                + "\nUsage: #nlp.findRelationshipByVerb(verb:string, text:string-variable)");
    }

    if (!(attributeExpressionExecutors[1] instanceof VariableExpressionExecutor)) {
        throw new ExecutionPlanCreationException("Second parameter should be a variable."
                + ".\nUsage: #nlp.findRelationshipByVerb(verb:string, text:string-variable)");
    }

    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Query parameters initialized. verb: %s Stream Parameters: %s", verb,
                abstractDefinition.getAttributeList()));
    }

    initPipeline();
    ArrayList<Attribute> attributes = new ArrayList<Attribute>(1);
    attributes.add(new Attribute(Constants.subject, Attribute.Type.STRING));
    attributes.add(new Attribute(Constants.object, Attribute.Type.STRING));
    attributes.add(new Attribute(Constants.verb, Attribute.Type.STRING));
    return attributes;
}

From source file:org.wso2.gpl.siddhi.extension.nlp.SemgrexPatternStreamProcessor.java

License:Open Source License

@Override
protected List<Attribute> init(AbstractDefinition abstractDefinition,
        ExpressionExecutor[] attributeExpressionExecutors, ExecutionPlanContext executionPlanContext) {
    if (logger.isDebugEnabled()) {
        logger.debug("Initializing Query ...");
    }//ww  w .  j a  v a 2s.  c om

    if (attributeExpressionLength < 2) {
        throw new ExecutionPlanCreationException(
                "Query expects at least two parameters. Received only " + attributeExpressionLength
                        + ".\nUsage: #nlp.findSemgrexPattern(regex:string, text:string-variable)");
    }

    String regex;
    try {
        if (attributeExpressionExecutors[0] instanceof ConstantExpressionExecutor) {
            regex = (String) attributeExpressionExecutors[0].execute(null);
        } else {
            throw new ExecutionPlanCreationException("First parameter should be a constant."
                    + ".\nUsage: #nlp.findSemgrexPattern(regex:string, text:string-variable)");
        }
    } catch (ClassCastException e) {
        throw new ExecutionPlanCreationException("First parameter should be of type string. Found "
                + attributeExpressionExecutors[0].getReturnType()
                + ".\nUsage: #nlp.findSemgrexPattern(regex:string, text:string-variable)");
    }

    try {
        regexPattern = SemgrexPattern.compile(regex);
    } catch (SemgrexParseException e) {
        throw new ExecutionPlanCreationException("Cannot parse given regex: " + regex, e);
    }

    if (!(attributeExpressionExecutors[1] instanceof VariableExpressionExecutor)) {
        throw new ExecutionPlanCreationException("Second parameter should be a variable."
                + ".\nUsage: #nlp.findSemgrexPattern(regex:string, text:string-variable)");
    }

    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Query parameters initialized. Regex: %s Stream Parameters: %s", regex,
                abstractDefinition.getAttributeList()));
    }

    initPipeline();

    ArrayList<Attribute> attributes = new ArrayList<Attribute>(1);
    attributes.add(new Attribute("match", Attribute.Type.STRING));

    // Find all named elements in the regular expression and add them to the output stream definition attributes
    Set<String> namedElementSet = new HashSet<String>();
    Pattern validationPattern = Pattern.compile(validationRegex);
    Matcher validationMatcher = validationPattern.matcher(regex);
    while (validationMatcher.find()) {
        //group 1 of the matcher gives the node name or the relation name
        namedElementSet.add(validationMatcher.group(1).trim());
    }

    attributeCount = 1;
    for (String namedElement : namedElementSet) {
        attributes.add(new Attribute(namedElement, Attribute.Type.STRING));
        namedElementParamPositions.put(namedElement, attributeCount++);
    }
    return attributes;
}

From source file:org.wso2.siddhi.extension.nlp.RelationshipByRegexTransformProcessor.java

License:Open Source License

@Override
protected void init(Expression[] expressions, List<ExpressionExecutor> expressionExecutors,
        StreamDefinition inStreamDefinition, StreamDefinition outStreamDefinition, String elementId,
        SiddhiContext siddhiContext) {/* w ww  .  jav  a  2  s .  c  o m*/
    if (logger.isDebugEnabled()) {
        logger.debug("Initializing Query ...");
    }

    if (expressions.length < 2) {
        throw new QueryCreationException(
                "Query expects at least two parameters. Received only " + expressions.length + ".\n"
                        + "Usage: findRelationshipByRegex(regex:string, text:string-variable)");
    }

    String regex;
    try {
        regex = ((StringConstant) expressions[0]).getValue();
    } catch (ClassCastException e) {
        logger.error("Error in reading parameter regex", e);
        throw new QueryCreationException(
                "First parameter should be of type string. Found " + Constants.getType(expressions[0]) + ".\n"
                        + "Usage: findRelationshipByRegex(regex:string, text:string-variable)");
    }

    try {
        regexPattern = SemgrexPattern.compile(regex);
    } catch (SemgrexParseException e) {
        logger.error("Error in parsing semgrex pattern", e);
        throw new QueryCreationException(
                "Cannot parse given regex: " + regex + " Error: [" + e.getMessage() + "]");
    }

    Set<String> namedNodeSet = new HashSet<String>();
    Pattern validationPattern = Pattern.compile(validationRegex);
    Matcher validationMatcher = validationPattern.matcher(regex);
    while (validationMatcher.find()) {
        //group 1 of the matcher gives the node name
        namedNodeSet.add(validationMatcher.group(1).trim());
    }

    if (!namedNodeSet.contains(Constants.subject)) {
        throw new QueryCreationException("Given regex " + regex + " does not contain a named node as subject. "
                + "Expect a node named as {}=subject");
    }

    if (!namedNodeSet.contains(Constants.object)) {
        throw new QueryCreationException("Given regex " + regex + " does not contain a named node as object. "
                + "Expect a node named as {}=object");
    }

    if (!namedNodeSet.contains(Constants.verb)) {
        throw new QueryCreationException("Given regex " + regex
                + " does not contain a named node as verb. Expect" + " a node named as {}=verb");
    }

    if (expressions[1] instanceof Variable) {
        inStreamParamPosition = inStreamDefinition
                .getAttributePosition(((Variable) expressions[1]).getAttributeName());
    } else {
        throw new QueryCreationException(
                "Second parameter should be a variable. Found " + Constants.getType(expressions[1]) + ".\n"
                        + "Usage: findRelationshipByRegex(regex:string, text:string-variable)");
    }

    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Query parameters initialized. Regex: %s Stream Parameters: %s", regex,
                inStreamDefinition.getAttributeList()));
    }

    initPipeline();

    if (outStreamDefinition == null) {
        this.outStreamDefinition = new StreamDefinition().name("relationshipByRegexMatchStream");

        this.outStreamDefinition.attribute(Constants.subject, Attribute.Type.STRING);
        this.outStreamDefinition.attribute(Constants.object, Attribute.Type.STRING);
        this.outStreamDefinition.attribute(Constants.verb, Attribute.Type.STRING);

        for (Attribute strDef : inStreamDefinition.getAttributeList()) {
            this.outStreamDefinition.attribute(strDef.getName(), strDef.getType());
        }
    }
}

From source file:org.wso2.siddhi.extension.nlp.RelationshipByVerbTransformProcessor.java

License:Open Source License

@Override
protected void init(Expression[] expressions, List<ExpressionExecutor> expressionExecutors,
        StreamDefinition inStreamDefinition, StreamDefinition outStreamDefinition, String elementId,
        SiddhiContext siddhiContext) {/*from  w w  w.  ja  v  a 2s . c o m*/
    if (logger.isDebugEnabled()) {
        logger.debug("Initializing Query ...");
    }

    if (expressions.length < 2) {
        throw new QueryCreationException(
                "Query expects at least two parameters. Received only " + expressions.length + ".\n"
                        + "Usage: findRelationshipByVerb(verb:string, text:string-variable)");
    }

    try {
        verb = ((StringConstant) expressions[0]).getValue();
    } catch (ClassCastException e) {
        logger.error("Error in reading parameter verb", e);
        throw new QueryCreationException(
                "First parameter should be of type string. Found " + Constants.getType(expressions[0]) + "\n"
                        + "Usage: findRelationshipByVerb(verb:string, text:string-variable)");
    }

    try {
        regexOptSubPattern = SemgrexPattern.compile(String.format(regexOptSub, verb));
        regexOptObjPattern = SemgrexPattern.compile(String.format(regexOptObj, verb));
    } catch (SemgrexParseException e) {
        logger.error("Error in initializing relation extracting pattern for verb", e);
        throw new QueryCreationException("First parameter is not a verb. Found " + verb + "\n"
                + "Usage: findRelationshipByVerb(verb:string, text:string-variable)");
    }

    if (expressions[1] instanceof Variable) {
        inStreamParamPosition = inStreamDefinition
                .getAttributePosition(((Variable) expressions[1]).getAttributeName());
    } else {
        throw new QueryCreationException(
                "Second parameter should be a variable. Found " + Constants.getType(expressions[1]) + "\n"
                        + "Usage: findRelationshipByVerb(verb:string, text:string-variable)");
    }

    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Query parameters initialized. Verb: %s Stream Parameters: %s", verb,
                inStreamDefinition.getAttributeList()));
    }

    initPipeline();

    if (outStreamDefinition == null) {
        this.outStreamDefinition = new StreamDefinition().name("relationshipByVerbMatchStream");

        this.outStreamDefinition.attribute(Constants.subject, Attribute.Type.STRING);
        this.outStreamDefinition.attribute(Constants.object, Attribute.Type.STRING);
        this.outStreamDefinition.attribute(Constants.verb, Attribute.Type.STRING);

        for (Attribute strDef : inStreamDefinition.getAttributeList()) {
            this.outStreamDefinition.attribute(strDef.getName(), strDef.getType());
        }
    }
}

From source file:org.wso2.siddhi.extension.nlp.SemgrexPatternTransformProcessor.java

License:Open Source License

@Override
protected void init(Expression[] expressions, List<ExpressionExecutor> expressionExecutors,
        StreamDefinition inStreamDefinition, StreamDefinition outStreamDefinition, String elementId,
        SiddhiContext siddhiContext) {/*w w  w .  ja  v  a2  s. c om*/
    if (logger.isDebugEnabled()) {
        logger.debug("Initializing Query ...");
    }

    if (expressions.length < 2) {
        throw new QueryCreationException("Query expects at least two parameters. Received only "
                + expressions.length + ".\n" + "Usage: findSemgrexPattern(regex:string, text:string)");
    }

    String regex;
    try {
        regex = ((StringConstant) expressions[0]).getValue();
    } catch (ClassCastException e) {
        logger.error("Error in reading parameter regex", e);
        throw new QueryCreationException(
                "First parameter should be of type string. Found " + Constants.getType(expressions[0]) + ".\n"
                        + "Usage: findSemgrexPattern(regex:string, text:string)");
    }

    try {
        regexPattern = SemgrexPattern.compile(regex);
    } catch (SemgrexParseException e) {
        logger.error("Error in parsing semgrex pattern", e);
        throw new QueryCreationException(
                "Cannot parse given regex " + regex + " Error: [" + e.getMessage() + "]");
    }

    if (expressions[1] instanceof Variable) {
        inStreamParamPosition = inStreamDefinition
                .getAttributePosition(((Variable) expressions[1]).getAttributeName());
    } else {
        throw new QueryCreationException(
                "Second parameter should be a variable. Found " + Constants.getType(expressions[1]) + ".\n"
                        + "Usage: findSemgrexPattern(regex:string, text:string)");
    }

    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Query parameters initialized. Regex: %s Stream Parameters: %s", regex,
                inStreamDefinition.getAttributeList()));
    }

    initPipeline();

    if (outStreamDefinition == null) {

        this.outStreamDefinition = new StreamDefinition().name("semgrexPatternMatchStream");

        this.outStreamDefinition.attribute("match", Attribute.Type.STRING);

        // Find all named elements in the regular expression and add them to the output stream definition attributes
        Set<String> namedElementSet = new HashSet<String>();
        Pattern validationPattern = Pattern.compile(validationRegex);
        Matcher validationMatcher = validationPattern.matcher(regex);
        while (validationMatcher.find()) {
            //group 1 of the matcher gives the node name or the relation name
            namedElementSet.add(validationMatcher.group(1).trim());
        }

        attributeCount = 1;
        for (String namedElement : namedElementSet) {
            this.outStreamDefinition.attribute(namedElement, Attribute.Type.STRING);
            namedElementParamPositions.put(namedElement, attributeCount);
            attributeCount++;
        }

        for (Attribute strDef : inStreamDefinition.getAttributeList()) {
            this.outStreamDefinition.attribute(strDef.getName(), strDef.getType());
        }
    }
}