org.drools.jsr94.rules.StatelessRuleSessionImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of drools-jsr94 Show documentation
Show all versions of drools-jsr94 Show documentation
Implements the JSR-94 API with Drools.
The newest version!
/*
* Copyright 2010 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.drools.jsr94.rules;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.rules.InvalidRuleSessionException;
import javax.rules.ObjectFilter;
import javax.rules.RuleExecutionSetNotFoundException;
import javax.rules.RuleRuntime;
import javax.rules.RuleSessionCreateException;
import javax.rules.StatelessRuleSession;
import org.drools.core.impl.StatelessKnowledgeSessionImpl;
import org.drools.jsr94.rules.admin.RuleExecutionSetImpl;
import org.drools.jsr94.rules.repository.RuleExecutionSetRepository;
import org.drools.jsr94.rules.repository.RuleExecutionSetRepositoryException;
import org.kie.api.runtime.StatelessKieSession;
/**
* The Drools implementation of the StatelessRuleSession
* interface which is a representation of a stateless rules engine session. A
* stateless rules engine session exposes a stateless rule execution API to an
* underlying rules engine.
*
* @see StatelessRuleSession
*/
public class StatelessRuleSessionImpl extends AbstractRuleSessionImpl
implements
StatelessRuleSession {
/**
* Gets the RuleExecutionSet
for this URI and associates it
* with a RuleBase.
*
* @param bindUri
* the URI the RuleExecutionSet
has been bound to
* @param properties
* additional properties used to create the
* RuleSession
implementation.
*
* @throws RuleExecutionSetNotFoundException
* if there is no rule set under the given URI
* @throws RuleSessionCreateException
*/
StatelessRuleSessionImpl(final String bindUri,
final Map properties,
final RuleExecutionSetRepository repository)
throws RuleExecutionSetNotFoundException, RuleSessionCreateException {
super( repository );
setProperties( properties );
RuleExecutionSetImpl ruleSet = null;
try {
ruleSet = (RuleExecutionSetImpl)
repository.getRuleExecutionSet(bindUri, properties);
} catch (RuleExecutionSetRepositoryException e) {
String s = "Error while retrieving rule execution set bound to: " + bindUri;
throw new RuleSessionCreateException(s, e);
}
if ( ruleSet == null ) {
throw new RuleExecutionSetNotFoundException( "RuleExecutionSet unbound: " + bindUri );
}
setRuleExecutionSet( ruleSet );
}
/**
* Initialize this RuleSession
* with a new WorkingMemory
.
*/
protected StatelessKieSession newStatelessSession() {
final StatelessKieSession session = this.getRuleExecutionSet().newStatelessSession();
final Map props = this.getProperties();
if ( props != null ) {
for ( final Iterator iterator = props.entrySet().iterator(); iterator.hasNext(); ) {
final Map.Entry entry = (Map.Entry) iterator.next();
session.setGlobal( (String) entry.getKey(),
entry.getValue() );
}
}
return session;
}
/**
* Executes the rules in the bound rule execution set using the supplied
* list of objects. A List
is returned containing the objects
* created by (or passed into the rule session) the executed rules that pass
* the filter test of the default RuleExecutionSet
* ObjectFilter
* (if present). The returned list may not neccessarily include all
* objects passed, and may include Object
s created by
* side-effects. The execution of a RuleExecutionSet
can add,
* remove and update objects. Therefore the returned object list is
* dependent on the rules that are part of the executed
* RuleExecutionSet
as well as Drools specific rule engine
* behavior.
*
* @param objects
* the objects used to execute rules.
*
* @return a List
containing the objects as a result of
* executing the rules.
*
* @throws InvalidRuleSessionException
* on illegal rule session state.
*/
public List executeRules(final List objects) throws InvalidRuleSessionException {
return executeRules( objects,
this.getRuleExecutionSet().getObjectFilter() );
}
/**
* Executes the rules in the bound rule execution set using the supplied
* list of objects. A List
is returned containing the objects
* created by (or passed into the rule engine) the executed rules and
* filtered with the supplied object filter. The returned list may not
* neccessarily include all objects passed, and may include
* Object
s created by side-effects. The execution of a
* RuleExecutionSet
can add, remove and update objects.
* Therefore the returned object list is dependent on the rules that are
* part of the executed RuleExecutionSet
as well as Drools
* specific rule engine behavior.
*
* @param objects
* the objects used to execute rules.
* @param filter
* the object filter.
*
* @return a List
containing the objects as a result of
* executing rules, after passing through the supplied object
* filter.
*
* @throws InvalidRuleSessionException
* on illegal rule session state.
*/
public List executeRules(final List objects,
final ObjectFilter filter) throws InvalidRuleSessionException {
StatelessKieSession session = newStatelessSession();
return ((StatelessKnowledgeSessionImpl)session).executeWithResults(objects, new ObjectFilterAdapter( filter ));
}
public int getType() throws InvalidRuleSessionException {
return RuleRuntime.STATELESS_SESSION_TYPE;
}
/**
* Ensures this RuleSession
is not
* in an illegal rule session state.
*
* @throws InvalidRuleSessionException on illegal rule session state.
*/
protected void checkRuleSessionValidity() throws InvalidRuleSessionException {
if ( getRuleExecutionSet() == null ) {
throw new InvalidRuleSessionException( "invalid rule session" );
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy