Search in sources :

Example 1 with WorkItemImpl

use of org.drools.core.process.instance.impl.WorkItemImpl in project drools by kiegroup.

the class InputMarshaller method readWorkItem.

public static WorkItem readWorkItem(MarshallerReaderContext context) throws IOException {
    ObjectInputStream stream = context.stream;
    WorkItemImpl workItem = new WorkItemImpl();
    workItem.setId(stream.readLong());
    workItem.setProcessInstanceId(stream.readLong());
    workItem.setName(stream.readUTF());
    workItem.setState(stream.readInt());
    // WorkItem Paramaters
    int nbVariables = stream.readInt();
    if (nbVariables > 0) {
        for (int i = 0; i < nbVariables; i++) {
            String name = stream.readUTF();
            try {
                int index = stream.readInt();
                ObjectMarshallingStrategy strategy = null;
                // Old way of retrieving strategy objects
                if (index >= 0) {
                    strategy = context.resolverStrategyFactory.getStrategy(index);
                    if (strategy == null) {
                        throw new IllegalStateException("No strategy of with index " + index + " available.");
                    }
                } else // New way
                if (index == -2) {
                    String strategyClassName = stream.readUTF();
                    // fix for backwards compatibility (5.x -> 6.x)
                    if ("org.drools.marshalling.impl.SerializablePlaceholderResolverStrategy".equals(strategyClassName)) {
                        strategyClassName = "org.drools.core.marshalling.impl.SerializablePlaceholderResolverStrategy";
                    }
                    strategy = context.resolverStrategyFactory.getStrategyObject(strategyClassName);
                    if (strategy == null) {
                        throw new IllegalStateException("No strategy of type " + strategyClassName + " available.");
                    }
                }
                Object value = strategy.read(stream);
                workItem.setParameter(name, value);
            } catch (ClassNotFoundException e) {
                throw new IllegalArgumentException("Could not reload variable " + name);
            }
        }
    }
    return workItem;
}
Also used : ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) WorkItemImpl(org.drools.core.process.instance.impl.WorkItemImpl) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) ObjectInputStream(java.io.ObjectInputStream)

Example 2 with WorkItemImpl

use of org.drools.core.process.instance.impl.WorkItemImpl in project drools by kiegroup.

the class RegisterWorkItemHandlerTest method testRegisterWorkItemHandlerWithStatelessSession.

@Test
public void testRegisterWorkItemHandlerWithStatelessSession() {
    String str = "package org.kie.workitem.test \n" + "import " + DefaultWorkItemManager.class.getCanonicalName() + "\n" + "import " + WorkItem.class.getCanonicalName() + "\n" + "import " + WorkItemImpl.class.getCanonicalName() + "\n" + "rule r1 when \n" + "then \n" + "  WorkItem wi = new WorkItemImpl(); \n" + "  wi.setName( \"wihandler\" ); \n" + "  DefaultWorkItemManager wim = ( DefaultWorkItemManager ) kcontext.getKieRuntime().getWorkItemManager(); \n" + "  wim.internalExecuteWorkItem(wi); \n" + "end \n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    final boolean[] answer = new boolean[] { false };
    StatelessKieSession ks = kbase.newStatelessKieSession();
    ks.execute(CommandFactory.newRegisterWorkItemHandlerCommand(new WorkItemHandler() {

        public void executeWorkItem(org.kie.api.runtime.process.WorkItem workItem, WorkItemManager manager) {
            answer[0] = true;
        }

        public void abortWorkItem(org.kie.api.runtime.process.WorkItem workItem, WorkItemManager manager) {
        // TODO Auto-generated method stub
        }
    }, "wihandler"));
    assertTrue(answer[0]);
}
Also used : WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) DefaultWorkItemManager(org.drools.core.process.instance.impl.DefaultWorkItemManager) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) WorkItemImpl(org.drools.core.process.instance.impl.WorkItemImpl) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) WorkItem(org.drools.core.process.instance.WorkItem) WorkItemManager(org.kie.api.runtime.process.WorkItemManager) DefaultWorkItemManager(org.drools.core.process.instance.impl.DefaultWorkItemManager) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 3 with WorkItemImpl

use of org.drools.core.process.instance.impl.WorkItemImpl in project drools by kiegroup.

the class JPAWorkItemManager method abortWorkItem.

