<commit_before><?php

/**
 * EasyRdf
 *
 * LICENSE
 *
 * Copyright (c) 2009-2010 <NAME>.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. The name of the author '<NAME>" may be used to endorse or
 *    promote products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * @package    EasyRdf
 * @copyright  Copyright (c) 2009-2010 <NAME>
 * @license    http://www.opensource.org/licenses/bsd-license.php
 * @version    $Id$
 */

/**
 * Container for collection of EasyRdf_Resources.
 *
 * @package    EasyRdf
 * @copyright  Copyright (c) 2009-2010 <NAME>
 * @license    http://www.opensource.org/licenses/bsd-license.php
 */
class EasyRdf_Graph
{
    /** The URI of the graph */
    private $_uri = null;

    /** Array of resources contained in the graph */
    private $_resources = array();

    /** Counter for the number of bnodes */
    private $_bNodeCount = 0;

    /** An HTTP Client object used by graph to fetch data */
    private static $_httpClient = null;


    /** Set the HTTP Client object used to fetch RDF data
     *
     * @param  object mixed $httpClient The new HTTP client object
     * @return object mixed The new HTTP client object
     */
    public static function setHttpClient($httpClient)
    {
        if (!is_object($httpClient) or $httpClient == null) {
            throw new InvalidArgumentException(
                "\$httpClient should be an object and cannot be null"
            );
        }
        return self::$_httpClient = $httpClient;
    }

    /** Get the HTTP Client object used to fetch RDF data
     *
     * If no HTTP Client has previously been set, then a new
     * default (EasyRdf_Http_Client) client will be created.
     *
     * @return object mixed The HTTP client object
     */
    public static function getHttpClient()
    {
        if (!self::$_httpClient) {
            self::$_httpClient = new EasyRdf_Http_Client();
        }
        return self::$_httpClient;
    }

    /**
     * Constructor
     *
     * If no URI is given then an empty graph is created.
     *
     * If a URI is supplied, but no data then the data will
     * be fetched from the URI.
     *
     * The document type is optional and can be specified if it
     * can't be guessed or got from the HTTP headers.
     *
     * @param  string  $uri     The URI of the graph
     * @param  string  $data    Data for the graph
     * @param  string  $format  The document type of the data
     * @return object EasyRdf_Graph
     */
    public function __construct($uri=null, $data=null, $format=null)
    {
        if ($uri) {
            $this->_uri = $uri;
            $this->load($uri, $data, $format);
        }
    }

    /** Get or create a resource stored in a graph
     *
     * If the resource did not previously exist, then a new resource will
     * be created. If you provide an RDF type and that type is registered
     * with the EasyRdf_TypeMapper, then the resource will be an instance
     * of the class registered.
     *
     * @param  string  $uri    The URI of the resource
     * @param  mixed   $types  RDF type of a new resouce (e.g. foaf:Person)
     * @return object EasyRdf_Resouce
     */
    public function resource($uri, $types = array())
    {
        if (!is_string($uri) or $uri == null or $uri == '') {
            throw new InvalidArgumentException(
                "\$uri should be a string and cannot be null or empty"
            );
        }

        // Expand the URI if it is shortened
        $uri = EasyRdf_Namespace::expand($uri);

        // Convert types to an array if it isn't one
        if (!$types) {
            $types = array();
        } else if (!is_array($types)) {
            $types = array($types);
        }

        // Create resource object if it doesn't already exist
        if (!array_key_exists($uri, $this->_resources)) {
            $resClass = 'EasyRdf_Resource';
            foreach ($types as $type) {
                $class = EasyRdf_TypeMapper::get($type);
                if ($class != null) {
                    $resClass = $class;
                    break;
                }
            }
            $this->_resources[$uri] = new $resClass($uri);
        }

        // Add the rdf:type triples
        foreach ($types as $type) {
            $type = $this->resource($type);
            $this->_resources[$uri]->add('rdf:type', $type);
        }

        return $this->_resources[$uri];
    }

    /**
     * Alias for $graph->resource()
     *
     * @deprecated 0.4
     */
    public function get($uri, $types = array())
    {
        return $this->resource($uri, $types);
    }

    /**
     * Create a new blank node in the graph and return it.
     *
     * If you provide an RDF type and that type is registered
     * with the EasyRdf_TypeMapper, then the resource will be an instance
     * of the class registered.
     *
     * @param  mixed   $types  RDF type of a new blank node (e.g. foaf:Person)
     * @return object EasyRdf_Resouce The new blank node
     */
    public function newBNode($types=array())
    {
        return $this->resource("_:eid".(++$this->_bNodeCount), $types);
    }

