在流口水中,java如何在不更改主代码的情况下增加条件数量?

时间:2019-04-04 10:02:14

标签: java drools

如何在不更改代码的情况下增加流口水的决策表中的条件数。由于所有条件都需要单独的getter和setter,因此如何使代码自动化?

package com.sample;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.DecisionTableConfiguration;
import org.drools.builder.DecisionTableInputType;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.Resource;
import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.StatefulKnowledgeSession;
import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * This is a sample class to launch a rule.
 */
public class DroolsTest 
    {

    public static final void main(String[] args) {
        try {
            // load up the knowledge base
            KnowledgeBase kbase = readKnowledgeBase();
            StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
            KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
            // go !
            System.out.println("Please enter the jewellery name: ");
            BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
            String name=reader.readLine();
            Jewellery jewel = new Jewellery();
            jewel.setType(name);
            ksession.insert(jewel);
            ksession.fireAllRules();
            System.out.println("The discount for "+name+" is "+jewel.getDiscount());
            logger.close();
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    private static KnowledgeBase readKnowledgeBase() throws Exception {
        DecisionTableConfiguration dtable=KnowledgeBuilderFactory.newDecisionTableConfiguration();
        dtable.setInputType(DecisionTableInputType.XLS);
        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        Resource xlsrs=ResourceFactory.newClassPathResource("hello.xls");
        kbuilder.add(xlsrs,ResourceType.DTABLE,dtable);
        KnowledgeBuilderErrors errors = kbuilder.getErrors();
        if (errors.size() > 0) {
            for (KnowledgeBuilderError error: errors) {
                System.err.println(error);
            }
            throw new IllegalArgumentException("Could not parse knowledge.");
        }
        KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
        kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
        return kbase;
    }




}

有一个单独的类别的珠宝,只有一个条件和一个动作。如何在不更改代码的情况下增加这些条件和动作。

package com.sample;

public class Jewellery 
{
    private String type;

    private int discount;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getDiscount() {
        return discount;
    }

    public void setDiscount(int discount) {
        this.discount = discount;
    }
}

0 个答案:

没有答案