org.drools.io.ResourceFactory Java Examples

The following examples show how to use org.drools.io.ResourceFactory. 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: TestMinimumRule.java    From attic-stratos with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    Resource resource = ResourceFactory.newFileResource(droolsFilePath);
    kbuilder.add(resource, ResourceType.DRL);
    KnowledgeBuilderErrors errors = kbuilder.getErrors();
    if (errors.size() > 0) {
        StringBuilder sb = new StringBuilder();
        for (KnowledgeBuilderError error : errors) {
            sb.append(error.getMessage());
        }
        if (sb.length() > 0) {
            log.error(sb.toString());
        }
        throw new IllegalArgumentException(String.format("Could not parse drools file: %s", droolsFilePath));
    }

    kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

    conf = ConfUtil.getInstance("src/test/resources/autoscaler.xml").getConfiguration();
}
 
Example #4
Source File: TestObsoletedMemberRule.java    From attic-stratos with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    Resource resource = ResourceFactory.newFileResource(droolsFilePath);
    kbuilder.add(resource, ResourceType.DRL);
    KnowledgeBuilderErrors errors = kbuilder.getErrors();
    if (errors.size() > 0) {
        StringBuilder sb = new StringBuilder();
        for (KnowledgeBuilderError error : errors) {
            sb.append(error.getMessage());
        }
        if (sb.length() > 0) {
            log.error(sb.toString());
        }
        throw new IllegalArgumentException(String.format("Could not parse drools file: %s", droolsFilePath));
    }

    kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    log.info("Knowledge base has been set up.");

    conf = ConfUtil.getInstance("src/test/resources/autoscaler.xml").getConfiguration();
}
 
Example #5
Source File: TestRulesPackaged.java    From attic-stratos with Apache License 2.0 6 votes vote down vote up
private void parseDroolsFile(String droolsFilePath) {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    Resource resource = ResourceFactory.newFileResource(droolsFilePath);
    kbuilder.add(resource, ResourceType.DRL);
    KnowledgeBuilderErrors errors = kbuilder.getErrors();
    if (errors.size() > 0) {
        StringBuilder sb = new StringBuilder();

        for (KnowledgeBuilderError error : errors) {
            sb.append(error.getMessage());
            log.error(error.getMessage());
        }
        if (sb.length() > 0) {
            log.error(sb.toString());
        }
        throw new IllegalArgumentException(String.format("Could not parse drools file: %s", droolsFilePath));
    }
}
 
Example #6
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 #7
Source File: DroolsBlueprintBean.java    From servicemix with Apache License 2.0 5 votes vote down vote up
private static Resource parseResource(String resource) {
    if ( resource.trim().startsWith( "classpath:" ) ) {
        return  ResourceFactory.newClassPathResource(resource.substring( resource.indexOf( ':' ) + 1 ),DroolsBlueprintBean.class);
    } else {
        return ResourceFactory.newUrlResource( resource );
    }
}
 
Example #8
Source File: RulesExecutorData.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public void initializeRules() {

    // To ensure the plugin classloader use for dependency resolution
    ClassLoader orig = Thread.currentThread().getContextClassLoader();
    ClassLoader loader = getClass().getClassLoader();
    Thread.currentThread().setContextClassLoader( loader );

    Resource ruleSet = null;
    if ( ruleString != null ) {
      ruleSet = ResourceFactory.newReaderResource( new StringReader( ruleString ) );
    } else {
      ruleSet = ResourceFactory.newFileResource( ruleFilePath );
    }
    kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add( ruleSet, ResourceType.DRL );

    if ( kbuilder.hasErrors() ) {
      System.out.println( kbuilder.getErrors().toString() );
      throw new RuntimeException( BaseMessages.getString( PKG, "RulesData.Error.CompileDRL" ) );
    }

    Collection<KnowledgePackage> pkgs = kbuilder.getKnowledgePackages();

    kbase = KnowledgeBaseFactory.newKnowledgeBase();
    // Cache the knowledge base as its creation is intensive
    kbase.addKnowledgePackages( pkgs );

    // reset classloader back to original
    Thread.currentThread().setContextClassLoader( orig );
  }
 
Example #9
Source File: RulesAccumulatorData.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public void initializeRules() {

    // To ensure the plugin classloader use for dependency resolution
    ClassLoader orig = Thread.currentThread().getContextClassLoader();
    ClassLoader loader = getClass().getClassLoader();
    Thread.currentThread().setContextClassLoader( loader );

    Resource ruleSet = null;
    if ( ruleString != null ) {
      ruleSet = ResourceFactory.newReaderResource( new StringReader( ruleString ) );
    } else {
      ruleSet = ResourceFactory.newFileResource( ruleFilePath );
    }
    kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add( ruleSet, ResourceType.DRL );

    if ( kbuilder.hasErrors() ) {
      System.out.println( kbuilder.getErrors().toString() );
      throw new RuntimeException( BaseMessages.getString( PKG, "RulesData.Error.CompileDRL" ) );
    }

    Collection<KnowledgePackage> pkgs = kbuilder.getKnowledgePackages();

    kbase = KnowledgeBaseFactory.newKnowledgeBase();
    // Cache the knowledge base as its creation is intensive
    kbase.addKnowledgePackages( pkgs );

    // reset classloader back to original
    Thread.currentThread().setContextClassLoader( orig );
  }