<commit_before>﻿/*
   Copyright 2012-2022 <NAME>

   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.
*/

using RDFSharp.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace RDFSharp.Store
{

    /// <summary>
    /// RDFStoreUtilities is a collector of reusable utility methods for RDF store management
    /// </summary>
    internal static class RDFStoreUtilities
    {
        #region Select
        /// <summary>
        /// Parses the current quadruple of the data reader
        /// </summary>
        internal static RDFQuadruple ParseQuadruple(IDataReader fetchedQuadruples)
        {
            if (fetchedQuadruples != null)
            {
                RDFContext qContext = new RDFContext(fetchedQuadruples["Context"].ToString());
                RDFResource qSubject = new RDFResource(fetchedQuadruples["Subject"].ToString());
                RDFResource qPredicate = new RDFResource(fetchedQuadruples["Predicate"].ToString());

                //SPO-flavour quadruple
                if (fetchedQuadruples["TripleFlavor"].ToString().Equals("1"))
                {
                    RDFResource qObject = new RDFResource(fetchedQuadruples["Object"].ToString());
                    return new RDFQuadruple(qContext, qSubject, qPredicate, qObject);
                }

                //SPL-flavour quadruple
                string literal = fetchedQuadruples["Object"].ToString();

                //PlainLiteral
                int lastIndexOfDatatype = literal.LastIndexOf("^^", StringComparison.OrdinalIgnoreCase);
                int lastIndexOfLanguage = literal.LastIndexOf("@", StringComparison.OrdinalIgnoreCase);
                if (!literal.Contains("^^")
                        || literal.EndsWith("^^")
                            || RDFModelUtilities.GetUriFromString(literal.Substring(lastIndexOfDatatype + 2)) == null)
                {
                    RDFPlainLiteral pLit = null;
                    if (RDFNTriples.regexLPL.Match(literal).Success)
                    {
                        string pLitValue = literal.Substring(0, lastIndexOfLanguage);
                        string pLitLang = literal.Substring(lastIndexOfLanguage + 1);
                        pLit = new RDFPlainLiteral(pLitValue, pLitLang);
                    }
                    else
                    {
                        pLit = new RDFPlainLiteral(literal);
                    }
                    return new RDFQuadruple(qContext, qSubject, qPredicate, pLit);
                }

                //TypedLiteral
                string tLitValue = literal.Substring(0, lastIndexOfDatatype);
                string tLitDatatype = literal.Substring(lastIndexOfDatatype + 2);
                RDFModelEnums.RDFDatatypes dt = RDFModelUtilities.GetDatatypeFromString(tLitDatatype);
                RDFTypedLiteral tLit = new RDFTypedLiteral(tLitValue, dt);
                return new RDFQuadruple(qContext, qSubject, qPredicate, tLit);
            }
            throw new RDFStoreException("Cannot parse quadruple because given \"fetchedQuadruples\" parameter is null.");
        }

        /// <summary>
        /// Selects the quadruples corresponding to the given pattern from the given store<commit_msg>Code optimizations<commit_after>﻿/*
   Copyright 2012-2022 <NAME>

   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.
*/

using RDFSharp.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace RDFSharp.Store
{

    /// <summary>
    /// RDFStoreUtilities is a collector of reusable utility methods for RDF store management
    /// </summary>
    internal static class RDFStoreUtilities
    {
        #region Select
        /// <summary>
        /// Parses the current quadruple of the data reader
        /// </summary>
        internal static RDFQuadruple ParseQuadruple(IDataReader fetchedQuadruples)
        {
            if (fetchedQuadruples != null)
            {
                RDFContext qContext = new RDFContext(fetchedQuadruples["Context"].ToString());
                RDFResource qSubject = new RDFResource(fetchedQuadruples["Subject"].ToString());
                RDFResource qPredicate = new RDFResource(fetchedQuadruples["Predicate"].ToString());

                //SPO-flavour quadruple
                if (fetchedQuadruples["TripleFlavor"].ToString().Equals("1"))
                {
                    RDFResource qObject = new RDFResource(fetchedQuadruples["Object"].ToString());
                    return new RDFQuadruple(qContext, qSubject, qPredicate, qObject);
                }

                //SPL-flavour quadruple
                string literal = fetchedQuadruples["Object"].ToString();

                //PlainLiteral
                int lastIndexOfDatatype = literal.LastIndexOf("^^", StringComparison.OrdinalIgnoreCase);
                if (!literal.Contains("^^")
                        || literal.EndsWith("^^")
                            || RDFModelUtilities.GetUriFromString(literal.Substring(lastIndexOfDatatype + 2)) == null)
                {
                    RDFPlainLiteral pLit = null;
                    if (RDFNTriples.regexLPL.Match(literal).Success)
                    {
                        int lastIndexOfLanguage = literal.LastIndexOf("@", StringComparison.OrdinalIgnoreCase);
                        string pLitValue = literal.Substring(0, lastIndexOfLanguage);
                        string pLitLang = literal.Substring(lastIndexOfLanguage + 1);
                        pLit = new RDFPlainLiteral(pLitValue, pLitLang);
                    }
                    else
                    {
                        pLit = new RDFPlainLiteral(literal);
                    }
                    return new RDFQuadruple(qContext, qSubject, qPredicate, pLit);
                }

                //TypedLiteral
                string tLitValue = literal.Substring(0, lastIndexOfDatatype);
                string tLitDatatype = literal.Substring(lastIndexOfDatatype + 2);
                RDFModelEnums.RDFDatatypes dt = RDFModelUtilities.GetDatatypeFromString(tLitDatatype);
                RDFTypedLiteral tLit = new RDFTypedLiteral(tLitValue, dt);
                return new RDFQuadruple(qContext, qSubject, qPredicate, tLit);
            }
            throw new RDFStoreException("Cannot parse quadruple because given \"fetchedQuadruples\" parameter is null.");
        }

        /// <summary>
        /// Selects the quadruples corresponding to the given pattern from the given store
{'fraction_non_alphanumeric': 0.08259897408917533, 'fraction_numerical': 0.0047349730369590955, 'mean_word_length': 2.0270700636942673, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6182716', 'n_tokens_mistral': 2049, 'n_tokens_neox': 1880, 'n_words': 508}