Java Code Examples for org.antlr.runtime.tree.Tree#getText()

The following examples show how to use org.antlr.runtime.tree.Tree#getText() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FunctionBlock.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Parse a tree for piece-wice linear membership function
 * @param tree : Tree to parse
 * @return A new membership function
 */
private MembershipFunction fclTreeFuzzifyTermPieceWiseLinear(Tree tree) {
	if (debug) Gpr.debug("Tree: " + tree.toStringTree());
	int numberOfPoints = tree.getChildCount() - 1;
	if (debug) Gpr.debug("\tNumber of points: " + numberOfPoints);

	Value x[] = new Value[numberOfPoints];
	Value y[] = new Value[numberOfPoints];
	for (int childNum = 1; childNum < tree.getChildCount(); childNum++) {
		Tree child = tree.getChild(childNum);
		if (debug) Gpr.debug("\t\tChild: " + child.toStringTree());
		String leaveName = child.getText();

		// It's a set of points? => Defines a piece-wise linear membership function
		if (leaveName.equalsIgnoreCase("POINT")) {
			x[childNum - 1] = new Value(child.getChild(0), this); // Parse and add each point
			y[childNum - 1] = new Value(child.getChild(1), this);
			if (debug) Gpr.debug("\t\tParsed point " + childNum + " x=" + x[childNum - 1] + ", y=" + y[childNum - 1]);
			if ((y[childNum - 1].getValue() < 0) || (y[childNum - 1].getValue() > 1)) throw new RuntimeException("\n\tError parsing line " + child.getLine() + " character " + child.getCharPositionInLine() + ": Membership function out of range (should be between 0 and 1). Value: '" + y[childNum - 1] + "'\n\tTree: " + child.toStringTree());
		} else throw new RuntimeException("Unknown (or unimplemented) option : " + leaveName);
	}
	return new MembershipFunctionPieceWiseLinear(x, y);
}
 
Example 2
Source File: RuleBlock.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Parse rule Implication Method (or rule activation method)
 * @param tree : Tree to parse
 */
private void fclTreeRuleBlockRule(Tree tree, RuleConnectionMethod and, RuleConnectionMethod or) {
	if (debug) Gpr.debug("Tree: " + tree.toStringTree());
	Rule fuzzyRule = new Rule(tree.getChild(0).getText(), this);

	for (int childNum = 1; childNum < tree.getChildCount(); childNum++) {
		Tree child = tree.getChild(childNum);
		if (debug) Gpr.debug("\t\tChild: " + child.toStringTree());
		String type = child.getText();

		if (type.equalsIgnoreCase("IF")) fuzzyRule.setAntecedents(fclTreeRuleBlockRuleIf(child.getChild(0), and, or));
		else if (type.equalsIgnoreCase("THEN")) fclTreeRuleBlockRuleThen(child, fuzzyRule);
		else if (type.equalsIgnoreCase("WITH")) fclTreeRuleBlockRuleWith(child, fuzzyRule);
		else throw new RuntimeException("Unknown (or unimplemented) rule block item: " + type);
	}

	add(fuzzyRule);
}
 
Example 3
Source File: HL7Query.java    From nifi with Apache License 2.0 6 votes vote down vote up
private Evaluator<?> buildReferenceEvaluator(final Tree tree) {
    switch (tree.getType()) {
        case MESSAGE:
            return new MessageEvaluator();
        case SEGMENT_NAME:
            return new SegmentEvaluator(new StringLiteralEvaluator(tree.getText()));
        case IDENTIFIER:
            return new DeclaredReferenceEvaluator(new StringLiteralEvaluator(tree.getText()));
        case DOT:
            final Tree firstChild = tree.getChild(0);
            final Tree secondChild = tree.getChild(1);
            return new DotEvaluator(buildReferenceEvaluator(firstChild), buildIntegerEvaluator(secondChild));
        case STRING_LITERAL:
            return new StringLiteralEvaluator(tree.getText());
        case NUMBER:
            return new IntegerLiteralEvaluator(Integer.parseInt(tree.getText()));
        default:
            throw new HL7QueryParsingException("Failed to build evaluator for " + tree.getText());
    }
}
 
Example 4
Source File: IgniteQueryResolverDelegate.java    From hibernate-ogm-ignite with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void registerJoinAlias(Tree aliasNode, PropertyPath path) {
	String alias = aliasNode.getText();
	Type type = propertyHelper.getPropertyType(
		propertyHelper.getEntityNameByAlias( path.getFirstNode().getName() ),
		path.getNodeNamesWithoutAlias() );
	if ( type.isEntityType() ) {
		propertyHelper.registerEntityAlias( type.getName(), alias );
	}
	else if ( type.isAssociationType() ) {
		propertyHelper.registerEntityAlias(
			( (AssociationType) type ).getAssociatedEntityName( sessionFactory ), alias );
	}
	else {
		throw new IllegalArgumentException( "Failed to determine type for alias '" + alias + "'" );
	}
}
 
