/** * Set all required type mappers * * @return void */ public static function setTypeMappers() { EasyRdf_Namespace::set('spl', 'http://spinrdf.org/spl#'); EasyRdf_Namespace::set('spin', 'http://spinrdf.org/spin#'); EasyRdf_Namespace::set('sp', 'http://spinrdf.org/sp#'); // Query EasyRdf_TypeMapper::set('sp:Ask', 'EasySpinRdf_Query_Ask'); EasyRdf_TypeMapper::set('sp:Select', 'EasySpinRdf_Query_Select'); EasyRdf_TypeMapper::set('sp:Describe', 'EasySpinRdf_Query_Describe'); EasyRdf_TypeMapper::set('sp:Construct', 'EasySpinRdf_Query_Construct'); // Elements EasyRdf_TypeMapper::set('sp:TriplePattern', 'EasySpinRdf_Element_TriplePattern'); EasyRdf_TypeMapper::set('sp:SubQuery', 'EasySpinRdf_Element_SubQuery'); EasyRdf_TypeMapper::set('sp:NamedGraph', 'EasySpinRdf_Element_NamedGraph'); EasyRdf_TypeMapper::set('sp:Optional', 'EasySpinRdf_Element_Optional'); EasyRdf_TypeMapper::set('sp:Union', 'EasySpinRdf_Element_Union'); EasyRdf_TypeMapper::set('sp:Bind', 'EasySpinRdf_Element_Bind'); EasyRdf_TypeMapper::set('sp:Filter', 'EasySpinRdf_Element_Filter'); EasyRdf_TypeMapper::set('sp:TriplePath', 'EasySpinRdf_Element_TriplePath'); }
<?php require_once 'BBC/Programmes/Base.php'; class BBC_Programmes_Version extends BBC_Programmes_Base { public function broadcasts() { return self::getGraph()->resourcesMatching('po:broadcast_of', $this); } } EasyRdf_TypeMapper::set('po:Version', 'BBC_Programmes_Version');
function age() { $birth = $this->birthEvent(); if ($birth) { $year = substr($birth->get('bio:date'), 0, 4); if ($year) { return date('Y') - $year; } } return 'unknown'; } } ## Add namespaces EasyRdf_Namespace::set('mo', 'http://purl.org/ontology/mo/'); EasyRdf_Namespace::set('bio', 'http://purl.org/vocab/bio/0.1/'); EasyRdf_TypeMapper::set('mo:MusicArtist', 'Model_MusicArtist'); ?> <html> <head><title>EasyRdf Artist Info Example</title></head> <body> <h1>EasyRdf Artist Info Example</h1> <?php echo form_tag(); echo text_field_tag('uri', 'http://www.bbc.co.uk/music/artists/70248960-cb53-4ea4-943a-edb18f7d336f.rdf', array('size' => 50)); echo submit_tag(); echo form_end_tag(); ?> <?php if (isset($_REQUEST['uri'])) {
} if (!is_string($class) or $class == null or $class == '') { throw new InvalidArgumentException("\$class should be a string and cannot be null or empty"); } $type = EasyRdf_Namespace::expand($type); return self::$map[$type] = $class; } /** * Delete an existing RDF type mapping. * * @param string $type The RDF type (e.g. foaf:Person) */ public static function delete($type) { if (!is_string($type) or $type == null or $type == '') { throw new InvalidArgumentException("\$type should be a string and cannot be null or empty"); } $type = EasyRdf_Namespace::expand($type); if (isset(self::$map[$type])) { unset(self::$map[$type]); } } } /* Register default set of mapped types */ EasyRdf_TypeMapper::set('rdf:Alt', 'EasyRdf_Container'); EasyRdf_TypeMapper::set('rdf:Bag', 'EasyRdf_Container'); EasyRdf_TypeMapper::set('rdf:List', 'EasyRdf_Collection'); EasyRdf_TypeMapper::set('rdf:Seq', 'EasyRdf_Container');
/** Work out the class to instantiate a resource as * @ignore */ protected function classForResource($uri) { $rdfType = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'; if (isset($this->index[$uri][$rdfType])) { foreach ($this->index[$uri][$rdfType] as $type) { if ($type['type'] == 'uri' or $type['type'] == 'bnode') { $class = EasyRdf_TypeMapper::get($type['value']); if ($class != null) { return $class; } } } } // Parsers don't typically add a rdf:type to rdf:List, so we have to // do a bit of 'inference' here using properties. if ($uri == 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil' or isset($this->index[$uri]['http://www.w3.org/1999/02/22-rdf-syntax-ns#first']) or isset($this->index[$uri]['http://www.w3.org/1999/02/22-rdf-syntax-ns#rest'])) { return 'EasyRdf_Collection'; } return 'EasyRdf_Resource'; }
$key = $property->shorten(); if ($key) { $properties[$key] = $property; } } } return $properties; } /** * Get the cardinality of a property. * * @return string '1' if the property takes a single value, otherwise 'N'. */ public function cardinality() { $types = $this->types(); # Apart from owl_FunctionalProperty, these rules really correct, # but they provide a good set of defaults if (in_array('owl:FunctionalProperty', $types) or in_array('owl:DatatypeProperty', $types) or in_array('owl:InverseFunctionalProperty', $types)) { return '1'; } else { return 'N'; } } } ## FIXME: Don't Repeat Yourself EasyRdf_TypeMapper::set('rdf:Property', 'EasyRdf_Owl_Property'); EasyRdf_TypeMapper::set('owl:Property', 'EasyRdf_Owl_Property'); EasyRdf_TypeMapper::set('owl:ObjectProperty', 'EasyRdf_Owl_Property'); EasyRdf_TypeMapper::set('owl:DatatypeProperty', 'EasyRdf_Owl_Property');
public function testInstantiate() { EasyRdf_TypeMapper::set('foaf:Person', 'MyType_Class'); $data = readFixture('foaf.json'); $graph = new EasyRdf_Graph('http://www.example.com/joe/foaf.rdf', $data, 'json'); $joe = $graph->resource('http://www.example.com/joe#me'); $this->assertClass('MyType_Class', $joe); $this->assertTrue($joe->myMethod()); }
<?php require_once 'BBC/Programmes/Base.php'; class BBC_Programmes_Broadcast extends BBC_Programmes_Base { } EasyRdf_TypeMapper::set('po:FirstBroadcast', 'BBC_Programmes_Broadcast'); EasyRdf_TypeMapper::set('po:RepeatBroadcast', 'BBC_Programmes_Broadcast');
<?php require_once 'BBC/Programmes/Programme.php'; class BBC_Programmes_Series extends BBC_Programmes_Programme { } EasyRdf_TypeMapper::set('po:Series', 'BBC_Programmes_Series');
EasyRdf_TypeMapper::set('sp:Count', 'EasySpinRdf_Expression_Aggregation_Count'); EasyRdf_TypeMapper::set('sp:Max', 'EasySpinRdf_Expression_Aggregation_Max'); EasyRdf_TypeMapper::set('sp:Min', 'EasySpinRdf_Expression_Aggregation_Min'); EasyRdf_TypeMapper::set('sp:Sum', 'EasySpinRdf_Expression_Aggregation_Sum'); EasyRdf_TypeMapper::set('sp:Asc', 'EasySpinRdf_Expression_Aggregation_Asc'); EasyRdf_TypeMapper::set('sp:Desc', 'EasySpinRdf_Expression_Aggregation_Desc'); // Mathematical EasyRdf_TypeMapper::set('sp:mul', 'EasySpinRdf_Expression_Mathematical_Multiply'); EasyRdf_TypeMapper::set('sp:add', 'EasySpinRdf_Expression_Mathematical_Add'); EasyRdf_TypeMapper::set('sp:sub', 'EasySpinRdf_Expression_Mathematical_Substract'); EasyRdf_TypeMapper::set('sp:divide', 'EasySpinRdf_Expression_Mathematical_Divide'); // Path EasyRdf_TypeMapper::set('sp:SeqPath', 'EasySpinRdf_Expression_Path_SeqPath'); EasyRdf_TypeMapper::set('sp:AltPath', 'EasySpinRdf_Expression_Path_AltPath'); EasyRdf_TypeMapper::set('sp:ModPath', 'EasySpinRdf_Expression_Path_ModPath'); EasyRdf_TypeMapper::set('sp:ReversePath', 'EasySpinRdf_Expression_Path_ReversePath'); /** * Abstract class that represents an SPIN resource * * @package EasySpinRdf * @copyright Conjecto - Blaise de Carné * @license http://www.opensource.org/licenses/bsd-license.php */ abstract class EasySpinRdf_Resource extends EasyRdf_Resource { /** * Get the SPARQL representation of the resource * @return string */ public abstract function getSparql(); /**
<?php require_once 'BBC/Programmes/Programme.php'; class BBC_Programmes_Clip extends BBC_Programmes_Programme { } EasyRdf_TypeMapper::set('po:Clip', 'BBC_Programmes_Clip');
return $html; } } # Extra namespaces we use EasyRdf_Namespace::set('vann', 'http://purl.org/vocab/vann/'); EasyRdf_Namespace::set('vs', 'http://www.w3.org/2003/06/sw-vocab-status/ns#'); ## Add mappings EasyRdf_TypeMapper::set('owl:Ontology', 'Phpspecgen_Vocab'); EasyRdf_TypeMapper::set('owl:Class', 'Phpspecgen_Class'); EasyRdf_TypeMapper::set('rdfs:Class', 'Phpspecgen_Class'); EasyRdf_TypeMapper::set('owl:Property', 'Phpspecgen_Property'); EasyRdf_TypeMapper::set('owl:DatatypeProperty', 'Phpspecgen_Property'); EasyRdf_TypeMapper::set('owl:ObjectProperty', 'Phpspecgen_Property'); EasyRdf_TypeMapper::set('owl:InverseFunctionalProperty', 'Phpspecgen_Property'); EasyRdf_TypeMapper::set('owl:FunctionalProperty', 'Phpspecgen_Property'); EasyRdf_TypeMapper::set('rdf:Property', 'Phpspecgen_Property'); ?> <html> <head> <title>phpspecgen</title> <link rel="stylesheet" type="text/css" href="style.css" /> </head> <body> <?php $options = getopt('u:'); if (!empty($_REQUEST['uri'])) { $uri = $_REQUEST['uri']; } elseif (array_key_exists('u', $options)) { $uri = $options['u']; }
{ return str_replace('_', '/', $this->className()) . '.php'; } /** * Get an array of properties for a class * * If no properties are found for the class, an empty array is returned. * * @return array An array of EasyRdf_Property associated with the class */ function classProperties($graph) { // FIXME: not ideal having to pass graph in here $properties = array(); # FIXME: cache this somehow? $owlThing = $graph->get('http://www.w3.org/2002/07/owl#Thing'); $superClass = $this->get('rdfs_subClassOf'); if ($superClass == $owlThing) { $superClass = ''; } $allProperties = EasyRdf_Owl_Property::findAll($graph); foreach ($allProperties as $name => $property) { if ($superClass == '' and (count($property->all('rdfs:domain')) == 0 or in_array($owlThing, $property->all('rdfs:domain'))) or in_array($this, $property->all('rdfs:domain'))) { array_push($properties, $property); } } return $properties; } } EasyRdf_TypeMapper::set('owl:Class', 'EasyRdf_Owl_Class');
<?php require_once 'BBC/Programmes/Programme.php'; class BBC_Programmes_Episode extends BBC_Programmes_Programme { public function broadcasts() { $broadcasts = array(); foreach ($this->all('po:version') as $version) { $broadcasts = array_merge(self::getGraph()->resourcesMatching('po:broadcast_of', $version), $broadcasts); } return $broadcasts; } } EasyRdf_TypeMapper::set('po:Episode', 'BBC_Programmes_Episode');
/** Work out the class to instantiate a resource as * @ignore */ protected function classForResource($uri) { $resClass = 'EasyRdf_Resource'; $rdfType = EasyRdf_Namespace::expand('rdf:type'); if (isset($this->_index[$uri][$rdfType])) { foreach ($this->_index[$uri][$rdfType] as $type) { if ($type['type'] == 'uri' or $type['type'] == 'bnode') { $class = EasyRdf_TypeMapper::get($type['value']); if ($class != null) { $resClass = $class; break; } } } } return $resClass; }
<?php require_once 'BBC/Programmes/Base.php'; class BBC_Programmes_Service extends BBC_Programmes_Base { } EasyRdf_TypeMapper::set('po:Service', 'BBC_Programmes_Service');
/** 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]; }
<?php require_once 'BBC/Programmes/Programme.php'; class BBC_Programmes_Brand extends BBC_Programmes_Programme { } EasyRdf_TypeMapper::set('po:Brand', 'BBC_Programmes_Brand');