📄 ruleparsertest.java
字号:
package org.drools.lang;
/*
* Copyright 2005 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.List;
import junit.framework.TestCase;
import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.Lexer;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.TokenStream;
import org.drools.compiler.DrlParser;
import org.drools.lang.descr.AndDescr;
import org.drools.lang.descr.AttributeDescr;
import org.drools.lang.descr.BoundVariableDescr;
import org.drools.lang.descr.ColumnDescr;
import org.drools.lang.descr.EvalDescr;
import org.drools.lang.descr.ExistsDescr;
import org.drools.lang.descr.FieldBindingDescr;
import org.drools.lang.descr.FunctionDescr;
import org.drools.lang.descr.LiteralDescr;
import org.drools.lang.descr.NotDescr;
import org.drools.lang.descr.OrDescr;
import org.drools.lang.descr.PackageDescr;
import org.drools.lang.descr.PredicateDescr;
import org.drools.lang.descr.QueryDescr;
import org.drools.lang.descr.ReturnValueDescr;
import org.drools.lang.descr.RuleDescr;
import org.drools.lang.dsl.DefaultExpanderResolver;
public class RuleParserTest extends TestCase {
private RuleParser parser;
protected void setUp() throws Exception {
super.setUp();
}
protected void tearDown() throws Exception {
super.tearDown();
}
public void testPackage_OneSegment() throws Exception {
final String packageName = parse( "package foo" ).package_statement();
assertEquals( "foo",
packageName );
assertFalse( this.parser.hasErrors() );
}
public void testPackage_MultipleSegments() throws Exception {
final String packageName = parse( "package foo.bar.baz;" ).package_statement();
assertEquals( "foo.bar.baz",
packageName );
assertFalse( this.parser.hasErrors() );
}
public void testProlog() throws Exception {
parse( "package foo; import com.foo.Bar; import com.foo.Baz;" ).prolog();
assertEquals( "foo",
this.parser.getPackageDescr().getName() );
assertEquals( 2,
this.parser.getPackageDescr().getImports().size() );
assertEquals( "com.foo.Bar",
this.parser.getPackageDescr().getImports().get( 0 ) );
assertEquals( "com.foo.Baz",
this.parser.getPackageDescr().getImports().get( 1 ) );
assertFalse( this.parser.hasErrors() );
}
public void testEmptyRule() throws Exception {
final RuleDescr rule = parseResource( "empty_rule.drl" ).rule();
assertNotNull( rule );
assertEquals( "empty",
rule.getName() );
assertNull( rule.getLhs() );
assertNull( rule.getConsequence() );
assertFalse( this.parser.hasErrors() );
}
public void FIXME_testKeywordCollisions() throws Exception {
//MN: this really needs the multiphase parser for it to work properly
final RuleParser parser = parseResource( "eol_funny_business.drl" );
parser.compilation_unit();
final PackageDescr pkg = parser.getPackageDescr();
assertEquals( 1,
pkg.getRules().size() );
assertFalse( parser.hasErrors() );
}
public void testTernaryExpression() throws Exception {
final RuleParser parser = parseResource( "ternary_expression.drl" );
parser.compilation_unit();
final PackageDescr pkg = parser.getPackageDescr();
RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals( 1,
pkg.getRules().size() );
assertFalse( parser.hasErrors() );
assertEqualsIgnoreWhitespace("if (speed > speedLimit ? true : false;) pullEmOver();", rule.getConsequence());
}
public void testLatinChars() throws Exception {
final DrlParser parser = new DrlParser();
final Reader drl = new InputStreamReader( this.getClass().getResourceAsStream( "latin-sample.drl" ) );
final Reader dsl = new InputStreamReader( this.getClass().getResourceAsStream( "latin.dsl" ) );
final PackageDescr pkg = parser.parse( drl,
dsl );
//MN: will get some errors due to the char encoding on my FC5 install
//others who use the right encoding may not see this, feel free to uncomment
//the following assertion.
//assertFalse( parser.hasErrors() );
assertEquals( "br.com.auster.drools.sample",
pkg.getName() );
assertEquals( 1,
pkg.getRules().size() );
}
public void testFunctionWithArrays() throws Exception {
final DrlParser parser = new DrlParser();
final Reader drl = new InputStreamReader( this.getClass().getResourceAsStream( "function_arrays.drl" ) );
final PackageDescr pkg = parser.parse( drl );
assertFalse( parser.hasErrors() );
assertEquals( "foo",
pkg.getName() );
assertEquals( 1,
pkg.getRules().size() );
final RuleDescr rule = (RuleDescr) pkg.getRules().get( 0 );
assertEqualsIgnoreWhitespace( "yourFunction(new String[3] {\"a\",\"b\",\"c\"});",
rule.getConsequence() );
final FunctionDescr func = (FunctionDescr) pkg.getFunctions().get( 0 );
assertEquals( "String[]",
func.getReturnType() );
assertEquals( "args[]",
func.getParameterNames().get( 0 ) );
assertEquals( "String",
func.getParameterTypes().get( 0 ) );
}
public void testAlmostEmptyRule() throws Exception {
final RuleDescr rule = parseResource( "almost_empty_rule.drl" ).rule();
assertNotNull( rule );
assertEquals( "almost_empty",
rule.getName() );
assertNotNull( rule.getLhs() );
assertEquals( "",
rule.getConsequence().trim() );
assertFalse( this.parser.hasErrors() );
}
public void testQuotedStringNameRule() throws Exception {
final RuleDescr rule = parseResource( "quoted_string_name_rule.drl" ).rule();
assertNotNull( rule );
assertEquals( "quoted string name",
rule.getName() );
assertNotNull( rule.getLhs() );
assertEquals( "",
rule.getConsequence().trim() );
assertFalse( this.parser.hasErrors() );
}
public void testNoLoop() throws Exception {
final RuleDescr rule = parseResource( "no-loop.drl" ).rule();
assertNotNull( rule );
assertEquals( "rule1",
rule.getName() );
final AttributeDescr att = (AttributeDescr) rule.getAttributes().get( 0 );
assertEquals( "false",
att.getValue() );
assertEquals( "no-loop",
att.getName() );
assertFalse( this.parser.hasErrors() );
}
public void testAutofocus() throws Exception {
final RuleDescr rule = parseResource( "autofocus.drl" ).rule();
assertNotNull( rule );
assertEquals( "rule1",
rule.getName() );
final AttributeDescr att = (AttributeDescr) rule.getAttributes().get( 0 );
assertEquals( "true",
att.getValue() );
assertEquals( "auto-focus",
att.getName() );
assertFalse( this.parser.hasErrors() );
}
public void testConsequenceWithDeclaration() throws Exception {
final RuleDescr rule = parseResource( "declaration-in-consequence.drl" ).rule();
assertNotNull( rule );
assertEquals( "myrule",
rule.getName() );
final String expected = "int i = 0; i = 1; i / 1; i == 1; i(i); i = 'i'; i.i.i; i\\i; i<i; i>i; i=\"i\"; ++i;" + "i++; --i; i--; i += i; i -= i; i *= i; i /= i;" + "int i = 5;" + "for(int j; j<i; ++j) {" + "System.out.println(j);}"
+ "Object o = new String(\"Hello\");" + "String s = (String) o;";
assertEqualsIgnoreWhitespace( expected,
rule.getConsequence() );
assertTrue( rule.getConsequence().indexOf( "++" ) > 0 );
assertTrue( rule.getConsequence().indexOf( "--" ) > 0 );
assertTrue( rule.getConsequence().indexOf( "+=" ) > 0 );
assertTrue( rule.getConsequence().indexOf( "==" ) > 0 );
//System.out.println(rule.getConsequence());
//note, need to assert that "i++" is preserved as is, no extra spaces.
assertFalse( this.parser.hasErrors() );
}
public void testLiteralBoolAndNegativeNumbersRule() throws Exception {
final RuleParser parser = parseResource( "literal_bool_and_negative.drl" );
final RuleDescr rule = parser.rule();
assertFalse( parser.hasErrors() );
assertNotNull( rule );
assertEquals( "simple_rule",
rule.getName() );
assertNotNull( rule.getLhs() );
assertEqualsIgnoreWhitespace( "cons();",
rule.getConsequence() );
final AndDescr lhs = rule.getLhs();
assertEquals( 3,
lhs.getDescrs().size() );
ColumnDescr col = (ColumnDescr) lhs.getDescrs().get( 0 );
assertEquals( 1,
col.getDescrs().size() );
LiteralDescr lit = (LiteralDescr) col.getDescrs().get( 0 );
assertEquals( "==",
lit.getEvaluator() );
assertEquals( "false",
lit.getText() );
assertEquals( "bar",
lit.getFieldName() );
assertEquals( false,
lit.isStaticFieldValue() );
col = (ColumnDescr) lhs.getDescrs().get( 1 );
assertEquals( 1,
col.getDescrs().size() );
lit = (LiteralDescr) col.getDescrs().get( 0 );
assertEquals( ">",
lit.getEvaluator() );
assertEquals( "-42",
lit.getText() );
assertEquals( "boo",
lit.getFieldName() );
col = (ColumnDescr) lhs.getDescrs().get( 2 );
assertEquals( 1,
col.getDescrs().size() );
lit = (LiteralDescr) col.getDescrs().get( 0 );
assertEquals( ">",
lit.getEvaluator() );
assertEquals( "-42.42",
lit.getText() );
assertEquals( "boo",
lit.getFieldName() );
assertFalse( parser.hasErrors() );
}
public void testChunkWithoutParens() throws Exception {
final String chunk = parse( "foo" ).paren_chunk();
assertEquals( "foo",
chunk );
assertFalse( this.parser.hasErrors() );
}
public void testChunkWithParens() throws Exception {
final String chunk = parse( "fnord()" ).paren_chunk();
assertEqualsIgnoreWhitespace( "fnord()",
chunk );
assertFalse( this.parser.hasErrors() );
}
public void testChunkWithParensAndQuotedString() throws Exception {
final String chunk = parse( "fnord(\"cheese\")" ).paren_chunk();
assertEqualsIgnoreWhitespace( "fnord(\"cheese\")",
chunk );
assertFalse( this.parser.hasErrors() );
}
public void testChunkWithRandomCharac5ters() throws Exception {
final String chunk = parse( "%*9dkj" ).paren_chunk();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -