Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2015 JBoss Inc
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
 */
 
 package org.drools.integrationtests;
 
 import  org.junit.Test;
 
 import java.util.List;
 
 import static junit.framework.Assert.assertNull;
 import static junit.framework.Assert.assertTrue;
 import static junit.framework.TestCase.fail;
 import static org.junit.Assert.assertEquals;
 
 public class MiscTest {
 
     protected KnowledgeBase loadKnowledgeBaseFromString(String... drlContentStrings) {
         return loadKnowledgeBaseFromStringnullnulldrlContentStrings );
     }
 
                                                         String... drlContentStrings) {
         return loadKnowledgeBaseFromStringconfignulldrlContentStrings );
     }
 
                                                         String... drlContentStrings) {
         return loadKnowledgeBaseFromStringnullkBaseConfigdrlContentStrings );
     }
 
                                                         KnowledgeBaseConfiguration kBaseConfig,
                                                         String... drlContentStrings) {
         KnowledgeBuilder kbuilder = config == null ? KnowledgeBuilderFactory.newKnowledgeBuilder() : KnowledgeBuilderFactory.newKnowledgeBuilderconfig );
         for ( String drlContentString : drlContentStrings ) {
             kbuilder.add( ResourceFactory.newByteArrayResource(drlContentString.getBytes()),
                           . );
         }
 
         if ( kbuilder.hasErrors() ) {
             failkbuilder.getErrors().toString() );
         }
         if ( kBaseConfig == null ) {
             kBaseConfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
         }
         KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBasekBaseConfig );
         kbase.addKnowledgePackageskbuilder.getKnowledgePackages() );
         return kbase;
     }
 
     @Test
     public void testNullValueInFrom() {
         // DROOLS-71
         String str =
                 "global java.util.List list\n" +
                 "\n" +
                 "rule R\n" +
                 "when\n" +
                 "    $i : Integer( ) from list\n" +
                 "then\n" +
                 "end";
 
         KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
         StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
 
         List<Integerlist = new ArrayList<Integer>();
         ksession.setGlobal("list"list);
        list.add(1);
        list.add(null);
        list.add(2);
        ksession.fireAllRules();
    }
    @Test
    public void testErrorReporting() {
        String str =
                "global java.util.List list\n" +
                "\n" +
                "rule R\n" +
                "when\n" +
                "    $i : Intege( ) from list\n" +
                "then\n" +
                "end";
        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        kbuilder.add( ResourceFactory.newByteArrayResource(str.getBytes()),
                      . );
        assertTrue(kbuilder.hasErrors());
        assertTrue(kbuilder.getErrors().toString().contains("Intege"));
    }
    @Test
    public void testUsingSessionClock() {
        //BZ-1058687
        String str =
                "global java.util.List list\n" +
                "\n" +
                "rule R\n" +
                "when\n" +
                "    $i : Integer( ) from list\n" +
                "then\n" +
                "end";
        KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
        KnowledgeSessionConfiguration ksconfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
        ksconfig.setOption(ClockTypeOption.get("pseudo"));
        StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(ksconfignull);
        // compilation fails on Java 6
        SessionPseudoClock clock = ksession.getSessionClock();
    }
    @Test
    public void testUsingNullConfiguration() {
        String str =
                "global java.util.List list\n" +
                "\n" +
                "rule R\n" +
                "when\n" +
                "    $i : Integer( ) from list\n" +
                "then\n" +
                "end";
        KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
        StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(nullnull);
    }
    
    @Test
    public void testConsequenceException() {
        // BZ-1077834
        String str =
                "package foo.bar\n" +
                "rule R\n" +
                "when\n" +
                "then\n" +
                "    throw new RuntimeException(\"foo\");" +
                "end";
        KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
        StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
        
        try {
            ksession.fireAllRules();
        } catchorg.drools.runtime.rule.ConsequenceException e ) {
            // this is correct, succeeds
        } catchException other) {
            fail("Wrong exception raised = "+other.getClass().getCanonicalName());
        }
    }
    @Test
    public void testConsequenceException2() {
        // BZ-1077834
        String str
                = "package foo.bar\n"
                  + "rule R\n"
                  + "when\n"
                  + "then\n"
                  + "    throw new RuntimeException(\"foo\");"
                  + "end";
        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes(Charset.forName("UTF-8"))), .);
        if (kbuilder.hasErrors()) {
            throw new RuntimeException("Drools compile errors: " + kbuilder.getErrors().toString());
        }
        KnowledgeBase kbase = kbuilder.newKnowledgeBase();
        StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
        try {
            ksession.fireAllRules();
        } catch (org.drools.runtime.rule.ConsequenceException e) {
            // this is correct, succeeds
        } catch (Exception other) {
            fail("Wrong exception raised = " + other.getClass().getCanonicalName());
        }
    }
    @Test
    public void testNotExistingEntryPoint() {
        // BZ-1099767
        String str =
                "package foo.bar\n" +
                "rule R\n" +
                "when\n" +
                "then\n" +
                "end";
        KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
        StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
        WorkingMemoryEntryPoint entryPoint = ksession.getWorkingMemoryEntryPoint("x");
        assertNull(entryPoint);
    }
    @Test
    public void testQueriesWithVariableUnification() throws Exception {
        // BZ-1100820
        String str = "";
        str += "package org.drools.compiler.test  \n";
        str += "import org.drools.model.Person \n";
        str += "query peeps( String $name, String $likes, int $age ) \n";
        str += "    $p : Person( $name := name, $likes := likes, $age := age ) \n";
        str += "end\n";
        KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
        StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
        Person p1 = new Person"darth",
                                "stilton",
                                100 );
        Person p2 = new Person"yoda",
                                "stilton",
                                300 );
        Person p3 = new Person"luke",
                                "brie",
                                300 );
        Person p4 = new Person"bobba",
                                "cheddar",
                                300 );
        ksession.insertp1 );
        ksession.insertp2 );
        ksession.insertp3 );
        ksession.insertp4 );
        QueryResults results = ksession.getQueryResults"peeps"new Object[]{...} );
        assertEquals( 4,
                      results.size() );
        List names = new ArrayList();
        for ( QueryResultsRow row : results ) {
            names.add( ((Personrow.get"$p" )).getName() );
        }
        assertEquals( 4,
                      names.size() );
        assertTruenames.contains"luke" ) );
        assertTruenames.contains"yoda" ) );
        assertTruenames.contains"bobba" ) );
        assertTruenames.contains"darth" ) );
        results = ksession.getQueryResults"peeps",
                                            new Object[]{.., 300} );
        assertEquals( 3,
                      results.size() );
        names = new ArrayList();
        for ( QueryResultsRow row : results ) {
            names.add( ((Personrow.get"$p" )).getName() );
        }
        assertEquals( 3,
                      names.size() );
        assertTruenames.contains"luke" ) );
        assertTruenames.contains"yoda" ) );
        assertTruenames.contains"bobba" ) );
        results = ksession.getQueryResults"peeps",
                                            new Object[]{."stilton", 300} );
        assertEquals( 1,
                      results.size() );
        names = new ArrayList();
        for ( QueryResultsRow row : results ) {
            names.add( ((Personrow.get"$p" )).getName() );
        }
        assertEquals( 1,
                      names.size() );
        assertTruenames.contains"yoda" ) );
        results = ksession.getQueryResults"peeps",
                                            new Object[]{."stilton".} );
        assertEquals( 2,
                      results.size() );
        names = new ArrayList();
        for ( QueryResultsRow row : results ) {
            names.add( ((Personrow.get"$p" )).getName() );
        }
        assertEquals( 2,
                      names.size() );
        assertTruenames.contains"yoda" ) );
        assertTruenames.contains"darth" ) );
        results = ksession.getQueryResults"peeps",
                                            new Object[]{"darth"..} );
        assertEquals( 1,
                      results.size() );
        names = new ArrayList();
        for ( QueryResultsRow row : results ) {
            names.add( ((Personrow.get"$p" )).getName() );
        }
        assertEquals( 1,
                      names.size() );
        assertTruenames.contains"darth" ) );
    }
    @Test
    public void testNotExistingFactHandle() {
        // BZ-1119731
        String str =
                "package foo.bar\n" +
                "rule R\n" +
                "when\n" +
                "then\n" +
                "end";
        KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
        StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
        FactHandle handle = ksession.getFactHandle("fact");
        assertNull(handle);
    }
New to GrepCode? Check out our FAQ X