Search in sources :

Example 16 with ByteArrayResource

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

the class PMML4Compiler method buildOutputResource.

private Resource buildOutputResource(Resource resource, String theory) {
    ByteArrayResource byteArrayResource = new ByteArrayResource(theory.getBytes(IoUtils.UTF8_CHARSET));
    byteArrayResource.setResourceType(ResourceType.PMML);
    if (resource.getSourcePath() != null) {
        String originalPath = resource.getSourcePath();
        int start = originalPath.lastIndexOf(File.separator);
        byteArrayResource.setSourcePath("generated-sources/" + originalPath.substring(start) + ".pmml");
    } else {
        byteArrayResource.setSourcePath("generated-sources/" + helper.getContext() + ".pmml");
    }
    return byteArrayResource;
}
Also used : ByteArrayResource(org.drools.core.io.impl.ByteArrayResource)

Example 17 with ByteArrayResource

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

the class BPMN2DataServiceImpl method buildProcessDefinition.

@Override
public ProcessDefinition buildProcessDefinition(String deploymentId, String bpmn2Content, KieContainer kieContainer, boolean cache) throws IllegalArgumentException {
    if (StringUtils.isEmpty(bpmn2Content)) {
        return null;
    }
    KnowledgeBuilder kbuilder = null;
    if (kieContainer != null && kieContainer.getClassLoader() != null) {
        KnowledgeBuilderConfigurationImpl pconf = new KnowledgeBuilderConfigurationImpl(kieContainer.getClassLoader());
        kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(pconf);
    } else {
        kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    }
    kbuilder.add(new ByteArrayResource(bpmn2Content.getBytes()), ResourceType.BPMN2);
    if (kbuilder.hasErrors()) {
        for (KnowledgeBuilderError error : kbuilder.getErrors()) {
            logger.error("Error: {}", error.getMessage());
        }
        logger.debug("Process Cannot be Parsed! \n {} \n", bpmn2Content);
        return null;
    }
    KiePackage pckg = kbuilder.getKnowledgePackages().iterator().next();
    Process process = pckg.getProcesses().iterator().next();
    ProcessDescriptor helper = (ProcessDescriptor) process.getMetaData().get("ProcessDescriptor");
    ProcessAssetDesc definition = fillProcessDefinition(helper, kieContainer);
    // cache the data if requested
    if (cache) {
        validateNonEmptyDeploymentIdAndProcessId(deploymentId, "no proc id");
        Map<String, ProcessDescriptor> definitions = null;
        synchronized (definitionCache) {
            Map<String, ProcessDescriptor> newDef = new ConcurrentHashMap<String, ProcessDescriptor>();
            definitions = definitionCache.putIfAbsent(deploymentId, newDef);
            if (definitions == null) {
                definitions = newDef;
            }
            definitions.put(process.getId(), helper);
        }
    }
    return definition;
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ProcessAssetDesc(org.jbpm.kie.services.impl.model.ProcessAssetDesc) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) KiePackage(org.kie.api.definition.KiePackage) Process(org.kie.api.definition.process.Process) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 18 with ByteArrayResource

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

the class TraitTest method testdeleteThings.

@Test
public void testdeleteThings() {
    String s1 = "package test;\n" + "import org.drools.core.factmodel.traits.*;\n" + "import org.drools.traits.core.factmodel.*;\n" + "global java.util.List list; \n" + "" + "declare trait A id : int end\n" + "declare trait B extends A end\n" + "declare trait C extends A end\n" + "declare trait D extends A end\n" + "declare trait E extends B end\n" + "" + "declare Core @Traitable id : int = 0 end \n" + "" + "rule \"Init\" when \n" + "then \n" + "   insert( new Core() );" + "end \n" + "" + "rule \"donManyThing\"\n" + "when\n" + "    $x : Core( id == 0 )\n" + "then\n" + "    don( $x, A.class );\n" + "    don( $x, B.class );\n" + "    don( $x, C.class );\n" + "    don( $x, D.class );\n" + "    don( $x, E.class );\n" + "end\n" + "\n" + "\n" + "" + "rule \"Mod\" \n" + "salience -10 \n" + "when \n" + "  $g : String( this == \"go\" ) \n" + "  $x : Core( id == 0 ) \n" + "then \n" + "  delete( $x ); \n\n" + "  delete( $g ); \n\n" + "end \n" + "";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    TraitFactoryImpl.setMode(mode, kbase);
    kbase.addPackages(kbuilder.getKnowledgePackages());
    List list = new ArrayList();
    KieSession ksession = kbase.newKieSession();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    ksession.insert("go");
    ksession.fireAllRules();
    for (Object o : ksession.getObjects()) {
        System.out.println(o);
    }
    assertEquals(0, ksession.getObjects().size());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) Test(org.junit.Test)

Example 19 with ByteArrayResource

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

the class TraitTest method testTraitModifyCore3.

