/*
 * Copyright 2017 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.
 * 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.benchmarks.dmn.buildtime;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.drools.benchmarks.common.ProviderException;
import org.drools.compiler.kie.builder.impl.ZipKieModule;
import org.kie.api.KieServices;
import org.kie.api.builder.ReleaseId;
import org.kie.api.builder.model.KieModuleModel;
import org.kie.api.runtime.KieContainer;
import org.kie.dmn.api.core.DMNRuntime;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.util.FileUtils;

@BenchmarkMode(Mode.AverageTime)
@State(Scope.Thread)
@Warmup(iterations = 40, time = 2, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 15, time = 2, timeUnit = TimeUnit.SECONDS)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public class DMNBuildKJarBenchmark {

    private KieServices kieServices;
    private ReleaseId releaseId;

    @Setup
    public void setup() throws ProviderException, IOException {
        kieServices = KieServices.Factory.get();
        releaseId = kieServices.newReleaseId("org.drools", "test-dmn-project", "1.0");
        prepareRepository();
    }

    @Benchmark
    public DMNRuntime createDMNRuntime() {
        KieContainer container = kieServices.newKieContainer(releaseId);
        return container.newKieSession().getKieRuntime(DMNRuntime.class);
    }

    private void prepareRepository() throws IOException {
        final File kJarFile = FileUtils.extractFromResource("/dmn/test-dmn-project-1.0.jar");
        final ZipKieModule zipKieModule = new ZipKieModule(releaseId, getKieModuleModel(), kJarFile);
        kieServices.getRepository().addKieModule(zipKieModule);
    }

    private KieModuleModel getKieModuleModel() {
        final KieModuleModel kieModuleModel = kieServices.newKieModuleModel();
        kieModuleModel
                .newKieBaseModel("default-kiebase")
                .addPackage("*")
                .setDefault(true)
                .newKieSessionModel("default-kiesession")
                .setDefault(true);
        return kieModuleModel;
    }
}

@@ -677,20 +677,27 @@
 java.io.
-File
+IOException
 ;%0Aimport
@@ -701,35 +701,36 @@
 ort java.io.
-IOException
+StringReader
 ;%0Aimport jav
@@ -748,32 +748,82 @@
 urrent.TimeUnit;
+%0A%0Aimport org.drools.benchmarks.common.DMNProvider;
 %0Aimport org.droo
@@ -884,46 +884,346 @@
 ols.
-compiler.kie.builder.impl.ZipKieModule
+benchmarks.common.providers.dmn.BusinessKnowledgeModelDMNProvider;%0Aimport org.drools.benchmarks.common.providers.dmn.ContextDMNProvider;%0Aimport org.drools.benchmarks.common.providers.dmn.DecisionDMNProvider;%0Aimport org.drools.benchmarks.common.providers.dmn.DecisionTableDMNProvider;%0Aimport org.drools.benchmarks.common.util.BuildtimeUtil
 ;%0Aim
@@ -1313,36 +1313,55 @@
 api.
-builder.model.KieModuleModel
+io.Resource;%0Aimport org.kie.api.io.ResourceType
 ;%0Aim
@@ -1847,47 +1847,8 @@
 mup;
-%0Aimport org.openjdk.jmh.util.FileUtils;
 %0A%0A@B
@@ -2328,100 +2328,18 @@
 d = 
-kieServices.newReleaseId(%22org.drools%22, %22test-dmn-project%22, %221.0%22);%0A        prepareRepository
+createKJar
 ();%0A
@@ -2573,30 +2573,28 @@
 ate 
-void prepareRepository
+ReleaseId createKJar
 () t
@@ -2605,32 +2605,33 @@
 s IOException %7B%0A
+%0A
         final Fi
@@ -2632,45 +2632,68 @@
 nal 
-File kJarFile = FileUtils.extractFrom
+Resource resource1 = kieServices.getResources().newClassPath
 Reso
@@ -2702,151 +2702,92 @@
 ce(%22
-/
 dmn/
-test-dmn-project-1.0.jar%22);%0A        final ZipKieModule zipKieModule = new ZipKieModule(releaseId, getKieModuleModel(), kJarFile
+validation.dmn%22);%0A        resource1.setResourceType(ResourceType.DMN
 );%0A
+%0A
         
 kieS
@@ -2786,273 +2786,498 @@
     
-kieServices.getRepository().addKieModule(zipKieModule);%0A    %7D%0A%0A    private KieModuleModel getKieModuleModel() %7B%0A        final KieModuleModel kieModuleModel = kieServices.newKieModuleModel();%0A        kieModuleModel%0A                .newKieBaseModel(%22default-kiebase
+return BuildtimeUtil.createKJarFromResources(false,%0A                                                          resource1,%0A                                                          getDMNResource(new BusinessKnowledgeModelDMNProvider(), %22bkm.dmn%22),%0A                                                          getDMNResource(new DecisionTableDMNProvider(), %22decisionTable.dmn%22),%0A                                                          getDMNResource(new DecisionDMNProvider(), %22decision.dmn
 %22)
+,
 %0A   
@@ -3293,181 +3293,417 @@
     
-.addPackage(%22*%22)
+                                          getDMNResource(new ContextDMNProvider(), %22context.dmn%22));
 %0A    
- 
+%7D%0A%0A
     
-       .setDefault(true)%0A                .newKieSessionModel(%22default-kiesession%22)%0A                .setDefault(true);%0A        return kieModuleModel
+private Resource getDMNResource(final DMNProvider provider, final String sourcePath) %7B%0A        return kieServices.getResources()%0A                .newReaderResource(new StringReader(provider.getDMN(1000)))%0A                .setResourceType(ResourceType.DMN)%0A                .setSourcePath(sourcePath)
 ;%0A  

Fix DMNBuildKJarBenchmark