Search in sources :

Example 1 with WorkingMemory

use of org.drools.WorkingMemory in project jBPM5-Developer-Guide by Salaboy.

the class ProcessAndEventMultiThreadIntegrationTest method createEventsSession.

private StatefulKnowledgeSession createEventsSession() {
    KnowledgeBuilder eventsKbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    eventsKbuilder.add(new ClassPathResource("analyze-process-events.drl"), ResourceType.DRL);
    if (eventsKbuilder.hasErrors()) {
        for (KnowledgeBuilderError error : eventsKbuilder.getErrors()) {
            System.out.println(">>> Error:" + error.getMessage());
        }
        fail(">>> Knowledge couldn't be parsed! ");
    }
    KnowledgeBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    config.setOption(EventProcessingOption.STREAM);
    KnowledgeBase eventsKbase = KnowledgeBaseFactory.newKnowledgeBase(config);
    eventsKbase.addKnowledgePackages(eventsKbuilder.getKnowledgePackages());
    StatefulKnowledgeSession eventsKsession = eventsKbase.newStatefulKnowledgeSession();
    ((StatefulKnowledgeSessionImpl) eventsKsession).getInternalWorkingMemory().addEventListener(new DefaultWorkingMemoryEventListener() {

        @Override
        public void objectInserted(ObjectInsertedEvent event) {
            event.getWorkingMemory().fireAllRules();
        }

        @Override
        public void objectUpdated(ObjectUpdatedEvent event) {
            event.getWorkingMemory().fireAllRules();
        }

        @Override
        public void objectRetracted(ObjectRetractedEvent event) {
            event.getWorkingMemory().fireAllRules();
        }
    });
    ((StatefulKnowledgeSessionImpl) eventsKsession).getInternalWorkingMemory().addEventListener(new DefaultAgendaEventListener() {

        @Override
        public void activationCreated(ActivationCreatedEvent event, WorkingMemory workingMemory) {
            workingMemory.fireAllRules();
        }
    });
    return eventsKsession;
}
Also used : KnowledgeBuilderError(org.drools.builder.KnowledgeBuilderError) WorkingMemory(org.drools.WorkingMemory) StatefulKnowledgeSession(org.drools.runtime.StatefulKnowledgeSession) ClassPathResource(org.drools.io.impl.ClassPathResource) KnowledgeBuilder(org.drools.builder.KnowledgeBuilder) KnowledgeBase(org.drools.KnowledgeBase) KnowledgeBaseConfiguration(org.drools.KnowledgeBaseConfiguration)

Example 2 with WorkingMemory

use of org.drools.WorkingMemory in project jBPM5-Developer-Guide by Salaboy.

the class ProcessAndEventIntegrationTest method createEventsSession.

private StatefulKnowledgeSession createEventsSession() {
    KnowledgeBuilder eventsKbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    eventsKbuilder.add(new ClassPathResource("analyze-process-events.drl"), ResourceType.DRL);
    if (eventsKbuilder.hasErrors()) {
        for (KnowledgeBuilderError error : eventsKbuilder.getErrors()) {
            System.out.println(">>> Error:" + error.getMessage());
        }
        fail(">>> Knowledge couldn't be parsed! ");
    }
    KnowledgeBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    config.setOption(EventProcessingOption.STREAM);
    KnowledgeBase eventsKbase = KnowledgeBaseFactory.newKnowledgeBase(config);
    eventsKbase.addKnowledgePackages(eventsKbuilder.getKnowledgePackages());
    StatefulKnowledgeSession eventsKsession = eventsKbase.newStatefulKnowledgeSession();
    ((StatefulKnowledgeSessionImpl) eventsKsession).getInternalWorkingMemory().addEventListener(new DefaultWorkingMemoryEventListener() {

        @Override
        public void objectInserted(ObjectInsertedEvent event) {
            event.getWorkingMemory().fireAllRules();
        }

        @Override
        public void objectUpdated(ObjectUpdatedEvent event) {
            event.getWorkingMemory().fireAllRules();
        }

        @Override
        public void objectRetracted(ObjectRetractedEvent event) {
            event.getWorkingMemory().fireAllRules();
        }
    });
    ((StatefulKnowledgeSessionImpl) eventsKsession).getInternalWorkingMemory().addEventListener(new DefaultAgendaEventListener() {

        @Override
        public void activationCreated(ActivationCreatedEvent event, WorkingMemory workingMemory) {
            workingMemory.fireAllRules();
        }
    });
    return eventsKsession;
}
Also used : KnowledgeBuilderError(org.drools.builder.KnowledgeBuilderError) WorkingMemory(org.drools.WorkingMemory) StatefulKnowledgeSession(org.drools.runtime.StatefulKnowledgeSession) ClassPathResource(org.drools.io.impl.ClassPathResource) KnowledgeBuilder(org.drools.builder.KnowledgeBuilder) KnowledgeBase(org.drools.KnowledgeBase) KnowledgeBaseConfiguration(org.drools.KnowledgeBaseConfiguration)