@Test
public void testTraitModifyCore3() {
    String s1 = "package test;\n" + "import org.drools.core.factmodel.traits.*;\n" + "import org.drools.traits.core.factmodel.*;\n" + "global java.util.List list; \n" + "" + "declare trait A id : int end\n" + "declare trait B extends A end\n" + "declare trait C extends A end\n" + "declare trait D extends A end\n" + "declare trait E extends B end\n" + "declare trait F extends C end\n" + "declare trait G extends D end\n" + "declare trait H extends D end\n" + "declare trait I extends E end\n" + "declare trait J extends F end\n" + "declare trait K extends G, H end\n" + "declare trait L extends G, H end\n" + "declare trait M extends I, J end\n" + "declare trait N extends K, L end\n" + "" + "declare Core @Traitable id : int = 0 end \n" + "" + "rule \"Init\" when \n" + "then \n" + "   insert( new Core() );" + "end \n" + "" + "rule \"donManyThing\"\n" + "when\n" + "    $x : Core( id == 0 )\n" + "then\n" + "    don( $x, A.class );\n" + "    don( $x, B.class );\n" + "    don( $x, D.class );\n" + "    don( $x, F.class );\n" + "    don( $x, E.class );\n" + "    don( $x, I.class );\n" + "    don( $x, K.class );\n" + "    don( $x, J.class );\n" + "    don( $x, C.class );\n" + "    don( $x, H.class );\n" + "    don( $x, G.class );\n" + "    don( $x, L.class );\n" + "    don( $x, M.class );\n" + "    don( $x, N.class );\n" + "end\n" + "\n" + "\n" + "\n" + "rule \"Log A\" when $x : A( id == 1 ) then System.out.println( \"A >> \" +  $x ); list.add( 1 ); end \n" + "rule \"Log B\" when $x : B( id == 1 ) then System.out.println( \"B >> \" +  $x ); list.add( 2 ); end \n" + "rule \"Log C\" when $x : C( id == 1 ) then System.out.println( \"C >> \" +  $x ); list.add( 3 ); end \n" + "rule \"Log D\" when $x : D( id == 1 ) then System.out.println( \"D >> \" +  $x ); list.add( 4 ); end \n" + "rule \"Log E\" when $x : E( id == 1 ) then System.out.println( \"E >> \" +  $x ); list.add( 5 ); end \n" + "rule \"Log F\" when $x : F( id == 1 ) then System.out.println( \"F >> \" +  $x ); list.add( 6 ); end \n" + "rule \"Log G\" when $x : G( id == 1 ) then System.out.println( \"G >> \" +  $x ); list.add( 7 ); end \n" + "rule \"Log H\" when $x : H( id == 1 ) then System.out.println( \"H >> \" +  $x ); list.add( 8 ); end \n" + "rule \"Log I\" when $x : I( id == 1 ) then System.out.println( \"I >> \" +  $x ); list.add( 9 ); end \n" + "rule \"Log J\" when $x : J( id == 1 ) then System.out.println( \"J >> \" +  $x ); list.add( 10 ); end \n" + "rule \"Log K\" when $x : K( id == 1 ) then System.out.println( \"K >> \" +  $x ); list.add( 11 ); end \n" + "rule \"Log L\" when $x : L( id == 1 ) then System.out.println( \"L >> \" +  $x ); list.add( 12 ); end \n" + "rule \"Log M\" when $x : M( id == 1 ) then System.out.println( \"M >> \" +  $x ); list.add( 13 ); end \n" + "rule \"Log N\" when $x : N( id == 1 ) then System.out.println( \"N >> \" +  $x ); list.add( 14 ); end \n" + "" + "rule \"Log Core\" when $x : Core( $id : id ) then System.out.println( \"Core >>>>>> \" +  $x ); end \n" + "" + "rule \"Mod\" \n" + "salience -10 \n" + "when \n" + "  String( this == \"go\" ) \n" + "  $x : Core( id == 0 ) \n" + "then \n" + "  System.out.println( \" ------------------------------------------------------------------------------ \" ); \n" + "  modify ( $x ) { setId( 1 ); }" + "end \n" + "";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    // not relevant
    TraitFactoryImpl.setMode(mode, kbase);
    kbase.addPackages(kbuilder.getKnowledgePackages());
    List list = new ArrayList();
    KieSession ksession = kbase.newKieSession();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    ksession.insert("go");
    ksession.fireAllRules();
    assertEquals(14, list.size());
    for (int j = 1; j <= 14; j++) {
        assertTrue(list.contains(j));
    }
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) NamedEntryPoint(org.drools.kiesession.entrypoints.NamedEntryPoint) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) Test(org.junit.Test)

Example 20 with ByteArrayResource

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

the class TraitTypeGenerationTest method testMultipleInheritanceWithPosition4.

@Test
public void testMultipleInheritanceWithPosition4() throws Exception {
    // DROOLS-249
    String drl = "package org.drools.test\n" + "declare trait PosTrait\n" + "@propertyReactive\n" + " field0 : int = 100 @position(1)\n" + " field1 : int = 101 @position(0)\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("field1", sw.getFields().get(0).getName());
    assertEquals("field2", sw.getFields().get(1).getName());
    assertEquals("mfield0", sw.getFields().get(2).getName());
    assertEquals("field0", sw.getFields().get(3).getName());
    assertEquals("mfield1", sw.getFields().get(4).getName());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) FactType(org.kie.api.definition.type.FactType) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) Test(org.junit.Test)

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