Installation:
composer require semsol/arc2:^3
Requires PHP 8.4+.
Basic RDF Operations:
use ARC2 as ARC;
// Initialize store (MySQL example)
$dbConfig = [
'db_name' => 'your_db',
'db_user' => 'user',
'db_pwd' => 'password',
'db_host' => 'localhost',
];
$store = ARC::getStore($dbConfig);
// Create a resource
$resource = $store->getResource('http://example.org/resource');
$resource->addProperty('http://example.org/property', 'value');
// Save to store
$store->saveResource($resource);
First SPARQL Query:
$query = "
SELECT ?subject ?property ?value
WHERE {
?subject ?property ?value .
}
LIMIT 10
";
$results = $store->query($query);
ARC2_Class for resource/property manipulation.ARC2_Store for queries (SELECT, INSERT, DELETE, etc.).Pattern: Use ARC2_Resource for structured data.
$resource = $store->getResource('http://example.org/person');
$resource->addProperty('http://xmlns.com/foaf/0.1/name', 'John Doe');
$resource->addProperty('http://xmlns.com/foaf/0.1/age', 30);
// Save with inferred triples (if enabled)
$store->saveResource($resource, ['infer' => true]);
Workflow:
getResource().addProperty().infer: true).Pattern: Use ARC2_Store::query() for SPARQL 1.0/1.1.
// SELECT with aggregates (note: SUM/AVG have bugs)
$query = "
SELECT ?person (COUNT(?friend) AS ?friendCount)
WHERE {
?person <http://xmlns.com/foaf/0.1/knows> ?friend .
}
GROUP BY ?person
";
$results = $store->query($query);
Integration Tips:
LOAD <url> to import RDF (Turtle, RDF/XML).
LOAD <http://example.com/data.ttl> INTO <http://example.com/graph>
INSERT/DELETE with CONSTRUCT for dynamic updates.
INSERT INTO <http://example.org/backup> {
?s ?p ?o .
}
WHERE {
?s <http://example.org/old> ?o .
}
Pattern: Configure DB adapter (PDO/mysqli) and caching.
$dbConfig = [
'db_adapter' => 'pdo', // or 'mysqli' (default)
'db_pdo_protocol' => 'mysql', // for PDO
'cache' => [
'enabled' => true,
'adapter' => 'apcu', // or 'file', 'memcache'
],
];
$store = ARC::getStore($dbConfig);
Laravel Integration:
.env:
ARC2_DB_NAME=laravel_arc2
ARC2_DB_USER=root
ARC2_DB_PASSWORD=secret
ARC2_DB_ADAPTER=pdo
$config = config('arc2');
$store = ARC::getStore($config);
PHP 8.4+ Requirement:
composer require php:^8.4 if needed.SPARQL Bugs:
SUM/AVG functions are bugged (see #99). Use COUNT/MIN/MAX as alternatives.LOAD with INTO may fail if the target graph doesn’t exist (create it manually).PDO vs. mysqli:
ARC2_Class::queryDB() (deprecated for direct DB access).Caching Quirks:
false.apcu cache requires pecl install apcu.Query Errors:
$store->setDebug(true);
ARC2_Store::getLastQuery().Performance:
CREATE INDEX idx_subject ON arc2_triples (subject);
LIMIT in queries to avoid timeouts.Data Corruption:
ARC2_Store::repair() if triples are missing:
$store->repair();
Custom SPARQL Functions:
ARC2_SPARQL_Function to add user-defined functions.CUSTOM_FUNC:
$store->registerSPARQLFunction('CUSTOM_FUNC', function($args) {
return array_sum($args);
});
Database Adapters:
ARC2_Store_Adapter_Abstract for new DB backends (e.g., PostgreSQL).Event Hooks:
ARC2_Store::preSave()/postSave() for pre/post-save logic.How can I help you explore Laravel packages today?