Example 5
Source File: FunctionBlock.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Parse a tree for singletons membership function series of points
 * @param tree : Tree to parse
 * @param numberOfPoints : Number of points in this function
 * @return A new membership function
 */
private MembershipFunction fclTreeFuzzifyTermSingletonsPoints(Tree tree, int numberOfPoints) {
	if (debug) Gpr.debug("Tree: " + tree.toStringTree());

	Value x[] = new Value[numberOfPoints];
	Value y[] = new Value[numberOfPoints];
	for (int childNum = 0; childNum < tree.getChildCount(); childNum++) {
		Tree child = tree.getChild(childNum);
		String leaveName = child.getText();
		if (debug) Gpr.debug("Sub-Parsing: " + leaveName);

		// It's a set of points? => Defines a piece-wise linear membership function
		if (leaveName.equalsIgnoreCase("(")) {
			x[childNum] = new Value(child.getChild(0), this); // Parse and add each point
			y[childNum] = new Value(child.getChild(1), this);

			if ((y[childNum].getValue() < 0) || (y[childNum].getValue() > 1)) throw new RuntimeException("\n\tError parsing line " + child.getLine() + " character " + child.getCharPositionInLine() + ": Membership function out of range (should be between 0 and 1). Value: '" + y[childNum] + "'\n\tTree: " + child.toStringTree());

			if (debug) Gpr.debug("Parsed point " + childNum + " x=" + x[childNum] + ", y=" + y[childNum]);
		} else throw new RuntimeException("Unknown (or unimplemented) option : " + leaveName);
	}
	return new MembershipFunctionGenericSingleton(x, y);
}
 
Example 6
Source File: HL7Query.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
private Evaluator<?> buildReferenceEvaluator(final Tree tree) {
    switch (tree.getType()) {
        case MESSAGE:
            return new MessageEvaluator();
        case SEGMENT_NAME:
            return new SegmentEvaluator(new StringLiteralEvaluator(tree.getText()));
        case IDENTIFIER:
            return new DeclaredReferenceEvaluator(new StringLiteralEvaluator(tree.getText()));
        case DOT:
            final Tree firstChild = tree.getChild(0);
            final Tree secondChild = tree.getChild(1);
            return new DotEvaluator(buildReferenceEvaluator(firstChild), buildIntegerEvaluator(secondChild));
        case STRING_LITERAL:
            return new StringLiteralEvaluator(tree.getText());
        case NUMBER:
            return new IntegerLiteralEvaluator(Integer.parseInt(tree.getText()));
        default:
            throw new HL7QueryParsingException("Failed to build evaluator for " + tree.getText());
    }
}
 
Example 7
Source File: FunctionBlock.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Parse a tree for "Fuzzify" item
 * @param tree : Tree to parse
 * @return Variable (old or created)
 */
private Variable fclTreeFuzzify(Tree tree) {
	Gpr.checkRootNode("FUZZIFY", tree);
	if (debug) Gpr.debug("Tree: " + tree.toStringTree());
	Tree child = tree.getChild(0);
	String varName = child.getText();

	// Get variable (or create a new one)
	Variable variable = getVariable(varName);
	if (variable == null) {
		variable = new Variable(varName);
		setVariable(varName, variable);
		if (debug) Gpr.debug("Variable '" + varName + "' does not exist => Creating it");
	}

	// Explore each sibling in this level
	for (int childNum = 1; childNum < tree.getChildCount(); childNum++) {
		child = tree.getChild(childNum);
		if (debug) Gpr.debug("\t\tChild: " + child.toStringTree());
		String leaveName = child.getText();

		if (leaveName.equalsIgnoreCase("TERM")) {
			LinguisticTerm linguisticTerm = fclTreeFuzzifyTerm(child, variable);
			variable.add(linguisticTerm);
		} else throw new RuntimeException("Unknown/Unimplemented item '" + leaveName + "'");
	}

	return variable;
}
 
Example 8
Source File: HL7Query.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
private BooleanEvaluator buildBooleanEvaluator(final Tree tree) {
    // TODO: add Date comparisons
    // LT/GT/GE/GE should allow for dates based on Field's Type
    // BETWEEN
    // DATE('2015/01/01')
    // DATE('2015/01/01 12:00:00')
    // DATE('24 HOURS AGO')
    // DATE('YESTERDAY')

    switch (tree.getType()) {
        case EQUALS:
            return new EqualsEvaluator(buildReferenceEvaluator(tree.getChild(0)), buildReferenceEvaluator(tree.getChild(1)));
        case NOT_EQUALS:
            return new NotEqualsEvaluator(buildReferenceEvaluator(tree.getChild(0)), buildReferenceEvaluator(tree.getChild(1)));
        case GT:
            return new GreaterThanEvaluator(buildReferenceEvaluator(tree.getChild(0)), buildReferenceEvaluator(tree.getChild(1)));
        case LT:
            return new LessThanEvaluator(buildReferenceEvaluator(tree.getChild(0)), buildReferenceEvaluator(tree.getChild(1)));
        case GE:
            return new GreaterThanOrEqualEvaluator(buildReferenceEvaluator(tree.getChild(0)), buildReferenceEvaluator(tree.getChild(1)));
        case LE:
            return new LessThanOrEqualEvaluator(buildReferenceEvaluator(tree.getChild(0)), buildReferenceEvaluator(tree.getChild(1)));
        case NOT:
            return new NotEvaluator(buildBooleanEvaluator(tree.getChild(0)));
        case AND:
            return new AndEvaluator(buildBooleanEvaluator(tree.getChild(0)), buildBooleanEvaluator(tree.getChild(1)));
        case OR:
            return new OrEvaluator(buildBooleanEvaluator(tree.getChild(0)), buildBooleanEvaluator(tree.getChild(1)));
        case IS_NULL:
            return new IsNullEvaluator(buildReferenceEvaluator(tree.getChild(0)));
        case NOT_NULL:
            return new NotNullEvaluator(buildReferenceEvaluator(tree.getChild(0)));
        default:
            throw new HL7QueryParsingException("Cannot build boolean evaluator for '" + tree.getText() + "'");
    }
}
 
Example 9
Source File: HL7Query.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
private IntegerEvaluator buildIntegerEvaluator(final Tree tree) {
    switch (tree.getType()) {
        case NUMBER:
            return new IntegerLiteralEvaluator(Integer.parseInt(tree.getText()));
        default:
            throw new HL7QueryParsingException("Failed to build Integer Evaluator for " + tree.getText());
    }
}
 
Example 10
Source File: FunctionBlock.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Parse a tree for "Variable" item (either input or output variables)
 * @param tree
 */
private void fclTreeVariables(Tree tree) {
	Gpr.checkRootNode("VAR_OUTPUT", "VAR_INPUT", tree);
	if (debug) Gpr.debug("Tree: " + tree.toStringTree());
	for (int childNum = 0; childNum < tree.getChildCount(); childNum++) {
		Tree child = tree.getChild(childNum);
		if (debug) Gpr.debug("\tChild: " + child.toStringTree());
		String varName = child.getText();
		Variable variable = new Variable(varName);
		if (debug) Gpr.debug("\tAdding variable: " + varName);

		// Set range?
		if (child.getChildCount() > 1) {
			Tree rangeTree = child.getChild(1);
			if (debug) Gpr.debug("\tRangeTree: " + rangeTree.toStringTree());
			double min = Gpr.parseDouble(rangeTree.getChild(0));
			double max = Gpr.parseDouble(rangeTree.getChild(1));

			if (debug) Gpr.debug("\tSetting universe to: [ " + min + " , " + max + " ]");
			variable.setUniverseMin(min);
			variable.setUniverseMax(max);
		}

		if (varibleExists(variable.getName())) if (debug) Gpr.debug("Warning: Variable '" + variable.getName() + "' duplicated");
		else setVariable(varName, variable); // OK? => Add variable
	}
}
 
Example 11
Source File: HL7Query.java    From nifi with Apache License 2.0 5 votes vote down vote up
private IntegerEvaluator buildIntegerEvaluator(final Tree tree) {
    switch (tree.getType()) {
        case NUMBER:
            return new IntegerLiteralEvaluator(Integer.parseInt(tree.getText()));
        default:
            throw new HL7QueryParsingException("Failed to build Integer Evaluator for " + tree.getText());
    }
}
 
Example 12
Source File: HL7Query.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
private HL7Query(final Tree tree, final String query) {
    this.tree = tree;
    this.query = query;

    List<Selection> select = null;
    BooleanEvaluator where = null;
    for (int i = 0; i < tree.getChildCount(); i++) {
        final Tree child = tree.getChild(i);

        switch (child.getType()) {
            case DECLARE:
                processDeclare(child);
                break;
            case SELECT:
                select = processSelect(child);
                break;
            case WHERE:
                where = processWhere(child);
                break;
            default:
                throw new HL7QueryParsingException("Found unexpected clause at root level: " + tree.getText());
        }
    }

    this.whereEvaluator = where;
    this.selections = select;
}
 
