Java Code Examples for org.drools.io.ResourceFactory#newFileResource()

The following examples show how to use org.drools.io.ResourceFactory#newFileResource() . 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: 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 2
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 3
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 4
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 5
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 6
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 );
  }