org.drools.KnowledgeBase Java Examples

The following examples show how to use org.drools.KnowledgeBase. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RulesDeployer.java    From activiti6-boot2 with Apache License 2.0 6 votes vote down vote up
public void deploy(DeploymentEntity deployment, Map<String, Object> deploymentSettings) {
  log.debug("Processing deployment {}", deployment.getName());

  KnowledgeBuilder knowledgeBuilder = null;

  DeploymentManager deploymentManager = Context.getProcessEngineConfiguration().getDeploymentManager();

  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();
    deploymentManager.getKnowledgeBaseCache().add(deployment.getId(), knowledgeBase);
  }
}
 
Example #2
Source File: AutoscalerRuleEvaluator.java    From attic-stratos with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #3
Source File: RulesDeployer.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
public void deploy(DeploymentEntity deployment, Map<String, Object> deploymentSettings) {
  log.debug("Processing deployment {}", deployment.getName());
  
  KnowledgeBuilder knowledgeBuilder = null;

  DeploymentManager deploymentManager = Context
    .getProcessEngineConfiguration()
    .getDeploymentManager();
  
  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();
    deploymentManager.getKnowledgeBaseCache().add(deployment.getId(), knowledgeBase);
  }
}
 
Example #4
Source File: BusinessRuleTaskActivityBehavior.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
public void execute(DelegateExecution execution) {
  ProcessDefinition processDefinition = ProcessDefinitionUtil.getProcessDefinition(execution.getProcessDefinitionId());
  String deploymentId = processDefinition.getDeploymentId();

  KnowledgeBase knowledgeBase = RulesHelper.findKnowledgeBaseByDeploymentId(deploymentId);
  StatefulKnowledgeSession ksession = knowledgeBase.newStatefulKnowledgeSession();

  if (variablesInputExpressions != null) {
    Iterator<Expression> itVariable = variablesInputExpressions.iterator();
    while (itVariable.hasNext()) {
      Expression variable = itVariable.next();
      ksession.insert(variable.getValue(execution));
    }
  }

  if (!rulesExpressions.isEmpty()) {
    RulesAgendaFilter filter = new RulesAgendaFilter();
    Iterator<Expression> itRuleNames = rulesExpressions.iterator();
    while (itRuleNames.hasNext()) {
      Expression ruleName = itRuleNames.next();
      filter.addSuffic(ruleName.getValue(execution).toString());
    }
    filter.setAccept(!exclude);
    ksession.fireAllRules(filter);

  } else {
    ksession.fireAllRules();
  }

  Collection<Object> ruleOutputObjects = ksession.getObjects();
  if (ruleOutputObjects != null && !ruleOutputObjects.isEmpty()) {
    Collection<Object> outputVariables = new ArrayList<Object>();
    for (Object object : ruleOutputObjects) {
      outputVariables.add(object);
    }
    execution.setVariable(resultVariable, outputVariables);
  }
  ksession.dispose();
  leave(execution);
}
 
Example #5
Source File: AutoscalerRuleEvaluator.java    From attic-stratos with Apache License 2.0 5 votes vote down vote up
private AutoscalerRuleEvaluator() {
    knowledgeBases = new HashMap<String, KnowledgeBase>();
    parseAndBuildKnowledgeBaseForDroolsFile(StratosConstants.OBSOLETE_CHECK_DROOL_FILE);
    parseAndBuildKnowledgeBaseForDroolsFile(StratosConstants.SCALE_CHECK_DROOL_FILE);
    parseAndBuildKnowledgeBaseForDroolsFile(StratosConstants.MIN_CHECK_DROOL_FILE);
    parseAndBuildKnowledgeBaseForDroolsFile(StratosConstants.MAX_CHECK_DROOL_FILE);
    parseAndBuildKnowledgeBaseForDroolsFile(StratosConstants.DEPENDENT_SCALE_CHECK_DROOL_FILE);


}
 
Example #6
Source File: AutoscalerRuleEvaluator.java    From attic-stratos with Apache License 2.0 5 votes vote down vote up
public void parseAndBuildKnowledgeBaseForDroolsFile(String drlFileName) {
    KnowledgeBase knowledgeBase = readKnowledgeBase(drlFileName);
    if (knowledgeBase == null) {
        log.error("Knowledge base couldn't be read for" +
                " [drool-file] " + drlFileName);
    } else {
        knowledgeBases.put(drlFileName, knowledgeBase);
        if (log.isDebugEnabled()) {
            log.debug("Drools file is parsed successfully:" +
                    " [ file-name] " + drlFileName);
        }
    }
}
 
Example #7
Source File: BusinessRuleTaskActivityBehavior.java    From activiti6-boot2 with Apache License 2.0 4 votes vote down vote up
public void execute(DelegateExecution execution) {
  ActivityExecution activityExecution = (ActivityExecution) execution;
  PvmProcessDefinition processDefinition = activityExecution.getActivity().getProcessDefinition();
  String deploymentId = processDefinition.getDeploymentId();
  
  KnowledgeBase knowledgeBase = RulesHelper.findKnowledgeBaseByDeploymentId(deploymentId); 
  StatefulKnowledgeSession ksession = knowledgeBase.newStatefulKnowledgeSession();
  
  if (variablesInputExpressions != null) {
    Iterator<Expression> itVariable = variablesInputExpressions.iterator();
    while (itVariable.hasNext()) {
      Expression variable = itVariable.next();
      ksession.insert(variable.getValue(execution));
    }
  }
  
  if (!rulesExpressions.isEmpty()) {
    RulesAgendaFilter filter = new RulesAgendaFilter();
    Iterator<Expression> itRuleNames = rulesExpressions.iterator();
    while (itRuleNames.hasNext()) {
      Expression ruleName = itRuleNames.next();
      filter.addSuffic(ruleName.getValue(execution).toString());
    }
    filter.setAccept(!exclude);
    ksession.fireAllRules(filter);
    
  } else {
    ksession.fireAllRules();
  }
  
  Collection<Object> ruleOutputObjects = ksession.getObjects();
  if (ruleOutputObjects != null && !ruleOutputObjects.isEmpty()) {
    Collection<Object> outputVariables = new ArrayList<Object>();
    for (Object object : ruleOutputObjects) {
      outputVariables.add(object);
    }
    execution.setVariable(resultVariable, outputVariables);
  }
  ksession.dispose();
  leave(activityExecution);
}