Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.05 KB | None | 0 0
  1. /*
  2.  * Copyright 2017 Red Hat, Inc. and/or its affiliates.
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *       http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16.  
  17. package org.drools.compiler.integrationtests;
  18.  
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import java.util.concurrent.Callable;
  22. import java.util.concurrent.CompletionService;
  23. import java.util.concurrent.Executor;
  24. import java.util.concurrent.ExecutorCompletionService;
  25. import java.util.concurrent.Executors;
  26. import java.util.concurrent.ThreadFactory;
  27.  
  28. import org.junit.Test;
  29. import org.kie.api.KieBase;
  30. import org.kie.api.io.ResourceType;
  31. import org.kie.api.runtime.KieSession;
  32. import org.kie.internal.utils.KieHelper;
  33.  
  34. import static org.junit.Assert.assertEquals;
  35.  
  36. public class ConcurrentSessionsTest {
  37.  
  38.     interface KieSessionExecutor {
  39.         boolean execute(KieSession kieSession, int counter);
  40.     }
  41.  
  42.     private void parallelTest(int repetitions, int threadCount, final KieSessionExecutor kieSessionExecutor, String... drls) {
  43.         for (int rep = 0; rep < repetitions; rep++) {
  44.             KieHelper kieHelper = new KieHelper();
  45.             for (String drl : drls) {
  46.                 kieHelper.addContent( drl, ResourceType.DRL );
  47.             }
  48.             final KieBase kieBase = kieHelper.build();
  49.  
  50.             Executor executor = Executors.newFixedThreadPool( threadCount, new ThreadFactory() {
  51.                 public Thread newThread( Runnable r ) {
  52.                     Thread t = new Thread( r );
  53.                     t.setDaemon( true );
  54.                     return t;
  55.                 }
  56.             } );
  57.  
  58.             Callable<Boolean>[] tasks = new Callable[threadCount];
  59.  
  60.             for ( int i = 0; i < threadCount; i++ ) {
  61.                 final int counter = i;
  62.                 tasks[i] = new Callable<Boolean>() {
  63.                     @Override
  64.                     public Boolean call() throws Exception {
  65.                         return kieSessionExecutor.execute( kieBase.newKieSession(), counter );
  66.                     }
  67.                 };
  68.             }
  69.  
  70.             CompletionService<Boolean> ecs = new ExecutorCompletionService<Boolean>( executor );
  71.             for ( Callable<Boolean> task : tasks ) {
  72.                 ecs.submit( task );
  73.             }
  74.  
  75.             int successCounter = 0;
  76.             for ( int i = 0; i < threadCount; i++ ) {
  77.                 try {
  78.                     if ( ecs.take().get() ) {
  79.                         successCounter++;
  80.                     }
  81.                 } catch (Exception e) {
  82.                     throw new RuntimeException( e );
  83.                 }
  84.             }
  85.  
  86.             assertEquals( threadCount, successCounter );
  87.         }
  88.     }
  89.  
  90.     @Test
  91.     public void test1() {
  92.         String drl = "rule R when String() then end";
  93.  
  94.         parallelTest( 10, 10, new KieSessionExecutor() {
  95.             @Override
  96.             public boolean execute( KieSession kieSession, int counter ) {
  97.                 kieSession.insert( "test" );
  98.                 return kieSession.fireAllRules() == 1;
  99.             }
  100.         }, drl );
  101.     }
  102.  
  103.     @Test
  104.     public void test2() {
  105.         String drl1 =
  106.                 "import " + Product.class.getCanonicalName() + ";\n" +
  107.                 "rule R1 when\n" +
  108.                 "  $s : String( this == \"odd\" )\n" +
  109.                 "  $p : Product( category == $s, firings not contains \"R1\" )\n" +
  110.                 "then\n" +
  111.                 "  $p.getFirings().add(\"R1\");\n" +
  112.                 "  $p.appendDescription($s);\n" +
  113.                 "  update($p);\n" +
  114.                 "end\n";
  115.  
  116.         String drl2 =
  117.                 "import " + Product.class.getCanonicalName() + ";\n" +
  118.                 "rule R2 when\n" +
  119.                 "  $s : String( this == \"pair\" )\n" +
  120.                 "  $p : Product( category == $s, firings not contains \"R2\" )\n" +
  121.                 "then\n" +
  122.                 "  $p.getFirings().add(\"R2\");\n" +
  123.                 "  $p.appendDescription($s);\n" +
  124.                 "  update($p);" +
  125.                 "end\n";
  126.  
  127.         parallelTest( 10, 10, new KieSessionExecutor() {
  128.             @Override
  129.             public boolean execute( KieSession kieSession, int counter ) {
  130.                 Product[] products = new Product[10];
  131.                 for (int i = 0; i < 10; i++) {
  132.                     products[i] = new Product( "" + i, i % 2 == 0 ? "pair" : "odd" );
  133.                 }
  134.  
  135.                 kieSession.insert( "odd" );
  136.                 kieSession.insert( "pair" );
  137.                 for (int i = 0; i < 10; i++) {
  138.                     kieSession.insert( products[i] );
  139.                 }
  140.  
  141.                 kieSession.fireAllRules();
  142.  
  143.                 for (int i = 0; i < 10; i++) {
  144.                     if ( !products[i].getCategory().equals( products[i].getDescription() ) ) {
  145.                         return false;
  146.                     }
  147.                 }
  148.                 return true;
  149.             }
  150.         }, drl1, drl2 );
  151.     }
  152.  
  153.     @Test
  154.     public void test3() {
  155.         String drl1 =
  156.                 "import " + Product.class.getCanonicalName() + ";\n" +
  157.                 "rule R1 when\n" +
  158.                 "  $s : String( this == \"odd\" )\n" +
  159.                 "  $p : Product( category == $s, firings not contains \"R1\" )\n" +
  160.                 "then\n" +
  161.                 "  $p.getFirings().add(\"R1\");\n" +
  162.                 "  $p.appendDescription($s);\n" +
  163.                 "  update($p);\n" +
  164.                 "end\n";
  165.  
  166.         String drl2 =
  167.                 "import " + Product.class.getCanonicalName() + ";\n" +
  168.                 "rule R2 when\n" +
  169.                 "  $s : String( this == \"pair\" )\n" +
  170.                 "  $p : Product( category == $s, firings not contains \"R2\" )\n" +
  171.                 "then\n" +
  172.                 "  $p.getFirings().add(\"R2\");\n" +
  173.                 "  $p.appendDescription($s);\n" +
  174.                 "  update($p);" +
  175.                 "end\n";
  176.  
  177.         parallelTest( 10, 10, new KieSessionExecutor() {
  178.             @Override
  179.             public boolean execute( KieSession kieSession, int counter ) {
  180.                 Product[] products = new Product[10];
  181.                 for (int i = 0; i < 10; i++) {
  182.                     products[i] = new Product( "" + i, i % 2 == 0 ? "pair" : "odd" );
  183.                 }
  184.  
  185.                 boolean pair = counter % 2 == 0;
  186.                 kieSession.insert( pair ? "pair" : "odd" );
  187.  
  188.                 for (int i = 0; i < 10; i++) {
  189.                     kieSession.insert( products[i] );
  190.                 }
  191.  
  192.                 kieSession.fireAllRules();
  193.  
  194.                 for (int i = 0; i < 10; i++) {
  195.                     if ( pair ) {
  196.                         if ( products[i].getCategory().equals( "pair" ) && !products[i].getDescription().equals( "pair" ) ) {
  197.                             return false;
  198.                         }
  199.                         if ( products[i].getCategory().equals( "odd" ) && !products[i].getDescription().equals( "" ) ) {
  200.                             return false;
  201.                         }
  202.                     }
  203.                     if ( !pair ) {
  204.                         if ( products[i].getCategory().equals( "pair" ) && !products[i].getDescription().equals( "" ) ) {
  205.                             return false;
  206.                         }
  207.                         if ( products[i].getCategory().equals( "odd" ) && !products[i].getDescription().equals( "odd" ) ) {
  208.                             return false;
  209.                         }
  210.                     }
  211.                 }
  212.                 return true;
  213.             }
  214.         }, drl1, drl2 );
  215.     }
  216.  
  217.     @Test
  218.     public void test4() {
  219.         String drl1 =
  220.                 "import " + Product.class.getCanonicalName() + ";\n" +
  221.                 "rule R1 when\n" +
  222.                 "  $s : String()\n" +
  223.                 "  $p : Product( category == $s, firings not contains \"R1\" )" +
  224.                 "  $n : Number( intValue > 5 ) from accumulate (" +
  225.                 "    $s_1 : String( this == $s ) and" +
  226.                 "    $p_1 : Product( category == $s_1 )" +
  227.                 "    ;count($p_1))\n" +
  228.                 "then\n" +
  229.                 "  $p.getFirings().add(\"R1\");\n" +
  230.                 "  $p.appendDescription($s);\n" +
  231.                 "  update($p);\n" +
  232.                 "end\n";
  233.  
  234.         String drl2 =
  235.                 "import " + Product.class.getCanonicalName() + ";\n" +
  236.                 "rule R2 when\n" +
  237.                 "  $s : String()\n" +
  238.                 "  $p : Product( category == $s, firings not contains \"R2\" )" +
  239.                 "  $n : Number( intValue < 5 ) from accumulate (" +
  240.                 "    $s_1 : String( this == $s ) and" +
  241.                 "    $p_1 : Product( category == $s_1 )" +
  242.                 "    ;count($p_1))\n" +
  243.                 "then\n" +
  244.                 "  $p.getFirings().add(\"R2\");\n" +
  245.                 "  $p.appendDescription($s);\n" +
  246.                 "  update($p);" +
  247.                 "end\n";
  248.  
  249.         parallelTest( 10, 10, new KieSessionExecutor() {
  250.             @Override
  251.             public boolean execute( KieSession kieSession, int counter ) {
  252.                 Product[] products = new Product[10];
  253.                 for (int i = 0; i < 10; i++) {
  254.                     products[i] = new Product( "" + i, i % 3 == 0 ? "few" : "many" );
  255.                 }
  256.  
  257.                 kieSession.insert( "few" );
  258.                 kieSession.insert( "many" );
  259.  
  260.                 for (int i = 0; i < 10; i++) {
  261.                     kieSession.insert( products[i] );
  262.                 }
  263.  
  264.                 kieSession.fireAllRules();
  265.  
  266.                 for (int i = 0; i < 10; i++) {
  267.                     if ( !products[i].getCategory().equals( products[i].getDescription() ) ) {
  268.                         return false;
  269.                     }
  270.                 }
  271.                 return true;
  272.             }
  273.         }, drl1, drl2 );
  274.     }
  275.  
  276.     public static class Product {
  277.         private final String id;
  278.         private final String category;
  279.  
  280.         private List<String> firings = new ArrayList<String>();
  281.  
  282.         private String description = "";
  283.  
  284.         public Product( String id, String category ) {
  285.             this.id = id;
  286.             this.category = category;
  287.         }
  288.  
  289.         public String getId() {
  290.             return id;
  291.         }
  292.  
  293.         public List<String> getFirings() {
  294.             return firings;
  295.         }
  296.  
  297.         public String getCategory() {
  298.             return category;
  299.         }
  300.  
  301.         public String getDescription() {
  302.             return description;
  303.         }
  304.  
  305.         public void appendDescription( String append ) {
  306.             description += append;
  307.         }
  308.     }
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement