Using the Decision Center API

You can use the Decision Center API to manage projects and baselines, create packages and rules, search the repository, and to lock, unlock, and commit project elements.

IlrSession is the main entry point to Decision Center. The following code shows how to open a Decision Center session:


// Open the <product> session
IlrSessionFactory factory = new IlrRemoteSessionFactory();
factory.connect(login, password, serverUrl, datasource);
IlrSession session = factory.getSession();
try {
	// Do something
} finally {
	session.close();
}

You use this class to do the following:

  • Retrieve, erase, or create projects

  • Manage baselines

  • Create packages and rules

  • Search the Decision Center repository

  • Lock, unlock, and commit project elements

Retrieve, erase, or create projects

The following code shows how to retrieve, erase, and create a project in Decision Center:


// Get the project by name
String projectName = "testProject";
IlrRuleProject testProject = (IlrRuleProject) IlrSessionHelper.getProjectNamed(session, projectName);
if (testProject != null) {
    // Erase a project
       session.eraseProject(testProject);
       System.out.println("Project Deleted");
}
// Create a project
testProject = (IlrRuleProject) IlrSessionHelper.createRuleProject(session, projectName);

Manage baselines

The following code shows how to open the current baseline and create a new baseline in Decision Center:


// Open current baseline
IlrBaseline currentBaseline = IlrSessionHelper.getCurrentBaseline(session, testProject);
session.setWorkingBaseline(currentBaseline);

// Create a new baseline
Date today = new Date();
String baselineName = "testBaseline " + today.toString();
IlrBaseline newBaseline = session.createBaselineFromCurrentState(baselineName);

Create packages and rules

The following code shows how to create a package and a rule in Decision Center:


// Create a package at the root level
String packageName = "testPackage";
IlrRulePackage testPackage = IlrSessionHelper.createRulePackage(session, null, packageName);

// Create a rule
String ruleName = "testRule";
IlrSessionHelper.createActionRule(session, testPackage, ruleName, "then print \"hello\";");

Search the Decision Center repository

The following code shows how to query the Decision Center repository:


{
	// Query with BQL
	String query = new String("Find all business rules such that the name of each business rule is \"testRule\"");
	IlrDefaultSearchCriteria criteria = new IlrDefaultSearchCriteria(query.toString());
	List summaries = session.findElements(criteria, IlrModelConstants.ELEMENT_SUMMARY);
	for (int i = 0; i < summaries.size(); i++) {
		IlrElementSummary ruleSummary = (IlrElementSummary) summaries.get(i);
		System.out.println(ruleSummary.getName());
	}
}

{
	// Query by code: find rule by name
	String name = "testRule";
	IlrBrmPackage metaModel = session.getBrmPackage();
	IlrDefaultSearchCriteria criteria = new IlrDefaultSearchCriteria(metaModel.getActionRule(), Arrays.asList(metaModel.getModelElement_Name()), Arrays.asList(name));
	List details = session.findElements(criteria, IlrModelConstants.ELEMENT_DETAILS);
	for (int i = 0; i < details.size(); i++) {
		IlrActionRule rule = (IlrActionRule) details.get(i);
		System.out.println(rule.getName());
	}
}

Lock, unlock, and commit project elements

The following code shows how to lock, unlock, and commit project elements in Decision Center:


// Lock element
session.lockElement(rule);

// Unlock element
session.unlockElement(rule);

// Modify a property and commit
rule.setRawValue(metaModel.getRule_Priority(), "2");
session.commit(rule);

// Modify an aggregated element and commit
IlrDefinition definition = rule.getDefinition();
definition.setRawValue(metaModel.getDefinition_Body(), "then print \"helloooo\";");
IlrCommitableObject co = new IlrCommitableObject(rule);
co.addModifiedElement(metaModel.getRuleArtifact_Definition(), definition);
session.commit(co);