本文整理汇总了Java中org.drools.KnowledgeBase类的典型用法代码示例。如果您正苦于以下问题:Java KnowledgeBase类的具体用法?Java KnowledgeBase怎么用?Java KnowledgeBase使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
KnowledgeBase类属于org.drools包,在下文中一共展示了KnowledgeBase类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readKnowledgeBase
import org.drools.KnowledgeBase; //导入依赖的package包/类
public static KnowledgeBase readKnowledgeBase(List<RuleResource> resources) {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
for (RuleResource res: resources) {
try {
kbuilder.add(ResourceFactory.newClassPathResource(res.getRuleResourceFile()), res.getResType());
} catch (Exception ex) {
kbuilder.add(ResourceFactory.newFileResource(res.getRuleResourceFile()), res.getResType());
}
}
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;
}
示例2: main
import org.drools.KnowledgeBase; //导入依赖的package包/类
public static void main(String[] args) {
try {
// load up the knowledge base
KnowledgeBase kbase = readKnowledgeBase();
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
// go !
PI pi = new PI();
pi.setIterStrategy(PI.IterStrategySetting.ballardMethod.getStrategy());
ksession.insert(pi);
ksession.fireAllRules();
System.out.println("PI: " + pi.getPi());
logger.close();
} catch (Throwable t) {
t.printStackTrace();
}
}
示例3: main
import org.drools.KnowledgeBase; //导入依赖的package包/类
public static void main(String[] args) {
try {
// load up the knowledge base
KnowledgeBase kbase = readKnowledgeBase();
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
// go !
Employ emp = new Employ();
emp.setEduInfo("master");
emp.setResume("tech");
emp.setAnnualExam("good");
emp.setAwardPunish("award");
ksession.insert(emp);
ksession.startProcess("myrules.salary");
ksession.fireAllRules();
System.out.println("Basic Salary: " + emp.getBasicSalary());
System.out.println("Duty Salary: " + emp.getDutySalary());
System.out.println("Bonus : " + emp.getBonus());
System.out.println("rate : " + emp.getPercent());
System.out.printf("Total Salary: %.0f" , emp.getTotalSalary());
logger.close();
} catch (Throwable t) {
t.printStackTrace();
}
}
示例4: deploy
import org.drools.KnowledgeBase; //导入依赖的package包/类
public void deploy(DeploymentEntity deployment) {
KnowledgeBuilder knowledgeBuilder = null;
DeploymentCache deploymentCache = Context
.getProcessEngineConfiguration()
.getDeploymentCache();
Map<String, ResourceEntity> resources = deployment.getResources();
for (String resourceName : resources.keySet()) {
log.info("Processing resource " + resourceName);
if (resourceName.endsWith(".drl")) { // is only parsing .drls sufficient? what about other rule dsl's? (@see ResourceType)
if (knowledgeBuilder==null) {
knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
}
ResourceEntity resourceEntity = resources.get(resourceName);
byte[] resourceBytes = resourceEntity.getBytes();
Resource droolsResource = ResourceFactory.newByteArrayResource(resourceBytes);
knowledgeBuilder.add(droolsResource, ResourceType.DRL);
}
}
if (knowledgeBuilder!=null) {
KnowledgeBase knowledgeBase = knowledgeBuilder.newKnowledgeBase();
deploymentCache.addKnowledgeBase(deployment.getId(), knowledgeBase);
}
}
示例5: DroolsFrameAdapter
import org.drools.KnowledgeBase; //导入依赖的package包/类
public DroolsFrameAdapter(KnowledgeBase kb) {
this.kb = kb;
setName("Drools Runtime Engine");
List<String> names = new ArrayList<String>();
for (KnowledgePackage pkg : kb.getKnowledgePackages()) {
names.add(pkg.getName());
}
addMeta("drools.packages", names);
// custom 'ALL' trigger, let the KnowledgeBase resolve what it wants to do.
addTrigger(new IFrameTrigger<PatientEvent<IPatientObject>>(PatientEvent.class, null) {
protected boolean doEval(PatientEvent<IPatientObject> event) {
// TODO: only return true if there was an activation?
return true;
}
});
}
示例6: readKnowledgeBase
import org.drools.KnowledgeBase; //导入依赖的package包/类
/**
* A private method for rule parsing.
*
* @param path
* of the .drl file.
* @return KnowledgeBase.
* @throws Exception
* Exception.
*/
private static KnowledgeBase readKnowledgeBase(String path) throws Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
FileInputStream fis = new FileInputStream(path);
kbuilder.add(ResourceFactory.newInputStreamResource(fis), ResourceType.DRL);
KnowledgeBuilderErrors errors = kbuilder.getErrors();
if (errors.size() > 0) {
for (KnowledgeBuilderError error : errors) {
System.err.println(error + " @line " + error.getLines());
}
throw new IllegalArgumentException("Could not parse knowledge.");
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
return kbase;
}
示例7: createKnowledgeBase
import org.drools.KnowledgeBase; //导入依赖的package包/类
/**
* Method for initializing Drools KnowledgeBase.
* @return KnowledgeBase that can be used in reasoning.
*/
private KnowledgeBase createKnowledgeBase() {
// Statistic.addTimestamp(reasoningID, System.currentTimeMillis());
knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
// Statistic.addTimestamp(reasoningID, System.currentTimeMillis());
Resource res = ResourceFactory.newFileResource(tmpDRLFile);
// Statistic.addTimestamp(reasoningID, System.currentTimeMillis());
knowledgeBuilder.add(res, ResourceType.DRL);
// Statistic.addTimestamp(reasoningID, System.currentTimeMillis());
if ( knowledgeBuilder.hasErrors() ) {
LOGGER.error(knowledgeBuilder.getErrors().toString());
List<ModelElement> conflictingElements = new ArrayList<ModelElement>();
conflictingElements.add(rModel.getProject());
Message errorMsg = new Message("Unable to translate Project:\n" + knowledgeBuilder.getErrors().toString(),
conflictingElements, Status.ERROR);
result.addMessage(errorMsg);
}
// Statistic.addTimestamp(reasoningID, System.currentTimeMillis());
KnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
// Statistic.addTimestamp(reasoningID, System.currentTimeMillis());
knowledgeBase.addKnowledgePackages(knowledgeBuilder.getKnowledgePackages());
// Statistic.addTimestamp(reasoningID, System.currentTimeMillis());
return knowledgeBase;
}
示例8: main
import org.drools.KnowledgeBase; //导入依赖的package包/类
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 !
Message message = new Message();
message.setMessage("Hello World");
message.setStatus(Message.HELLO);
ksession.insert(message);
ksession.fireAllRules();
logger.close();
} catch (Throwable t) {
t.printStackTrace();
}
}
示例9: getKnowlegeBase
import org.drools.KnowledgeBase; //导入依赖的package包/类
private static KnowledgeBase getKnowlegeBase(List<EntityRuleDO> rules) {
if (rules == null || rules.size() == 0) {
return null;
}
StringBuilder sb = new StringBuilder();
for (EntityRuleDO entityRuleDO : rules) {
sb.append(entityRuleDO.getRule()).append("\n");
}
KnowledgeBuilderConfiguration config = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
config.setOption(EvaluatorOption.get("startsWith", new StartsWithEvaluatorDefinition()));
config.setOption(EvaluatorOption.get("endsWith", new EndsWithEvaluatorDefinition()));
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(config);
kbuilder.add(ResourceFactory.newByteArrayResource(sb.toString().getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
throw new ServiceException("Problem building cross field validation rules: " + kbuilder.getErrors());
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
return kbase;
}
示例10: executeViewRule
import org.drools.KnowledgeBase; //导入依赖的package包/类
@Override
public boolean executeViewRule(ServiceContext context, Long ruleId, BaseObject<?> baseObject) {
EntityRuleDO rule = entityRuleDAO.getById(ruleId);
if (rule != null) {
KnowledgeBase knowlegeBase = getKnowlegeBase(Lists.newArrayList(rule));
StatefulKnowledgeSession session = knowlegeBase.newStatefulKnowledgeSession();
for (Permission permission : userService.findAvailablePermissions(context)) {
session.insert(permission);
}
List<ValidationError> errorList = Lists.newArrayList();
session.setGlobal("errorList", errorList);
session.insert(baseObject);
session.fireAllRules();
session.dispose();
return errorList.isEmpty();
}
return true;
}
示例11: readKnowledgeBase
import org.drools.KnowledgeBase; //导入依赖的package包/类
private static KnowledgeBase readKnowledgeBase(String drlFileName) {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
String configDir = CarbonUtils.getCarbonConfigDirPath();
String droolsDir = configDir + File.separator + StratosConstants.DROOLS_DIR_NAME;
Resource resource = ResourceFactory.newFileResource(droolsDir + File.separator + drlFileName);
kbuilder.add(resource, ResourceType.DRL);
KnowledgeBuilderErrors errors = kbuilder.getErrors();
if (errors.size() > 0) {
for (KnowledgeBuilderError error : errors) {
log.error(error.getMessage());
}
throw new IllegalArgumentException("Could not parse knowledge");
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
return kbase;
}
示例12: AbstractDroolsEngine
import org.drools.KnowledgeBase; //导入依赖的package包/类
public AbstractDroolsEngine(KnowledgeBuilder kbuilder, Object[] globals,
Environment env, KnowledgeBaseConfiguration config,
SessionConfiguration sessionConf, String changesetPath,
Boolean hasKnowledgeAgent) {
KnowledgeBase kbase = createKBase(kbuilder, config);
if (hasKnowledgeAgent) {
createKnowledgeAgent(kbase, "MyKAgent");
wm = (ReteooStatefulSession) ((KnowledgeBaseImpl) kagent
.getKnowledgeBase()).ruleBase.newStatefulSession(
(SessionConfiguration) sessionConf, env);
ksession = new StatefulKnowledgeSessionImpl(wm, kagent
.getKnowledgeBase());
} else {
ksession = kbase.newStatefulKnowledgeSession(sessionConf, env);
}
for (Object global : globals) {
ksession.setGlobal(global.getClass().getSimpleName(), global);
}
}
示例13: createKnowledgeBase
import org.drools.KnowledgeBase; //导入依赖的package包/类
private KnowledgeBase createKnowledgeBase() {
log().debug("createKnowledgeBase: Creating Drools KnowledgeBase");
final Properties props = new Properties();
props.setProperty("drools.dialect.java.compiler.lnglevel", "1.6");
final PackageBuilderConfiguration conf = new PackageBuilderConfiguration(props);
KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder(conf);
// Use the rules file defined in the configuration file
// We will not throw an exception if the rules failed to be parsed
builder.add(ResourceFactory.newFileResource(m_configDao.getRulesFile()), ResourceType.DRL);
if( builder.hasErrors() ) {
log().error("Failed to create Drools KnowledgeBase: " + builder.getErrors().toString());
return null;
}
KnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
knowledgeBase.addKnowledgePackages(builder.getKnowledgePackages());
return knowledgeBase;
}
示例14: getRuleBackendRuntime
import org.drools.KnowledgeBase; //导入依赖的package包/类
public RuleBackendRuntime getRuleBackendRuntime(Map<String, String> properties,
ClassLoader classLoader){
ClassLoader existingClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(classLoader);
Properties knowledgeBaseProperties = new Properties();
knowledgeBaseProperties.putAll(properties);
KnowledgeBaseConfiguration knowledgeBaseConfiguration
= KnowledgeBaseFactory.newKnowledgeBaseConfiguration(knowledgeBaseProperties, classLoader);
KnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase(
knowledgeBaseConfiguration);
KnowledgeBuilderConfiguration builderConfiguration
= KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(
builderConfiguration);
Thread.currentThread().setContextClassLoader(existingClassLoader);
return new DroolsBackendRuntime(knowledgeBase, knowledgeBuilder , classLoader);
}
示例15: getStatefulKnowledgeSession
import org.drools.KnowledgeBase; //导入依赖的package包/类
public static StatefulKnowledgeSession getStatefulKnowledgeSession(String path) {
KnowledgeBase kbase;
StatefulKnowledgeSession ksession;
KnowledgeRuntimeLogger logger;
try {
kbase = readKnowledgeBase(path);
ksession = kbase.newStatefulKnowledgeSession();
logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
logger.close();
if(ksession != null) {
return ksession;
} else { return null; }
} catch(Throwable t) {
System.out.println("X:\tErr in DroolsTools: cannot read knowlegde base\n\t"+t.getMessage());
//t.printStackTrace();
return null;
}
}