    /**
     * Load RDF data into the graph.
     *
     * If a URI is supplied, but no data then the data will
     * be fetched from the URI.
     *<commit_msg>Removed EasyRdf_Graph::get() that was deprecated in version 0.4<commit_after><?php

/**
 * EasyRdf
 *
 * LICENSE
 *
 * Copyright (c) 2009-2010 <NAME>.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. The name of the author '<NAME>" may be used to endorse or
 *    promote products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * @package    EasyRdf
 * @copyright  Copyright (c) 2009-2010 <NAME>
 * @license    http://www.opensource.org/licenses/bsd-license.php
 * @version    $Id$
 */

/**
 * Container for collection of EasyRdf_Resources.
 *
 * @package    EasyRdf
 * @copyright  Copyright (c) 2009-2010 <NAME>
 * @license    http://www.opensource.org/licenses/bsd-license.php
 */
class EasyRdf_Graph
{
    /** The URI of the graph */
    private $_uri = null;

    /** Array of resources contained in the graph */
    private $_resources = array();

    /** Counter for the number of bnodes */
    private $_bNodeCount = 0;

    /** An HTTP Client object used by graph to fetch data */
    private static $_httpClient = null;


    /** Set the HTTP Client object used to fetch RDF data
     *
     * @param  object mixed $httpClient The new HTTP client object
     * @return object mixed The new HTTP client object
     */
    public static function setHttpClient($httpClient)
    {
        if (!is_object($httpClient) or $httpClient == null) {
            throw new InvalidArgumentException(
                "\$httpClient should be an object and cannot be null"
            );
        }
        return self::$_httpClient = $httpClient;
    }

    /** Get the HTTP Client object used to fetch RDF data
     *
     * If no HTTP Client has previously been set, then a new
     * default (EasyRdf_Http_Client) client will be created.
     *
     * @return object mixed The HTTP client object
     */
    public static function getHttpClient()
    {
        if (!self::$_httpClient) {
            self::$_httpClient = new EasyRdf_Http_Client();
        }
        return self::$_httpClient;
    }

    /**
     * Constructor
     *
     * If no URI is given then an empty graph is created.
     *
     * If a URI is supplied, but no data then the data will
     * be fetched from the URI.
     *
     * The document type is optional and can be specified if it
     * can't be guessed or got from the HTTP headers.
     *
     * @param  string  $uri     The URI of the graph
     * @param  string  $data    Data for the graph
     * @param  string  $format  The document type of the data
     * @return object EasyRdf_Graph
     */
    public function __construct($uri=null, $data=null, $format=null)
    {
        if ($uri) {
            $this->_uri = $uri;
            $this->load($uri, $data, $format);
        }
    }

    /** Get or create a resource stored in a graph
     *
     * If the resource did not previously exist, then a new resource will
     * be created. If you provide an RDF type and that type is registered
     * with the EasyRdf_TypeMapper, then the resource will be an instance
     * of the class registered.
     *
     * @param  string  $uri    The URI of the resource
     * @param  mixed   $types  RDF type of a new resouce (e.g. foaf:Person)
     * @return object EasyRdf_Resouce
     */
    public function resource($uri, $types = array())
    {
        if (!is_string($uri) or $uri == null or $uri == '') {
            throw new InvalidArgumentException(
                "\$uri should be a string and cannot be null or empty"
            );
        }

        // Expand the URI if it is shortened
        $uri = EasyRdf_Namespace::expand($uri);

        // Convert types to an array if it isn't one
        if (!$types) {
            $types = array();
        } else if (!is_array($types)) {
            $types = array($types);
        }

        // Create resource object if it doesn't already exist
        if (!array_key_exists($uri, $this->_resources)) {
            $resClass = 'EasyRdf_Resource';
            foreach ($types as $type) {
                $class = EasyRdf_TypeMapper::get($type);
                if ($class != null) {
                    $resClass = $class;
                    break;
                }
            }
            $this->_resources[$uri] = new $resClass($uri);
        }

        // Add the rdf:type triples
        foreach ($types as $type) {
            $type = $this->resource($type);
            $this->_resources[$uri]->add('rdf:type', $type);
        }

        return $this->_resources[$uri];
    }

    /**
     * Create a new blank node in the graph and return it.
     *
     * If you provide an RDF type and that type is registered
     * with the EasyRdf_TypeMapper, then the resource will be an instance
     * of the class registered.
     *
     * @param  mixed   $types  RDF type of a new blank node (e.g. foaf:Person)
     * @return object EasyRdf_Resouce The new blank node
     */
    public function newBNode($types=array())
    {
        return $this->resource("_:eid".(++$this->_bNodeCount), $types);
    }

    /**
     * Load RDF data into the graph.
     *
     * If a URI is supplied, but no data then the data will
     * be fetched from the URI.
     *
{'fraction_non_alphanumeric': 0.09153581003446955, 'fraction_numerical': 0.004595940252776714, 'mean_word_length': 2.2936427850655905, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '620476', 'n_tokens_mistral': 3696, 'n_tokens_neox': 3482, 'n_words': 1464}