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[] originalBytes, byte[] currentBytes, List<TypeDeclarationDescr> typeDeclarations) {
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 originalPkg = new DrlParser().parse(new ByteArrayResource(originalBytes));
PackageDescr currentPkg = new DrlParser().parse(new ByteArrayResource(currentBytes));
String pkgName = isEmpty(currentPkg.getName()) ? getDefaultPackageName() : currentPkg.getName();
String oldPkgName = isEmpty(originalPkg.getName()) ? getDefaultPackageName() : originalPkg.getName();
if (!oldPkgName.equals(pkgName)) {
// so it is useless to further investigate other changes
return pkgcs;
}
pkgcs.setPackageName(pkgName);
for (RuleDescr crd : currentPkg.getRules()) {
pkgcs.getLoadOrder().add(new ResourceChangeSet.RuleLoadOrder(pkgName, crd.getName(), crd.getLoadOrder()));
}
// needs to be cloned
List<RuleDescr> orules = new ArrayList<>(originalPkg.getRules());
diffDescrs(originalBytes, currentBytes, pkgcs, orules, currentPkg.getRules(), ResourceChange.Type.RULE, RULE_CONVERTER);
// needs to be cloned
List<FunctionDescr> ofuncs = new ArrayList<>(originalPkg.getFunctions());
diffDescrs(originalBytes, currentBytes, pkgcs, ofuncs, currentPkg.getFunctions(), ResourceChange.Type.FUNCTION, FUNC_CONVERTER);
// needs to be cloned
List<GlobalDescr> oglobals = new ArrayList<>(originalPkg.getGlobals());
diffDescrs(originalBytes, currentBytes, pkgcs, oglobals, currentPkg.getGlobals(), ResourceChange.Type.GLOBAL, GLOBAL_CONVERTER);
for (TypeDeclarationDescr typeDeclaration : currentPkg.getTypeDeclarations()) {
if (isEmpty(typeDeclaration.getNamespace())) {
typeDeclaration.setNamespace(isEmpty(currentPkg.getNamespace()) ? DEFAULT_PACKAGE : currentPkg.getNamespace());
}
typeDeclarations.add(typeDeclaration);
}
} catch (Exception e) {
logger.error("Error analyzing the contents of " + file + ". Skipping.", e);
}
}
pkgcs.getChanges().sort(Comparator.comparingInt(r -> r.getChangeType().ordinal()));
return pkgcs;
}
use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.
the class MapPersistenceTest method createPersistentSessionWithRules.
@Test
public void createPersistentSessionWithRules() {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
String rule = "package org.drools.persistence.map.impl\n";
rule += "import org.drools.persistence.map.impl.Buddy;\n";
rule += "rule \"echo2\" \n";
rule += "dialect \"mvel\"\n";
rule += "when\n";
rule += " $m : Buddy()\n";
rule += "then\n";
rule += " System.out.println(\"buddy inserted\")";
rule += "end\n";
kbuilder.add(new ByteArrayResource(rule.getBytes()), ResourceType.DRL);
KnowledgeBuilderErrors errors = kbuilder.getErrors();
if (errors != null && errors.size() > 0) {
for (KnowledgeBuilderError error : errors) {
logger.warn("Error: " + error.getMessage());
}
Assert.fail("KnowledgeBase did not build");
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
KieSession ksession = createSession(kbase);
FactHandle buddyFactHandle = ksession.insert(new Buddy());
ksession.fireAllRules();
Assert.assertEquals(1, ksession.getObjects().size());
ksession = disposeAndReloadSession(ksession, kbase);
Assert.assertNotNull(ksession);
Assert.assertEquals(1, ksession.getObjects().size());
Assert.assertNull("An object can't be retrieved with a FactHandle from a disposed session", ksession.getObject(buddyFactHandle));
}
use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.
the class TraitTypeGenerationTest method testMultipleInheritanceWithPosition6.
@Test
public void testMultipleInheritanceWithPosition6() throws Exception {
// DROOLS-249
String drl = "package org.drools.test\n" + "declare trait PosTrait\n" + "@propertyReactive\n" + " field0 : int = 100 //@position(0)\n" + " field1 : int = 101 //@position(1)\n" + " field2 : int = 102 //@position(0)\n" + "end\n" + "\n" + "declare trait SecPosTrait\n" + "@propertyReactive\n" + " field3 : int = 100 //@position(0)\n" + " field1 : int = 101 //@position(1)\n" + "end\n" + "\n" + "declare trait MultiInhPosTrait extends PosTrait, SecPosTrait\n" + "@propertyReactive\n" + " mfield0 : int = 200 //@position(0)\n" + " mfield1 : int = 201 //@position(2)\n" + "end";
KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kBuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
if (kBuilder.hasErrors()) {
System.err.println(kBuilder.getErrors());
}
assertFalse(kBuilder.hasErrors());
InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
knowledgeBase.addPackages(kBuilder.getKnowledgePackages());
FactType sw = knowledgeBase.getFactType("org.drools.test", "MultiInhPosTrait");
assertEquals(6, sw.getFields().size());
assertEquals("field0", sw.getFields().get(0).getName());
assertEquals("field1", sw.getFields().get(1).getName());
assertEquals("field2", sw.getFields().get(2).getName());
assertEquals("field3", sw.getFields().get(3).getName());
assertEquals("mfield0", sw.getFields().get(4).getName());
assertEquals("mfield1", sw.getFields().get(5).getName());
}
use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.
the class TraitTypeGenerationTest method testMultipleInheritanceWithPosition2.
@Test
public void testMultipleInheritanceWithPosition2() throws Exception {
// DROOLS-249
String drl = "package org.drools.test\n" + "declare trait PosTrait\n" + "@propertyReactive\n" + " field0 : int = 100 //@position(0)\n" + " field1 : int = 101 //@position(1)\n" + " field2 : int = 102 //@position(0)\n" + "end\n" + "\n" + "declare trait MultiInhPosTrait extends PosTrait\n" + "@propertyReactive\n" + " mfield0 : int = 200 @position(0)\n" + " mfield1 : int = 201 @position(2)\n" + "end";
KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kBuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
if (kBuilder.hasErrors()) {
System.err.println(kBuilder.getErrors());
}
assertFalse(kBuilder.hasErrors());
InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
knowledgeBase.addPackages(kBuilder.getKnowledgePackages());
FactType sw = knowledgeBase.getFactType("org.drools.test", "MultiInhPosTrait");
assertEquals(5, sw.getFields().size());
assertEquals("mfield0", sw.getFields().get(0).getName());
assertEquals("field0", sw.getFields().get(1).getName());
assertEquals("mfield1", sw.getFields().get(2).getName());
assertEquals("field1", sw.getFields().get(3).getName());
assertEquals("field2", sw.getFields().get(4).getName());
}
use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.
the class TraitTypeGenerationTest method testMultipleInheritanceWithPosition7.
@Test
public void testMultipleInheritanceWithPosition7() throws Exception {
// DROOLS-249
String drl = "package org.drools.test\n" + "declare trait PosTrait\n" + "@propertyReactive\n" + " field0 : int = 100 @position(0)\n" + " field1 : int = 101 @position(1)\n" + " field2 : int = 102 @position(0)\n" + "end\n" + "\n" + "declare trait SecPosTrait\n" + "@propertyReactive\n" + " field3 : int = 100 @position(2)\n" + " field1 : int = 101 //@position(1)\n" + "end\n" + "\n" + "declare trait MultiInhPosTrait extends PosTrait, SecPosTrait\n" + "@propertyReactive\n" + " mfield0 : int = 200 @position(0)\n" + " mfield1 : int = 201 @position(2)\n" + "end";
KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kBuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
if (kBuilder.hasErrors()) {
System.err.println(kBuilder.getErrors());
}
assertFalse(kBuilder.hasErrors());
InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
knowledgeBase.addPackages(kBuilder.getKnowledgePackages());
FactType sw = knowledgeBase.getFactType("org.drools.test", "MultiInhPosTrait");
assertEquals(6, sw.getFields().size());
assertEquals("field0", sw.getFields().get(0).getName());
assertEquals("field2", sw.getFields().get(1).getName());
assertEquals("mfield0", sw.getFields().get(2).getName());
assertEquals("field1", sw.getFields().get(3).getName());
assertEquals("field3", sw.getFields().get(4).getName());
assertEquals("mfield1", sw.getFields().get(5).getName());
}
Aggregations