I've two guided decision tables in one project. My requirement is to execute only those rules which belong to one decision table at any given point of time. I tried to use RuleNameEndsWithAgendaFilter("some suffix") with FireAllRulesCommand class but Kie server is not filtering the rules based on the passed AgendaFilter. It runs all the rules every time.
Drools Workbench version 7.2.0.Final and Drools Kie Server version 7.2.0.Final.
Below is the code snippet for the same:
KieServicesConfiguration configuration = KieServicesFactory.newRestConfiguration(serverUrl, user, password);
Set<Class<?>> allClasses = new HashSet<Class<?>>();
allClasses.add(OrderItem.class);
configuration.addExtraClasses(allClasses);
configuration.setMarshallingFormat(MarshallingFormat.JAXB);
OrderItem oi = new OrderItem("Mobile", 7000.00, 0.00, "");
KieServicesClient client = KieServicesFactory.newKieServicesClient(configuration);
// work with rules
List<ExecutableCommand<?>> commands = new ArrayList<ExecutableCommand<?>>();
BatchExecutionCommandImpl executionCommand = new BatchExecutionCommandImpl(commands, "defaultKieSession");
InsertObjectCommand insertObjectCommand = new InsertObjectCommand();
insertObjectCommand.setOutIdentifier("orderItem");
insertObjectCommand.setObject(oi);
FireAllRulesCommand fireAllRulesCommand = new FireAllRulesCommand();
fireAllRulesCommand.setAgendaFilter(new RuleNameEndsWithAgendaFilter("MyRuleSuffix", true));
commands.add(insertObjectCommand);
commands.add(fireAllRulesCommand);
RuleServicesClient ruleClient = client.getServicesClient(RuleServicesClient.class);
ServiceResponse<String> response = ruleClient.executeCommands(containerId, executionCommand);
System.out.println(response.getResult());
You cannot use the standard class RuleNameEndsWithAgendaFilter for filtering the rules of a decision table because rules from one table don't have equal endings.
Try RuleNameStartsWithAgendaFilter, possibly after renaming your tables.
Related
For performance optimisation we are trying to read data from Mongo secondary server for selected scenarios. I am using the inline query using "withReadPreference(ReadPreference.secondaryPreferred())" to read the data, PFB the code snippet.
What I want to confirm the data we are getting is coming from secondary server after executing the inline query highlighted, is there any method available to check the same from Java or Springboot
public User read(final String userId) {
final ObjectId objectId = new ObjectId(userId);
final User user = collection.withReadPreference(ReadPreference.secondaryPreferred()).findOne(objectId).as(User.class);
return user;
}
Pretty much the same way in Java. Note we use secondary() not secondaryPrefered(); this guarantees reads from secondary ONLY:
import com.mongodb.ReadPreference;
{
// This is your "regular" primaryPrefered collection:
MongoCollection<BsonDocument> tcoll = db.getCollection("myCollection", BsonDocument.class);
// ... various operations on tcoll, then create a new
// handle that FORCES reads from secondary and will timeout and
// fail if no secondary can be found:
MongoCollection<BsonDocument> xcoll = tcoll.withReadPreference(ReadPreference.secondary());
BsonDocument f7 = xcoll.find(queryExpr).first();
}
I have simple Business process with rule executed before and after RestService WorkItem
BPM Process
I also defined the Rest Work Handler definition in the settings.
Rest Work Handler Definition Install Rest Work Item Handler.
Using Java KIE API calling RuleServicesClient to execute Rules and BPM Process.
KieServices kieServices = KieServices.Factory.get();
CredentialsProvider credentialsProvider = new EnteredCredentialsProvider(USERNAME, PASSWORD);
KieServicesConfiguration kieServicesConfig = KieServicesFactory.newRestConfiguration(KIE_SERVER_URL, credentialsProvider);
// Set the Marshaling Format to JSON. Other options are JAXB and XSTREAM
kieServicesConfig.setMarshallingFormat(MarshallingFormat.JSON);
KieServicesClient kieServicesClient = KieServicesFactory.newKieServicesClient(kieServicesConfig);
// Retrieve the RuleServices Client.
RuleServicesClient rulesClient = kieServicesClient.getServicesClient(RuleServicesClient.class);
List<Command<?>> commands = new ArrayList<>();
KieCommands commandFactory = kieServices.getCommands();
commands.add(commandFactory.newInsert(new RestFlowRequest("Sample"), "SampleRequest"));
commands.add(commandFactory.newStartProcess("RuleFlowSample.DecisionRestBPM"));
//commands.add(commandFactory.newFireAllRules("numberOfFiredRules"));
//ProcessServicesClient processService
// = kieServicesClient.getServicesClient(ProcessServicesClient.class);
//processService.startProcess(CONTAINER_ID,"RuleFlowSample.DecisionRestBPM");
BatchExecutionCommand batchExecutionCommand = commandFactory.newBatchExecution(commands);
ServiceResponse<ExecutionResults> response = rulesClient.executeCommandsWithResults(CONTAINER_ID, batchExecutionCommand);
It fails to execute the Rest Service Task with following error
Error Thrown By KIE Server
If change the code to start process using ProcessServicesClient then Business Process executes without any issue but rules don't execute.
You are using the correct approach using commands.add(commandFactory.newStartProcess("RuleFlowSample.DecisionRestBPM"));"
I tried it using below code(https://github.com/jbossdemocentral/kie-server-client-examples/blob/master/src/main/java/com/redhat/demo/qlb/loan_application/Main.java) and it works fine :
KieServices kieServices = KieServices.Factory.get();
CredentialsProvider credentialsProvider = new EnteredCredentialsProvider(USERNAME, PASSWORD);
KieServicesConfiguration kieServicesConfig = KieServicesFactory.newRestConfiguration(KIE_SERVER_URL, credentialsProvider);
// Set the Marshaling Format to JSON. Other options are JAXB and XSTREAM
kieServicesConfig.setMarshallingFormat(MarshallingFormat.JSON);
KieServicesClient kieServicesClient = KieServicesFactory.newKieServicesClient(kieServicesConfig);
// Retrieve the RuleServices Client.
RuleServicesClient rulesClient = kieServicesClient.getServicesClient(RuleServicesClient.class);
/*
* Create the list of commands that we want to fire against the rule engine. In this case we insert 2 objects, applicant and loan,
* and we trigger a ruleflow (with the StartProcess command).
*/
List<Command<?>> commands = new ArrayList<>();
KieCommands commandFactory = kieServices.getCommands();
//The identifiers that we provide in the insert commands can later be used to retrieve the object from the response.
commands.add(commandFactory.newInsert(getApplicant(), "applicant"));
commands.add(commandFactory.newInsert(getLoan(), "loan"));
commands.add(commandFactory.newStartProcess("loan-application.loan-application-decision-flow"));
For testing purpose please remove rest handler and try with script task and see the result.
I have a session in Drools 6.3.x containing e few million facts.
I'm building an interactive application that, among other things, allows the user to filter the facts as he wants. In my mind, this filter is basically a pattern. Once the facts are filtered, the logic is applied to produce the desired result.
Due to the interactive nature of the application, I'd rather not to wait for the user input to build a KieBase, derive a KieSession, load the few million facts and fire the rules all the times.
Ideally, I'd like to create a KieBase containing the logic of the application once, derive a KieSession once, load all the facts once, and the inject/remove rules on the fly depending on the user input. How can I do that in Drools 6.3? I know for sure it was possible with Drools 5.
Some code to contextualise my question. This snippet shows how to set up the Drools session:
KieHelper helper = new KieHelper();
String location = "/drools/logic.drl";
InputStream stream = getClass().getResourceAsStream(location);
Resource resource = ResourceFactory.newInputStreamResource(stream);
helper.addResource(resource, ResourceType.DRL);
Results results = helper.verify();
if (results.hasMessages(Message.Level.ERROR)) {
System.out.println(results.getMessages());
System.exit(0);
}
KieBase base = helper.build();
KieSession session = base.newKieSession();
for (Source source : sources) {
for (Object fact : source.getFacts()) {
session.insert(fact);
}
}
session.fireAllRules();
As far as I can see, the KieBase doesn't provide any way to add new rules (only ways to remove them). So I don't know how to proceed from the following snippet, if not initialising the KieSession from scratch...
String rule = "package boot\n" +
"\n" +
"rule \"Stamp\"\n" +
"when\n" +
"\t$o: Object()\n" +
"then\n" +
"\tSystem.out.println($o);\n" +
"end\n";
InputStream ruleStream = new ByteArrayInputStream(rule.getBytes());
Resource ruleResource = ResourceFactory.newInputStreamResource(ruleStream);
helper.addResource(ruleResource, ResourceType.DRL);
Any suggestion?
I'm finding this incredibly frustrating. I'm trying to use the InventoryFacadeClient to call either the Change or Sync web services to update product availability. The issue I'm facing is that I can't seem to instantiate all of the required DataTypes to populate the request.
It's quite confusing, I wanted to call ChangeInventory but can't compose the request, and started down SyncProductAvailability but again, can't compose the request.
The problem below is that the ProductIdentifierType is null, and there's no corresponding "createProductIdentifierType" on the Factory....I'm not sure what I"m missing here, the factory seems to be half baked...
If someone can help me complete this code, it would be great?
public void setUp() throws Exception {
String METHOD_NAME = "setUp";
logger.info("{} entering", METHOD_NAME);
super.setUp();
InventoryFacadeClient iClient = super.initializeInventoryClient(false);
InventoryFactory f = com.ibm.commerce.inventory.datatypes.InventoryFactory.eINSTANCE;
com.ibm.commerce.inventory.facade.datatypes.InventoryFactory cf = iClient.getInventoryFactory();
CommerceFoundationFactory fd = iClient.getCommerceFoundationFactory();
// we must have customised the SyncProductAvailability web service to
// handle ATP inventory model.
SyncProductAvailabilityDataAreaType dataArea = f.createSyncProductAvailabilityDataAreaType();
SyncProductAvailabilityType sat = f.createSyncProductAvailabilityType();
sat.setDataArea(dataArea);
DocumentRoot root = cf.createDocumentRoot();
sat.setVersionID(root.getInventoryAvailabilityBODVersion());
ProductAvailabilityType pat = f.createProductAvailabilityType();
ProductIdentifierType pid = pat.getProductIdentifier();
I found the answer to this on another forum. I was missing the right CommerceFoundationFactory - the class the ProductIdentifierType is created from is:
com.ibm.commerce.foundation.datatypes.CommerceFoundationFactory fd2 = com.ibm.commerce.foundation.datatypes.CommerceFoundationFactory.eINSTANCE;
fd2.createProductIdentifierType
We have two different query strategies that we'd ideally like to operate in conjunction on our site without opening redundant connections. One strategy uses the enterprise library to pull Database objects and Execute_____(DbCommand)s on the Database, without directly selecting any sort of connection. Effectively like this:
Database db = DatabaseFactory.CreateDatabase();
DbCommand q = db.GetStoredProcCommand("SomeProc");
using (IDataReader r = db.ExecuteReader(q))
{
List<RecordType> rv = new List<RecordType>();
while (r.Read())
{
rv.Add(RecordType.CreateFromReader(r));
}
return rv;
}
The other, newer strategy, uses a library that asks for an IDbConnection, which it Close()es immediately after execution. So, we do something like this:
DbConnection c = DatabaseFactory.CreateDatabase().CreateConnection();
using (QueryBuilder qb = new QueryBuilder(c))
{
return qb.Find<RecordType>(ConditionCollection);
}
But, the connection returned by CreateConnection() isn't the same one used by the Database.ExecuteReader(), which is apparently left open between queries. So, when we call a data access method using the new strategy after one using the old strategy inside a TransactionScope, it causes unnecessary promotion -- promotion that I'm not sure we have the ability to configure for (we don't have administrative access to the SQL Server).
Before we go down the path of modifying the query-builder-library to work with the Enterprise Library's Database objects ... Is there a way to retrieve, if existent, the open connection last used by one of the Database.Execute_______() methods?
Yes, you can get the connection associated with a transaction. Enterprise Library internally manages a collection of transactions and the associated database connections so if you are in a transaction you can retrieve the connection associated with a database using the static TransactionScopeConnections.GetConnection method:
using (var scope = new TransactionScope())
{
IEnumerable<RecordType> records = GetRecordTypes();
Database db = DatabaseFactory.CreateDatabase();
DbConnection connection = TransactionScopeConnections.GetConnection(db).Connection;
}
public static IEnumerable<RecordType> GetRecordTypes()
{
Database db = DatabaseFactory.CreateDatabase();
DbCommand q = db.GetStoredProcCommand("GetLogEntries");
using (IDataReader r = db.ExecuteReader(q))
{
List<RecordType> rv = new List<RecordType>();
while (r.Read())
{
rv.Add(RecordType.CreateFromReader(r));
}
return rv;
}
}