use of org.drools.core.ClassObjectFilter in project drools by kiegroup.
the class TraitMapCoreTest method donCustomMapTest.
@Test()
public void donCustomMapTest() {
String source = "package org.drools.traits.test; \n" + "import java.util.*\n;" + "import " + TraitMapCoreTest.DomainMap.class.getCanonicalName() + ";\n" + "" + "global List list; \n" + "" + "" + "declare trait PersonMap" + "@propertyReactive \n" + " name : String \n" + " age : int \n" + " height : Double \n" + "end\n" + "" + "" + "rule Don \n" + "when \n" + " $m : Map( this[ \"age\"] == 18 ) " + "then \n" + " don( $m, PersonMap.class );\n" + "end \n" + "" + "rule Log \n" + "when \n" + " $p : PersonMap( name == \"john\", age > 10 ) \n" + "then \n" + " modify ( $p ) { \n" + " setHeight( 184.0 ); \n" + " }" + "end \n";
KieSession ksession = loadKnowledgeBaseFromString(source).newKieSession();
TraitFactoryImpl.setMode(VirtualPropertyMode.MAP, ksession.getKieBase());
List list = new ArrayList();
ksession.setGlobal("list", list);
HashMap map = new DomainMap();
map.put("name", "john");
map.put("age", 18);
ksession.insert(map);
ksession.fireAllRules();
assertTrue(map.containsKey("height"));
assertEquals(map.get("height"), 184.0);
assertEquals(2, ksession.getObjects().size());
assertEquals(1, ksession.getObjects(new ClassObjectFilter(DomainMap.class)).size());
}
use of org.drools.core.ClassObjectFilter in project jbpm by kiegroup.
the class CaseServiceImpl method internalGetCaseFileInstance.
/*
* internal methods
*/
@SuppressWarnings("unchecked")
protected CaseFileInstance internalGetCaseFileInstance(String caseId, String deploymentId) {
logger.debug("Retrieving case file from working memory for case " + caseId);
Collection<CaseFileInstance> caseFiles = (Collection<CaseFileInstance>) processService.execute(deploymentId, CaseContext.get(caseId), commandsFactory.newGetObjects(new ClassObjectFilter(CaseFileInstance.class)));
if (caseFiles.size() == 0) {
throw new CaseNotFoundException("Case with id " + caseId + " was not found");
} else if (caseFiles.size() == 1) {
CaseFileInstance caseFile = caseFiles.iterator().next();
logger.debug("Single case file {} found in working memory", caseFile);
// apply authorization
Map<String, Object> filteredData = authorizationManager.filterByDataAuthorization(caseId, caseFile, caseFile.getData());
((CaseFileInstanceImpl) caseFile).setData(filteredData);
for (Object variable : caseFile.getData().values()) {
if (variable instanceof LazyLoaded<?>) {
((LazyLoaded<?>) variable).load();
}
}
return caseFile;
}
logger.warn("Multiple case files found in working memory (most likely not using PER_CASE strategy), trying to filter out...");
CaseFileInstance caseFile = caseFiles.stream().filter(cf -> cf.getCaseId().equals(caseId)).findFirst().orElse(null);
logger.warn("Case file {} after filtering {}", caseFile, (caseFile == null ? "not found" : "found"));
if (caseFile != null) {
// apply authorization
Map<String, Object> filteredData = authorizationManager.filterByDataAuthorization(caseId, caseFile, caseFile.getData());
((CaseFileInstanceImpl) caseFile).setData(filteredData);
for (Object variable : caseFile.getData().values()) {
if (variable instanceof LazyLoaded<?>) {
((LazyLoaded<?>) variable).load();
}
}
}
return caseFile;
}
use of org.drools.core.ClassObjectFilter in project jbpm by kiegroup.
the class ModifyRoleAssignmentCommand method execute.
@Override
public Void execute(Context context) {
KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
Collection<? extends Object> caseFiles = ksession.getObjects(new ClassObjectFilter(CaseFileInstance.class));
if (caseFiles.size() != 1) {
throw new IllegalStateException("Not able to find distinct case file - found case files " + caseFiles.size());
}
CaseFileInstance caseFile = (CaseFileInstance) caseFiles.iterator().next();
FactHandle factHandle = ksession.getFactHandle(caseFile);
CaseEventSupport caseEventSupport = getCaseEventSupport(context);
if (add) {
caseEventSupport.fireBeforeCaseRoleAssignmentAdded(caseFile.getCaseId(), caseFile, roleName, entity);
((CaseAssignment) caseFile).assign(roleName, entity);
caseEventSupport.fireAfterCaseRoleAssignmentAdded(caseFile.getCaseId(), caseFile, roleName, entity);
} else {
caseEventSupport.fireBeforeCaseRoleAssignmentRemoved(caseFile.getCaseId(), caseFile, roleName, entity);
((CaseAssignment) caseFile).remove(roleName, entity);
caseEventSupport.fireAfterCaseRoleAssignmentRemoved(caseFile.getCaseId(), caseFile, roleName, entity);
}
ksession.update(factHandle, caseFile);
triggerRules(ksession);
return null;
}
use of org.drools.core.ClassObjectFilter in project jbpm by kiegroup.
the class RemoveDataCaseFileInstanceCommand method execute.
@Override
public Void execute(Context context) {
KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
Collection<? extends Object> caseFiles = ksession.getObjects(new ClassObjectFilter(CaseFileInstance.class));
if (caseFiles.size() != 1) {
throw new IllegalStateException("Not able to find distinct case file - found case files " + caseFiles.size());
}
CaseFileInstance caseFile = (CaseFileInstance) caseFiles.iterator().next();
// apply authorization
authorizationManager.checkDataAuthorization(caseFile.getCaseId(), caseFile, variableNames);
FactHandle factHandle = ksession.getFactHandle(caseFile);
Map<String, Object> remove = new HashMap<>();
variableNames.forEach(p -> remove.put(p, caseFile.getData(p)));
CaseEventSupport caseEventSupport = getCaseEventSupport(context);
caseEventSupport.fireBeforeCaseDataRemoved(caseFile.getCaseId(), caseFile, caseFile.getDefinitionId(), remove);
variableNames.forEach(p -> caseFile.remove(p));
ksession.update(factHandle, caseFile);
triggerRules(ksession);
caseEventSupport.fireAfterCaseDataRemoved(caseFile.getCaseId(), caseFile, caseFile.getDefinitionId(), remove);
return null;
}
use of org.drools.core.ClassObjectFilter in project jbpm by kiegroup.
the class VariableScopeInstance method internalSetVariable.
public void internalSetVariable(String name, Object value) {
if (name.startsWith(VariableScope.CASE_FILE_PREFIX)) {
String nameInCaseFile = name.replaceFirst(VariableScope.CASE_FILE_PREFIX, "");
// store it under case file rather regular variables
@SuppressWarnings("unchecked") Collection<CaseData> caseFiles = (Collection<CaseData>) getProcessInstance().getKnowledgeRuntime().getObjects(new ClassObjectFilter(CaseData.class));
if (caseFiles.size() == 1) {
CaseData caseFile = (CaseData) caseFiles.iterator().next();
FactHandle factHandle = getProcessInstance().getKnowledgeRuntime().getFactHandle(caseFile);
caseFile.add(nameInCaseFile, value);
getProcessInstance().getKnowledgeRuntime().update(factHandle, caseFile);
return;
}
}
// not a case, store it in normal variables
variables.put(name, value);
}
Aggregations