Search in sources :

Example 51 with ClassObjectFilter

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());
}
Also used : ClassObjectFilter(org.drools.core.ClassObjectFilter) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) Test(org.junit.Test)

Example 52 with ClassObjectFilter

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;
}
Also used : CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) ClassObjectFilter(org.drools.core.ClassObjectFilter) CaseFileInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CaseFileInstanceImpl) Collection(java.util.Collection) CaseNotFoundException(org.jbpm.casemgmt.api.CaseNotFoundException) Map(java.util.Map) HashMap(java.util.HashMap) LazyLoaded(org.kie.internal.utils.LazyLoaded)

Example 53 with ClassObjectFilter

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;
}
Also used : CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) CaseEventSupport(org.jbpm.casemgmt.impl.event.CaseEventSupport) ClassObjectFilter(org.drools.core.ClassObjectFilter) FactHandle(org.kie.api.runtime.rule.FactHandle) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext) CaseAssignment(org.kie.api.runtime.process.CaseAssignment)

Example 54 with ClassObjectFilter

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;
}
Also used : CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) CaseEventSupport(org.jbpm.casemgmt.impl.event.CaseEventSupport) ClassObjectFilter(org.drools.core.ClassObjectFilter) FactHandle(org.kie.api.runtime.rule.FactHandle) HashMap(java.util.HashMap) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext)

Example 55 with ClassObjectFilter

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);
}
Also used : CaseData(org.kie.api.runtime.process.CaseData) ClassObjectFilter(org.drools.core.ClassObjectFilter) FactHandle(org.kie.api.runtime.rule.FactHandle) Collection(java.util.Collection)

Aggregations

ClassObjectFilter (org.drools.core.ClassObjectFilter)55 KieSession (org.kie.api.runtime.KieSession)44 Test (org.junit.Test)40 FactHandle (org.kie.api.runtime.rule.FactHandle)29 InternalFactHandle (org.drools.core.common.InternalFactHandle)24 KieBase (org.kie.api.KieBase)19 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)17 ArrayList (java.util.ArrayList)15 List (java.util.List)14 Collection (java.util.Collection)12 CaseFileInstance (org.jbpm.casemgmt.api.model.instance.CaseFileInstance)11 Person (org.drools.compiler.Person)8 KiePackage (org.kie.api.definition.KiePackage)8 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)8 HashMap (java.util.HashMap)7 KieHelper (org.kie.internal.utils.KieHelper)6 Cheese (org.drools.compiler.Cheese)5 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)5 RegistryContext (org.drools.core.command.impl.RegistryContext)4 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)4