org.kie.api.definition.rule.Rule类的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(12.1k)|赞(0)|评价(0)|浏览(132)

本文整理了Java中org.kie.api.definition.rule.Rule类的一些代码示例,展示了Rule类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Rule类的具体详情如下:
包路径:org.kie.api.definition.rule.Rule
类名称:Rule

Rule介绍

[英]Public Rule interface for runtime rule inspection.
[中]用于运行时规则检查的公共规则接口。

代码示例

代码示例来源:origin: kiegroup/jbpm

public boolean accept(Match activation) {
    return !"Cancel".equals(activation.getRule().getName());
  }
}

代码示例来源:origin: kiegroup/optaplanner

/**
 * Reward a match by the {@link ConstraintWeight} multiplied with the weightMultiplier for all score levels.
 * @param kcontext never null, the magic variable in DRL
 * @param weightMultiplier at least 0
 */
public void reward(RuleContext kcontext, int weightMultiplier) {
  Rule rule = kcontext.getRule();
  BiConsumer<RuleContext, Integer> matchExecutor = matchExecutorByNumberMap.get(rule);
  if (matchExecutor == null) {
    throw new IllegalStateException("The DRL rule (" + rule.getPackageName() + ":" + rule.getName()
        + ") does not match a @" + ConstraintWeight.class.getSimpleName() + " on the @"
        + ConstraintConfiguration.class.getSimpleName() + " annotated class.");
  }
  matchExecutor.accept(kcontext, weightMultiplier);
}

代码示例来源:origin: kiegroup/optaplanner

.getConstraintConfigurationClass();
    throw new IllegalArgumentException("The constraintWeight (" + constraintWeight
        + ") for constraintPackage (" + rule.getPackageName()
        + ") and constraintName (" + rule.getName()
        + ") of constraintConfigurationClass (" + constraintConfigurationClass
        + ") must have an initScore (" + constraintWeight.getInitScore() + ") equal to 0.\n"
        .getConstraintConfigurationClass();
    throw new IllegalArgumentException("The @" + ConstraintWeight.class.getSimpleName()
        + " annotation for constraintPackage (" + rule.getPackageName()
        + ") and constraintName (" + rule.getName()
        + ") of constraintConfigurationClass (" + constraintConfigurationClass
        + ") must have a positive or zero constraintWeight (" + constraintWeight + ").\n"
          .getConstraintConfigurationClass();
      throw new IllegalArgumentException("The bendable constraintWeight (" + constraintWeight
          + ") for constraintPackage (" + rule.getPackageName()
          + ") and constraintName (" + rule.getName()
          + ") of constraintConfigurationClass (" + constraintConfigurationClass
          + ") has a hardLevelsSize (" + bendableConstraintWeight.getHardLevelsSize()
kieSession.setGlobal(GLOBAL_SCORE_HOLDER_KEY, workingScoreHolder);

代码示例来源:origin: org.drools/drools-compiler

kBase = (InternalKnowledgeBase) map.get( "x" );
KieSession session = kBase.newKieSession();
kBase.addPackages(kpkgs);
session.setGlobal( "list",
          new ArrayList() );
session.insert( bob );
((InternalWorkingMemory)session).flushPropagations();
org.kie.api.definition.rule.Rule[] rules = (org.kie.api.definition.rule.Rule[]) kBase.getPackage("org.drools.compiler.test").getRules().toArray(new org.kie.api.definition.rule.Rule[0] );
       rules[0].getName() );
assertEquals( "match Person 2",
       rules[1].getName() );
assertEquals( "match Person 3",
       rules[2].getName() );
assertEquals( "match Integer",
       rules[3].getName() );
       session.getObjects().size());
assertEquals( bob,
       IteratorToList.convert( session.getObjects().iterator() ).get( 0 ) );

代码示例来源:origin: org.drools/drools-compiler

private void testAgendaFilter(final AgendaFilter agendaFilter, final String expectedMatchingRuleName) {
    final String str = "package org.drools.compiler\n" +
        "rule Aaa when then end\n" +
        "rule Bbb when then end\n";

    final KieBase kbase = loadKnowledgeBaseFromString(str);
    final KieSession ksession = createKnowledgeSession(kbase);

    final org.kie.api.event.rule.AgendaEventListener ael = mock(org.kie.api.event.rule.AgendaEventListener.class);
    ksession.addEventListener(ael);

    final int rules = ksession.fireAllRules(agendaFilter);
    assertEquals(1, rules);

    final ArgumentCaptor<org.kie.api.event.rule.AfterMatchFiredEvent> arg = ArgumentCaptor.forClass(org.kie.api.event.rule.AfterMatchFiredEvent.class);
    verify(ael).afterMatchFired(arg.capture());
    assertThat(arg.getValue().getMatch().getRule().getName(), is(expectedMatchingRuleName));
  }
}

