Package org.drools.simulation.fluent.simulation.impl

Examples of org.drools.simulation.fluent.simulation.impl.DefaultSimulationFluent


        ReleaseId releaseId = TestUtils.createKJarWithMultipleResources("TestKbase",
                new String[]{"BPMN-SimpleExclusiveGatewayProcess.bpmn2"}, new ResourceType[]{ResourceType.BPMN2});
       
        SimulationDataProvider provider = context.getDataProvider();
       
        SimulationFluent f = new DefaultSimulationFluent();
        // @formatter:off
        // FIXME why building knowledge base on this level does not work??
        int numberOfAllInstances = 10;
        int counter = 0;
        // default interval 2 seconds, meaning each step in a path will be started after 2 seconds
        long interval = 2*1000*60;
        for (SimulationPath path : paths) {
           
            double probability = provider.calculatePathProbability(path);
            f.newPath("path" + counter);
           
            // count how many instances/steps should current path have
            int instancesOfPath = (int) (numberOfAllInstances * probability);
           
            for (int i = 0; i < instancesOfPath; i++) {
                f.newStep( interval * i )
                    .newKieSession( releaseId, "TestKbase.KSession1" )
                        .end(World.ROOT, StatefulKnowledgeSession.class.getName())
                    .addCommand(new SimulateProcessPathCommand("defaultPackage.test", context, path));
            }
           
            counter++;
        }
        f.runSimulation();
        // @formatter:on
       
    }
View Full Code Here


        context.getRepository().setSimulationInfo(new SimulationInfo(System.currentTimeMillis(), processId, numberOfAllInstances, interval));
       
        ReleaseId releaseId = createKJarWithMultipleResources("TestKbase",
                new String[]{bpmn2Container}, new ResourceType[]{ResourceType.BPMN2});
       
        SimulationFluent f = new DefaultSimulationFluent();
        // @formatter:off       
        int counter = 0;
        int remainingInstances = numberOfAllInstances;
        for (SimulationPath path : paths) {
           
            double probability = provider.calculatePathProbability(path);
            f.newPath("path" + counter);

            int instancesOfPath = 1;
            // count how many instances/steps should current path have
            if (numberOfAllInstances > 1) {
                instancesOfPath = (int) Math.round((numberOfAllInstances * probability));
               
                // ensure that we won't exceed total number of instance due to rounding
                if (instancesOfPath > remainingInstances) {
                    instancesOfPath = remainingInstances;
                }
               
                remainingInstances -= instancesOfPath;
                       
                for (int i = 0; i < instancesOfPath; i++) {
                    f.newStep( interval * i )
                        .newKieSession( releaseId, "TestKbase.KSession1" )
                            .end(World.ROOT, StatefulKnowledgeSession.class.getName())
                        .addCommand(new SimulateProcessPathCommand(processId, context, path));
                }
            } else {
                f.newStep( interval )
                .newKieSession( releaseId, "TestKbase.KSession1" )
                    .end(World.ROOT, StatefulKnowledgeSession.class.getName())
                .addCommand(new SimulateProcessPathCommand(processId, context, path));
                break;
            }
           
            counter++;
        }
        f.runSimulation();
        // @formatter:on
       
        context.getRepository().getSimulationInfo().setEndTime(context.getMaxEndTime());
       
        return context.getRepository();
View Full Code Here

        context.getRepository().setSimulationInfo(new SimulationInfo(System.currentTimeMillis(), processId, numberOfAllInstances, interval));
       
        ReleaseId releaseId = createKJarWithMultipleResources("TestKbase",
                new String[]{bpmn2Container}, new ResourceType[]{ResourceType.BPMN2});
       
        SimulationFluent f = new DefaultSimulationFluent();
        // @formatter:off       
        int counter = 0;
        int remainingInstances = numberOfAllInstances;
        for (SimulationPath path : paths) {
           
            double probability = provider.calculatePathProbability(path);
            f.newPath("path" + counter);

            int instancesOfPath = 1;
            // count how many instances/steps should current path have
            if (numberOfAllInstances > 1) {
                instancesOfPath = (int) Math.round((numberOfAllInstances * probability));
               
                // ensure that we won't exceed total number of instance due to rounding
                if (instancesOfPath > remainingInstances) {
                    instancesOfPath = remainingInstances;
                }
               
                remainingInstances -= instancesOfPath;
                       
                for (int i = 0; i < instancesOfPath; i++) {
                    f.newStep( interval * i )
                        .newKieSession( releaseId, "TestKbase.KSession1" )
                            .end(World.ROOT, StatefulKnowledgeSession.class.getName())
                        .addCommand(new SimulateProcessPathCommand(processId, context, path));
                }
            } else {
                f.newStep( interval )
                .newKieSession( releaseId, "TestKbase.KSession1" )
                    .end(World.ROOT, StatefulKnowledgeSession.class.getName())
                .addCommand(new SimulateProcessPathCommand(processId, context, path));
                break;
            }
           
            counter++;
        }
        f.runSimulation();
        // @formatter:on
       
        context.getRepository().getSimulationInfo().setEndTime(context.getMaxEndTime());
       
        return context.getRepository();
View Full Code Here

        context.getRepository().setSimulationInfo(new SimulationInfo(System.currentTimeMillis(), processId, numberOfAllInstances, interval));
       
        final ReleaseId releaseId = createKJarWithMultipleResources(processId,
                new String[]{bpmn2Container}, new ResourceType[]{ResourceType.BPMN2});
       
        SimulationFluent f = new DefaultSimulationFluent(){

            final KieServices kieServices = KieServices.Factory.get();
            final KieContainer kieContainer = kieServices.newKieContainer( releaseId );

            @Override
            public KieSessionSimulationFluent newKieSession(final ReleaseId releaseId, final String id) {
                assureActiveStep();
                activeKieSessionId = id == null ? DEFAULT_ID : id;
                addCommand( new NewKieSessionCommand(null, null) {

                    @Override
                    public KieSession execute(Context context) {
                        return id != null ? kieContainer.newKieSession( id ) : kieContainer.newKieSession();
                    }
                });
                addCommand( new SetVariableCommandFromLastReturn( StatefulKnowledgeSession.class.getName() ) );

                return new DefaultStatefulKnowledgeSessionSimFluent( this );
            }
        };

        // @formatter:off       
        int counter = 0;
        int remainingInstances = numberOfAllInstances;
        for (SimulationPath path : paths) {
            // only paths that can be started are considered
            if (!path.isStartable()) {
                continue;
            }
            double probability = path.getProbability();
            f.newPath("path" + counter);

            int instancesOfPath = 1;
            // count how many instances/steps should current path have
            if (numberOfAllInstances > 1) {
                instancesOfPath = (int) Math.round((numberOfAllInstances * probability));
               
                // ensure that we won't exceed total number of instance due to rounding
                if (instancesOfPath > remainingInstances) {
                    instancesOfPath = remainingInstances;
                }
               
                remainingInstances -= instancesOfPath;
                       
                for (int i = 0; i < instancesOfPath; i++) {
                    f.newStep( interval * i )
                        .newKieSession( releaseId, null)
                            .end()
                        .addCommand(new SimulateProcessPathCommand(processId, context, path))
                        .addCommand( new SetVariableCommandFromLastReturn( StatefulKnowledgeSession.class.getName() ))
                        .addCommand(new DisposeCommand());
                }
            } else {
                f.newStep(interval)
                .newKieSession(releaseId, null)
                    .end()
                .addCommand(new SimulateProcessPathCommand(processId, context, path))
                .addCommand( new SetVariableCommandFromLastReturn( StatefulKnowledgeSession.class.getName() ))
                .addCommand(new DisposeCommand());
                break;
            }
           
            counter++;
// currently standalone paths within single definition are not supported
//            if (probability == 1) {
//                // in case given path has probability of 100% there is a need to reset the remaining instances
//                // as this is standalone process path
//                remainingInstances = numberOfAllInstances;
//            }
        }
        f.runSimulation();
        // @formatter:on
       
        context.getRepository().getSimulationInfo().setEndTime(context.getMaxEndTime());

        return context.getRepository();
