gpt4 book ai didi

java - 电子表格决策可以被打破吗?

转载 作者:太空宇宙 更新时间:2023-11-04 09:19:11 25 4
gpt4 key购买 nike

我尝试使用 Excel 格式 (*.xlsx)(一种电子表格)的表格决策。我不想将电子表格放在应用程序的文件夹资源中。所以我创建了一个文件夹:folder/Discount.xls。

但是当我运行程序时,它会抛出“文件未找到”异常。如果电子表格文件不在资源文件夹中,则似乎无法加载,因此在 jar 中。

我想知道我是否可以在电子表格时制作一个程序,以便我的规则可以动态加载?我会选择一个或另一个电子表格来对我的规则进行热更改。但不知道是否可行。

我有一些代码:

    KieServices kieServices = KieServices.Factory.get();
File file = new File("folder/Discount.xls");
Resource resource = ResourceFactory.newFileResource(file);
KieFileSystem kieFileSystem = kieServices.newKieFileSystem().write(resource);
KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
kieBuilder.buildAll();
KieRepository kieRepository = kieServices.getRepository();
ReleaseId krDefaultReleaseId = kieRepository.getDefaultReleaseId();
KieContainer kieContainer = kieServices.newKieContainer(krDefaultReleaseId);
KieSession kieSession = kieContainer.newKieSession();

前面的代码抛出运行时异常:

Exception in thread "main" java.lang.RuntimeException: Cannot find KieModule: org.default:artifact:1.0.0

最佳答案

我找到了问题的解决方案。电子表格可以位于 java 资源文件夹之外的其他文件夹中。我的类测试如下:

public class Main {

private static InternalKnowledgeBase createKnowledgeBaseFromSpreadsheet() throws Exception {

DecisionTableConfiguration decisionTableConfiguration = KnowledgeBuilderFactory.newDecisionTableConfiguration();
decisionTableConfiguration.setInputType(DecisionTableInputType.XLS);
KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
File file = new File("folder/Discount.xls");
Resource resourceFile = ResourceFactory.newFileResource(file);
knowledgeBuilder.add(resourceFile, ResourceType.DTABLE, decisionTableConfiguration);

if (knowledgeBuilder.hasErrors()) {
throw new RuntimeException(knowledgeBuilder.getErrors().toString());
}

InternalKnowledgeBase internalKnowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
Collection<KiePackage> kiePackages = knowledgeBuilder.getKnowledgePackages();
internalKnowledgeBase.addPackages(kiePackages);
return internalKnowledgeBase;
}

public static void main(String[] args) {

(new Main()).run();
}

public void run() {
System.out.println("--- Start Code ---");
StatelessKieSession session = null;

try {
InternalKnowledgeBase knowledgeBase = createKnowledgeBaseFromSpreadsheet();
session = knowledgeBase.newStatelessKieSession();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Customer customer = new Customer(CustomerType.INDIVIDUAL, 1);
System.out.println(customer.toString());
session.execute(customer);
System.out.println(customer.toString());
System.out.println("--- End Code ---");
}
}

我的客户类(必须添加 getter、setter 并覆盖 toString 方法):

public class Customer {

public enum CustomerType {
INDIVIDUAL, BUSINESS;
}
private CustomerType type;
private int years;
private int discount;

public Customer(CustomerType individual, int years) {
this.type = individual;
this.years = years;
}
}

我的 *.pom 文件包含:

<dependencies>
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-ci</artifactId>
<version>7.28.0.Final</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-decisiontables</artifactId>
<version>7.28.0.Final</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
<scope>runtime</scope>
</dependency>

关于java - 电子表格决策可以被打破吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58559224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com