代码示例来源:origin: org.drools/drools-compiler

@Test
public void testPopulatedSingleRuleNoSharingWithSubnetworkAtStart() throws Exception {
  InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
  InternalWorkingMemory wm = ((InternalWorkingMemory)kbase.newKieSession());
  wm.insert(new A(1));
  wm.insert(new A(2));
  wm.insert(new D(1));
  wm.insert(new E(1));
  wm.insert(new C(2));
  wm.fireAllRules();
  kbase.addPackages( buildKnowledgePackage("r1", "   A() not( B() and C() ) D() E()\n") );
  List list = new ArrayList();
  wm.setGlobal("list", list);
  wm.fireAllRules();
  assertEquals(2, list.size() );
  assertEquals( "r1", ((Match)list.get(0)).getRule().getName() );
  assertEquals( "r1", ((Match)list.get(1)).getRule().getName() );
  kbase.removeRule("org.kie", "r1");
  wm.insert(new A(1));
  wm.fireAllRules();
  assertEquals(2, list.size() );
}

代码示例来源:origin: org.drools/drools-compiler

ksession.addEventListener( new AgendaEventListener() {
ksession.setGlobal( "list",
          list );
ksession.insert( "go1" );
FactHandle go2 = ksession.insert( "go2" );
ksession.fireAllRules();
       cancelled.size() );
assertEquals( "rule1",
       ((MatchCancelledEvent) cancelled.get( 0 )).getMatch().getRule().getName() );
ksession.dispose();

代码示例来源:origin: org.drools/drools-compiler

kbase.addPackages( Collections.singleton( kpkg ) );
kbase = map.get( "x" );
final org.kie.api.definition.rule.Rule[] rules = kbase.getKiePackages().iterator().next().getRules().toArray( new org.kie.api.definition.rule.Rule[0] );
assertEquals( 4,
       rules.length );
       rules[0].getName() );
assertEquals( "match Person 2",
       rules[1].getName() );
assertEquals( "match Person 3",
       rules[2].getName() );
assertEquals( "match Integer",
       rules[3].getName() );
KieSession ksession = kbase.newKieSession();
ksession.setGlobal( "list",
          new ArrayList() );
ksession.insert( bob );
       ksession.getFactCount() );
assertEquals( bob,
       ksession.getObjects().iterator().next() );

代码示例来源:origin: org.drools/drools-compiler

List list;
kbase.addPackages( kpkgs2 );
kbase = SerializationHelper.serializeObject(kbase);
assertEquals( 3, kbase.getKiePackages().size() );
KiePackage test = null, test2 = null;
assertNotNull( test2 );
assertEquals( "rule1",
       test.getRules().iterator().next().getName() );
assertEquals( "rule2",
       test2.getRules().iterator().next().getName() );
kbase.removeRule( test.getName(),
         test.getRules().iterator().next().getName() );
for( KiePackage kpkg : kbase.getKiePackages() ) {
       test2.getRules().size() );
assertEquals( "rule2",
       test2.getRules().iterator().next().getName() );
list = new ArrayList( ksession.getObjects( new ClassObjectFilter( Person.class ) ) );
kbase.removeRule( test2.getName(),
         test2.getRules().iterator().next().getName() );
kbase = SerializationHelper.serializeObject(kbase);

代码示例来源:origin: org.drools/drools-compiler

kbase.addPackages( kbuilder.getKnowledgePackages() );
KieSession ksession = kbase.newKieSession();
ksession.addEventListener( ael );
ksession.fireAllRules();
verify( ael, never() ).afterMatchFired( any( AfterMatchFiredEvent.class ) );
assertFalse( kbuilder.getErrors().toString(), kbuilder.hasErrors() );
ksession.fireAllRules();
ArgumentCaptor<AfterMatchFiredEvent> capt = ArgumentCaptor.forClass( AfterMatchFiredEvent.class );
verify( ael, times(1) ).afterMatchFired( capt.capture() );
assertThat( "R1", is( capt.getValue().getMatch().getRule().getName() ) );
ksession.fireAllRules();
verify( ael, times(2) ).afterMatchFired( capt.capture() );
assertThat( "R2", is( capt.getAllValues().get( 2 ).getMatch().getRule().getName() ) );
ksession.dispose();

代码示例来源:origin: org.kie/kie-api

@Override
public String getMessage() {
  StringBuilder sb = new StringBuilder( "Exception executing consequence for " );
  Rule rule = null;
  if( match != null && ( rule = match.getRule() ) != null ){
    String packageName = rule.getPackageName();
    String ruleName = rule.getName();
    sb.append( "rule \"" ).append( ruleName ).append( "\" in " ).append( packageName );
  } else {
    sb.append( "rule, name unknown" );
  }
  sb.append( ": " ).append( super.getMessage() );
  return sb.toString();
}

代码示例来源:origin: org.drools/drools-compiler