public void abortWorkItem(long id) {
    PersistenceContext context = getPersistenceContext();
    WorkItemInfo workItemInfo = null;
    if (this.workItems != null) {
        workItemInfo = this.workItems.get(id);
        if (workItemInfo != null) {
            workItemInfo = (WorkItemInfo) context.merge(workItemInfo);
        }
    }
    if (workItemInfo == null) {
        workItemInfo = (WorkItemInfo) context.findWorkItem(id);
    }
    // work item may have been aborted
    if (workItemInfo != null) {
        WorkItem workItem = (WorkItemImpl) internalGetWorkItem(workItemInfo);
        ProcessInstance processInstance = kruntime.getProcessInstance(workItem.getProcessInstanceId());
        workItem.setState(WorkItem.ABORTED);
        // process instance may have finished already
        if (processInstance != null) {
            processInstance.signalEvent("workItemAborted", workItem);
        }
        context.remove(workItemInfo);
        if (workItems != null) {
            workItems.remove(workItem.getId());
        }
    }
}
Also used : WorkItemImpl(org.drools.core.process.instance.impl.WorkItemImpl) PersistenceContext(org.drools.persistence.api.PersistenceContext) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkItem(org.drools.core.process.instance.WorkItem) WorkItemInfo(org.drools.persistence.info.WorkItemInfo)

Example 4 with WorkItemImpl

use of org.drools.core.process.instance.impl.WorkItemImpl in project drools by kiegroup.

the class JPAWorkItemManager method internalExecuteWorkItem.

public void internalExecuteWorkItem(WorkItem workItem) {
    Environment env = this.kruntime.getEnvironment();
    WorkItemInfo workItemInfo = new WorkItemInfo(workItem, env);
    PersistenceContext context = getPersistenceContext();
    workItemInfo = (WorkItemInfo) context.persist(workItemInfo);
    ((WorkItemImpl) workItem).setId(workItemInfo.getId());
    if (this.workItems == null) {
        this.workItems = new HashMap<Long, WorkItemInfo>();
    }
    workItems.put(workItem.getId(), workItemInfo);
    WorkItemHandler handler = (WorkItemHandler) this.workItemHandlers.get(workItem.getName());
    if (handler != null) {
        handler.executeWorkItem(workItem, this);
    } else {
        throwWorkItemNotFoundException(workItem);
    }
}
Also used : WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) WorkItemImpl(org.drools.core.process.instance.impl.WorkItemImpl) Environment(org.kie.api.runtime.Environment) PersistenceContext(org.drools.persistence.api.PersistenceContext) WorkItemInfo(org.drools.persistence.info.WorkItemInfo)

Example 5 with WorkItemImpl

use of org.drools.core.process.instance.impl.WorkItemImpl in project drools by kiegroup.

the class JPAWorkItemManager method internalAbortWorkItem.

public void internalAbortWorkItem(long id) {
    PersistenceContext context = getPersistenceContext();
    WorkItemInfo workItemInfo = (WorkItemInfo) context.findWorkItem(id);
    // work item may have been aborted
    if (workItemInfo != null) {
        WorkItemImpl workItem = (WorkItemImpl) internalGetWorkItem(workItemInfo);
        WorkItemHandler handler = (WorkItemHandler) this.workItemHandlers.get(workItem.getName());
        if (handler != null) {
            handler.abortWorkItem(workItem, this);
        } else {
            if (workItems != null) {
                workItems.remove(id);
                throwWorkItemNotFoundException(workItem);
            }
        }
        if (workItems != null) {
            workItems.remove(id);
        }
        context.remove(workItemInfo);
    }
}
Also used : WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) WorkItemImpl(org.drools.core.process.instance.impl.WorkItemImpl) PersistenceContext(org.drools.persistence.api.PersistenceContext) WorkItemInfo(org.drools.persistence.info.WorkItemInfo)

Aggregations

WorkItemImpl (org.drools.core.process.instance.impl.WorkItemImpl)5 PersistenceContext (org.drools.persistence.api.PersistenceContext)3 WorkItemInfo (org.drools.persistence.info.WorkItemInfo)3 WorkItemHandler (org.kie.api.runtime.process.WorkItemHandler)3 WorkItem (org.drools.core.process.instance.WorkItem)2 ObjectInputStream (java.io.ObjectInputStream)1 WorkingMemoryEntryPoint (org.drools.core.WorkingMemoryEntryPoint)1 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)1 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)1 DefaultWorkItemManager (org.drools.core.process.instance.impl.DefaultWorkItemManager)1 Test (org.junit.Test)1 ObjectMarshallingStrategy (org.kie.api.marshalling.ObjectMarshallingStrategy)1 Environment (org.kie.api.runtime.Environment)1 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)1 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)1 WorkItemManager (org.kie.api.runtime.process.WorkItemManager)1 EntryPoint (org.kie.api.runtime.rule.EntryPoint)1 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)1