Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package org.fusesource.demo.main;
import org.kie.api.definition.type.FactType;
import org.kie.api.io.ResourceType;
import org.kie.internal.KnowledgeBase;
import org.kie.internal.KnowledgeBaseFactory;
import org.kie.internal.builder.KnowledgeBuilder;
import org.kie.internal.builder.KnowledgeBuilderFactory;
import org.kie.internal.io.ResourceFactory;
import org.kie.internal.runtime.StatefulKnowledgeSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MainApp4 {
private static final Logger logger = LoggerFactory.getLogger(MainApp4.class);
public static void main(String[] args) throws Exception {
Knowledge kie = new Knowledge();
kie.build();
}
static protected StatefulKnowledgeSession createKnowledgeSession(KnowledgeBase kbase) {
return kbase.newStatefulKnowledgeSession();
}
public static class Knowledge {
public void build() throws IllegalAccessException, InstantiationException {
/* KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
Resource res = ResourceFactory.newClassPathResource("changeset.xml",getClass())
.setResourceType(ResourceType.CHANGE_SET);
kfs.write(res);
KieBuilder kbi = ks.newKieBuilder(kfs).buildAll();
// Create Session
KieSession kse = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
// Create KieBase
KieBase kbase = kse.getKieBase();*/
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("changeset.xml",
getClass()),
ResourceType.CHANGE_SET);
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
StatefulKnowledgeSession kse = createKnowledgeSession(kbase);
FactType loanType = kbase.getFactType("mortgages","LoanApplication");
FactType applicantType = kbase.getFactType("mortgages","Applicant");
FactType incomeType = kbase.getFactType("mortgages", "IncomeSource");
logger.info(">> FactTypes created for LoanApplication, Applicant, IncomeSource");
Object loan = loanType.newInstance();
loanType.set(loan, "amount", 200000);
loanType.set(loan, "deposit", 1500);
loanType.set(loan, "lengthYears", 30);
Object applicant = applicantType.newInstance();
applicantType.set(applicant,"name","charles");
// applicantType.set(applicant,"creditRating","OK");
// applicantType.set(applicant,"creditRating","Sub prime");
applicantType.set(applicant,"age",35);
Object income = incomeType.newInstance();
// incomeType.set(income, "type", "Job");
incomeType.set(income, "type", "Asset");
incomeType.set(income, "amount", 65000);
logger.info(">> Objects instantiated");
kse.insert(loan);
kse.insert(applicant);
kse.insert(income);
logger.info(">> Facts added to the Session");
kse.fireAllRules();
logger.info(">> Rules fired");
logger.info("The loan application is now:\n" + loan);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment