<commit_before>package org.drools.jsr94.rules;

/*
 * Copyright 2005 JBoss Inc
 * 
 * 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.
 */

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.rules.ObjectFilter;
import javax.rules.StatelessRuleSession;

import junit.framework.TestCase;

/**
 * Test the <code>StatelessRuleSession</code> implementation.
 *
 * @author <NAME> (n_alex <at>codehaus.org)
 * @author <a href="mailto:<EMAIL>"><NAME> </a>
 * @author <a href="mailto:<EMAIL>"><NAME> </a>
 * @see StatelessRuleSession
 */
public class StatelessRuleSessionTest extends TestCase {

    private ExampleRuleEngineFacade sessionBuilder;

    private final String            bindUri         = "sisters.drl";
    private final String            bindUri_drl     = "sisters_expander.drl";
    private final String            bindUri_dsl     = "sisters_expander.dsl";
    private final String            bindUri_globals = "sisters_globals.drl";

    /**
     * Setup the test case.
     * normal drl, drl with dsl, drl with global
     */
    protected void setUp() throws Exception {
        super.setUp();
        this.sessionBuilder = new ExampleRuleEngineFacade();
        this.sessionBuilder.addRuleExecutionSet( this.bindUri,
                                                 StatelessRuleSessionTest.class.getResourceAsStream( this.bindUri ) );

        final Map map = new HashMap();
        final Reader reader = new InputStreamReader( StatelessRuleSessionTest.class.getResourceAsStream( this.bindUri_dsl ) );

        map.put( "dsl",
                 this.getDSLText( reader ).toString() );
        this.sessionBuilder.addRuleExecutionSet( this.bindUri_drl,
                                                 StatelessRuleSessionTest.class.getResourceAsStream( this.bindUri_drl ),
                                                 map );

        this.sessionBuilder.addRuleExecutionSet( this.bindUri_globals,
                                                 StatelessRuleSessionTest.class.getResourceAsStream( this.bindUri_globals ) );

    }

    /*
     * Taken from DRLParser
     */
    private StringBuffer getDSLText(final Reader reader) throws IOException {
        final StringBuffer text = new StringBuffer();

        final char[] buf = new char[1024];
        int len = 0;

        while ( (len = reader.read( buf )) >= 0 ) {
            text.append( buf,
                         0,
                         len );
        }
        return text;
    }

    /**
     * Test executeRules with globals.
     */
    public void testExecuteRulesGlobals() throws Exception {
        final java.util.Map map = new HashMap();
        java.util.Vector v = new java.util.Vector();
        map.put( "vector",
                 v );
        final StatelessRuleSession statelessSession = this.sessionBuilder.getStatelessRuleSession( this.bindUri_globals,
                                                                                                   map );

        final List inObjects = new ArrayList();

        final Person bob = new Person( "bob" );
        inObjects.add( bob );

        final Person jeannie = new Person( "jeannie" );
        jeannie.addSister( "rebecca" );
        inObjects.add( jeannie );

        final Person rebecca = new Person( "rebecca" );
        rebecca.addSister( "jeannie" );
        inObjects.add( rebecca );

        // execute the rules
        final List outList = statelessSession.executeRules( inObjects );

        assertEquals( "incorrect size",
                      5,
                      outList.size() );

        assertContains( outList,
                        bob );

        assertContains( outList,
                        rebecca );

        assertContains( outList,
                        jeannie );

        assertContains( outList,
                        "rebecca and jeannie are sisters" );

        assertContains( outList,
                        "jeannie and rebecca are sisters" );

        v = (java.util.Vector) map.get( "vector" );

        assertNotNull( "Global Vector null",
                       v );

        assertContains( v,
                        "rebecca and jeannie are sisters" );

        assertContains( v,
                        "jeannie and rebecca are sisters" );

        assertEquals( "Vector v incorrect size",
                      2,
                      v.size() );

        statelessSession.release();
    }

    /**
     * Test executeRules with normal drl.
     */
    public void testExecuteRules() throws Exception {
        final StatelessRuleSession statelessSession = this.sessionBuilder.getStatelessRuleSession( this.bindUri );

        final List inObjects = new ArrayList();

        final Person bob = new Person( "bob" );
        inObjects.add( bob );

        final Person jeannie = new Person( "jeannie" );
        jeannie.addSister( "rebecca" );
        inObjects.add( jeannie );

        final Person rebecca = new Person( "rebecca" );
        rebecca.addSister( "jeannie" );
        inObjects.add( rebecca );

        // execute the rules
        final List outList = statelessSession.executeRules( inObjects );

        assertEquals( "incorrect size",
                      5,
                      outList.size() );

        assertContains( outList,
                        bob );

        assertContains( outList,
                        rebecca );

        assertContains( outList,
                        jeannie );

        assertContains( outList,
                        "rebecca and jeannie are sisters" );

        assertContains( outList,
                        "jeannie and rebecca are sisters" );

        statelessSession.release();
    }

