예제 #1
0
  public static void main(String args[]) throws Exception {

    // location for an existing dictionary
    final String dictionaryLocation = "./oracle/rules/rulesexampleproject/MyRulesDictionary.rules";

    // Load an existing dictionary, create a session handle in case JDeveloper or some other
    // user is also editing the dictionary.

    RuleDictionary dictionary = loadRuleDictionary(dictionaryLocation).createHandle();

    // Delete the existing model, in case we ran previously
    dictionary.getDataModel().clear();

    // Add a new Java Fact to the dictionary
    dictionary = addFactsToDictionary(dictionary, rulesproject.ItemT.class);

    // Add a new bucketset to the dictionary
    BucketSet licenseBucketSet = addBucketSet(dictionary);

    // Associate the new BucketSet with new Fact field
    associateBucketSetToFact(licenseBucketSet, "ItemT", "productWeight", dictionary.getDataModel());

    // update dictionary
    if (!updateRuleDictionary(dictionary)) System.out.println("UNABLE to update dictionary.");

    // check for existing ruleset named MyRuleSet
    RuleSet myRuleSet = dictionary.getRuleSet("MyRuleSet");

    // if it does exist, because we ran before, remove it
    if (myRuleSet != null) {
      dictionary.removeRuleSet("MyRuleSet");
      if (!updateRuleDictionary(dictionary)) System.out.println("UNABLE to update dictionary.");
      System.out.println("Removed old ruleset");
    }

    // Add a new Ruleset
    myRuleSet = dictionary.createEmptyRuleSet("MyRuleSet");
    if (!updateRuleDictionary(dictionary)) System.out.println("UNABLE to update dictionary.");

    // Add a new 'if-then' rule to the RuleSet
    addNewRuleToRuleset(myRuleSet);
    if (!updateRuleDictionary(dictionary)) System.out.println("UNABLE to update dictionary.");

    // Add a decisionTable with rules that use the Bucketset
    addDecisiontTableRuleToRuleset(dictionary, licenseBucketSet, myRuleSet);

    // Update and rewrite the dictionary file

    boolean success = updateRuleDictionary(dictionary);
    if (success) {
      storeRuleDictionary(dictionary, dictionaryLocation);
      System.out.println("Wrote dictionary to filesystem");
    } else System.out.println("Unable to update dictionary");
  }
  public static void main(String[] args) throws SDKException, Exception {
    final String dictionaryLocation = "./oracle/rules/com/sella/products/InsuranceProducts.rules";
    String dictionaryName = "InsuranceProducts";
    String dictionaryPackage = "com.sella.products";
    RuleDictionary dictionary =
        RuleUtil.createDictionaryInMemory(dictionaryName, dictionaryPackage);

    // Delete the existing model
    dictionary.getDataModel().clear();

    // Add a new Java Fact to the dictionary
    dictionary = RuleUtil.addFactsToDictionary(dictionary, Person.class);
    dictionary = RuleUtil.addFactsToDictionary(dictionary, Person.PolicyType.class);
    dictionary = RuleUtil.addFactsToDictionary(dictionary, Product.class);

    // Add a new bucketset to the dictionary
    BucketSet ageBucketSet = addAgeBucketSet(dictionary);

    // Associate the new BucketSet with new Fact field
    RuleUtil.associateBucketSetToFact(ageBucketSet, "Person", "age", dictionary.getDataModel());

    // update dictionary
    if (!RuleUtil.updateRuleDictionary(dictionary)) {
      System.out.println("Dictionary update failed.");
    } else {
      System.out.println("Dictionary Updated.");
    }

    // Create 'Decision Function'
    DecisionFunction decisionFunction =
        createDecisionFunction(
            dictionary, "ProductRuleDecisionService", "com.sella.facts.Person", false, false);
    decisionFunction.getDecisionFunctionInputTable().get(0).setList(false);
    decisionFunction.getDecisionFunctionInputTable().get(0).setTree(false);

    // Add 'Decision Function' output parameter
    addDecisionFunctionOutput(decisionFunction);

    if (!RuleUtil.updateRuleDictionary(dictionary)) {
      System.out.println("Dictionary update failed.");
    } else {
      System.out.println("Dictionary Updated.");
    }

    // set 'Decision Function' output parameter type
    decisionFunction.getDecisionFunctionOutputTable().get(0).setList(true);

    if (!RuleUtil.updateRuleDictionary(dictionary)) {
      System.out.println("Dictionary update failed.");
    } else {
      System.out.println("Dictionary Updated.");
    }

    // check for existing ruleset named MyRuleSet
    RuleSet myRuleSet = dictionary.getRuleSet("ProductRuleDecisionService");

    // if it does exist, remove it
    if (myRuleSet != null) {
      dictionary.removeRuleSet("ProposalProductRules");
      if (!RuleUtil.updateRuleDictionary(dictionary)) {
        System.out.println("Dictionary update failed.");
      } else {
        System.out.println("Removed old ruleset");
      }
    } else {
      // Add a new Ruleset
      myRuleSet = dictionary.createEmptyRuleSet("ProductRuleDecisionService");
      if (!RuleUtil.updateRuleDictionary(dictionary)) {
        System.out.println("Dictionary update failed.");
      } else {
        System.out.println("Dictionary Updated.");
      }
    }

    // Add a decisionTable with rules that use the Bucketset
    addDecisiontTableRuleToRuleset(dictionary, ageBucketSet, myRuleSet);

    // Update and rewrite the dictionary file
    boolean success = RuleUtil.updateRuleDictionary(dictionary);
    if (success) {
      RuleUtil.storeRuleDictionary(dictionary, dictionaryLocation);
      System.out.println("Wrote dictionary to filesystem");
    } else {
      System.out.println("Unable to update dictionary");
    }
  }