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

/*
 * 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.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Serializable;
import java.io.StringReader;
import java.net.URL;
import java.util.Map;

import javax.rules.admin.RuleExecutionSet;
import javax.rules.admin.RuleExecutionSetCreateException;
import javax.rules.admin.RuleExecutionSetProvider;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.drools.compiler.DrlParser;
import org.drools.compiler.DroolsParserException;
import org.drools.compiler.PackageBuilder;
import org.drools.lang.descr.PackageDescr;
import org.drools.rule.Package;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * The Drools implementation of the <code>RuleExecutionSetProvider</code>
 * interface which defines <code>RuleExecutionSet</code> creation methods for
 * defining <code>RuleExecutionSet</code>s from potentially serializable
 * resources.
 *
 * @see RuleExecutionSetProvider
 *
 * @author <NAME> (n_alex <at>codehaus.org)
 * @author <a href="mailto:<EMAIL>"><NAME> </a>
 * @author <a href="mailto:<EMAIL>"><NAME> </a>
 */
public class RuleExecutionSetProviderImpl
    implements
    RuleExecutionSetProvider {
    /**
     * Creates a <code>RuleExecutionSet</code> implementation from an XML
     * Document and additional Drools-specific properties. A Drools-specific
     * rule execution set is read from the supplied XML Document.
     *
     * @param ruleExecutionSetElement the XML element that is the source of the
     *        rule execution set
     * @param properties additional properties used to create the
     *        <code>RuleExecutionSet</code> implementation.
     *        May be <code>null</code>.
     *
     * @throws RuleExecutionSetCreateException on rule execution set creation
     *         error.
     *
     * @return The created <code>RuleExecutionSet</code>.
     */
    public RuleExecutionSet createRuleExecutionSet(Element ruleExecutionSetElement,
                                                   Map properties) throws RuleExecutionSetCreateException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder;

        try {
            /*
             * This should unpack rules from a xml-file, which has the following structure:
             * <rule-execution-set>
                 <name>RuleExecutionSet2</name>
                     <description>Stateful RuleExecutionSet for the TCK for Drools</description>
                     <code>
                     <!-- enter rules code for Drools 3 here -->
                     </code>
                     </rule-execution-set>
             */
            docBuilder = factory.newDocumentBuilder();
            Document doc1 = docBuilder.newDocument();
            doc1.appendChild( ruleExecutionSetElement );
            Reader reader = new StringReader( ruleExecutionSetElement.getChildNodes().item( 2 ).getNodeValue() );

            DrlParser parser = new DrlParser();
            PackageDescr packageDescr;
            packageDescr = parser.parse( reader );

            //          pre build the package
            PackageBuilder builder = new PackageBuilder();
            builder.addPackage( packageDescr );
            Package pkg = builder.getPackage();

            LocalRuleExecutionSetProviderImpl localRuleExecutionSetProvider = new LocalRuleExecutionSetProviderImpl();
            return localRuleExecutionSetProvider.createRuleExecutionSet( pkg,
                                                                         properties );

        } catch ( ParserConfigurationException e ) {
            throw new RuleExecutionSetCreateException( "could not create RuleExecutionSet: " + e );
        } catch ( IOException e ) {
            throw new RuleExecutionSetCreateException( "could not create RuleExecutionSet: " + e );
        } catch ( DroolsParserException e ) {
            throw new RuleExecutionSetCreateException( "could not create RuleExecutionSet: " + e );
        }

    }

    /**
     * Creates a <code>RuleExecutionSet</code> implementation from a
     * Drools-specific Abstract Syntax Tree (AST) representation and
     * Drools-specific properties.<commit_msg>createRuleExecutionSet works now with drools2.5 code and XmlPackageReader as DefaultHandler<commit_after>package org.drools.jsr94.rules.admin;

/*
 * 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.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Serializable;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URL;
import java.util.Map;

import javax.rules.admin.RuleExecutionSet;
import javax.rules.admin.RuleExecutionSetCreateException;
import javax.rules.admin.RuleExecutionSetProvider;

import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.drools.compiler.PackageBuilder;
import org.drools.lang.descr.PackageDescr;
import org.drools.rule.Package;
import org.drools.xml.XmlPackageReader;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXResult;

/**
 * The Drools implementation of the <code>RuleExecutionSetProvider</code>
 * interface which defines <code>RuleExecutionSet</code> creation methods for
 * defining <code>RuleExecutionSet</code>s from potentially serializable
 * resources.
 *
 * @see RuleExecutionSetProvider
 *
 * @author <NAME> (n_alex <at>codehaus.org)
 * @author <a href="mailto:<EMAIL>"><NAME> </a>
 * @author <a href="mailto:<EMAIL>"><NAME> </a>
 */
public class RuleExecutionSetProviderImpl
    implements
    RuleExecutionSetProvider {
    /**
     * Creates a <code>RuleExecutionSet</code> implementation from an XML
     * Document and additional Drools-specific properties. A Drools-specific
     * rule execution set is read from the supplied XML Document.
     *
     * @param ruleExecutionSetElement the XML element that is the source of the
     *        rule execution set
     * @param properties additional properties used to create the
     *        <code>RuleExecutionSet</code> implementation.
     *        May be <code>null</code>.
     *
     * @throws RuleExecutionSetCreateException on rule execution set creation
     *         error.
     *
     * @return The created <code>RuleExecutionSet</code>.
     */
    public RuleExecutionSet createRuleExecutionSet(Element ruleExecutionSetElement,
                                                   Map properties) throws RuleExecutionSetCreateException {
    	  try
          {
//    		 Prepare the DOM source
    	      Source source = new DOMSource( ruleExecutionSetElement );

    		  XmlPackageReader xmlPackageReader = new XmlPackageReader( );
              // Prepare the result
              SAXResult result = new SAXResult( xmlPackageReader );

              // Create a transformer
              Transformer xformer =
                  TransformerFactory.newInstance( ).newTransformer( );

              // Traverse the DOM tree
              xformer.transform( source, result );

              PackageDescr packageDescr = xmlPackageReader.getPackageDescr();

              //          pre build the package
              PackageBuilder builder = new PackageBuilder();
              builder.addPackage( packageDescr );
              Package pkg = builder.getPackage();

              LocalRuleExecutionSetProviderImpl localRuleExecutionSetProvider = new LocalRuleExecutionSetProviderImpl();
              return localRuleExecutionSetProvider.createRuleExecutionSet( pkg,
                                                                           properties );
          }
          catch ( TransformerException e )
          {
              throw new RuleExecutionSetCreateException(
                  "could not create RuleExecutionSet: " + e );
          }


    }

    /**
     * Creates a <code>RuleExecutionSet</code> implementation from a
     * Drools-specific Abstract Syntax Tree (AST) representation and
     * Drools-specific properties.
{'fraction_non_alphanumeric': 0.08119879705485844, 'fraction_numerical': 0.0032147671886342427, 'mean_word_length': 2.8088467614533967, 'pattern_counts': {'":': 0, '<': 64, '<?xml version=': 0, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 18}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7492098', 'n_tokens_mistral': 2425, 'n_tokens_neox': 2356, 'n_words': 707}