Java Code Examples for org.kie.api.definition.KiePackage#getRules()

The following examples show how to use org.kie.api.definition.KiePackage#getRules() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RuleEngine.java    From support-rulesengine with Apache License 2.0 6 votes vote down vote up
public List<String> getRulenames() {
  List<String> result = new ArrayList<>();
  try {
    KiePackage kpkg = kbase.getKiePackage(packageName);
    if (kpkg != null) {
      Collection<Rule> rules = kpkg.getRules();
      if (rules != null) {
        for (Rule rule : rules) {
          result.add(rule.getName());
        }
      }
    }
    return result;
  } catch (Exception ex) {
    logger.error(ex.getMessage());
    throw new ServiceException(ex);
  }
}
 
Example 2
Source File: BaseTest.java    From iot-ocp with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() {
    KieServices kieServices = KieServices.Factory.get();

    KieContainer kContainer = kieServices.getKieClasspathContainer();
    Results verifyResults = kContainer.verify();
    for (Message m : verifyResults.getMessages()) {
        LOG.debug("Kie container message: {}", m);
    }

    KieBase kieBase = kContainer.getKieBase();
    LOG.debug("Created kieBase");

    for ( KiePackage kp : kieBase.getKiePackages() ) {
        for (Rule rule : kp.getRules()) {
            LOG.debug("kp " + kp + " rule " + rule.getName());
        }
    }

    session = kieBase.newKieSession();
    LOG.info(session.getGlobals().toString());
    Logger kieLogger = LoggerFactory.getLogger(this.getClass());
   // session.setGlobal("logger",kieLogger);
}
 
Example 3
Source File: RulesConfiguration.java    From cia with Apache License 2.0 6 votes vote down vote up
/**
 * Kie container.
 *
 * @return the kie container
 */
@Bean
public KieContainer kieContainer() {
	final KieServices kieServices = KieServices.Factory.get();

	final KieContainer kContainer = kieServices.getKieClasspathContainer();
	LOGGER.info("Using classloader {}, parent {}",kContainer.getClassLoader(),kContainer.getClassLoader().getParent());
	
	final Results verifyResults = kContainer.verify();
	for (final Message m : verifyResults.getMessages()) {
		LOGGER.warn("Kie container message: {}", m);
	}

	final KieBase kieBase = kContainer.getKieBase();
	for (final KiePackage kp : kieBase.getKiePackages()) {
		for (final Rule rule : kp.getRules()) {
			LOGGER.info("Loadded Rule: {} {}", kp, rule.getName());
		}
	}
	

	return kContainer;
}
 
Example 4
Source File: DroolsUtil.java    From qzr with Apache License 2.0 6 votes vote down vote up
/**
 * Return a string containing the packages used to build the knowledge base.
 */
public static String knowledgeBaseDetails(KieBase kbase) {
    if (kbase == null) {
        return "Knowledge Base is null.";
    } else {
        StringBuilder sb = new StringBuilder(
                "Knowledge base built from the following packages:");
        Collection<KiePackage> packages = kbase
                .getKiePackages();
        for (KiePackage kp : packages) {
            sb.append("\n    Package: [" + kp.getName() + "]");
            for (Rule rule : kp.getRules()) {
                sb.append("\n        Rule: [" + rule.getName() + "]");
            }
        }
        return sb.toString();
    }
}
 
Example 5
Source File: KieBaseIncludesTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method which determines the number of rules in the {@link KieBase}.
 *
 * @param kieBase
 *            the {@link KieBase}
 * @return the number of rules in the {@link KieBase}
 */
private static long getNumberOfRules(KieBase kieBase) {
    long nrOfRules = 0;

    Collection<KiePackage> kiePackages = kieBase.getKiePackages();
    for (KiePackage nextKiePackage : kiePackages) {
        Collection<Rule> rules = nextKiePackage.getRules();
        System.out.println(rules);
        nrOfRules += rules.size();
    }
    return nrOfRules;
}
 
Example 6
Source File: DroolsTestUtil.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public static Map<String, Rule> rulestoMap( KieBase kbase ) {
    List<Rule> rules = new ArrayList();
    for ( KiePackage pkg : ((KnowledgeBaseImpl)kbase).getPackages() ) {
        for ( Rule rule : pkg.getRules() ) {
            rules.add(rule);
        }
    }

    return rulestoMap( rules );
}
 
Example 7
Source File: DroolsUtil.java    From qzr with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @return A String detailing the packages and rules in this knowledge base.
 */
public static String kbaseDetails(KieBase kbase) {
    StringBuilder sb = new StringBuilder();
    for (KiePackage p : kbase.getKiePackages()) {
        sb.append("\n  Package : " + p.getName());
        for (Rule r : p.getRules()) {
            sb.append("\n    Rule: " + r.getName());
        }
    }
    return "Knowledge base built with packages: " + sb.toString();
}
 