Example 13
Source File: HL7Query.java    From nifi with Apache License 2.0 5 votes vote down vote up
private String getSelectedName(final Tree selectable) {
    if (selectable.getChildCount() == 0) {
        return selectable.getText();
    } else if (selectable.getType() == DOT) {
        return getSelectedName(selectable.getChild(0)) + "." + getSelectedName(selectable.getChild(1));
    } else {
        return selectable.getChild(selectable.getChildCount() - 1).getText();
    }
}
 
Example 14
Source File: RuleBlock.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Parse rule 'THEN' (or rule's weight)
 * @param tree : Tree to parse
 */
private void fclTreeRuleBlockRuleThen(Tree tree, Rule fuzzyRule) {
	if (debug) Gpr.debug("Tree: " + tree.toStringTree());

	for (int childNum = 0; childNum < tree.getChildCount(); childNum++) {
		Tree child = tree.getChild(childNum);
		if (debug) Gpr.debug("\t\tChild: " + child.toStringTree());
		String thenVariable = child.getText();

		String thenValue = child.getChild(0).getText();
		Variable variable = getVariable(thenVariable);
		if (variable == null) throw new RuntimeException("Variable " + thenVariable + " does not exist");
		fuzzyRule.addConsequent(variable, thenValue, false);
	}
}
 
Example 15
Source File: FunctionBlock.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Builds rule set based on FCL tree (parsed from an FCL file)
 * @param tree : Tree to use
 * @return : RuleSet's name (or "" if no name)
 */
public String fclTree(Tree tree) {
	if (debug) Gpr.debug("Tree: " + tree.toStringTree());
	Gpr.checkRootNode("FUNCTION_BLOCK", tree);
	ruleBlocks = new HashMap<String, RuleBlock>();

	boolean firstChild = true;
	int ruleBlockCount = 1;

	// Add every child
	for (int childNum = 0; childNum < tree.getChildCount(); childNum++) {
		Tree child = tree.getChild(childNum);
		if (debug) Gpr.debug("\t\tChild: " + child.toStringTree());
		String leaveName = child.getText();

		if (firstChild) name = leaveName;
		else if (leaveName.equalsIgnoreCase("VAR_INPUT")) fclTreeVariables(child);
		else if (leaveName.equalsIgnoreCase("VAR_OUTPUT")) fclTreeVariables(child);
		else if (leaveName.equalsIgnoreCase("FUZZIFY")) fclTreeFuzzify(child);
		else if (leaveName.equalsIgnoreCase("DEFUZZIFY")) fclTreeDefuzzify(child);
		else if (leaveName.equalsIgnoreCase("RULEBLOCK")) {
			// Create and parse RuleBlock
			RuleBlock ruleBlock = new RuleBlock(this);
			String rbname = ruleBlock.fclTree(child);

			if (rbname.equals("")) rbname = "RuleBlock_" + ruleBlockCount; // Create name if none is given
			ruleBlockCount++;

			// Add RuleBlock
			ruleBlocks.put(rbname, ruleBlock);
		} else throw new RuntimeException("Unknown item '" + leaveName + "':\t" + child.toStringTree());

		firstChild = false;
	}

	return name;
}
 
Example 16
Source File: FTSQueryParser.java    From alfresco-data-model with GNU Lesser General Public License v3.0 4 votes vote down vote up
static private String getText(Tree node, boolean returnTextFromUnknownNodes)
{
    String text = node.getText();
    int index;
    switch (node.getType())
    {
    case FTSParser.FTSWORD:
    case FTSParser.FTSPRE:
    case FTSParser.FTSWILD:
        index = text.indexOf('\\');
        if (index == -1)
        {
            return text;
        }
        else
        {
            return unescape(text);
        }
    case FTSParser.FTSPHRASE:
        String phrase = text.substring(1, text.length() - 1);
        index = phrase.indexOf('\\');
        if (index == -1)
        {
            return phrase;
        }
        else
        {
            return unescape(phrase);
        }
    case FTSParser.ID:
        index = text.indexOf('\\');
        if (index == -1)
        {
            return ISO9075.decode(text);
        }
        else
        {
            return ISO9075.decode(unescape(text));
        }
    case FTSParser.URI:
    case FTSParser.OR:
    case FTSParser.AND:
    case FTSParser.NOT:
    case FTSParser.TILDA:
    case FTSParser.PLUS:
    case FTSParser.MINUS:
    case FTSParser.COLON:
    case FTSParser.STAR:
    case FTSParser.DOTDOT:
    case FTSParser.DOT:
    case FTSParser.AMP:
    case FTSParser.EXCLAMATION:
    case FTSParser.BAR:
    case FTSParser.EQUALS:
    case FTSParser.QUESTION_MARK:
    case FTSParser.TO:
    case FTSParser.COMMA:
    case FTSParser.CARAT:
    case FTSParser.DOLLAR:
    case FTSParser.AT:
    case FTSParser.PERCENT:
    case FTSParser.DECIMAL_INTEGER_LITERAL:
    case FTSParser.FLOATING_POINT_LITERAL:
    case FTSParser.DATETIME:
        return text;
    default:
        if(returnTextFromUnknownNodes)
        {
            return text;
        }
        else
        {
            return "";
        }
    }
}
 
Example 17
Source File: IgniteQueryResolverDelegate.java    From hibernate-ogm-ignite with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public PathedPropertyReferenceSource normalizePropertyPathIntermediary(PropertyPath path, Tree propertyName) {
	return new PathedPropertyReference( propertyName.getText(), null, false );
}
 
Example 18
Source File: IgniteQueryResolverDelegate.java    From hibernate-ogm-ignite with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void pushFromStrategy(JoinType joinType, Tree assosiationFetchTree, Tree propertyFetchTree, Tree alias) {
	this.currentAlias = alias.getText();
}
 
Example 19
Source File: GrammarRootAST.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public String getGrammarName() {
	Tree t = getChild(0);
	if ( t!=null ) return t.getText();
	return null;
}
 
Example 20
Source File: FunctionBlock.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Parse a tree for "Defuzzify" item
 * @param tree : Tree to parse
 * @return Variable (old or created)
 */
private Variable fclTreeDefuzzify(Tree tree) {
	Gpr.checkRootNode("DEFUZZIFY", tree);
	if (debug) Gpr.debug("Tree: " + tree.toStringTree());
	String defuzzificationMethodType = "COG";

	Tree child = tree.getChild(0);
	String varName = child.getText();

	// Get variable (or create a new one)
	Variable variable = getVariable(varName);
	if (variable == null) {
		variable = new Variable(varName);
		setVariable(varName, variable);
		if (debug) Gpr.debug("Variable '" + varName + "' does not exist => Creating it");
	}

	//---
	// Explore each sibling in this level
	//---
	for (int childNum = 1; childNum < tree.getChildCount(); childNum++) {
		child = tree.getChild(childNum);
		String leaveName = child.getText();
		if (debug) Gpr.debug("\t\tChild: " + child.toStringTree());

		if (leaveName.equalsIgnoreCase("TERM")) {
			// Linguistic term
			LinguisticTerm linguisticTerm = fclTreeFuzzifyTerm(child, variable);
			variable.add(linguisticTerm);
		} else if (leaveName.equalsIgnoreCase("ACCU")) // Accumulation method
			throw new RuntimeException("Accumulation method (ACCU) must be defined at RULE_BLOCK");
		// ruleAccumulationMethodType = child.getChild(0).getText();
		else if (leaveName.equalsIgnoreCase("METHOD")) // Defuzzification method
			defuzzificationMethodType = child.getChild(0).getText();
		else if (leaveName.equalsIgnoreCase("DEFAULT")) {
			// Default value
			String defaultValueStr = child.getChild(0).getText();
			if (defaultValueStr.equalsIgnoreCase("NC")) variable.setDefaultValue(Double.NaN); // Set it to "No Change"?
			else variable.setDefaultValue(Gpr.parseDouble(child.getChild(0))); // Set value
		} else if (leaveName.equalsIgnoreCase("RANGE")) {
			// Range values (universe min / max)
			double universeMin = Gpr.parseDouble(child.getChild(0));
			double universeMax = Gpr.parseDouble(child.getChild(1));
			if (universeMax <= universeMin) throw new RuntimeException("Range's min is grater than range's max! RANGE := ( " + universeMin + " .. " + universeMax + " );");
			variable.setUniverseMax(universeMax);
			variable.setUniverseMin(universeMin);
		} else throw new RuntimeException("Unknown/Unimplemented item '" + leaveName + "'");
	}

	// Defuzzification method
	Defuzzifier defuzzifier = createDefuzzifier(defuzzificationMethodType, variable);
	variable.setDefuzzifier(defuzzifier);

	return variable;
}