diff options
Diffstat (limited to 'policy-core/src/main')
-rw-r--r-- | policy-core/src/main/java/org/onap/policy/drools/util/KieUtils.java | 124 |
1 files changed, 83 insertions, 41 deletions
diff --git a/policy-core/src/main/java/org/onap/policy/drools/util/KieUtils.java b/policy-core/src/main/java/org/onap/policy/drools/util/KieUtils.java index 5500d110..babc0413 100644 --- a/policy-core/src/main/java/org/onap/policy/drools/util/KieUtils.java +++ b/policy-core/src/main/java/org/onap/policy/drools/util/KieUtils.java @@ -22,10 +22,13 @@ package org.onap.policy.drools.util; import java.io.File; import java.io.IOException; -import java.nio.charset.StandardCharsets; import java.nio.file.Files; -import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; +import lombok.NonNull; import org.drools.compiler.kie.builder.impl.InternalKieModule; import org.drools.compiler.kproject.models.KieModuleModelImpl; import org.kie.api.KieServices; @@ -34,44 +37,52 @@ import org.kie.api.builder.KieFileSystem; import org.kie.api.builder.Message; import org.kie.api.builder.ReleaseId; import org.kie.api.builder.model.KieModuleModel; +import org.kie.api.definition.KiePackage; +import org.kie.api.definition.rule.Rule; +import org.kie.api.runtime.KieContainer; +import org.kie.api.runtime.KieSession; import org.kie.scanner.KieMavenRepository; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Kie related utilities. */ public class KieUtils { + private static final Logger logger = LoggerFactory.getLogger(KieUtils.class); + private KieUtils() { // Utility class } /** * Installs a rules artifact in the local maven repository. - * - * @param kmodule kmodule specification - * @param pom pom - * @param drlKJarPath path used in kjar drl to the actual drl - * @param drl rules in drl language - * - * @return releaseId result o a sucessful installation - * @throws IOException error accessing necessary resources */ - public static ReleaseId installArtifact(String kmodule, String pom, String drlKJarPath, - String drl) throws IOException { + public static ReleaseId installArtifact(File kmodule, File pom, String drlKJarPath, @NonNull List<File> drls) + throws IOException { KieModuleModel kieModule = KieModuleModelImpl.fromXML(kmodule); final KieFileSystem kieFileSystem = KieServices.Factory.get().newKieFileSystem(); kieFileSystem.writeKModuleXML(kieModule.toXML()); - kieFileSystem.writePomXML(pom); - kieFileSystem.write(drlKJarPath, drl); + kieFileSystem.writePomXML(new String(Files.readAllBytes(pom.toPath()))); + for (File drl : drls) { + kieFileSystem.write(drlKJarPath + drl.getName(), new String(Files.readAllBytes(drl.toPath()))); + } - KieBuilder kieBuilder = kieBuild(kieFileSystem); + KieBuilder kieBuilder = build(kieFileSystem); + return getReleaseId(kieBuilder, pom); + } - Path pomPath = Files.createTempFile("policy-core-", ".pom"); - Files.write(pomPath, pom.getBytes(StandardCharsets.UTF_8)); - File pomFile = pomPath.toFile(); - pomFile.deleteOnExit(); + /** + * Installs a rules artifact in the local maven repository. + */ + public static ReleaseId installArtifact(File kmodule, File pom, String drlKJarPath, File drl) + throws IOException { + return installArtifact(kmodule, pom, drlKJarPath, Collections.singletonList(drl)); + } + private static ReleaseId getReleaseId(KieBuilder kieBuilder, File pomFile) { ReleaseId releaseId = kieBuilder.getKieModule().getReleaseId(); KieMavenRepository .getKieMavenRepository() @@ -82,34 +93,65 @@ public class KieUtils { } /** - * Installs a rules artifact in the local maven repository. - * - * @param kmodule kmodule specification - * @param pom pom - * @param drlKJarPath path used in kjar drl to the actual drl - * @param drl rules in drl language - * - * @return releaseId result o a sucessful installation - * @throws IOException error accessing necessary resources + * Get Knowledge Bases. */ - public static ReleaseId installArtifact(File kmodule, File pom, String drlKJarPath, File drl) throws IOException { - KieModuleModel kieModule = KieModuleModelImpl.fromXML(kmodule); + public static List<String> getBases(KieContainer container) { + return new ArrayList<>(container.getKieBaseNames()); + } - final KieFileSystem kieFileSystem = KieServices.Factory.get().newKieFileSystem(); - kieFileSystem.writeKModuleXML(kieModule.toXML()); - kieFileSystem.writePomXML(new String(Files.readAllBytes(pom.toPath()))); - kieFileSystem.write(drlKJarPath, new String(Files.readAllBytes(drl.toPath()))); + /** + * Get Packages. + */ + public static List<KiePackage> getPackages(KieContainer container) { + return getBases(container).stream() + .flatMap(base -> container.getKieBase(base).getKiePackages().stream()) + .collect(Collectors.toList()); + } + + /** + * Get Package Names. + */ + public static List<String> getPackageNames(KieContainer container) { + return getPackages(container).stream() + .map(KiePackage::getName) + .collect(Collectors.toList()); + } - KieBuilder kieBuilder = kieBuild(kieFileSystem); + /** + * Get Rules. + */ + public static List<Rule> getRules(KieContainer container) { + return getPackages(container).stream() + .flatMap(kiePackage -> kiePackage.getRules().stream()) + .collect(Collectors.toList()); + } - ReleaseId releaseId = kieBuilder.getKieModule().getReleaseId(); - KieMavenRepository - .getKieMavenRepository() - .installArtifact(releaseId, (InternalKieModule) kieBuilder.getKieModule(), pom); - return releaseId; + /** + * Get Rule Names. + */ + public static List<String> getRuleNames(KieContainer container) { + return getRules(container).stream() + .map(Rule::getName) + .collect(Collectors.toList()); + } + + /** + * Get Facts. + */ + public static List<Object> getFacts(KieSession session) { + return session.getFactHandles().stream() + .map(session::getObject) + .collect(Collectors.toList()); + } + + /** + * Create Container. + */ + public static KieContainer createContainer(ReleaseId releaseId) { + return KieServices.Factory.get().newKieContainer(releaseId); } - private static KieBuilder kieBuild(KieFileSystem kieFileSystem) { + private static KieBuilder build(KieFileSystem kieFileSystem) { KieBuilder kieBuilder = KieServices.Factory.get().newKieBuilder(kieFileSystem); List<Message> messages = kieBuilder.buildAll().getResults().getMessages(); if (messages != null && !messages.isEmpty()) { |