Search in sources :

Example 26 with DrlParser

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

the class KnowledgeBuilderImpl method generatedDrlToPackageDescr.

private PackageDescr generatedDrlToPackageDescr(Resource resource, String generatedDrl) throws DroolsParserException {
    // dump the generated DRL if the dump dir was configured
    if (this.configuration.getDumpDir() != null) {
        dumpDrlGeneratedFromDTable(this.configuration.getDumpDir(), generatedDrl, resource.getSourcePath());
    }
    DrlParser parser = new DrlParser(configuration.getLanguageLevel());
    PackageDescr pkg = parser.parse(resource, new StringReader(generatedDrl));
    this.results.addAll(parser.getErrors());
    if (pkg == null) {
        addBuilderResult(new ParserError(resource, "Parser returned a null Package", 0, 0));
    } else {
        pkg.setResource(resource);
    }
    return parser.hasErrors() ? null : pkg;
}
Also used : ParserError(org.drools.compiler.compiler.ParserError) StringReader(java.io.StringReader) DrlParser(org.drools.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr)

Example 27 with DrlParser

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

the class KnowledgeBuilderImpl method addPackageFromDrl.

/**
 * Load a rule package from DRL source and associate all loaded artifacts
 * with the given resource.
 *
 * @param reader
 * @param sourceResource the source resource for the read artifacts
 * @throws DroolsParserException
 * @throws IOException
 */
public void addPackageFromDrl(final Reader reader, final Resource sourceResource) throws DroolsParserException, IOException {
    this.resource = sourceResource;
    final DrlParser parser = new DrlParser(configuration.getLanguageLevel());
    final PackageDescr pkg = parser.parse(sourceResource, reader);
    this.results.addAll(parser.getErrors());
    if (pkg == null) {
        addBuilderResult(new ParserError(sourceResource, "Parser returned a null Package", 0, 0));
    }
    if (!parser.hasErrors()) {
        addPackage(pkg);
    }
    this.resource = null;
}
Also used : ParserError(org.drools.compiler.compiler.ParserError) DrlParser(org.drools.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr)

Example 28 with DrlParser

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

the class ParserTest method testErrorsParser.

@Test
public void testErrorsParser() throws Exception {
    final DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
    assertEquals(0, parser.getErrors().size());
    parser.parse(new InputStreamReader(getClass().getResourceAsStream("errors_parser_multiple.drl")));
    assertTrue(parser.hasErrors());
    assertTrue(parser.getErrors().size() > 0);
    assertTrue(parser.getErrors().get(0) instanceof ParserError);
    final ParserError first = ((ParserError) parser.getErrors().get(0));
    assertTrue(first.getMessage() != null);
    assertFalse(first.getMessage().equals(""));
}
Also used : InputStreamReader(java.io.InputStreamReader) ParserError(org.drools.compiler.compiler.ParserError) DrlParser(org.drools.compiler.compiler.DrlParser) Test(org.junit.Test)

Example 29 with DrlParser

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

the class DRLDumperTest method testDumpers.

@Test
public void testDumpers() throws Exception {
    final DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
    final Resource resource = new InputStreamResource(getClass().getResourceAsStream("test_Dumpers.drl"));
    final PackageDescr pkg = parser.parse(resource);
    if (parser.hasErrors()) {
        for (final DroolsError error : parser.getErrors()) {
            logger.warn(error.toString());
        }
        fail(parser.getErrors().toString());
    }
    KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase(pkg));
    KieSession ksession = kbase.newKieSession();
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    final Cheese brie = new Cheese("brie", 12);
    ksession.insert(brie);
    ksession.fireAllRules();
    assertEquals(3, list.size());
    assertEquals("3 1", list.get(0));
    assertEquals("MAIN", list.get(1));
    assertEquals("1 1", list.get(2));
    final DrlDumper drlDumper = new DrlDumper();
    final String drlResult = drlDumper.dump(pkg);
    System.out.println(drlResult);
    kbase = SerializationHelper.serializeObject(loadKnowledgeBaseFromString(drlResult));
    ksession = kbase.newKieSession();
    list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.insert(brie);
    ksession.fireAllRules();
    assertEquals(3, list.size());
    assertEquals("3 1", list.get(0));
    assertEquals("MAIN", list.get(1));
    assertEquals("1 1", list.get(2));
}
Also used : DroolsError(org.drools.compiler.compiler.DroolsError) KieBase(org.kie.api.KieBase) InputStreamResource(org.drools.core.io.impl.InputStreamResource) Resource(org.kie.api.io.Resource) ArrayList(java.util.ArrayList) DrlParser(org.drools.compiler.compiler.DrlParser) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Cheese(org.drools.compiler.Cheese) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) DrlDumper(org.drools.compiler.lang.DrlDumper) InputStreamResource(org.drools.core.io.impl.InputStreamResource) Test(org.junit.Test)

