summaryrefslogtreecommitdiffstats
path: root/policy-core/src/main
diff options
context:
space:
mode:
authorjhh <jorge.hernandez-herrero@att.com>2019-11-06 13:38:31 -0600
committerjhh <jorge.hernandez-herrero@att.com>2019-11-06 15:00:58 -0600
commita42adf807f21116b7a952233d448d18afa7980b9 (patch)
tree55daad229eb366c0ccf9e2c3b048fb9bd1bffa0c /policy-core/src/main
parent6d7b161d73cb0b6e69c8df84de3b7094eda9119d (diff)
Drools Controller upgrades Tests
Issue-ID: POLICY-1407 Signed-off-by: jhh <jorge.hernandez-herrero@att.com> Change-Id: I6963fced5608b62a41b73fef5070c21cf45d6fbe Signed-off-by: jhh <jorge.hernandez-herrero@att.com>
Diffstat (limited to 'policy-core/src/main')
-rw-r--r--policy-core/src/main/java/org/onap/policy/drools/util/KieUtils.java124
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()) {