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());
}
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"));
}
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());
}
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());
}
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;
}
Aggregations