@Test
public void testQueryRemoval() throws Exception {
  KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("simple_query_test.drl"));
  KieSession session = createKieSession( kbase );
  final Cheese stilton = new Cheese( "stinky",
                    5 );
  session.insert( stilton );
  session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
  org.kie.api.runtime.rule.QueryResults results = session.getQueryResults( "simple query" );
  assertEquals( 1,
         results.size() );
  Rule rule = kbase.getKiePackage( "org.drools.compiler.test" ).getRules().iterator().next();
  assertEquals( "simple query",
         rule.getName());
  kbase.removeQuery( "org.drools.compiler.test",
            "simple query" );
  assertTrue( kbase.getKiePackage( "org.drools.compiler.test" ).getRules().isEmpty() );
  try {
    results = session.getQueryResults( "simple query" );
  } catch ( Exception e ) {
    assertTrue( e.getMessage().endsWith( "does not exist") );
  }
}

代码示例来源:origin: deepu105/drools-demo

@Override
public void afterMatchFired(AfterMatchFiredEvent event) {
  Rule rule = event.getMatch().getRule();
  String ruleName = rule.getName();
  Map<String, Object> ruleMetaDataMap = rule.getMetaData();
  activationList.add(new Activation(ruleName));
  StringBuilder sb = new StringBuilder("Rule fired: " + ruleName);
  if (ruleMetaDataMap.size() > 0) {
    sb.append("\n  With [" + ruleMetaDataMap.size() + "] meta-data:");
    for (String key : ruleMetaDataMap.keySet()) {
      sb.append("\n    key=" + key + ", value="
          + ruleMetaDataMap.get(key));
    }
  }
  log.debug(sb.toString());
}

代码示例来源:origin: org.drools/drools-compiler

@Override
  public void onDeleteMatch( Match match ) {
    String rule = match.getRule().getName();
    if (rule.equals( "Negative" )) {
      list.remove("-" + match.getDeclarationValue( "$n" ));
    }
  }
} );

代码示例来源:origin: icclab/cyclops

@Override
  public void afterMatchFired(AfterMatchFiredEvent event) {
    logger.log(LogConstants.RULE, String.format("Rule match fired for: \"%s\", on following objects: %s", event.getMatch().getRule().getName(), new Gson().toJson(event.getMatch().getObjects())));
    super.afterMatchFired(event);
  }
}

代码示例来源:origin: org.drools/drools-compiler

@Override
  public boolean accept(Rule rule) {
    return rule.getName().equals("xxx");
  }
}));

代码示例来源:origin: org.drools/knowledge-api

public Map<String, Object> getMetaData() {
  return delegate.getMetaData();
}

代码示例来源:origin: org.drools/knowledge-api

public String getPackageName() {
  return delegate.getPackageName();
}

代码示例来源:origin: org.drools/drools-compiler

KieSession session = kBase.newKieSession();
kBase = (InternalKnowledgeBase) map.get( "x" );
kBase.addPackages( kpkgs );
session.setGlobal( "list",
          new ArrayList() );
session.insert( bob );
((InternalWorkingMemory)session).flushPropagations();
org.kie.api.definition.rule.Rule[] rules = (org.kie.api.definition.rule.Rule[]) kBase.getPackage("org.drools.compiler.test").getRules().toArray(new org.kie.api.definition.rule.Rule[0] );
       rules[0].getName() );
assertEquals( "match Person 2",
       rules[1].getName() );
assertEquals( "match Person 3",
       rules[2].getName() );
assertEquals( "match Integer",
       rules[3].getName() );
assertEquals(1, session.getObjects().size());
assertEquals( bob,
       IteratorToList.convert( session.getObjects().iterator() ).get(0) );

代码示例来源:origin: org.drools/drools-compiler

@Test(timeout=10000)
public void testIsAOperator() {
  String source = "org/drools/compiler/factmodel/traits/testTraitIsA2.drl";
  KieSession ksession = getSession( source );
  TraitFactory.setMode( mode, ksession.getKieBase() );
  AgendaEventListener ael = mock( AgendaEventListener.class );
  ksession.addEventListener( ael );
  Person student = new Person( "student", 18 );
  ksession.insert( student );
  ksession.fireAllRules();
  ArgumentCaptor<AfterMatchFiredEvent> cap = ArgumentCaptor.forClass( AfterMatchFiredEvent.class );
  verify( ael,
      times( 3 ) ).afterMatchFired( cap.capture() );
  List<AfterMatchFiredEvent> values = cap.getAllValues();
  assertThat( values.get( 0 ).getMatch().getRule().getName(),
        is( "create student" ) );
  assertThat( values.get( 1 ).getMatch().getRule().getName(),
        is( "print student" ) );
  assertThat( values.get( 2 ).getMatch().getRule().getName(),
        is( "print school" ) );
}

相关文章