Example 30 with DrlParser

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

the class ChangeSetBuilder method diffResource.

private static ResourceChangeSet diffResource(String file, byte[] ob, byte[] cb) {
    ResourceChangeSet pkgcs = new ResourceChangeSet(file, ChangeType.UPDATED);
    ResourceType type = ResourceType.determineResourceType(file);
    if (ResourceType.DRL.equals(type) || ResourceType.GDRL.equals(type) || ResourceType.RDRL.equals(type) || ResourceType.TDRL.equals(type)) {
        try {
            PackageDescr opkg = new DrlParser().parse(new ByteArrayResource(ob));
            PackageDescr cpkg = new DrlParser().parse(new ByteArrayResource(cb));
            String pkgName = isEmpty(cpkg.getName()) ? getDefaultPackageName() : cpkg.getName();
            String oldPkgName = isEmpty(opkg.getName()) ? getDefaultPackageName() : opkg.getName();
            if (!oldPkgName.equals(pkgName)) {
                // so it is useless to further investigate other changes
                return pkgcs;
            }
            for (RuleDescr crd : cpkg.getRules()) {
                pkgcs.getLoadOrder().add(new ResourceChangeSet.RuleLoadOrder(pkgName, crd.getName(), crd.getLoadOrder()));
            }
            // needs to be cloned
            List<RuleDescr> orules = new ArrayList<>(opkg.getRules());
            diffDescrs(ob, cb, pkgcs, orules, cpkg.getRules(), ResourceChange.Type.RULE, RULE_CONVERTER);
            // needs to be cloned
            List<FunctionDescr> ofuncs = new ArrayList<>(opkg.getFunctions());
            diffDescrs(ob, cb, pkgcs, ofuncs, cpkg.getFunctions(), ResourceChange.Type.FUNCTION, FUNC_CONVERTER);
            // needs to be cloned
            List<GlobalDescr> oglobals = new ArrayList<>(opkg.getGlobals());
            diffDescrs(ob, cb, pkgcs, oglobals, cpkg.getGlobals(), ResourceChange.Type.GLOBAL, GLOBAL_CONVERTER);
        } catch (Exception e) {
            logger.error("Error analyzing the contents of " + file + ". Skipping.", e);
        }
    }
    pkgcs.getChanges().sort(Comparator.comparingInt(r -> r.getChangeType().ordinal()));
    return pkgcs;
}
Also used : GlobalDescr(org.drools.compiler.lang.descr.GlobalDescr) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) Arrays(java.util.Arrays) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) ResourceChange(org.kie.internal.builder.ResourceChange) ChangeType(org.kie.internal.builder.ChangeType) LoggerFactory(org.slf4j.LoggerFactory) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ResourceType(org.kie.api.io.ResourceType) FunctionDescr(org.drools.compiler.lang.descr.FunctionDescr) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) StringUtils.isEmpty(org.drools.core.util.StringUtils.isEmpty) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) GlobalDescr(org.drools.compiler.lang.descr.GlobalDescr) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet) Collection(java.util.Collection) Set(java.util.Set) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) List(java.util.List) DrlParser(org.drools.compiler.compiler.DrlParser) Comparator(java.util.Comparator) StringUtils(org.drools.core.util.StringUtils) ArrayList(java.util.ArrayList) ResourceType(org.kie.api.io.ResourceType) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet) FunctionDescr(org.drools.compiler.lang.descr.FunctionDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) DrlParser(org.drools.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr)

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