Search in sources :

Example 51 with ByteArrayResource

use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.

the class PropertyReactivityTest method testPRBindingOnNonexistingWatchedAttribute.

@Test(timeout = 10000)
public void testPRBindingOnNonexistingWatchedAttribute() {
    final String str = "package org.drools.test;\n" + "\n" + "import org.drools.compiler.integrationtests.PropertyReactivityTest.Klass4;\n" + "\n" + "global java.util.List list;\n" + "\n" + "rule \"Get Person name\"\n" + "salience 1\n" + "when\n" + "  $x : Klass4( ) @watch( nmae )\n" + "then\n" + "end\n";
    final KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    knowledgeBuilder.add(new ByteArrayResource(str.getBytes()), ResourceType.DRL);
    System.out.println(knowledgeBuilder.getErrors());
    assertTrue(knowledgeBuilder.hasErrors());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) Test(org.junit.Test)

Example 52 with ByteArrayResource

use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.

the class Misc2Test method testFactStealing.

@Test
public void testFactStealing() throws Exception {
    String drl = "package org.drools.test; \n" + "import org.drools.compiler.Person; \n " + "global java.util.List list; \n" + "\n" + "\n" + "rule Sleep \n " + "salience 1000 \n" + "when then \n" + "  System.out.println( Thread.currentThread().getName() + \"Zlip\" ); \n" + "  Thread.sleep( 100 ); \n" + "end \n" + "" + "rule FireAtWill\n" + "when  \n" + "  $p : Person( $n : name ) \n" + "then \n" + "  System.out.println( Thread.currentThread().getName() + \" Ill continue later \" ); \n" + "  Thread.sleep( 100 ); \n" + "  System.out.println( Thread.currentThread().getName() + \" Hello >> \" + $n );\n" + "  list.add( $n ); \n" + "end\n" + "\n" + "rule ImDone\n" + "timer( expr:0 )\n" + "when\n" + "  $p : Person()\n" + "then\n" + "  System.out.println( Thread.currentThread().getName() + \"Take out \" + $p ); \n" + "  delete( $p );\n" + "  System.out.println( Thread.currentThread().getName() + \"Taken out \" + $p ); \n" + "  if ( list.isEmpty() ) { list.add( $p.getName() ); } \n" + "end\n" + "\n";
    KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    knowledgeBuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
    if (knowledgeBuilder.hasErrors()) {
        fail(knowledgeBuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(knowledgeBuilder.getKnowledgePackages());
    KieSession knowledgeSession = kbase.newKieSession();
    ArrayList list = new ArrayList();
    knowledgeSession.setGlobal("list", list);
    knowledgeSession.insert(new Person("mark", 67));
    knowledgeSession.fireAllRules();
    Thread.sleep(500);
    assertEquals(1, list.size());
    assertTrue(list.contains("mark"));
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 53 with ByteArrayResource

use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.

the class Misc2Test method testPrimitiveGlobals.

@Test
public void testPrimitiveGlobals() {
    String drl = "package org.drools.compiler.integrationtests\n" + "\n" + "global int foo;\n" + "\n" + "";
    KnowledgeBuilder kb = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kb.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
    System.out.println(kb.getErrors());
    assertTrue(kb.hasErrors());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) Test(org.junit.Test)

Example 54 with ByteArrayResource

use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.

the class Misc2Test method testFailedStaticImport.

@Test
public void testFailedStaticImport() {
    // DROOLS-155
    String drl = "package org.drools.test; \n" + "" + "import function org.does.not.exist.Foo; \n" + "" + "" + "rule X when\n" + "then\n" + "end";
    KnowledgeBuilder kb = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kb.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
    assertTrue(kb.hasErrors());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) Test(org.junit.Test)

Example 55 with ByteArrayResource

use of org.drools.core.io.impl.ByteArrayResource 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

ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)79 Test (org.junit.Test)67 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)67 KieSession (org.kie.api.runtime.KieSession)33 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)29 ArrayList (java.util.ArrayList)27 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)25 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)24 CommonTraitTest (org.drools.traits.compiler.CommonTraitTest)20 List (java.util.List)19 FactType (org.kie.api.definition.type.FactType)16 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)5 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)5 BitSet (java.util.BitSet)4 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)4 PackageDescr (org.drools.drl.ast.descr.PackageDescr)4 NamedEntryPoint (org.drools.kiesession.entrypoints.NamedEntryPoint)4 KieBase (org.kie.api.KieBase)4 StringReader (java.io.StringReader)3 LinkedList (java.util.LinkedList)3