aboutsummaryrefslogtreecommitdiffstats
path: root/policy-core
diff options
context:
space:
mode:
Diffstat (limited to 'policy-core')
-rw-r--r--policy-core/src/main/java/org/onap/policy/drools/util/KieUtils.java124
-rw-r--r--policy-core/src/test/java/org/onap/policy/drools/core/DroolsContainerTest.java6
-rw-r--r--policy-core/src/test/java/org/onap/policy/drools/util/KieUtilsTest.java122
3 files changed, 208 insertions, 44 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()) {
diff --git a/policy-core/src/test/java/org/onap/policy/drools/core/DroolsContainerTest.java b/policy-core/src/test/java/org/onap/policy/drools/core/DroolsContainerTest.java
index d23bfd0c..d168ca8b 100644
--- a/policy-core/src/test/java/org/onap/policy/drools/core/DroolsContainerTest.java
+++ b/policy-core/src/test/java/org/onap/policy/drools/core/DroolsContainerTest.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* policy-core
* ================================================================================
- * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -54,13 +54,13 @@ public class DroolsContainerTest {
KieUtils.installArtifact(
Paths.get("src/test/resources/drools-artifact-1.1/src/main/resources/META-INF/kmodule.xml").toFile(),
Paths.get("src/test/resources/drools-artifact-1.1/pom.xml").toFile(),
- "src/main/resources/rules/org/onap/policy/drools/core/test/rules.drl",
+ "src/main/resources/rules/org/onap/policy/drools/core/test/",
Paths.get("src/test/resources/drools-artifact-1.1/src/main/resources/rules.drl").toFile());
KieUtils.installArtifact(
Paths.get("src/test/resources/drools-artifact-1.2/src/main/resources/META-INF/kmodule.xml").toFile(),
Paths.get("src/test/resources/drools-artifact-1.2/pom.xml").toFile(),
- "src/main/resources/rules/org/onap/policy/drools/core/test/rules.drl",
+ "src/main/resources/rules/org/onap/policy/drools/core/test/",
Paths.get("src/test/resources/drools-artifact-1.2/src/main/resources/rules.drl").toFile());
}
diff --git a/policy-core/src/test/java/org/onap/policy/drools/util/KieUtilsTest.java b/policy-core/src/test/java/org/onap/policy/drools/util/KieUtilsTest.java
new file mode 100644
index 00000000..83f62b32
--- /dev/null
+++ b/policy-core/src/test/java/org/onap/policy/drools/util/KieUtilsTest.java
@@ -0,0 +1,122 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.drools.util;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.io.IOException;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.stream.Collectors;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.kie.api.builder.ReleaseId;
+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;
+
+/**
+ * Kie Utils Tests.
+ */
+public class KieUtilsTest {
+
+ private static KieContainer container;
+ private static KieSession session;
+
+ /**
+ * Test class initialization.
+ */
+ @BeforeClass
+ public static void createArtifact() throws Exception {
+ ReleaseId releaseId =
+ KieUtils.installArtifact(
+ Paths.get("src/test/resources/drools-artifact-1.1/src/main/resources/META-INF/kmodule.xml").toFile(),
+ Paths.get("src/test/resources/drools-artifact-1.1/pom.xml").toFile(),
+ "src/main/resources/rules/org/onap/policy/drools/core/test/",
+ Paths.get("src/test/resources/drools-artifact-1.1/src/main/resources/rules.drl").toFile());
+
+ container = KieUtils.createContainer(releaseId);
+ session = container.getKieBase("rules").newKieSession();
+ }
+
+ @Test
+ public void installArtifact() throws IOException {
+ ReleaseId releaseId =
+ KieUtils.installArtifact(
+ Paths.get("src/test/resources/drools-artifact-1.1/src/main/resources/META-INF/kmodule.xml").toFile(),
+ Paths.get("src/test/resources/drools-artifact-1.1/pom.xml").toFile(),
+ "src/main/resources/rules/org/onap/policy/drools/core/test/",
+ Paths.get("src/test/resources/drools-artifact-1.1/src/main/resources/rules.drl").toFile());
+
+ assertNotNull(releaseId);
+ }
+
+ @Test
+ public void installArtifactList() throws IOException {
+ ReleaseId releaseId =
+ KieUtils.installArtifact(
+ Paths.get("src/test/resources/drools-artifact-1.1/src/main/resources/META-INF/kmodule.xml").toFile(),
+ Paths.get("src/test/resources/drools-artifact-1.1/pom.xml").toFile(),
+ "src/main/resources/rules/org/onap/policy/drools/core/test/",
+ Collections.singletonList(
+ Paths.get("src/test/resources/drools-artifact-1.1/src/main/resources/rules.drl").toFile()));
+
+ assertNotNull(releaseId);
+ }
+
+ @Test
+ public void getBases() {
+ assertEquals(Collections.singletonList("rules"), KieUtils.getBases(container));
+ }
+
+ @Test
+ public void getPackages() {
+ assertEquals(Arrays.asList("java.util", "java.util.concurrent", "org.onap.policy.drools.core.test"),
+ KieUtils.getPackages(container) .stream().map(KiePackage::getName).collect(Collectors.toList()));
+ }
+
+ @Test
+ public void getPackageNames() {
+ assertEquals(Arrays.asList("java.util", "java.util.concurrent", "org.onap.policy.drools.core.test"),
+ new ArrayList<>(KieUtils.getPackageNames(container)));
+ }
+
+ @Test
+ public void getRules() {
+ assertEquals(Arrays.asList("Initialization", "Add elements of an int list"),
+ KieUtils.getRules(container).stream().map(Rule::getName).collect(Collectors.toList()));
+ }
+
+ @Test
+ public void getRuleNames() {
+ assertEquals(Arrays.asList("Initialization", "Add elements of an int list"),
+ KieUtils.getRuleNames(container).stream().collect(Collectors.toList()));
+ }
+
+ @Test
+ public void getFacts() {
+ assertEquals(0, KieUtils.getFacts(session).size());
+ }
+} \ No newline at end of file