View Full Code Here

        context.getRepository().setSimulationInfo(new SimulationInfo(System.currentTimeMillis(), processId, numberOfAllInstances, interval));
       
        ReleaseId releaseId = createKJarWithMultipleResources("TestKbase",
                new String[]{bpmn2Container}, new ResourceType[]{ResourceType.BPMN2});
       
        SimulationFluent f = new DefaultSimulationFluent();
        // @formatter:off       
        int counter = 0;
        int remainingInstances = numberOfAllInstances;
        for (SimulationPath path : paths) {
           
            double probability = provider.calculatePathProbability(path);
            f.newPath("path" + counter);

            int instancesOfPath = 1;
            // count how many instances/steps should current path have
            if (numberOfAllInstances > 1) {
                instancesOfPath = (int) Math.round((numberOfAllInstances * probability));
               
                // ensure that we won't exceed total number of instance due to rounding
                if (instancesOfPath > remainingInstances) {
                    instancesOfPath = remainingInstances;
                }
               
                remainingInstances -= instancesOfPath;
                       
                for (int i = 0; i < instancesOfPath; i++) {
                    f.newStep( interval * i )
                        .newKieSession( releaseId, "TestKbase.KSession1" )
                            .end(World.ROOT, StatefulKnowledgeSession.class.getName())
                        .addCommand(new SimulateProcessPathCommand(processId, context, path));
                }
            } else {
                f.newStep( interval )
                .newKieSession( releaseId, "TestKbase.KSession1" )
                    .end(World.ROOT, StatefulKnowledgeSession.class.getName())
                .addCommand(new SimulateProcessPathCommand(processId, context, path));
                break;
            }
           
            counter++;
        }
        f.runSimulation();
        // @formatter:on
       
        context.getRepository().getSimulationInfo().setEndTime(context.getMaxEndTime());
       
        return context.getRepository();
View Full Code Here

public class CompactFluentTest extends SimulateTestBase {

    @Test
    public void testSimpleForAllAssertionsTypes() throws IOException {
        SimulationFluent f = new DefaultSimulationFluent();

        VariableContext<Person> pc = f.<Person> getVariableContext();

        List<String> imports = new ArrayList<String>();
        imports.add( "org.junit.Assert.assertThat" );
        imports.add( "org.hamcrest.CoreMatchers.is" );
        imports.add( "org.hamcrest.CoreMatchers.equalTo" );
        imports.add( "org.hamcrest.CoreMatchers.allOf" );

        ReflectiveMatcherFactory rf = new ReflectiveMatcherFactory( imports );

        String str = "package org.drools.simulation.test\n" +
                     "import " + Person.class.getName() + "\n" +
                     "rule updateAge no-loop when  $p : Person() then modify( $p ) { setAge( $p.getAge() + 10 ) }; end\n";
       
        ReleaseId releaseId = createKJar( "org.test.KBase1", str );
       
        // @formatter:off       
        f.newKieSession( releaseId, "org.test.KBase1.KSession1" )
            .insert( new Person( "yoda", 150 ) ).set( "y" )
            .fireAllRules()
            // show testing inside of ksession execution
            .test( "y.name == 'yoda'" )
            .test( "y.age == 160" )
View Full Code Here

        // @formatter:on
    }

    @Test
    public void testAssertionsFail() throws IOException {
        SimulationFluent f = new DefaultSimulationFluent();

        VariableContext<Person> pc = f.<Person> getVariableContext();

        List<String> imports = new ArrayList<String>();
        imports.add( "org.junit.Assert.assertThat" );
        imports.add( "org.hamcrest.CoreMatchers.is" );
        imports.add( "org.hamcrest.CoreMatchers.equalTo" );
        imports.add( "org.hamcrest.CoreMatchers.allOf" );

        ReflectiveMatcherFactory rf = new ReflectiveMatcherFactory( imports );

        String str = "package org.drools.simulation.test\n" +
                     "import " + Person.class.getName() + "\n" +
                     "rule updateAge no-loop when  $p : Person() then modify( $p ) { setAge( $p.getAge() + 10 ) }; end\n";

        ReleaseId releaseId = createKJar( "org.test.KBase1", str );

        // @formatter:off       
        f.newKieSession(releaseId, "org.test.KBase1.KSession1")
            .insert( new Person( "yoda", 150 ) ).set( "y" )
            .fireAllRules()
            // show testing inside of ksession execution
            .test( "y.age == 110" );
        // @formatter:on   

        boolean fail = false;
        try {
            f.runSimulation();
        } catch ( AssertionError e ) {
            fail = true;
        }
        assertTrue( "Assertion should have failed",
                    fail );

        f = new DefaultSimulationFluent();
        // @formatter:off       
        f.newKieSession(releaseId, "org.test.KBase1.KSession1")
             .insert( new Person( "yoda", 150 ) ).set( "y" )
             .fireAllRules()
             // show testing inside of ksession execution
             .test( rf.assertThat( "y.age", matcher( "equalTo", "120" ) ) )
             .test( rf.assertThat( "y.age, equalTo(120)" ) );       
        // @formatter:on   
       

        fail = false;
        try {
            f.runSimulation();
        } catch ( AssertionError e) {
            fail = true;
        }
        assertTrue( "Assertion should have failed", fail );
View Full Code Here

        //        }         
    }