    /**
     * Test executeRules drl with dsl.
     */
    public void testExecuteRules_dsl() throws Exception {
        final StatelessRuleSession statelessSession = this.sessionBuilder.getStatelessRuleSession( this.bindUri_drl );

        final List inObjects = new ArrayList();

        final Person bob = new Person( "bob" );<commit_msg>tests for support for reading xml from Reader/Stream added<commit_after>package org.drools.jsr94.rules;

/*
 * Copyright 2005 JBoss Inc
 *
 * 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.
 */

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.rules.ConfigurationException;
import javax.rules.ObjectFilter;
import javax.rules.RuleServiceProvider;
import javax.rules.RuleServiceProviderManager;
import javax.rules.StatelessRuleSession;
import javax.rules.admin.LocalRuleExecutionSetProvider;
import javax.rules.admin.RuleExecutionSet;
import javax.rules.admin.RuleExecutionSetCreateException;
import javax.rules.admin.RuleExecutionSetRegisterException;

import junit.framework.TestCase;

/**
 * Test the <code>StatelessRuleSession</code> implementation.
 *
 * @author <NAME> (n_alex <at>codehaus.org)
 * @author <a href="mailto:<EMAIL>"><NAME> </a>
 * @author <a href="mailto:<EMAIL>"><NAME> </a>
 * @see StatelessRuleSession
 */
public class StatelessRuleSessionTest extends TestCase {

    private ExampleRuleEngineFacade sessionBuilder;

    private final String            bindUri         = "sisters.drl";
    private final String            bindUri_drl     = "sisters_expander.drl";
    private final String            bindUri_dsl     = "sisters_expander.dsl";
    private final String            bindUri_xml         = "sisters.xml";
    private final String            bindUri_globals = "sisters_globals.drl";

    /**
     * Setup the test case.
     * normal drl, drl with dsl, drl with global
     */
    protected void setUp() throws Exception {
        super.setUp();
        this.sessionBuilder = new ExampleRuleEngineFacade();
        this.sessionBuilder.addRuleExecutionSet( this.bindUri,
                                                 StatelessRuleSessionTest.class.getResourceAsStream( this.bindUri ) );

        final Map map = new HashMap();
        final Reader reader = new InputStreamReader( StatelessRuleSessionTest.class.getResourceAsStream( this.bindUri_dsl ) );

        map.put( "dsl",
                 this.getDSLText( reader ).toString() );
        this.sessionBuilder.addRuleExecutionSet( this.bindUri_drl,
                                                 StatelessRuleSessionTest.class.getResourceAsStream( this.bindUri_drl ),
                                                 map );
        final Map map_xml = new HashMap();
        map_xml.put( "xml", "xml" );
        this.sessionBuilder.addRuleExecutionSet( this.bindUri_xml,
                StatelessRuleSessionTest.class.getResourceAsStream( this.bindUri_xml ),
                map_xml );


        this.sessionBuilder.addRuleExecutionSet( this.bindUri_globals,
                                                 StatelessRuleSessionTest.class.getResourceAsStream( this.bindUri_globals ) );

    }
    

    /*
     * Taken from DRLParser
     */
    private StringBuffer getDSLText(final Reader reader) throws IOException {
        final StringBuffer text = new StringBuffer();

        final char[] buf = new char[1024];
        int len = 0;

        while ( (len = reader.read( buf )) >= 0 ) {
            text.append( buf,
                         0,
                         len );
        }
        return text;
    }

    public void testCreateRuleExecutionSetFromStreamWithXml(){
    	
		try {
			final Map map_xml = new HashMap();
	        map_xml.put( "xml", "xml" );
	        
	        RuleServiceProvider ruleServiceProvider;
	        RuleServiceProviderManager.registerRuleServiceProvider( "http://drools.org/",
	                RuleServiceProviderImpl.class );

	        ruleServiceProvider = RuleServiceProviderManager.getRuleServiceProvider( "http://drools.org/" );

	        LocalRuleExecutionSetProvider ruleSetProvider = ruleServiceProvider.getRuleAdministrator().getLocalRuleExecutionSetProvider( null );
			final RuleExecutionSet ruleExecutionSet = ruleSetProvider.createRuleExecutionSet( StatelessRuleSessionTest.class.getResourceAsStream( this.bindUri_xml ),
	        		map_xml );
			assertNotNull(ruleExecutionSet);
		} catch (RemoteException e) {
			fail();
		} catch (ConfigurationException e) {
			fail();
		} catch (RuleExecutionSetCreateException e) {
			fail();
		} catch (IOException e) {
			fail();
		}
    }
public void testCreateRuleExecutionSetFromStreamReaderWithXml(){    	
		try {
			final Map map_xml = new HashMap();
	        map_xml.put( "xml", "xml" );
	        
	        RuleServiceProvider ruleServiceProvider;
	        RuleServiceProviderManager.registerRuleServiceProvider( "http://drools.org/",
	                RuleServiceProviderImpl.class );

	        ruleServiceProvider = RuleServiceProviderManager.getRuleServiceProvider( "http://drools.org/" );

	        LocalRuleExecutionSetProvider ruleSetProvider = ruleServiceProvider.getRuleAdministrator().getLocalRuleExecutionSetProvider( null );
			final Reader ruleReader = new InputStreamReader( StatelessRuleSessionTest.class.getResourceAsStream( this.bindUri_xml ) );
			final RuleExecutionSet ruleExecutionSet = ruleSetProvider.createRuleExecutionSet( ruleReader,
	        		map_xml );
			assertNotNull(ruleExecutionSet);

		} catch (RemoteException e) {
			fail();
		} catch (ConfigurationException e) {
			fail();
		} catch (RuleExecutionSetCreateException e) {
			fail();
		} catch (IOException e) {
			fail();
		}       
    }
    /**
     * Test executeRules with globals.
     */
    public void testExecuteRulesGlobals() throws Exception {
        final java.util.Map map = new HashMap();
        java.util.Vector v = new java.util.Vector();
        map.put( "vector",
                 v );
        final StatelessRuleSession statelessSession = this.sessionBuilder.getStatelessRuleSession( this.bindUri_globals,
                                                                                                   map );

        final List inObjects = new ArrayList();

        final Person bob = new Person( "bob" );
        inObjects.add( bob );

        final Person jeannie = new Person( "jeannie" );
        jeannie.addSister( "rebecca" );
        inObjects.add( jeannie );

        final Person rebecca = new Person( "rebecca" );
        rebecca.addSister( "jeannie" );
        inObjects.add( rebecca );

        // execute the rules
        final List outList = statelessSession.executeRules( inObjects );

        assertEquals( "incorrect size",
                      5,
                      outList.size() );

        assertContains( outList,
                        bob );

        assertContains( outList,
                        rebecca );

        assertContains( outList,
                        jeannie );

        assertContains( outList,
                        "rebecca and jeannie are sisters" );

        assertContains( outList,
                        "jeannie and rebecca are sisters" );

        v = (java.util.Vector) map.get( "vector" );

        assertNotNull( "Global Vector null",
                       v );

        assertContains( v,
                        "rebecca and jeannie are sisters" );

        assertContains( v,
                        "jeannie and rebecca are sisters" );

        assertEquals( "Vector v incorrect size",
                      2,
                      v.size() );

        statelessSession.release();
    }

    /**
     * Test executeRules with normal drl.
     */
    public void testExecuteRules() throws Exception {
        final StatelessRuleSession statelessSession = this.sessionBuilder.getStatelessRuleSession( this.bindUri );

        final List inObjects = new ArrayList();

        final Person bob = new Person( "bob" );
        inObjects.add( bob );

        final Person jeannie = new Person( "jeannie" );
        jeannie.addSister( "rebecca" );
        inObjects.add( jeannie );

        final Person rebecca = new Person( "rebecca" );
        rebecca.addSister( "jeannie" );
        inObjects.add( rebecca );

        // execute the rules
        final List outList = statelessSession.executeRules( inObjects );

        assertEquals( "incorrect size",
                      5,
                      outList.size() );

        assertContains( outList,
                        bob );

        assertContains( outList,
                        rebecca );

        assertContains( outList,
                        jeannie );

        assertContains( outList,
                        "rebecca and jeannie are sisters" );

        assertContains( outList,
                        "jeannie and rebecca are sisters" );

        statelessSession.release();
    }
    /**
     * Test executeRules with normal drl.
     */
    public void testExecuteRulesWithXml() throws Exception {
        final StatelessRuleSession statelessSession = this.sessionBuilder.getStatelessRuleSession( this.bindUri_xml );

        final List inObjects = new ArrayList();

        final Person bob = new Person( "bob" );
        inObjects.add( bob );

        final Person jeannie = new Person( "jeannie" );
        jeannie.addSister( "rebecca" );
        inObjects.add( jeannie );

        final Person rebecca = new Person( "rebecca" );
        rebecca.addSister( "jeannie" );
        inObjects.add( rebecca );

        // execute the rules
        final List outList = statelessSession.executeRules( inObjects );

        assertEquals( "incorrect size",
                      5,
                      outList.size() );

        assertContains( outList,
                        bob );

        assertContains( outList,
                        rebecca );

        assertContains( outList,
                        jeannie );

        assertContains( outList,
                        "rebecca and jeannie are sisters" );

        assertContains( outList,
                        "jeannie and rebecca are sisters" );

        statelessSession.release();
    }

    /**
     * Test executeRules drl with dsl.
     */
    public void testExecuteRules_dsl() throws Exception {
        final StatelessRuleSession statelessSession = this.sessionBuilder.getStatelessRuleSession( this.bindUri_drl );

        final List inObjects = new ArrayList();

        final Person bob = new Person( "bob" );
{'fraction_non_alphanumeric': 0.0877995767316822, 'fraction_numerical': 0.0023451352742664302, 'mean_word_length': 2.0249134948096885, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 23}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3112854', 'n_tokens_mistral': 4633, 'n_tokens_neox': 4320, 'n_words': 1175}