Here you can see how correlationkey can be created then you would need to cast it to CorrelationKeyInfo and set the correct process instance id for it.
Personally I would go for another approach is to remodel the process sot use regular start event so it can be started directly with correlation key on it and then next activity would be intermediate catch signal event so wait for the signal to arrive.
Here is script that create CorreletionKey from "Script task" (thanks for code examples):
CorrelationKeyInfo correlationKeyInfo = new CorrelationKeyInfo(); correlationKeyInfo.setProcessInstanceId(kcontext.getProcessInstance().getId()); correlationKeyInfo.setName("processKey"); correlationKeyInfo.addProperty(new CorrelationPropertyInfo("property1", startEvent.getProperty1())); correlationKeyInfo.addProperty(new CorrelationPropertyInfo("property2", startEvent.getProperty2())); ProcessPersistenceContext context2 = ((ProcessPersistenceContextManager) kcontext.getKieRuntime().getEnvironment() .get( EnvironmentName.PERSISTENCE_CONTEXT_MANAGER )).getProcessPersistenceContext(); context2.persist(correlationKeyInfo);
This script works well.
Why I did not use: KieInternalServices.Factory.get().newCorrelationKeyFactory() to create instances of CorreleationKey's and CorrelationProperty?
In that way CorrelationProperties have the property "name" with NULL value - so I have unnamed variable.
By the way, when we search ProcessInstance by CorrelationKey the "name: atribute of CorrelationProperty is ignored.
<named-query name="GetProcessInstanceIdByCorrelation"> <query> select key.processInstanceId from CorrelationKeyInfo key left join key.properties props where cast(:elem_count as integer) = (select count(id) from CorrelationPropertyInfo cpi where cpi.correlationKey.id = key.id) and props.value in :properties group by key.id,key.processInstanceId having count(key.id) = :elem_count </query>
Is this correct? Why the "name" property form CorrelationProperty is ignored?
name of correlation key is only for completeness with bpmn2 spec and at the moment is not really used. What actually is the correlation key is its properties as you can have then multiple valued correlation and that's why it strictly relies on the properties rather the name.
Name of CorrelationKey is unused - OK.
Why name of CorrelationProperty is unused?
There is a multiple CorrelationProperty inside CorrelationKey.
If we put two values into CorrelationPropery - we can not recognize them.
it's similar as for correlation key, name (or attribute) is for the completeness sake when it comes to model but when you look at the CorrelationKeyFactory there is no such elements exposed meaning they are not in use. Current implementation recognize the number of properties and their values and don't care about they names. So if you have multiple keys with same values it is invalid correlation key then.