This is an automated email from the ASF dual-hosted git repository.

mariofusco pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-kie-drools.git


The following commit(s) were added to refs/heads/main by this push:
     new 2383c27a37 [KIE-724] allow to write rules in yaml format making files 
with extension .drl.yaml to be a native drools resource type (#5592)
2383c27a37 is described below

commit 2383c27a37d490ea81c23962430fdb0e58d4efc5
Author: Mario Fusco <[email protected]>
AuthorDate: Tue Nov 28 08:25:32 2023 +0100

    [KIE-724] allow to write rules in yaml format making files with extension 
.drl.yaml to be a native drools resource type (#5592)
    
    * [KIE-724] allow to write rules in yaml format making files with extension 
.drl.yaml to be a native drools resource type
    
    * add missing header
---
 .../builder/impl/ResourceHandlerManager.java       |   7 +-
 .../impl/resources/YamlResourceHandler.java        |  67 ++++++++++++
 .../org/drools/drl/extensions/YamlFactory.java     |  37 +++++++
 .../org/drools/drl/extensions/YamlProvider.java    |  27 +++++
 .../java/org/drools/drlonyaml/cli/Drl2Yaml.java    |  23 ++---
 .../java/org/drools/drlonyaml/cli/Yaml2Drl.java    |  22 +---
 .../pom.xml                                        |  17 ++-
 .../drlonyaml/integration/tests/Message.java       |  36 +++++++
 .../integration/tests/ProgrammaticProjectTest.java | 115 +++++++++++++++++++++
 .../src/test/resources/logback.xml                 |  38 +++++++
 .../java/org/drools/drlonyaml/model/Utils.java     |  16 +++
 .../drools-drlonyaml-todrl/pom.xml                 |   4 +
 .../drools/drlonyaml/todrl/YAMLtoDrlDumper.java    |   7 ++
 .../drools/drlonyaml/todrl/YamlProviderImpl.java   |  38 +++++++
 .../org.drools.drl.extensions.YamlProvider         |   1 +
 drools-drlonyaml-parent/pom.xml                    |   1 +
 .../src/main/java/org/kie/api/io/ResourceType.java |   5 +
 17 files changed, 412 insertions(+), 49 deletions(-)

diff --git 
a/drools-compiler/src/main/java/org/drools/compiler/builder/impl/ResourceHandlerManager.java
 
b/drools-compiler/src/main/java/org/drools/compiler/builder/impl/ResourceHandlerManager.java
index 267fdb17ac..816a1822d2 100644
--- 
a/drools-compiler/src/main/java/org/drools/compiler/builder/impl/ResourceHandlerManager.java
+++ 
b/drools-compiler/src/main/java/org/drools/compiler/builder/impl/ResourceHandlerManager.java
@@ -26,6 +26,7 @@ import 
org.drools.compiler.builder.impl.resources.DrlResourceHandler;
 import org.drools.compiler.builder.impl.resources.DslrResourceHandler;
 import org.drools.compiler.builder.impl.resources.ResourceHandler;
 import org.drools.compiler.builder.impl.resources.TemplateResourceHandler;
+import org.drools.compiler.builder.impl.resources.YamlResourceHandler;
 import org.drools.drl.parser.lang.dsl.DefaultExpander;
 import org.kie.api.builder.ReleaseId;
 import org.kie.api.io.ResourceType;
@@ -39,12 +40,14 @@ public class ResourceHandlerManager {
     public ResourceHandlerManager(KnowledgeBuilderConfigurationImpl 
configuration, ReleaseId releaseId, Supplier<DefaultExpander> dslExpander){
         this.mappers = asList(
                 new DrlResourceHandler(configuration),
+                new YamlResourceHandler(configuration),
                 new TemplateResourceHandler(configuration, releaseId, 
dslExpander),
                 new DslrResourceHandler(configuration, dslExpander) ,
                 new DecisionTableResourceHandler(configuration, releaseId));
 
         this.orderedResourceTypes = asList(
                 ResourceType.DRL,
+                ResourceType.YAML,
                 ResourceType.GDRL,
                 ResourceType.RDRL,
                 ResourceType.DESCR,
@@ -59,10 +62,6 @@ public class ResourceHandlerManager {
         return this.orderedResourceTypes;
     }
 
-    public List<ResourceHandler> getMappers(){
-        return this.mappers;
-    }
-
     public ResourceHandler handlerForType(ResourceType type) {
         for (ResourceHandler mapper : this.mappers) {
             if (mapper.handles(type)) {
diff --git 
a/drools-compiler/src/main/java/org/drools/compiler/builder/impl/resources/YamlResourceHandler.java
 
b/drools-compiler/src/main/java/org/drools/compiler/builder/impl/resources/YamlResourceHandler.java
new file mode 100644
index 0000000000..6c07fd1339
--- /dev/null
+++ 
b/drools-compiler/src/main/java/org/drools/compiler/builder/impl/resources/YamlResourceHandler.java
@@ -0,0 +1,67 @@
+/**
+ * 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.drools.compiler.builder.impl.resources;
+
+import java.io.IOException;
+import java.io.StringReader;
+
+import org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl;
+import org.drools.drl.ast.descr.PackageDescr;
+import org.drools.drl.extensions.YamlFactory;
+import org.drools.drl.parser.DrlParser;
+import org.drools.drl.parser.DroolsParserException;
+import org.drools.drl.parser.ParserError;
+import org.drools.io.DescrResource;
+import org.kie.api.io.Resource;
+import org.kie.api.io.ResourceConfiguration;
+import org.kie.api.io.ResourceType;
+import org.kie.internal.builder.conf.LanguageLevelOption;
+
+public class YamlResourceHandler extends ResourceHandler {
+    public YamlResourceHandler(KnowledgeBuilderConfigurationImpl 
configuration) {
+        super(configuration);
+    }
+
+    @Override
+    public boolean handles(ResourceType type) {
+        return type == ResourceType.YAML;
+    }
+
+    public PackageDescr process(Resource resource, ResourceConfiguration 
resourceConfig) throws DroolsParserException, IOException {
+        PackageDescr pkg;
+        boolean hasErrors = false;
+        if (resource instanceof DescrResource) {
+            pkg = (PackageDescr) ((DescrResource) resource).getDescr();
+        } else {
+            String drl = YamlFactory.loadFromResource(resource);
+            DrlParser parser = new 
DrlParser(this.configuration.getOption(LanguageLevelOption.KEY));
+            pkg = parser.parse(new StringReader(drl));
+            this.results.addAll(parser.getErrors());
+            if (pkg == null) {
+                this.results.add(new ParserError(resource, "Parser returned a 
null Package", 0, 0));
+            }
+            hasErrors = parser.hasErrors();
+        }
+        if (pkg != null) {
+            pkg.setResource(resource);
+        }
+        return hasErrors ? null : pkg;
+    }
+
+}
\ No newline at end of file
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
new file mode 100644
index 0000000000..fcfc21b932
--- /dev/null
+++ 
b/drools-drl/drools-drl-extensions/src/main/java/org/drools/drl/extensions/YamlFactory.java
@@ -0,0 +1,37 @@
+/**
+ * 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.drools.drl.extensions;
+
+import org.kie.api.internal.utils.KieService;
+import org.kie.api.io.Resource;
+
+public class YamlFactory {
+
+    private static class YamlProviderHolder {
+        private static final YamlProvider provider = 
KieService.load(YamlProvider.class);
+    }
+
+    public static YamlProvider getYamlProvider() {
+        return YamlProviderHolder.provider;
+    }
+
+    public static String loadFromResource(Resource resource) {
+        return getYamlProvider().loadFromResource(resource);
+    }
+}
diff --git 
a/drools-drl/drools-drl-extensions/src/main/java/org/drools/drl/extensions/YamlProvider.java
 
b/drools-drl/drools-drl-extensions/src/main/java/org/drools/drl/extensions/YamlProvider.java
new file mode 100644
index 0000000000..8fd017e971
--- /dev/null
+++ 
b/drools-drl/drools-drl-extensions/src/main/java/org/drools/drl/extensions/YamlProvider.java
@@ -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 org.drools.drl.extensions;
+
+import org.kie.api.internal.utils.KieService;
+import org.kie.api.io.Resource;
+
+public interface YamlProvider extends KieService {
+
+    String loadFromResource(Resource resource);
+}
diff --git 
a/drools-drlonyaml-parent/drools-drlonyaml-cli/src/main/java/org/drools/drlonyaml/cli/Drl2Yaml.java
 
b/drools-drlonyaml-parent/drools-drlonyaml-cli/src/main/java/org/drools/drlonyaml/cli/Drl2Yaml.java
index 48230d48d1..eb05d19728 100644
--- 
a/drools-drlonyaml-parent/drools-drlonyaml-cli/src/main/java/org/drools/drlonyaml/cli/Drl2Yaml.java
+++ 
b/drools-drlonyaml-parent/drools-drlonyaml-cli/src/main/java/org/drools/drlonyaml/cli/Drl2Yaml.java
@@ -18,9 +18,6 @@
  */
 package org.drools.drlonyaml.cli;
 
-import static org.drools.drlonyaml.cli.utils.Utils.conventionInputStream;
-import static org.drools.drlonyaml.cli.utils.Utils.conventionOutputConsumer;
-
 import java.io.File;
 import java.io.InputStream;
 import java.io.StringReader;
@@ -31,15 +28,14 @@ import org.drools.drl.ast.descr.PackageDescr;
 import org.drools.drl.parser.DrlParser;
 import org.drools.drlonyaml.model.DrlPackage;
 import org.drools.util.IoUtils;
-
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
-import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature;
-
 import picocli.CommandLine.Command;
 import picocli.CommandLine.Option;
 import picocli.CommandLine.Parameters;
 
+import static org.drools.drlonyaml.cli.utils.Utils.conventionInputStream;
+import static org.drools.drlonyaml.cli.utils.Utils.conventionOutputConsumer;
+import static org.drools.drlonyaml.model.Utils.getYamlMapper;
+
 /**
  * Note: beyond different annotations, Parameters and Options are managed per 
subcommand,
  * in order to have them listed after the specific subcommand on the CLI.
@@ -55,14 +51,7 @@ public class Drl2Yaml implements Callable<Integer> {
     private InputStream inputStream;
     
     private static final DrlParser drlParser = new DrlParser();
-    private static final ObjectMapper mapper;
-    static {
-        YAMLFactory yamlFactory = YAMLFactory.builder()
-                .enable(Feature.MINIMIZE_QUOTES)
-                .build();
-        mapper = new ObjectMapper(yamlFactory);
-    }
-    
+
     @Override
     public Integer call() throws Exception {
         inputStream = conventionInputStream(inputFile);
@@ -76,7 +65,7 @@ public class Drl2Yaml implements Callable<Integer> {
         PackageDescr pkgDescr = drlParser.parse(new StringReader(drl));
         DrlPackage model = DrlPackage.from(pkgDescr);
         StringWriter writer = new StringWriter();
-        mapper.writeValue(writer, model);
+        getYamlMapper().writeValue(writer, model);
         final String yaml = writer.toString();
         writer.close();
         return yaml;
diff --git 
a/drools-drlonyaml-parent/drools-drlonyaml-cli/src/main/java/org/drools/drlonyaml/cli/Yaml2Drl.java
 
b/drools-drlonyaml-parent/drools-drlonyaml-cli/src/main/java/org/drools/drlonyaml/cli/Yaml2Drl.java
index 35ed656881..00123c54df 100644
--- 
a/drools-drlonyaml-parent/drools-drlonyaml-cli/src/main/java/org/drools/drlonyaml/cli/Yaml2Drl.java
+++ 
b/drools-drlonyaml-parent/drools-drlonyaml-cli/src/main/java/org/drools/drlonyaml/cli/Yaml2Drl.java
@@ -18,9 +18,6 @@
  */
 package org.drools.drlonyaml.cli;
 
-import static org.drools.drlonyaml.cli.utils.Utils.conventionInputStream;
-import static org.drools.drlonyaml.cli.utils.Utils.conventionOutputConsumer;
-
 import java.io.File;
 import java.io.InputStream;
 import java.util.concurrent.Callable;
@@ -28,15 +25,14 @@ import java.util.concurrent.Callable;
 import org.drools.drlonyaml.model.DrlPackage;
 import org.drools.drlonyaml.todrl.YAMLtoDrlDumper;
 import org.drools.util.IoUtils;
-
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
-import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature;
-
 import picocli.CommandLine.Command;
 import picocli.CommandLine.Option;
 import picocli.CommandLine.Parameters;
 
+import static org.drools.drlonyaml.cli.utils.Utils.conventionInputStream;
+import static org.drools.drlonyaml.cli.utils.Utils.conventionOutputConsumer;
+import static org.drools.drlonyaml.model.Utils.getYamlMapper;
+
 /**
  * Note: beyond different annotations, Parameters and Options are managed per 
subcommand,
  * in order to have them listed after the specific subcommand on the CLI.
@@ -51,14 +47,6 @@ public class Yaml2Drl implements Callable<Integer> {
     private File inputFile;
     private InputStream inputStream;
     
-    private static final ObjectMapper mapper;
-    static {
-        YAMLFactory yamlFactory = YAMLFactory.builder()
-                .enable(Feature.MINIMIZE_QUOTES)
-                .build();
-        mapper = new ObjectMapper(yamlFactory);
-    }
-    
     @Override
     public Integer call() throws Exception {
         inputStream = conventionInputStream(inputFile);
@@ -69,7 +57,7 @@ public class Yaml2Drl implements Callable<Integer> {
     }
     
     public static String yaml2drl(String yaml) throws Exception {
-        DrlPackage readValue = mapper.readValue(yaml, DrlPackage.class);
+        DrlPackage readValue = getYamlMapper().readValue(yaml, 
DrlPackage.class);
         final String drlText = YAMLtoDrlDumper.dumpDRL(readValue);
         return drlText;
     }
diff --git a/drools-drlonyaml-parent/drools-drlonyaml-todrl/pom.xml 
b/drools-drlonyaml-parent/drools-drlonyaml-integration-tests/pom.xml
similarity index 82%
copy from drools-drlonyaml-parent/drools-drlonyaml-todrl/pom.xml
copy to drools-drlonyaml-parent/drools-drlonyaml-integration-tests/pom.xml
index 37a2ab9fbc..4acce993d5 100644
--- a/drools-drlonyaml-parent/drools-drlonyaml-todrl/pom.xml
+++ b/drools-drlonyaml-parent/drools-drlonyaml-integration-tests/pom.xml
@@ -25,21 +25,21 @@
     <artifactId>drools-drlonyaml-parent</artifactId>
     <version>8.45.0-SNAPSHOT</version>
   </parent>
-  <artifactId>drools-drlonyaml-todrl</artifactId>
-  <name>Drools :: DRL on YAML :: to DRL emitter</name>
+  <artifactId>drools-drlonyaml-integration-tests</artifactId>
+  <name>Drools :: DRL on YAML :: Integration Tests</name>
 
   <properties>
-    <java.module.name>org.drools.drlonyaml.todrl</java.module.name>
+    <java.module.name>org.drools.drlonyaml.integration.tests</java.module.name>
   </properties>
 
   <dependencies>
     <dependency>
       <groupId>org.drools</groupId>
-      <artifactId>drools-drlonyaml-model</artifactId>
+      <artifactId>drools-drlonyaml-todrl</artifactId>
     </dependency>
     <dependency>
-      <groupId>org.freemarker</groupId>
-      <artifactId>freemarker</artifactId>
+      <groupId>org.drools</groupId>
+      <artifactId>drools-engine</artifactId>
     </dependency>
     <dependency><!-- For unit test logging: configure in 
src/test/resources/logback-test.xml -->
       <groupId>ch.qos.logback</groupId>
@@ -56,10 +56,5 @@
       <artifactId>assertj-core</artifactId>
       <scope>test</scope>
     </dependency>
-    <dependency>
-      <groupId>org.drools</groupId>
-      <artifactId>drools-drl-parser</artifactId>
-      <scope>test</scope>
-    </dependency>
   </dependencies>
 </project>
diff --git 
a/drools-drlonyaml-parent/drools-drlonyaml-integration-tests/src/test/java/org/drools/drlonyaml/integration/tests/Message.java
 
b/drools-drlonyaml-parent/drools-drlonyaml-integration-tests/src/test/java/org/drools/drlonyaml/integration/tests/Message.java
new file mode 100644
index 0000000000..ca5d3e5104
--- /dev/null
+++ 
b/drools-drlonyaml-parent/drools-drlonyaml-integration-tests/src/test/java/org/drools/drlonyaml/integration/tests/Message.java
@@ -0,0 +1,36 @@
+/**
+ * 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.drools.drlonyaml.integration.tests;
+
+public class Message {
+
+    private final String text;
+
+    public Message(String text) {
+        this.text = text;
+    }
+
+    public String getText() {
+        return text;
+    }
+
+    public int getSize() {
+        return text.length();
+    }
+}
diff --git 
a/drools-drlonyaml-parent/drools-drlonyaml-integration-tests/src/test/java/org/drools/drlonyaml/integration/tests/ProgrammaticProjectTest.java
 
b/drools-drlonyaml-parent/drools-drlonyaml-integration-tests/src/test/java/org/drools/drlonyaml/integration/tests/ProgrammaticProjectTest.java
new file mode 100644
index 0000000000..b9259d09f1
--- /dev/null
+++ 
b/drools-drlonyaml-parent/drools-drlonyaml-integration-tests/src/test/java/org/drools/drlonyaml/integration/tests/ProgrammaticProjectTest.java
@@ -0,0 +1,115 @@
+/**
+ * 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.drools.drlonyaml.integration.tests;
+
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.drools.drl.ast.descr.PackageDescr;
+import org.drools.drl.parser.DrlParser;
+import org.drools.drlonyaml.model.DrlPackage;
+import org.drools.model.codegen.ExecutableModelProject;
+import org.junit.Test;
+import org.kie.api.runtime.KieSession;
+import org.kie.internal.utils.KieHelper;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.drools.drlonyaml.model.Utils.getYamlMapper;
+
+public class ProgrammaticProjectTest {
+
+    @Test
+    public void testDrl() {
+        KieSession ksession = new KieHelper()
+                .addContent(getDrlRule(), 
"org/drools/drlonyaml/integration/tests/rule.drl")
+                .build(ExecutableModelProject.class)
+                .newKieSession();
+
+        checkKieSession(ksession);
+    }
+
+    @Test
+    public void testYaml() {
+        KieSession ksession = new KieHelper()
+                .addContent(getYamlRule(), 
"org/drools/drlonyaml/integration/tests/rule.drl.yaml")
+                .build(ExecutableModelProject.class)
+                .newKieSession();
+
+        checkKieSession(ksession);
+    }
+
+    private static void checkKieSession(KieSession ksession) {
+        List<String> result = new ArrayList<>();
+        ksession.setGlobal("result", result);
+
+        ksession.insert(new Message("test"));
+        ksession.insert(new Message("Hello World"));
+        ksession.insert(10);
+        ksession.insert(11);
+
+        int count = ksession.fireAllRules();
+        assertThat(count).isEqualTo(1);
+        assertThat(result).hasSize(1);
+        assertThat(result.get(0)).isEqualTo("Hello World");
+    }
+
+    private String drl2yaml(String drl) {
+        try (StringWriter writer = new StringWriter()) {
+            PackageDescr pkgDescr = new DrlParser().parse(new 
StringReader(drl));
+            DrlPackage model = DrlPackage.from(pkgDescr);
+            getYamlMapper().writeValue(writer, model);
+            return writer.toString();
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    private String getDrlRule() {
+        return "package org.drools.drlonyaml.integration.tests\n" +
+                "\n" +
+                "global java.util.List result;\n" +
+                "\n" +
+                "rule R when\n" +
+                "    $i : Integer()\n" +
+                "    $m : Message( size == $i )\n" +
+                "then\n" +
+                "    result.add( $m.getText() );\n" +
+                "end";
+    }
+
+    private String getYamlRule() {
+        return "name: org.drools.drlonyaml.integration.tests\n" +
+                "globals:\n" +
+                "- type: java.util.List\n" +
+                "  id: result\n" +
+                "rules:\n" +
+                "- name: R\n" +
+                "  when:\n" +
+                "  - given: Integer\n" +
+                "    as: $i\n" +
+                "  - given: Message\n" +
+                "    as: $m\n" +
+                "    having:\n" +
+                "    - size == $i\n" +
+                "  then: |\n" +
+                "    result.add( $m.getText() );";
+    }
+}
diff --git 
a/drools-drlonyaml-parent/drools-drlonyaml-integration-tests/src/test/resources/logback.xml
 
b/drools-drlonyaml-parent/drools-drlonyaml-integration-tests/src/test/resources/logback.xml
new file mode 100644
index 0000000000..2e72b33bc5
--- /dev/null
+++ 
b/drools-drlonyaml-parent/drools-drlonyaml-integration-tests/src/test/resources/logback.xml
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+
+<configuration>
+
+  <appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
+    <encoder>
+      <pattern>%-5level %msg%n</pattern>
+    </encoder>
+  </appender>
+
+  <logger name="org.kie" level="warn"/>
+  <logger name="org.drools" level="warn"/>
+
+  <root level="info">
+    <appender-ref ref="consoleAppender" />
+  </root>
+
+</configuration>
diff --git 
a/drools-drlonyaml-parent/drools-drlonyaml-model/src/main/java/org/drools/drlonyaml/model/Utils.java
 
b/drools-drlonyaml-parent/drools-drlonyaml-model/src/main/java/org/drools/drlonyaml/model/Utils.java
index a6c7f138a0..7c5836be99 100644
--- 
a/drools-drlonyaml-parent/drools-drlonyaml-model/src/main/java/org/drools/drlonyaml/model/Utils.java
+++ 
b/drools-drlonyaml-parent/drools-drlonyaml-model/src/main/java/org/drools/drlonyaml/model/Utils.java
@@ -21,6 +21,9 @@ package org.drools.drlonyaml.model;
 import java.util.ArrayList;
 import java.util.List;
 
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
+import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
 import org.drools.drl.ast.descr.AndDescr;
 import org.drools.drl.ast.descr.BaseDescr;
 import org.drools.drl.ast.descr.ExistsDescr;
@@ -28,6 +31,15 @@ import org.drools.drl.ast.descr.NotDescr;
 import org.drools.drl.ast.descr.PatternDescr;
 
 public class Utils {
+    private static final ObjectMapper mapper;
+
+    static {
+        YAMLFactory yamlFactory = YAMLFactory.builder()
+                .enable(YAMLGenerator.Feature.MINIMIZE_QUOTES)
+                .build();
+        mapper = new ObjectMapper(yamlFactory);
+    }
+
     private Utils() {
         // only static methods.
     }
@@ -53,4 +65,8 @@ public class Utils {
         }
         return results;
     }
+
+    public static ObjectMapper getYamlMapper() {
+        return mapper;
+    }
 }
diff --git a/drools-drlonyaml-parent/drools-drlonyaml-todrl/pom.xml 
b/drools-drlonyaml-parent/drools-drlonyaml-todrl/pom.xml
index 37a2ab9fbc..ca6a18e6e4 100644
--- a/drools-drlonyaml-parent/drools-drlonyaml-todrl/pom.xml
+++ b/drools-drlonyaml-parent/drools-drlonyaml-todrl/pom.xml
@@ -33,6 +33,10 @@
   </properties>
 
   <dependencies>
+    <dependency>
+      <groupId>org.drools</groupId>
+      <artifactId>drools-drl-extensions</artifactId>
+    </dependency>
     <dependency>
       <groupId>org.drools</groupId>
       <artifactId>drools-drlonyaml-model</artifactId>
diff --git 
a/drools-drlonyaml-parent/drools-drlonyaml-todrl/src/main/java/org/drools/drlonyaml/todrl/YAMLtoDrlDumper.java
 
b/drools-drlonyaml-parent/drools-drlonyaml-todrl/src/main/java/org/drools/drlonyaml/todrl/YAMLtoDrlDumper.java
index 9c615393c2..7d937a1629 100644
--- 
a/drools-drlonyaml-parent/drools-drlonyaml-todrl/src/main/java/org/drools/drlonyaml/todrl/YAMLtoDrlDumper.java
+++ 
b/drools-drlonyaml-parent/drools-drlonyaml-todrl/src/main/java/org/drools/drlonyaml/todrl/YAMLtoDrlDumper.java
@@ -27,6 +27,8 @@ import freemarker.template.Configuration;
 import freemarker.template.Template;
 import freemarker.template.TemplateExceptionHandler;
 
+import static org.drools.drlonyaml.model.Utils.getYamlMapper;
+
 public class YAMLtoDrlDumper {
     public static final Configuration CONFIGURATION = config();
     
@@ -50,4 +52,9 @@ public class YAMLtoDrlDumper {
         out.close();
         return result;
     }
+
+    public static String yaml2drl(String yaml) throws Exception {
+        DrlPackage readValue = getYamlMapper().readValue(yaml, 
DrlPackage.class);
+        return YAMLtoDrlDumper.dumpDRL(readValue);
+    }
 }
diff --git 
a/drools-drlonyaml-parent/drools-drlonyaml-todrl/src/main/java/org/drools/drlonyaml/todrl/YamlProviderImpl.java
 
b/drools-drlonyaml-parent/drools-drlonyaml-todrl/src/main/java/org/drools/drlonyaml/todrl/YamlProviderImpl.java
new file mode 100644
index 0000000000..fe8924750e
--- /dev/null
+++ 
b/drools-drlonyaml-parent/drools-drlonyaml-todrl/src/main/java/org/drools/drlonyaml/todrl/YamlProviderImpl.java
@@ -0,0 +1,38 @@
+/**
+ * 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.drools.drlonyaml.todrl;
+
+import org.drools.drl.extensions.YamlProvider;
+import org.drools.util.IoUtils;
+import org.kie.api.io.Resource;
+
+import static org.drools.drlonyaml.todrl.YAMLtoDrlDumper.yaml2drl;
+
+public class YamlProviderImpl implements YamlProvider {
+
+    @Override
+    public String loadFromResource(Resource resource) {
+        try {
+            String content = new 
String(IoUtils.readBytesFromInputStream(resource.getInputStream()));
+            return yaml2drl(content);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+}
diff --git 
a/drools-drlonyaml-parent/drools-drlonyaml-todrl/src/main/resources/META-INF/services/org.drools.drl.extensions.YamlProvider
 
b/drools-drlonyaml-parent/drools-drlonyaml-todrl/src/main/resources/META-INF/services/org.drools.drl.extensions.YamlProvider
new file mode 100644
index 0000000000..47adfda8c2
--- /dev/null
+++ 
b/drools-drlonyaml-parent/drools-drlonyaml-todrl/src/main/resources/META-INF/services/org.drools.drl.extensions.YamlProvider
@@ -0,0 +1 @@
+org.drools.drlonyaml.todrl.YamlProviderImpl
\ No newline at end of file
diff --git a/drools-drlonyaml-parent/pom.xml b/drools-drlonyaml-parent/pom.xml
index db0bfdba1d..66f2239ee3 100644
--- a/drools-drlonyaml-parent/pom.xml
+++ b/drools-drlonyaml-parent/pom.xml
@@ -36,5 +36,6 @@
     <module>drools-drlonyaml-todrl</module>
     <module>drools-drlonyaml-cli</module>
     <module>drools-drlonyaml-cli-tests</module>
+    <module>drools-drlonyaml-integration-tests</module>
   </modules>
 </project>
diff --git a/kie-api/src/main/java/org/kie/api/io/ResourceType.java 
b/kie-api/src/main/java/org/kie/api/io/ResourceType.java
index 3b0f22416c..868e269853 100644
--- a/kie-api/src/main/java/org/kie/api/io/ResourceType.java
+++ b/kie-api/src/main/java/org/kie/api/io/ResourceType.java
@@ -258,6 +258,11 @@ public class ResourceType implements Serializable {
                                                                       
"src/main/resources",
                                                                       "no_op");
 
+    public static final ResourceType YAML = addResourceTypeToRegistry("YAML",
+                                                                      "YAML 
format DRL",
+                                                                      
"src/main/resources",
+                                                                      
"drl.yaml", "drl.yml");
+
     public static ResourceType determineResourceType(final String 
resourceName) {
         for ( Map.Entry<String, ResourceType> entry : CACHE.entrySet() ) {
             if (resourceName.endsWith(entry.getKey())) {


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to