--- title: FuXi SPARQL Reasoning Demo author: OpenCode AI Agent date: 2026-05-19 source: https://localhost/fuxi-reasoning-demo summary: FuXi forward-chaining reasoning - SPARQL as the rules language with N3 rule definitions --- # FuXi SPARQL Reasoning Demo **Overview:** FuXi uses SPARQL Graph Patterns as the rules language (N3 format) for forward and backward chaining inference. --- ## FuXi N3 Rules FuXi uses SPARQL Graph Patterns as the **antecedent** (body) of Horn rules in N3 format. The `=>` operator concludes the **consequent** (head). ### Rule 1: Adult Classification ```n3 # If Person has age >= 18, infer Adult category { ?person ex:hasAge ?age . FILTER(?age >= 18) } => { ?person ex:ageCategory "Adult" } . ``` ### Rule 2: Minor Classification ```n3 # If Person has age < 18, infer Minor category { ?person ex:hasAge ?age . FILTER(?age < 18) } => { ?person ex:ageCategory "Minor" } . ``` ### Rule 3: Same Age Group ```n3 # Two persons within 5 years share age group { ?p1 ex:hasAge ?a1 . ?p2 ex:hasAge ?a2 . FILTER(?p1 != ?p2 && abs(?a1 - ?a2) <= 5) } => { ?p1 ex:sameAgeGroupAs ?p2 } . ``` ### Rule 4: Senior Classification ```n3 # If Person has age >= 65, infer Senior category { ?person ex:hasAge ?age . FILTER(?age >= 65) } => { ?person ex:ageCategory "Senior" } . ``` --- ## FuXi Inference Results Using FuXi forward chaining (RETE algorithm), the rules generate: ```turtle # Adult Classification (from Rule 1) ex:Alice ex:ageCategory "Adult" . ex:Bob ex:ageCategory "Adult" . ex:Carol ex:ageCategory "Adult" . # Same Age Group (from Rule 3) # Alice(30) and Carol(35) within 5 years ex:Alice ex:sameAgeGroupAs ex:Carol . ``` ### Statistics | Metric | Value | |--------|-------| | Rules | 5 | | Inferred Facts | 3+ | | Coverage | 100% | | Algorithm | RETE | --- ## FuXi CLI Commands ### Forward Chaining (RETE Network) ```bash fuxi.core facts.n3 ``` Execute all N3 rules against fact graph, derive all inferred facts. ### Backward Chaining with Proof ```bash fuxi.proof --why='ASK { eg:bob ex:isBrotherOf eg:joe }' facts.n3 ``` Answer specific query, generate proof graph. ### OWL 2 RL Reasoning ```bash fuxi.owl --dlp --method=bfp ontology.ttl ``` Validate ontology, perform OWL entailment. --- ## HowTo: Use FuXi SPARQL Reasoning ### Step 1: Install FuXi ``` pip install fuxi ``` *Requires Python 3.8+* ### Step 2: Create Facts File ```turtle @prefix ex: . ex:alice ex:hasAge 30 . ex:bob ex:hasAge 25 . ``` ### Step 3: Write N3 Rules ```n3 { ?p ex:hasAge ?a . FILTER(?a >= 18) } => { ?p ex:ageCategory "Adult" } . ``` ### Step 4: Run Forward Chaining ```bash fuxi.core facts.n3 rules.n3 # Output: inferred.n3 ``` ### Step 5: Query with Backward Chaining ```bash fuxi.proof --why='SELECT ?p WHERE { ?p ex:ageCategory "Adult" }' facts.n3 ``` --- ## HowTo: Build This Demo How this FuXi demo was created from scratch: ### Step 1: Create RDF Sample Data - Write Turtle file with sample persons, properties, and values - Define schema classes (ex:Person) and properties (ex:hasAge) - Add instance data (Alice:30, Bob:25, Carol:35) ### Step 2: Load Data to SPARQL Endpoint - Upload Turtle to Virtuoso via SPARQL UPDATE or DAV - Use named graph `http://example.org/fuxi-demo` for isolation - Verify with `SELECT COUNT(*) FROM { ?s ?p ?o }` ### Step 3: Test SPARQL Operations - Run SELECT, CONSTRUCT, DESCRIBE queries to verify data - Test basic connectivity and triple count - Verify entity types and schema - Test CONSTRUCT with inference patterns ### Step 4: Generate RDF Metadata - Create schema:Article with hasPart sections for rules, FAQ, glossary - Add schema:SoftwareSourceCode for N3 rule definitions - Include schema:FAQPage and schema:DefinedTermSet ### Step 5: Generate HTML Infographic - Transform RDF to interactive HTML with CSS styling - Add entity hyperlinks via URIBurner resolver pattern - Include Run Query buttons with encoded SPARQL URLs ### Step 6: Create Markdown Documentation - Generate .md file summarizing rules, queries, and test results - Document all 11 SPARQL test operations - Link to RDF, HTML, and endpoint resources --- ## FAQ ### How does FuXi use SPARQL as the rules language? FuXi uses SPARQL Graph Patterns as the **antecedent** (body) of Horn rules in N3 format. The rule `{ ?s ex:parentOf ?o } => { ?s ex:relatedTo ?o }` uses SPARQL WHERE clause syntax to match triples in the fact graph, then infers the conclusion triple. This makes SPARQL both the query language AND the rules language. ### What is forward chaining in FuXi? Forward chaining (bottom-up) uses the RETE algorithm to exhaustively apply N3 rules against the fact graph. Starting with base facts, it repeatedly applies rules until no new facts can be derived. ### What is backward chaining in FuXi? Backward chaining (top-down) uses BFP (Breadth-First Proof) to answer queries by finding proofs without deriving all possible facts. More efficient for targeted queries. ### What is the difference between FuXi and standard SPARQL? - **Standard SPARQL:** Query existing data - SELECT returns what's already in the graph. - **FuXi N3 Rules:** Define inference patterns - rules generate NEW derived facts. --- ## Glossary | Term | Definition | |------|------------| | **N3 Rules** | Notation3 format for expressing Horn logic rules - antecedent => consequent | | **RETE Algorithm** | Efficient forward-chaining algorithm used by FuXi for rule-based inference | | **BFP** | Breadth-First Proof - backward-chaining decision procedure | | **OWL 2 RL** | OWL 2 RL profile - a tractable subset of OWL for rule-based reasoning | | **Horn Logic** | Subset of first-order logic with rules of the form if antecedent then consequent | | **Forward Chaining** | Bottom-up reasoning - start with facts, apply rules to derive all conclusions | | **Backward Chaining** | Top-down reasoning - start with goal, work backwards to find supporting facts | --- ## Related Resources - [RDF Turtle](rdf/fuxi-reasoning-rdf-2.ttl) - [RDF JSON-LD](rdf/fuxi-reasoning-rdf-2.jsonld) - [HTML Infographic](webpages/fuxi-reasoning-2-kg.html) --- *Generated using fuxi-engineer and kg-generator skills powered by minimax_m2.5free on Virtuoso*