From bbeb03b8e7ff4ef58d09d992bd9626fbcb25b869 Mon Sep 17 00:00:00 2001 From: mariofusco Date: Tue, 28 Nov 2023 18:04:51 +0100 Subject: [PATCH] [KIE-726] create a maven archetype for rules in yaml format --- .../drools/drl/extensions/YamlFactory.java | 10 ++ .../kie-drools-yaml-archetype/.gitignore | 14 ++ .../kie-drools-yaml-archetype/pom.xml | 90 +++++++++++++ .../META-INF/maven/archetype-metadata.xml | 71 ++++++++++ .../resources/archetype-resources/pom.xml | 100 ++++++++++++++ .../src/main/java/Measurement.java | 71 ++++++++++ .../src/main/resources/META-INF/kmodule.xml | 27 ++++ .../src/main/resources/rules.drl.yaml | 30 +++++ .../src/test/java/RuleTest.java | 123 ++++++++++++++++++ .../src/test/resources/logback-test.xml | 32 +++++ .../archetype.properties | 27 ++++ .../projects/integrationtestDefaults/goal.txt | 1 + .../integrationtestDefaults/reference/pom.xml | 97 ++++++++++++++ .../src/main/java/it/pkg/Measurement.java | 51 ++++++++ .../src/main/resources/META-INF/kmodule.xml | 24 ++++ .../src/main/resources/it/pkg/rules.drl.yaml | 14 ++ .../src/test/java/it/pkg/RuleTest.java | 103 +++++++++++++++ .../src/test/resources/logback-test.xml | 32 +++++ kie-archetypes/pom.xml | 1 + kie-maven-plugin/pom.xml | 5 + .../invoker.properties | 26 ++++ .../pom.xml | 77 +++++++++++ .../src/main/java/org/yaml/Measurement.java | 51 ++++++++ .../src/main/resources/META-INF/kmodule.xml | 27 ++++ .../main/resources/org/yaml/rules.drl.yaml | 13 ++ .../kie/maven/plugin/ittests/YamlTestIT.java | 85 ++++++++++++ 26 files changed, 1202 insertions(+) create mode 100644 kie-archetypes/kie-drools-yaml-archetype/.gitignore create mode 100644 kie-archetypes/kie-drools-yaml-archetype/pom.xml create mode 100644 kie-archetypes/kie-drools-yaml-archetype/src/main/resources/META-INF/maven/archetype-metadata.xml create mode 100644 kie-archetypes/kie-drools-yaml-archetype/src/main/resources/archetype-resources/pom.xml create mode 100644 kie-archetypes/kie-drools-yaml-archetype/src/main/resources/archetype-resources/src/main/java/Measurement.java create mode 100644 kie-archetypes/kie-drools-yaml-archetype/src/main/resources/archetype-resources/src/main/resources/META-INF/kmodule.xml create mode 100644 kie-archetypes/kie-drools-yaml-archetype/src/main/resources/archetype-resources/src/main/resources/rules.drl.yaml create mode 100644 kie-archetypes/kie-drools-yaml-archetype/src/main/resources/archetype-resources/src/test/java/RuleTest.java create mode 100644 kie-archetypes/kie-drools-yaml-archetype/src/main/resources/archetype-resources/src/test/resources/logback-test.xml create mode 100644 kie-archetypes/kie-drools-yaml-archetype/src/test/resources/projects/integrationtestDefaults/archetype.properties create mode 100644 kie-archetypes/kie-drools-yaml-archetype/src/test/resources/projects/integrationtestDefaults/goal.txt create mode 100644 kie-archetypes/kie-drools-yaml-archetype/src/test/resources/projects/integrationtestDefaults/reference/pom.xml create mode 100644 kie-archetypes/kie-drools-yaml-archetype/src/test/resources/projects/integrationtestDefaults/reference/src/main/java/it/pkg/Measurement.java create mode 100644 kie-archetypes/kie-drools-yaml-archetype/src/test/resources/projects/integrationtestDefaults/reference/src/main/resources/META-INF/kmodule.xml create mode 100644 kie-archetypes/kie-drools-yaml-archetype/src/test/resources/projects/integrationtestDefaults/reference/src/main/resources/it/pkg/rules.drl.yaml create mode 100644 kie-archetypes/kie-drools-yaml-archetype/src/test/resources/projects/integrationtestDefaults/reference/src/test/java/it/pkg/RuleTest.java create mode 100644 kie-archetypes/kie-drools-yaml-archetype/src/test/resources/projects/integrationtestDefaults/reference/src/test/resources/logback-test.xml create mode 100644 kie-maven-plugin/src/it/kie-maven-plugin-test-kjar-15-yaml/invoker.properties create mode 100644 kie-maven-plugin/src/it/kie-maven-plugin-test-kjar-15-yaml/pom.xml create mode 100644 kie-maven-plugin/src/it/kie-maven-plugin-test-kjar-15-yaml/src/main/java/org/yaml/Measurement.java create mode 100644 kie-maven-plugin/src/it/kie-maven-plugin-test-kjar-15-yaml/src/main/resources/META-INF/kmodule.xml create mode 100644 kie-maven-plugin/src/it/kie-maven-plugin-test-kjar-15-yaml/src/main/resources/org/yaml/rules.drl.yaml create mode 100644 kie-maven-plugin/src/it/kie-maven-plugin-test-kjar-15-yaml/src/test/java-filtered/org/kie/maven/plugin/ittests/YamlTestIT.java diff --git a/drools-drl/drools-drl-extensions/src/main/java/org/drools/drl/extensions/YamlFactory.java b/drools-drl/drools-drl-extensions/src/main/java/org/drools/drl/extensions/YamlFactory.java index fcfc21b9322..ef564015bd0 100644 --- a/drools-drl/drools-drl-extensions/src/main/java/org/drools/drl/extensions/YamlFactory.java +++ b/drools-drl/drools-drl-extensions/src/main/java/org/drools/drl/extensions/YamlFactory.java @@ -18,20 +18,30 @@ */ package org.drools.drl.extensions; +import org.drools.base.common.MissingDependencyException; import org.kie.api.internal.utils.KieService; import org.kie.api.io.Resource; public class YamlFactory { + private static final String NO_YAML = "You're trying to parse a rule file in YAML format without having drools yaml support. Please add the module org.drools:drools-drlonyaml-todrl to your classpath."; + private static class YamlProviderHolder { private static final YamlProvider provider = KieService.load(YamlProvider.class); } public static YamlProvider getYamlProvider() { + if (YamlProviderHolder.provider == null) { + throwExceptionForMissingYaml(); + } return YamlProviderHolder.provider; } public static String loadFromResource(Resource resource) { return getYamlProvider().loadFromResource(resource); } + + private static void throwExceptionForMissingYaml() { + throw new MissingDependencyException(NO_YAML); + } } diff --git a/kie-archetypes/kie-drools-yaml-archetype/.gitignore b/kie-archetypes/kie-drools-yaml-archetype/.gitignore new file mode 100644 index 00000000000..adf03446e4d --- /dev/null +++ b/kie-archetypes/kie-drools-yaml-archetype/.gitignore @@ -0,0 +1,14 @@ +/target +/local + +# Eclipse, Netbeans and IntelliJ files +/.* +!.gitignore +/nbproject +/*.ipr +/*.iws +/*.iml +*.xpr + +# Repository wide ignore mac DS_Store files +.DS_Store diff --git a/kie-archetypes/kie-drools-yaml-archetype/pom.xml b/kie-archetypes/kie-drools-yaml-archetype/pom.xml new file mode 100644 index 00000000000..b684ef46d9a --- /dev/null +++ b/kie-archetypes/kie-drools-yaml-archetype/pom.xml @@ -0,0 +1,90 @@ + + + + 4.0.0 + + org.kie + kie-archetypes + 8.45.0-SNAPSHOT + + + kie-drools-yaml-archetype + maven-archetype + + KIE :: Drools Maven Archetype with Executable Model and YAML format rules + + Drools rule example with executable model and traditional API and rule style with rules in yaml format. + Use property droolsVersion to specify which version of Drools to use in a generated project. + + http://drools.org + + + $ + + + + + + src/main/resources + true + + + + + src/test/resources + false + + **/pom.xml + **/archetype.properties + + + + src/test/resources + true + + **/pom.xml + **/archetype.properties + + + + + + + org.apache.maven.archetype + archetype-packaging + 3.2.1 + + + + + + + maven-archetype-plugin + 3.2.1 + + true + + + + + + diff --git a/kie-archetypes/kie-drools-yaml-archetype/src/main/resources/META-INF/maven/archetype-metadata.xml b/kie-archetypes/kie-drools-yaml-archetype/src/main/resources/META-INF/maven/archetype-metadata.xml new file mode 100644 index 00000000000..9d9247c21ec --- /dev/null +++ b/kie-archetypes/kie-drools-yaml-archetype/src/main/resources/META-INF/maven/archetype-metadata.xml @@ -0,0 +1,71 @@ + + + + + + ${project.version} + + + ${version.org.slf4j} + + + ${version.junit} + + + ${version.ch.qos.logback} + + + + + src/main/java + + **/*.java + + + + src/main/resources + + **/*.xml + + + + src/main/resources + + **/*.drl.yaml + + + + src/test/java + + **/*.java + + + + src/test/resources + + **/*.xml + + + + diff --git a/kie-archetypes/kie-drools-yaml-archetype/src/main/resources/archetype-resources/pom.xml b/kie-archetypes/kie-drools-yaml-archetype/src/main/resources/archetype-resources/pom.xml new file mode 100644 index 00000000000..163da146465 --- /dev/null +++ b/kie-archetypes/kie-drools-yaml-archetype/src/main/resources/archetype-resources/pom.xml @@ -0,0 +1,100 @@ +#set( $symbol_pound = '#' ) +#set( $symbol_dollar = '$' ) +#set( $symbol_escape = '\' ) + + + + 4.0.0 + + ${dollar}{groupId} + ${dollar}{artifactId} + ${dollar}{version} + kjar + + ${dollar}{artifactId} + + + UTF-8 + 11 + ${droolsVersion} + ${slf4jVersion} + ${junitVersion} + ${logbackVersion} + + + + + + org.drools + drools-bom + pom + ${drools-version} + import + + + + + + + + org.drools + drools-engine + + + org.drools + drools-drlonyaml-todrl + + + org.drools + drools-xml-support + + + + junit + junit + ${junit-version} + test + + + ch.qos.logback + logback-classic + ${logback-version} + test + + + + + + + maven-compiler-plugin + 3.8.1 + + + org.kie + kie-maven-plugin + ${drools-version} + true + + + + + diff --git a/kie-archetypes/kie-drools-yaml-archetype/src/main/resources/archetype-resources/src/main/java/Measurement.java b/kie-archetypes/kie-drools-yaml-archetype/src/main/resources/archetype-resources/src/main/java/Measurement.java new file mode 100644 index 00000000000..9c8f9f2950e --- /dev/null +++ b/kie-archetypes/kie-drools-yaml-archetype/src/main/resources/archetype-resources/src/main/java/Measurement.java @@ -0,0 +1,71 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +#* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. +*# +#set( $symbol_pound = '#' ) +#set( $symbol_dollar = '$' ) +#set( $symbol_escape = '\' ) +package ${package}; + +public class Measurement { + private String id; + private String val; + + public Measurement(String id, String val) { + super(); + this.id = id; + this.val = val; + } + + public String getId() { + return id; + } + + public String getVal() { + return val; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("Measurement ["); + if (id != null) + builder.append("id=").append(id).append(", "); + if (val != null) + builder.append("val=").append(val); + builder.append("]"); + return builder.toString(); + } +} diff --git a/kie-archetypes/kie-drools-yaml-archetype/src/main/resources/archetype-resources/src/main/resources/META-INF/kmodule.xml b/kie-archetypes/kie-drools-yaml-archetype/src/main/resources/archetype-resources/src/main/resources/META-INF/kmodule.xml new file mode 100644 index 00000000000..d0701d88834 --- /dev/null +++ b/kie-archetypes/kie-drools-yaml-archetype/src/main/resources/archetype-resources/src/main/resources/META-INF/kmodule.xml @@ -0,0 +1,27 @@ +#set( $symbol_pound = '#' ) +#set( $symbol_dollar = '$' ) +#set( $symbol_escape = '\' ) + + + + + diff --git a/kie-archetypes/kie-drools-yaml-archetype/src/main/resources/archetype-resources/src/main/resources/rules.drl.yaml b/kie-archetypes/kie-drools-yaml-archetype/src/main/resources/archetype-resources/src/main/resources/rules.drl.yaml new file mode 100644 index 00000000000..3bf3bcd0cb6 --- /dev/null +++ b/kie-archetypes/kie-drools-yaml-archetype/src/main/resources/archetype-resources/src/main/resources/rules.drl.yaml @@ -0,0 +1,30 @@ +#* +* Copyright 2022 Red Hat, Inc. and/or its affiliates. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* +* 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. +*# +#set( $symbol_pound = '#' ) +#set( $symbol_dollar = '$' ) +#set( $symbol_escape = '\' ) +name: ${package} +globals: +- type: java.util.Set + id: controlSet +rules: +- name: will execute per each Measurement having ID color + when: + - given: Measurement + having: + - id == "color" + - "$colorVal : val" + then: | + controlSet.add($colorVal); \ No newline at end of file diff --git a/kie-archetypes/kie-drools-yaml-archetype/src/main/resources/archetype-resources/src/test/java/RuleTest.java b/kie-archetypes/kie-drools-yaml-archetype/src/main/resources/archetype-resources/src/test/java/RuleTest.java new file mode 100644 index 00000000000..050caa121fe --- /dev/null +++ b/kie-archetypes/kie-drools-yaml-archetype/src/main/resources/archetype-resources/src/test/java/RuleTest.java @@ -0,0 +1,123 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +#* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. +*# +#set( $symbol_pound = '#' ) +#set( $symbol_dollar = '$' ) +#set( $symbol_escape = '\' ) +package ${package}; + +import java.util.HashSet; +import java.util.Set; + +import org.drools.model.codegen.ExecutableModelProject; +import org.junit.Test; +import org.kie.api.KieBase; +import org.kie.api.KieServices; +import org.kie.api.builder.KieFileSystem; +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; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class RuleTest { + static final Logger LOG = LoggerFactory.getLogger(RuleTest.class); + + @Test + public void test() { + KieContainer kContainer = createKieContainer(); + + LOG.info("Creating kieBase"); + KieBase kieBase = kContainer.getKieBase(); + + LOG.info("There should be rules: "); + for ( KiePackage kp : kieBase.getKiePackages() ) { + for (Rule rule : kp.getRules()) { + LOG.info("kp " + kp + " rule " + rule.getName()); + } + } + + LOG.info("Creating kieSession"); + KieSession session = kieBase.newKieSession(); + + try { + LOG.info("Populating globals"); + Set check = new HashSet(); + session.setGlobal("controlSet", check); + + LOG.info("Now running data"); + + Measurement mRed = new Measurement("color", "red"); + session.insert(mRed); + session.fireAllRules(); + + Measurement mGreen = new Measurement("color", "green"); + session.insert(mGreen); + session.fireAllRules(); + + Measurement mBlue = new Measurement("color", "blue"); + session.insert(mBlue); + session.fireAllRules(); + + LOG.info("Final checks"); + + assertEquals("Size of object in Working Memory is 3", 3, session.getObjects().size()); + assertTrue("contains red", check.contains("red")); + assertTrue("contains green", check.contains("green")); + assertTrue("contains blue", check.contains("blue")); + } finally { + session.dispose(); + } + } + + private KieContainer createKieContainer() { + // Programmatically collect resources and build a KieContainer + KieServices ks = KieServices.Factory.get(); + KieFileSystem kfs = ks.newKieFileSystem(); + String packagePath = "${dollar}{package}".replace(".", "/"); + kfs.write("src/main/resources/" + packagePath + "/rules.drl.yaml", + ks.getResources().newInputStreamResource(this.getClass().getClassLoader().getResourceAsStream(packagePath + "/rules.drl.yaml"))); + ReleaseId releaseId = ks.newReleaseId("${dollar}{groupId}", "${dollar}{artifactId}", "${dollar}{version}"); + kfs.generateAndWritePomXML(releaseId); + ks.newKieBuilder(kfs).buildAll(ExecutableModelProject.class); + return ks.newKieContainer(releaseId); + } +} \ No newline at end of file diff --git a/kie-archetypes/kie-drools-yaml-archetype/src/main/resources/archetype-resources/src/test/resources/logback-test.xml b/kie-archetypes/kie-drools-yaml-archetype/src/main/resources/archetype-resources/src/test/resources/logback-test.xml new file mode 100644 index 00000000000..58a20a47330 --- /dev/null +++ b/kie-archetypes/kie-drools-yaml-archetype/src/main/resources/archetype-resources/src/test/resources/logback-test.xml @@ -0,0 +1,32 @@ + + + + + + %d [%t] %-5p %m%n + + + + + + + \ No newline at end of file diff --git a/kie-archetypes/kie-drools-yaml-archetype/src/test/resources/projects/integrationtestDefaults/archetype.properties b/kie-archetypes/kie-drools-yaml-archetype/src/test/resources/projects/integrationtestDefaults/archetype.properties new file mode 100644 index 00000000000..c7ece5a6066 --- /dev/null +++ b/kie-archetypes/kie-drools-yaml-archetype/src/test/resources/projects/integrationtestDefaults/archetype.properties @@ -0,0 +1,27 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +# + +package=it.pkg +version=0.1-SNAPSHOT +groupId=archetype.it +artifactId=basic +droolsVersion=${project.version} +slf4jVersion=1.7.2 +junitVersion=4.11 +logbackVersion=1.2.9 diff --git a/kie-archetypes/kie-drools-yaml-archetype/src/test/resources/projects/integrationtestDefaults/goal.txt b/kie-archetypes/kie-drools-yaml-archetype/src/test/resources/projects/integrationtestDefaults/goal.txt new file mode 100644 index 00000000000..31ed2f87a27 --- /dev/null +++ b/kie-archetypes/kie-drools-yaml-archetype/src/test/resources/projects/integrationtestDefaults/goal.txt @@ -0,0 +1 @@ +clean verify diff --git a/kie-archetypes/kie-drools-yaml-archetype/src/test/resources/projects/integrationtestDefaults/reference/pom.xml b/kie-archetypes/kie-drools-yaml-archetype/src/test/resources/projects/integrationtestDefaults/reference/pom.xml new file mode 100644 index 00000000000..cf2ffc0924c --- /dev/null +++ b/kie-archetypes/kie-drools-yaml-archetype/src/test/resources/projects/integrationtestDefaults/reference/pom.xml @@ -0,0 +1,97 @@ + + + + 4.0.0 + + archetype.it + basic + 0.1-SNAPSHOT + kjar + + basic + + + UTF-8 + 11 + ${project.version} + 1.7.2 + 4.11 + 1.2.9 + + + + + + org.drools + drools-bom + pom + ${dollar}{drools-version} + import + + + + + + + + org.drools + drools-engine + + + org.drools + drools-drlonyaml-todrl + + + org.drools + drools-xml-support + + + + junit + junit + ${junit-version} + test + + + ch.qos.logback + logback-classic + ${logback-version} + test + + + + + + + maven-compiler-plugin + 3.8.1 + + + org.kie + kie-maven-plugin + ${drools-version} + true + + + + + diff --git a/kie-archetypes/kie-drools-yaml-archetype/src/test/resources/projects/integrationtestDefaults/reference/src/main/java/it/pkg/Measurement.java b/kie-archetypes/kie-drools-yaml-archetype/src/test/resources/projects/integrationtestDefaults/reference/src/main/java/it/pkg/Measurement.java new file mode 100644 index 00000000000..26a3ff54708 --- /dev/null +++ b/kie-archetypes/kie-drools-yaml-archetype/src/test/resources/projects/integrationtestDefaults/reference/src/main/java/it/pkg/Measurement.java @@ -0,0 +1,51 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +package it.pkg; + +public class Measurement { + private String id; + private String val; + + public Measurement(String id, String val) { + super(); + this.id = id; + this.val = val; + } + + public String getId() { + return id; + } + + public String getVal() { + return val; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("Measurement ["); + if (id != null) + builder.append("id=").append(id).append(", "); + if (val != null) + builder.append("val=").append(val); + builder.append("]"); + return builder.toString(); + } +} diff --git a/kie-archetypes/kie-drools-yaml-archetype/src/test/resources/projects/integrationtestDefaults/reference/src/main/resources/META-INF/kmodule.xml b/kie-archetypes/kie-drools-yaml-archetype/src/test/resources/projects/integrationtestDefaults/reference/src/main/resources/META-INF/kmodule.xml new file mode 100644 index 00000000000..decf47d1de2 --- /dev/null +++ b/kie-archetypes/kie-drools-yaml-archetype/src/test/resources/projects/integrationtestDefaults/reference/src/main/resources/META-INF/kmodule.xml @@ -0,0 +1,24 @@ + + + + + diff --git a/kie-archetypes/kie-drools-yaml-archetype/src/test/resources/projects/integrationtestDefaults/reference/src/main/resources/it/pkg/rules.drl.yaml b/kie-archetypes/kie-drools-yaml-archetype/src/test/resources/projects/integrationtestDefaults/reference/src/main/resources/it/pkg/rules.drl.yaml new file mode 100644 index 00000000000..62a2f1944a4 --- /dev/null +++ b/kie-archetypes/kie-drools-yaml-archetype/src/test/resources/projects/integrationtestDefaults/reference/src/main/resources/it/pkg/rules.drl.yaml @@ -0,0 +1,14 @@ + +name: it.pkg +globals: +- type: java.util.Set + id: controlSet +rules: +- name: will execute per each Measurement having ID color + when: + - given: Measurement + having: + - id == "color" + - "$colorVal : val" + then: | + controlSet.add($colorVal); \ No newline at end of file diff --git a/kie-archetypes/kie-drools-yaml-archetype/src/test/resources/projects/integrationtestDefaults/reference/src/test/java/it/pkg/RuleTest.java b/kie-archetypes/kie-drools-yaml-archetype/src/test/resources/projects/integrationtestDefaults/reference/src/test/java/it/pkg/RuleTest.java new file mode 100644 index 00000000000..07dd2a90bc7 --- /dev/null +++ b/kie-archetypes/kie-drools-yaml-archetype/src/test/resources/projects/integrationtestDefaults/reference/src/test/java/it/pkg/RuleTest.java @@ -0,0 +1,103 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +package it.pkg; + +import java.util.HashSet; +import java.util.Set; + +import org.drools.model.codegen.ExecutableModelProject; +import org.junit.Test; +import org.kie.api.KieBase; +import org.kie.api.KieServices; +import org.kie.api.builder.KieFileSystem; +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; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class RuleTest { + static final Logger LOG = LoggerFactory.getLogger(RuleTest.class); + + @Test + public void test() { + KieContainer kContainer = createKieContainer(); + + LOG.info("Creating kieBase"); + KieBase kieBase = kContainer.getKieBase(); + + LOG.info("There should be rules: "); + for ( KiePackage kp : kieBase.getKiePackages() ) { + for (Rule rule : kp.getRules()) { + LOG.info("kp " + kp + " rule " + rule.getName()); + } + } + + LOG.info("Creating kieSession"); + KieSession session = kieBase.newKieSession(); + + try { + LOG.info("Populating globals"); + Set check = new HashSet(); + session.setGlobal("controlSet", check); + + LOG.info("Now running data"); + + Measurement mRed = new Measurement("color", "red"); + session.insert(mRed); + session.fireAllRules(); + + Measurement mGreen = new Measurement("color", "green"); + session.insert(mGreen); + session.fireAllRules(); + + Measurement mBlue = new Measurement("color", "blue"); + session.insert(mBlue); + session.fireAllRules(); + + LOG.info("Final checks"); + + assertEquals("Size of object in Working Memory is 3", 3, session.getObjects().size()); + assertTrue("contains red", check.contains("red")); + assertTrue("contains green", check.contains("green")); + assertTrue("contains blue", check.contains("blue")); + } finally { + session.dispose(); + } + } + + private KieContainer createKieContainer() { + // Programmatically collect resources and build a KieContainer + KieServices ks = KieServices.Factory.get(); + KieFileSystem kfs = ks.newKieFileSystem(); + String packagePath = "it.pkg".replace(".", "/"); + kfs.write("src/main/resources/" + packagePath + "/rules.drl.yaml", + ks.getResources().newInputStreamResource(this.getClass().getClassLoader().getResourceAsStream(packagePath + "/rules.drl.yaml"))); + ReleaseId releaseId = ks.newReleaseId("archetype.it", "basic", "0.1-SNAPSHOT"); + kfs.generateAndWritePomXML(releaseId); + ks.newKieBuilder(kfs).buildAll(ExecutableModelProject.class); + return ks.newKieContainer(releaseId); + } +} \ No newline at end of file diff --git a/kie-archetypes/kie-drools-yaml-archetype/src/test/resources/projects/integrationtestDefaults/reference/src/test/resources/logback-test.xml b/kie-archetypes/kie-drools-yaml-archetype/src/test/resources/projects/integrationtestDefaults/reference/src/test/resources/logback-test.xml new file mode 100644 index 00000000000..58a20a47330 --- /dev/null +++ b/kie-archetypes/kie-drools-yaml-archetype/src/test/resources/projects/integrationtestDefaults/reference/src/test/resources/logback-test.xml @@ -0,0 +1,32 @@ + + + + + + %d [%t] %-5p %m%n + + + + + + + \ No newline at end of file diff --git a/kie-archetypes/pom.xml b/kie-archetypes/pom.xml index d06d304611c..3b9d4eb99de 100644 --- a/kie-archetypes/pom.xml +++ b/kie-archetypes/pom.xml @@ -40,6 +40,7 @@ kie-drools-dmn-archetype kie-drools-exec-model-ruleunit-archetype kie-drools-exec-model-archetype + kie-drools-yaml-archetype diff --git a/kie-maven-plugin/pom.xml b/kie-maven-plugin/pom.xml index 0605f956983..6483ad51852 100644 --- a/kie-maven-plugin/pom.xml +++ b/kie-maven-plugin/pom.xml @@ -255,6 +255,11 @@ drools-decisiontables runtime + + org.drools + drools-drlonyaml-todrl + runtime + org.drools drools-templates diff --git a/kie-maven-plugin/src/it/kie-maven-plugin-test-kjar-15-yaml/invoker.properties b/kie-maven-plugin/src/it/kie-maven-plugin-test-kjar-15-yaml/invoker.properties new file mode 100644 index 00000000000..c5669b72883 --- /dev/null +++ b/kie-maven-plugin/src/it/kie-maven-plugin-test-kjar-15-yaml/invoker.properties @@ -0,0 +1,26 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +# + +# A comma or space separated list of goals/phases to execute, may +# specify an empty list to execute the default goal of the IT project. +# Environment variables used by maven plugins can be added here +invoker.goals = clean install + +# Uncomment the following to debug invoker. Do note that you have to connect the remote debugger after "maven-invoker-plugin:3.2.0:run" has been print on console +#invoker.mavenOpts=-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 diff --git a/kie-maven-plugin/src/it/kie-maven-plugin-test-kjar-15-yaml/pom.xml b/kie-maven-plugin/src/it/kie-maven-plugin-test-kjar-15-yaml/pom.xml new file mode 100644 index 00000000000..1471752feb6 --- /dev/null +++ b/kie-maven-plugin/src/it/kie-maven-plugin-test-kjar-15-yaml/pom.xml @@ -0,0 +1,77 @@ + + + + + + 4.0.0 + + org.kie + kie-maven-plugin-test-kjar-parent + @org.kie.version@ + ../kie-maven-plugin-test-kjar-setup/kie-maven-plugin-test-kjar-parent/pom.xml + + + kie-maven-plugin-test-kjar-15-yaml + + kjar + + + + + org.kie + kie-api + ${org.kie.version} + + + org.drools + drools-model-compiler + ${org.kie.version} + + + org.drools + drools-canonical-model + ${org.kie.version} + + + org.drools + drools-core + ${org.kie.version} + + + org.drools + drools-compiler + ${org.kie.version} + + + org.drools + drools-drlonyaml-todrl + ${org.kie.version} + + + org.kie + kie-maven-plugin-test-kjar-common + @org.kie.version@ + + + + diff --git a/kie-maven-plugin/src/it/kie-maven-plugin-test-kjar-15-yaml/src/main/java/org/yaml/Measurement.java b/kie-maven-plugin/src/it/kie-maven-plugin-test-kjar-15-yaml/src/main/java/org/yaml/Measurement.java new file mode 100644 index 00000000000..e341d6943d5 --- /dev/null +++ b/kie-maven-plugin/src/it/kie-maven-plugin-test-kjar-15-yaml/src/main/java/org/yaml/Measurement.java @@ -0,0 +1,51 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +package org.yaml; + +public class Measurement { + private String id; + private String val; + + public Measurement(String id, String val) { + super(); + this.id = id; + this.val = val; + } + + public String getId() { + return id; + } + + public String getVal() { + return val; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("Measurement ["); + if (id != null) + builder.append("id=").append(id).append(", "); + if (val != null) + builder.append("val=").append(val); + builder.append("]"); + return builder.toString(); + } +} diff --git a/kie-maven-plugin/src/it/kie-maven-plugin-test-kjar-15-yaml/src/main/resources/META-INF/kmodule.xml b/kie-maven-plugin/src/it/kie-maven-plugin-test-kjar-15-yaml/src/main/resources/META-INF/kmodule.xml new file mode 100644 index 00000000000..1fe23858ca9 --- /dev/null +++ b/kie-maven-plugin/src/it/kie-maven-plugin-test-kjar-15-yaml/src/main/resources/META-INF/kmodule.xml @@ -0,0 +1,27 @@ + + + + + + + + \ No newline at end of file diff --git a/kie-maven-plugin/src/it/kie-maven-plugin-test-kjar-15-yaml/src/main/resources/org/yaml/rules.drl.yaml b/kie-maven-plugin/src/it/kie-maven-plugin-test-kjar-15-yaml/src/main/resources/org/yaml/rules.drl.yaml new file mode 100644 index 00000000000..a9f81455d9c --- /dev/null +++ b/kie-maven-plugin/src/it/kie-maven-plugin-test-kjar-15-yaml/src/main/resources/org/yaml/rules.drl.yaml @@ -0,0 +1,13 @@ +name: org.yaml +globals: +- type: java.util.Set + id: controlSet +rules: +- name: will execute per each Measurement having ID color + when: + - given: Measurement + having: + - id == "color" + - "$colorVal : val" + then: | + controlSet.add($colorVal); \ No newline at end of file diff --git a/kie-maven-plugin/src/it/kie-maven-plugin-test-kjar-15-yaml/src/test/java-filtered/org/kie/maven/plugin/ittests/YamlTestIT.java b/kie-maven-plugin/src/it/kie-maven-plugin-test-kjar-15-yaml/src/test/java-filtered/org/kie/maven/plugin/ittests/YamlTestIT.java new file mode 100644 index 00000000000..ded38386ade --- /dev/null +++ b/kie-maven-plugin/src/it/kie-maven-plugin-test-kjar-15-yaml/src/test/java-filtered/org/kie/maven/plugin/ittests/YamlTestIT.java @@ -0,0 +1,85 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +package org.kie.maven.plugin.ittests; + +import java.io.File; +import java.lang.reflect.Constructor; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.HashSet; +import java.util.Set; + +import org.kie.api.KieBase; +import org.kie.api.runtime.KieContainer; + +import org.assertj.core.api.Assertions; +import org.junit.Test; +import org.kie.api.runtime.KieSession; + +import org.yaml.Measurement; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class YamlTestIT { + + private final static String GROUP_ID = "org.kie"; + private final static String GAV_ARTIFACT_ID = "kie-maven-plugin-test-kjar-15-yaml"; + private static final String GAV_VERSION = "${org.kie.version}"; + private final static String KBASE_NAME = "YamlKBase"; + + + @Test + public void testYamlRules() throws Exception { + final URL targetLocation = YamlTestIT.class.getProtectionDomain().getCodeSource().getLocation(); + final KieContainer kieContainer = ITTestsUtils.getKieContainer(targetLocation, GAV_ARTIFACT_ID, GAV_VERSION); + final KieBase kieBase = kieContainer.getKieBase(KBASE_NAME); + Assertions.assertThat(kieBase).isNotNull(); + KieSession kSession = null; + try { + + kSession = kieBase.newKieSession(); + Assertions.assertThat(kSession).isNotNull(); + + Set check = new HashSet(); + kSession.setGlobal("controlSet", check); + + Measurement mRed = new Measurement("color", "red"); + kSession.insert(mRed); + kSession.fireAllRules(); + + Measurement mGreen = new Measurement("color", "green"); + kSession.insert(mGreen); + kSession.fireAllRules(); + + Measurement mBlue = new Measurement("color", "blue"); + kSession.insert(mBlue); + kSession.fireAllRules(); + + assertEquals("Size of object in Working Memory is 3", 3, kSession.getObjects().size()); + assertTrue("contains red", check.contains("red")); + assertTrue("contains green", check.contains("green")); + assertTrue("contains blue", check.contains("blue")); + + } finally { + kSession.dispose(); + } + } +} +