Example 3 with WorkingMemory

use of org.drools.WorkingMemory in project oxTrust by GluuFederation.

the class SsoLoginAction method start.

public String start() {
    if (initialized) {
        return OxTrustConstants.RESULT_SUCCESS;
    }
    FacesContext facesContext = FacesContext.getCurrentInstance();
    HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
    relyingPartyId = request.getHeader("relyingPartyId");
    setActionUrl(request.getHeader("actionUrl"));
    log.debug("relyingPartyId is" + relyingPartyId);
    log.debug("actionUrl is" + actionUrl);
    if (StringHelper.isEmpty(relyingPartyId)) {
        facesMessages.add(FacesMessage.SEVERITY_ERROR, "Direct access to this page is not supported");
    // return Configuration.RESULT_FAILURE;
    }
    try {
        log.debug("Getting SSL HTTP Client");
        // Create HTTP local context
        // Bind cookie store to the local context
        // Add user cookies
        log.debug("Setting HTTP Client cookies from user session");
    } catch (Exception ex) {
        log.error("Failed to initialize HTTP Client", ex);
        facesMessages.add(FacesMessage.SEVERITY_ERROR, "Failed to prepare login form");
    // return Configuration.RESULT_FAILURE;
    }
    initialized = true;
    RuleBase ruleBase = null;
    try {
        log.info("Checking for customized login pages");
        InputStream is = getClass().getClassLoader().getResourceAsStream("selection.drl");
        if (is != null) {
            log.info("Login page customization rules found.");
            Reader reader = new InputStreamReader(is);
            try {
                ruleBase = RuleBaseLoader.getInstance().loadFromReader(reader);
                WorkingMemory workingMemory = ruleBase.newStatefulSession();
                workingMemory.insert(relyingPartyId);
                // workingMemory.insert(contextKey);
                // workingMemory.insert(relayState);
                // workingMemory.insert(relayStateValue);
                // workingMemory.insert(requestedSessionId);
                List<String> viewId = new ArrayList<String>();
                workingMemory.insert(viewId);
                workingMemory.fireAllRules();
                if (viewId.size() > 0) {
                    log.info("Login page customization rules fired: " + viewId.get(0));
                    facesService.redirect(viewId.get(0));
                }
            } finally {
                IOUtils.closeQuietly(reader);
            }
        }
    } catch (CheckedDroolsException e) {
        e.printStackTrace();
    } catch (IOException e) {
        log.warn("There were error reading selection.drl");
    }
    return OxTrustConstants.RESULT_SUCCESS;
}
Also used : FacesContext(javax.faces.context.FacesContext) InputStreamReader(java.io.InputStreamReader) CheckedDroolsException(org.drools.CheckedDroolsException) InputStream(java.io.InputStream) WorkingMemory(org.drools.WorkingMemory) ArrayList(java.util.ArrayList) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) IOException(java.io.IOException) CheckedDroolsException(org.drools.CheckedDroolsException) HttpServletRequest(javax.servlet.http.HttpServletRequest) RuleBase(org.drools.RuleBase)

Aggregations

WorkingMemory (org.drools.WorkingMemory)3 KnowledgeBase (org.drools.KnowledgeBase)2 KnowledgeBaseConfiguration (org.drools.KnowledgeBaseConfiguration)2 KnowledgeBuilder (org.drools.builder.KnowledgeBuilder)2 KnowledgeBuilderError (org.drools.builder.KnowledgeBuilderError)2 ClassPathResource (org.drools.io.impl.ClassPathResource)2 StatefulKnowledgeSession (org.drools.runtime.StatefulKnowledgeSession)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 ArrayList (java.util.ArrayList)1 FacesContext (javax.faces.context.FacesContext)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 CheckedDroolsException (org.drools.CheckedDroolsException)1 RuleBase (org.drools.RuleBase)1