    @Test @Ignore("Doing newKSession on the same path twice doesn't make the second one the active one") // TODO FIXME
    public void testMultipleKsessionsWithSteps() throws IOException {
        SimulationFluent f = new DefaultSimulationFluent();

        VariableContext<Person> pc = f.<Person> getVariableContext();

        List<String> imports = new ArrayList<String>();
        imports.add( "org.junit.Assert.assertThat" );
        imports.add( "org.hamcrest.CoreMatchers.is" );
        imports.add( "org.hamcrest.CoreMatchers.equalTo" );
        imports.add( "org.hamcrest.CoreMatchers.allOf" );
        //        imports.add( PersonMatchers.class.getName() + ".name" );
        //        imports.add( PersonMatchers.class.getName() + ".age" );

        ReflectiveMatcherFactory rf = new ReflectiveMatcherFactory( imports );

        String str1 = "package org.drools.simulation.test\n" +
                      "import " + Person.class.getName() + "\n" +
                      "rule updateAge1 no-loop when  $p : Person() then modify( $p ) { setAge( $p.getAge() + 10 ) }; end\n";

        String str2 = "package org.drools.simulation.test\n" +
                      "import " + Person.class.getName() + "\n" +
                      "rule updateAge2 no-loop when  $p : Person() then modify( $p ) { setAge( $p.getAge() + 20 ) }; end\n";

        ReleaseId releaseId = createKJar( "org.test.KBase1", str1,
                    "org.test.KBase2", str2 );

        // @formatter:off
        f.newRelativeStep( 100 )
        .newKieSession(releaseId, "org.test.KBase1.KSession1")
            .insert( new Person( "yoda1",
                                 150 ) ).set( "y1" )
            .fireAllRules()
            .newRelativeStep( 200 )
View Full Code Here

public class StandardjBPM5FluentTest extends SimulateTestBase {

    @Test
    public void testUsingImplicit() throws IOException {
        SimulationFluent f = new DefaultSimulationFluent();
       
        VariableContext<Person> pc = f.<Person> getVariableContext();

        List<String> imports = new ArrayList<String>();
        imports.add( "org.hamcrest.MatcherAssert.assertThat" );
        imports.add( "org.hamcrest.CoreMatchers.is" );
        imports.add( "org.hamcrest.CoreMatchers.equalTo" );
        imports.add( "org.hamcrest.CoreMatchers.allOf" );

        ReflectiveMatcherFactory rf = new ReflectiveMatcherFactory( imports );

        String str = "package org.drools.simulation.test\n" +
                "import " + Person.class.getName() + "\n" +
                "global java.util.List list\n" +
                "rule setTime\n" +
                "  when\n" +
                "  then\n" +
                "    list.add( kcontext.getKnowledgeRuntime().getSessionClock().getCurrentTime() );\n" +
                "end\n" +
                "rule updateAge no-loop\n" +
                "  when\n" +
                "    $p : Person()\n" +
                "  then\n" +
                "    list.add( kcontext.getKnowledgeRuntime().getSessionClock().getCurrentTime() );\n" +
                "    modify( $p ) {\n" +
                "      setAge( $p.getAge() + 10 )\n" +
                "    };\n" +
                "end\n";
        String strProcess = "<definitions id='Definition' "
                + "targetNamespace='http://www.jboss.org/drools' "
                + "typeLanguage='http://www.java.com/javaTypes' "
                + "expressionLanguage='http://www.mvel.org/2.0' "
                + "xmlns='http://www.omg.org/spec/BPMN/20100524/MODEL' "
                + "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
                + "xsi:schemaLocation='http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd' "
                + "xmlns:g='http://www.jboss.org/drools/flow/gpd' "
                + "xmlns:bpmndi='http://www.omg.org/spec/BPMN/20100524/DI' "
                + "xmlns:dc='http://www.omg.org/spec/DD/20100524/DC' "
                + "xmlns:di='http://www.omg.org/spec/DD/20100524/DI' "
                + "xmlns:tns='http://www.jboss.org/drools'>"
                            + " <process id='DummyProcess' name='Sample Process'>"
                                    + "<startEvent id='_1' name='StartProcess' />"
                                    + "<scriptTask id='_2' name='Script 1' >"
                                       + "<script>System.out.println('Script 1 - Executing .. ');</script> "
                                    + "</scriptTask>"
                                    + "<scriptTask id='_3' name='Script 2' >"
                                        + "<script>System.out.println('Script 2 - Executing .. ');</script>"
                                    + "</scriptTask>"
                                    + "<endEvent id='_4' name='End' >"
                                        + "<terminateEventDefinition/>"
                                    + "</endEvent>"
                                    + "<sequenceFlow id='_1-_2' sourceRef='_1' targetRef='_2' />"
                                    + "<sequenceFlow id='_2-_3' sourceRef='_2' targetRef='_3' />"
                                    + "<sequenceFlow id='_3-_4' sourceRef='_3' targetRef='_4' />"
                            + "</process>"
                + "</definitions>";
       
        ReleaseId releaseId = createKJarWithMultipleResources( "org.test.KBase1", new String[] {str, strProcess} , new ResourceType[] {ResourceType.DRL, ResourceType.BPMN2} );
       
        List list = new ArrayList();
       
        VariableContext<?> vc = f.getVariableContext();
        // @formatter:off         
        f.newPath("init")
            .newStep( 0 )
                .newKieSession( releaseId, "org.test.KBase1.KSession1" )
                    .setGlobal( "list", list ).set( "list" )
                    .startProcess("DummyProcess")
                    .fireAllRules()
View Full Code Here

public class SimulationFluentTest extends SimulateTestBase {

    @Test
    public void testUsingImplicit() throws IOException {
        SimulationFluent f = new DefaultSimulationFluent();
       
        VariableContext<Person> pc = f.<Person> getVariableContext();

        List<String> imports = new ArrayList<String>();
        imports.add( "org.hamcrest.MatcherAssert.assertThat" );
        imports.add( "org.hamcrest.CoreMatchers.is" );
        imports.add( "org.hamcrest.CoreMatchers.equalTo" );
        imports.add( "org.hamcrest.CoreMatchers.allOf" );

        ReflectiveMatcherFactory rf = new ReflectiveMatcherFactory( imports );

        String str = "package org.test\n" +
                     "import " + Person.class.getName() + "\n" +
                     "global java.util.List list\n" +
                     "rule setTime when then list.add( kcontext.getKnowledgeRuntime().getSessionClock().getCurrentTime() );\n end\n " +
                     "rule updateAge no-loop when  $p : Person() then list.add( kcontext.getKnowledgeRuntime().getSessionClock().getCurrentTime() );\n modify( $p ) { setAge( $p.getAge() + 10 ) }; end\n";
       
        ReleaseId releaseId = createKJar( "org.test.KBase1", str );
       
        List list = new ArrayList();
       
        VariableContext<?> vc = f.getVariableContext();
        // @formatter:off         
        f.newPath("init")
        .newStep( 0 )
        .newKieSession( releaseId, "org.test.KBase1.KSession1" )
            .setGlobal( "list", list ).set( "list" )
            .fireAllRules()
            .end()
View Full Code Here

TOP

Related Classes of org.drools.simulation.fluent.simulation.impl.DefaultSimulationFluent

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.