Example 8
Source File: KieMetaInfoBuilder.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public KieModuleMetaInfo generateKieModuleMetaInfo(ResourceStore trgMfs) {
    // TODO: I think this method is wrong because it is only inspecting packages that are included
    // in at least one kbase, but I believe it should inspect all packages, even if not included in
    // any kbase, as they could be included in the future
    Map<String, TypeMetaInfo> typeInfos = new HashMap<String, TypeMetaInfo>();
    Map<String, Set<String>> rulesPerPackage = new HashMap<String, Set<String>>();

    KieModuleModel kieModuleModel = kModule.getKieModuleModel();
    for ( String kieBaseName : kieModuleModel.getKieBaseModels().keySet() ) {
        KnowledgeBuilderImpl kBuilder = (KnowledgeBuilderImpl) kModule.getKnowledgeBuilderForKieBase( kieBaseName );
        Map<String, PackageRegistry> pkgRegistryMap = kBuilder.getPackageRegistry();

        KieModuleCache.KModuleCache.Builder _kmoduleCacheBuilder = createCacheBuilder();
        KieModuleCache.CompilationData.Builder _compData = createCompilationData();

        for ( KiePackage kPkg : kBuilder.getKnowledgePackages() ) {
            PackageRegistry pkgRegistry = pkgRegistryMap.get( kPkg.getName() );
            JavaDialectRuntimeData runtimeData = (JavaDialectRuntimeData) pkgRegistry.getDialectRuntimeRegistry().getDialectData( "java" );

            List<String> types = new ArrayList<String>();
            for ( FactType factType : kPkg.getFactTypes() ) {
                Class< ? > typeClass = ((ClassDefinition) factType).getDefinedClass();
                TypeDeclaration typeDeclaration = pkgRegistry.getPackage().getTypeDeclaration( typeClass );
                if ( typeDeclaration != null ) {
                    typeInfos.put( typeClass.getName(), new TypeMetaInfo(typeDeclaration) );
                }

                String className = factType.getName();
                String internalName = className.replace('.', '/') + ".class";
                if (trgMfs != null) {
                    byte[] bytes = runtimeData.getBytecode( internalName );
                    if ( bytes != null ) {
                        trgMfs.write( internalName, bytes, true );
                    }
                }
                types.add( internalName );
            }

            Set<String> rules = rulesPerPackage.get( kPkg.getName() );
            if( rules == null ) {
                rules = new HashSet<String>();
            }
            for ( Rule rule : kPkg.getRules() ) {
                if( !rules.contains( rule.getName() ) ) {
                    rules.add(rule.getName());
                }
            }
            if (!rules.isEmpty()) {
                rulesPerPackage.put(kPkg.getName(), rules);
            }

            addToCompilationData(_compData, runtimeData, types);
        }

        _kmoduleCacheBuilder.addCompilationData( _compData.build() );
        if (trgMfs != null) {
            writeCompilationDataToTrg( _kmoduleCacheBuilder.build(), kieBaseName, trgMfs );
        }
    }
    return new KieModuleMetaInfo(typeInfos, rulesPerPackage);
}
 
Example 9
Source File: KnowledgeBuilderTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testKnowledgeProviderWithRules() {
    KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder();

    String str = "";
    str += "package org.drools.compiler.test1\n";
    str += "rule rule1\n";
    str += "when\n";
    str += "then\n";
    str += "end\n\n";
    str += "rule rule2\n";
    str += "when\n";
    str += "then\n";
    str += "end\n";
    builder.add( ResourceFactory.newByteArrayResource( str.getBytes() ), ResourceType.DRL );

    str = "package org.drools.compiler.test2\n";
    str += "rule rule3\n";
    str += "when\n";
    str += "then\n";
    str += "end\n\n";
    str += "rule rule4\n";
    str += "when\n";
    str += "then\n";
    str += "end\n";
    builder.add( ResourceFactory.newByteArrayResource( str.getBytes() ), ResourceType.DRL );

    Collection<KiePackage> pkgs = builder.getKnowledgePackages();
    assertNotNull( pkgs );
    assertEquals( 2, pkgs.size() );

    KiePackage test1 = getKnowledgePackage(pkgs, "org.drools.compiler.test1" );
    Collection<Rule> rules = test1.getRules();
    assertEquals( 2, rules.size() );
    Rule rule = getRule( rules, "rule1" );
    assertEquals("rule1", rule.getName() );
    rule = getRule( rules, "rule2" );
    assertEquals( "rule2", rule.getName() );

    KiePackage test2 = getKnowledgePackage(pkgs, "org.drools.compiler.test2" );
    rules = test2.getRules();
    assertEquals( 2, rules.size() );
    rule = getRule( rules, "rule3" );
    assertEquals("rule3", rule.getName() );
    rule = getRule( rules, "rule4" );
    assertEquals( "rule4", rule.getName() );
}