Search in sources :

Example 21 with DrlParser

use of org.drools.compiler.compiler.DrlParser in project drools by kiegroup.

the class VerifierComponentTest method testVisit.

@Test
public void testVisit() throws Exception {
    // Drools Package description from Drl file
    Reader drlReader = new InputStreamReader(Verifier.class.getResourceAsStream("Misc3.drl"));
    PackageDescr descr = new DrlParser(LanguageLevelOption.DRL5).parse(drlReader);
    // Drools Verifier objects
    VerifierData verifierData = VerifierReportFactory.newVerifierData();
    PackageDescrVisitor visitor = new PackageDescrVisitor(verifierData, Collections.EMPTY_LIST);
    visitor.visitPackageDescr(descr);
    // Collect the results.
    Collection<VerifierComponent> datas = verifierData.getAll();
    VerifierComponent[] components = datas.toArray(new VerifierComponent[datas.size()]);
    // Misc3.drl
    assertVerifierComponent((TextConsequence) components[0], 51);
    assertVerifierComponent((TextConsequence) components[1], 42);
    assertVerifierComponent((Field) components[2], 48);
    assertVerifierComponent((Field) components[3], 39);
    assertVerifierComponent((Field) components[4], 40);
    assertVerifierComponent((Field) components[5], 41);
    assertVerifierComponent((Import) components[6], 19);
    assertVerifierComponent((ObjectType) components[7], 48);
    assertVerifierComponent((ObjectType) components[8], 19);
    assertVerifierComponent((ObjectType) components[9], 39);
    assertVerifierComponent((ObjectType) components[10], 40);
    assertVerifierComponent((ObjectType) components[11], 41);
    assertVerifierComponent((RuleOperatorDescr) components[12], 48);
    assertVerifierComponent((PatternOperatorDescr) components[13], 48);
    assertVerifierComponent((PatternOperatorDescr) components[14], 49);
    assertVerifierComponent((PatternOperatorDescr) components[15], 50);
    assertVerifierComponent((RuleOperatorDescr) components[16], 39);
    assertVerifierComponent((PatternOperatorDescr) components[17], 39);
    assertVerifierComponent((PatternOperatorDescr) components[18], 40);
    assertVerifierComponent((PatternOperatorDescr) components[19], 41);
    assertVerifierComponent((Pattern) components[20], 48);
    assertVerifierComponent((Pattern) components[21], 49);
    assertVerifierComponent((Pattern) components[22], 50);
    assertVerifierComponent((Pattern) components[23], 39);
    assertVerifierComponent((Pattern) components[24], 40);
    assertVerifierComponent((Pattern) components[25], 41);
    assertVerifierComponent((NumberRestriction) components[26], 48);
    assertVerifierComponent((NumberRestriction) components[27], 49);
    assertVerifierComponent((NumberRestriction) components[28], 50);
    assertVerifierComponent((NumberRestriction) components[29], 39);
    assertVerifierComponent((NumberRestriction) components[30], 40);
    assertVerifierComponent((NumberRestriction) components[31], 41);
    assertVerifierComponent((NumberRestriction) components[32], 41);
    assertVerifierComponent((VerifierRule) components[33], 46);
    assertVerifierComponent((VerifierRule) components[34], 37);
    assertVerifierComponent((RulePackage) components[35], 17);
    assertVerifierComponent((SubPattern) components[36], 48);
    assertVerifierComponent((SubPattern) components[37], 49);
    assertVerifierComponent((SubPattern) components[38], 50);
    assertVerifierComponent((SubPattern) components[39], 39);
    assertVerifierComponent((SubPattern) components[40], 40);
    assertVerifierComponent((SubPattern) components[41], 41);
    assertVerifierComponent((SubRule) components[42], 46);
    assertVerifierComponent((SubRule) components[43], 37);
    assertVerifierComponent((WorkingMemory) components[44], -1);
}
Also used : InputStreamReader(java.io.InputStreamReader) VerifierData(org.drools.verifier.data.VerifierData) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) DrlParser(org.drools.compiler.compiler.DrlParser) Verifier(org.drools.verifier.Verifier) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) VerifierComponent(org.drools.verifier.data.VerifierComponent) Test(org.junit.Test)

Example 22 with DrlParser

use of org.drools.compiler.compiler.DrlParser in project drools by kiegroup.

the class RuleModelDRLPersistenceImpl method parseDrl.

private RuleDescr parseDrl(final ExpandedDRLInfo expandedDRLInfo) {
    DrlParser drlParser = new DrlParser();
    PackageDescr packageDescr;
    try {
        packageDescr = drlParser.parse(true, expandedDRLInfo.plainDrl);
        if (drlParser.hasErrors()) {
            throw new RuleModelUnmarshallingException();
        }
    } catch (DroolsParserException e) {
        throw new RuntimeException(e);
    }
    expandedDRLInfo.registerGlobalDescrs(packageDescr.getGlobals());
    return packageDescr.getRules().get(0);
}
Also used : DrlParser(org.drools.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) DroolsParserException(org.drools.compiler.compiler.DroolsParserException)

Example 23 with DrlParser

use of org.drools.compiler.compiler.DrlParser in project drools by kiegroup.

the class TestBaseOld method getTestData.

@SuppressWarnings("unchecked")
public Collection<? extends Object> getTestData(InputStream stream, VerifierData data) throws Exception {
    Reader drlReader = new InputStreamReader(stream);
    PackageDescr descr = new DrlParser(LanguageLevelOption.DRL5).parse(drlReader);
    PackageDescrVisitor packageDescrVisitor = new PackageDescrVisitor(data, Collections.EMPTY_LIST);
    packageDescrVisitor.visitPackageDescr(descr);
    // Rules with relations
    return data.getAll();
}
Also used : InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) DrlParser(org.drools.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) PackageDescrVisitor(org.drools.verifier.visitor.PackageDescrVisitor)

Example 24 with DrlParser

use of org.drools.compiler.compiler.DrlParser in project drools-wb by kiegroup.

the class FactModelPersistence method toModel.

private static List<FactMetaModel> toModel(String drl) throws DroolsParserException {
    Preconditions.checkNotNull(drl, "The string representing DRL can't be null!");
    if (drl.startsWith("#advanced") || drl.startsWith("//advanced")) {
        throw new DroolsParserException("Using advanced editor");
    }
    final DrlParser parser = new DrlParser();
    final StringReader reader = new StringReader(drl);
    final PackageDescr pkg = parser.parse(reader);
    if (parser.hasErrors()) {
        throw new DroolsParserException("The model drl " + drl + " is not valid");
    }
    if (pkg == null) {
        return emptyList();
    }
    final List<TypeDeclarationDescr> types = pkg.getTypeDeclarations();
    final List<FactMetaModel> list = new ArrayList<FactMetaModel>(types.size());
    for (final TypeDeclarationDescr td : types) {
        final FactMetaModel mm = new FactMetaModel();
        mm.setName(td.getTypeName());
        mm.setSuperType(td.getSuperTypeName());
        final Map<String, TypeFieldDescr> fields = td.getFields();
        for (Map.Entry<String, TypeFieldDescr> en : fields.entrySet()) {
            final String fieldName = en.getKey();
            final TypeFieldDescr descr = en.getValue();
            final FieldMetaModel fm = new FieldMetaModel(fieldName, descr.getPattern().getObjectType());
            mm.getFields().add(fm);
        }
        for (final AnnotationDescr descr : td.getAnnotations()) {
            final String annotationName = descr.getName();
            final Map<String, String> values = extractStringValues(descr);
            final AnnotationMetaModel am = new AnnotationMetaModel(annotationName, values);
            mm.getAnnotations().add(am);
        }
        list.add(mm);
    }
    return list;
}
Also used : TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) ArrayList(java.util.ArrayList) FactMetaModel(org.drools.workbench.screens.factmodel.model.FactMetaModel) DroolsParserException(org.drools.compiler.compiler.DroolsParserException) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr) FieldMetaModel(org.drools.workbench.screens.factmodel.model.FieldMetaModel) TypeFieldDescr(org.drools.compiler.lang.descr.TypeFieldDescr) StringReader(java.io.StringReader) DrlParser(org.drools.compiler.compiler.DrlParser) AnnotationMetaModel(org.drools.workbench.screens.factmodel.model.AnnotationMetaModel) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) HashMap(java.util.HashMap) Map(java.util.Map)

Example 25 with DrlParser

use of org.drools.compiler.compiler.DrlParser in project drools by kiegroup.

the class KnowledgeBuilderImpl method drlToPackageDescr.

PackageDescr drlToPackageDescr(Resource resource) throws DroolsParserException, IOException {
    PackageDescr pkg;
    boolean hasErrors = false;
    if (resource instanceof DescrResource) {
        pkg = (PackageDescr) ((DescrResource) resource).getDescr();
    } else {
        final DrlParser parser = new DrlParser(configuration.getLanguageLevel());
        pkg = parser.parse(resource);
        this.results.addAll(parser.getErrors());
        if (pkg == null) {
            addBuilderResult(new ParserError(resource, "Parser returned a null Package", 0, 0));
        }
        hasErrors = parser.hasErrors();
    }
    if (pkg != null) {
        pkg.setResource(resource);
    }
    return hasErrors ? null : pkg;
}
Also used : ParserError(org.drools.compiler.compiler.ParserError) DescrResource(org.drools.core.io.impl.DescrResource) DrlParser(org.drools.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr)

Aggregations

DrlParser (org.drools.compiler.compiler.DrlParser)44 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)43 Test (org.junit.Test)32 InputStreamReader (java.io.InputStreamReader)10 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)9 StringReader (java.io.StringReader)8 CompositePackageDescr (org.drools.compiler.lang.descr.CompositePackageDescr)6 ArrayList (java.util.ArrayList)5 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)5 ParserError (org.drools.compiler.compiler.ParserError)5 DrlDumper (org.drools.compiler.lang.DrlDumper)4 Reader (java.io.Reader)3 List (java.util.List)3 DroolsParserException (org.drools.compiler.compiler.DroolsParserException)3 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)3 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)3 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)3 Ignore (org.junit.Ignore)3 AttributeDescr (org.drools.compiler.lang.descr.AttributeDescr)2 MVELConsequence (org.drools.core.base.mvel.MVELConsequence)2