package com.huigou.bpm.webservice.demo.rule;

import com.huigou.bpm.webservice.demo.model.User;
import com.huigou.bpm.webservice.model.RuleResult;
import com.huigou.util.FileHelper;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.core.impl.InternalKnowledgeBase;
import org.drools.io.ResourceFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.kie.api.KieServices;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.internal.utils.KieHelper;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:applicationContext.xml"})
public class TestUnit {
    private static KieContainer container = null;
    private KieSession statefulKieSession = null;

    @Test
    public void test() {
        KieServices kieServices = KieServices.Factory.get();
        container = kieServices.getKieClasspathContainer();
        statefulKieSession = container.newKieSession("myAgeSession");
//        User usersss = new User("张三",11);
//        String sss = "张三";
        RuleResult result  = new RuleResult();
        List<Object> ss = new ArrayList<>();
//        ss.add(sss);
        ss.add("A2B3AA1015BD63D6E050007F01001148");
//        statefulKieSession.insert(usersss);
        statefulKieSession.insert(ss);
        statefulKieSession.insert(result);
//        statefulKieSession.insert(ss);
        statefulKieSession.fireAllRules();
//        statefulKieSession.fireAllRules(10);
        statefulKieSession.dispose();
        System.out.println("规则执行完毕");
        System.out.println(result.isSuccess());
//        File file = new File("E:\\IdeaProjects\\topsun-pt\\huigou\\huigou-webservice\\src\\main\\resources\\rules\\age.drl");
//        if (file.exists()) {
//            try {
//                StringBuffer b = new StringBuffer();
//                FileReader reader = new FileReader(file);
////                FileInputStream fi = new FileInputStream(file);
////                FileOutputStream fo = new FileOutputStream(file);
//                BufferedReader br = new BufferedReader(reader);
//                String line = br.readLine();
//                while (line != null) {
////                    b.append("\n"+line);
//                    b.append(line);
//                    line = br.readLine();
//                }
//                br.close();
//                reader.close();
////                System.out.println(b);
//
////                byte[] bt = new byte[fi.available()];
////                fi.read(bt);
////                ByteArrayOutputStream baos = new ByteArrayOutputStream();
////                baos.write(bt);
////                baos.close();
////                fi.close();
//                KieSession ks = null;
//                KieHelper kh = new KieHelper();
//                kh.addContent(b.toString(), ResourceType.DRL);
////                kh.addResource(ResourceFactory.newByteArrayResource(bt),ResourceType.DRL);
//                ks = kh.build().newKieSession();
//                User usersss = new User("张三",11);
//                ks.insert(usersss);
//                ks.fireAllRules();
//                ks.dispose();
//
//            }catch(Exception e){
//                e.printStackTrace();
//            }
//        }
    }

}
