#4173 SPARQL Queries of Hazard Report Knowledge Graph (using RDF conversion)

#4173 SPARQL Queries of Hazard Report Knowledge Graph (using RDF conversion)

#4173 Given the following knowledge graph schema: """Schema:


Entities:


Program, Mission, Subsystem, Key_Entity,

Hazard_Report, Hazard_Cause, Hazard_Control, Hazard_Verification,

Document, Doc_Section


Optional: SubProgram, Vehicle, Element, System, Component


Relationships:


(Program)-[:HAS_MISSION]->(Mission)


(Mission)-[:HAS_SUBSYSTEM]->(Subsystem)


(Mission)-[:HAS_HAZARD_REPORT]->(Hazard_Report)


(Subsystem)-[:HAS_HAZARD_REPORT]->(Hazard_Report)


(Key_Entity)-[:REFERENCED_BY]->(Hazard_Report)


(Hazard_Report)-[:HAS_CAUSE]->(Hazard_Cause)


(Hazard_Cause)-[:HAS_CONTROL]->(Hazard_Control)


(Hazard_Control)-[:HAS_VERIFICATION]->(Hazard_Verification)


(Mission)-[:HAS_DOC]->(Document)


(Hazard_Report)-[:HAS_DOC]->(Document)


(Hazard_Cause)-[:HAS_DOC]->(Document)


(Hazard_Control)-[:HAS_DOC]->(Document)


(Hazard_Verif)-[:HAS_DOC]->(Document)


(Key_Entity)-[:HAS_DOC]->(Document)


(Document)-[:HAS_SECTION->(Doc_Section)


(Hazard_Control)-[:DERIVED_FROM]->(Doc_Section)     #Software Requirements Spec


(Hazard_Control)-[:IMPLEMENTED_BY]->(Doc_Section)   #Software Design Document


(Hazard_Verification)-[:VERIFIED_BY]->(Doc_Section) #Software Test Report""", 


provide 15 useful SPARQL queries

Building on that schema, these SPARQL queries are designed to bridge the gap between high-level mission safety and low-level engineering documentation. Since the schema covers the lifecycle from Hazard Reports to Software Test Reports, these queries focus on traceability, impact analysis, and compliance.

I have used the prefix ex: as a placeholder for your specific namespace.


1. Safety Traceability & Compliance

Q1: The Full Safety Chain

Retrieve the entire "thread" for a specific hazard, from cause to verification.

SELECT ?hazard ?cause ?control ?verification
WHERE {
  ?hazard a ex:Hazard_Report ;
          ex:HAS_CAUSE ?cause .
  ?cause ex:HAS_CONTROL ?control .
  ?control ex:HAS_VERIFICATION ?verification .
}

Q2: Software Requirements Coverage

List all Hazard Controls and the specific Software Requirement sections (Doc_Section) they are derived from.

SELECT ?control ?requirementSection
WHERE {
  ?control a ex:Hazard_Control ;
           ex:DERIVED_FROM ?requirementSection .
  ?requirementSection a ex:Doc_Section .
}

Q3: Unverified Hazard Controls

Find Hazard Controls that do not yet have an associated Hazard Verification.

SELECT ?control
WHERE {
  ?control a ex:Hazard_Control .
  FILTER NOT EXISTS { ?control ex:HAS_VERIFICATION ?v . }
}

Q4: Verification Testing Status

Identify which Doc_Section (e.g., a Software Test Report) is responsible for verifying a specific Hazard.

SELECT ?hazard ?testReportSection
WHERE {
  ?hazard ex:HAS_CAUSE/ex:HAS_CONTROL/ex:HAS_VERIFICATION ?verif .
  ?verif ex:VERIFIED_BY ?testReportSection .
}

2. Impact Analysis

Q5: Key Entity Hazard Impact

If a Key_Entity (like a specific sensor or actuator) fails, which Hazard Reports are impacted?

SELECT ?keyEntity ?hazard
WHERE {
  ?keyEntity ex:REFERENCED_BY ?hazard .
  ?hazard a ex:Hazard_Report .
}

Q6: Subsystem Risk Profile

Count the number of Hazard Reports associated with each Subsystem.

SELECT ?subsystem (COUNT(?hazard) AS ?hazardCount)
WHERE {
  ?subsystem ex:HAS_HAZARD_REPORT ?hazard .
}
GROUP BY ?subsystem
ORDER BY DESC(?hazardCount)

Q7: Document Change Impact

If a specific Document is updated, which Hazard Controls might need to be re-evaluated?

SELECT DISTINCT ?control
WHERE {
  ?control ex:HAS_DOC ?doc .
  # Or derived from a section within that doc
  UNION {
    ?control ex:DERIVED_FROM ?section .
    ?doc ex:HAS_SECTION ?section .
  }
}

3. Mission & Program Oversight

Q8: Mission-Wide Document Registry

List all documents associated with a specific Mission, categorized by what they relate to (Hazard, Cause, etc.).

SELECT ?doc ?relatedEntity
WHERE {
  ex:SpecificMission ex:HAS_DOC ?doc .
  ?relatedEntity ex:HAS_DOC ?doc .
}

Q9: Cross-Mission Subsystem Comparison

Find Subsystems that appear in multiple Missions and their associated Hazard Reports.

SELECT ?subsystem ?mission ?hazard
WHERE {
  ?mission ex:HAS_SUBSYSTEM ?subsystem .
  ?subsystem ex:HAS_HAZARD_REPORT ?hazard .
}
ORDER BY ?subsystem

Q10: Program-to-Verification Trace

Trace from the top-level Program down to the specific Test Reports (Doc_Section).

SELECT ?program ?mission ?hazard ?testSection
WHERE {
  ?program ex:HAS_MISSION ?mission .
  ?mission ex:HAS_HAZARD_REPORT ?hazard .
  ?hazard ex:HAS_CAUSE/ex:HAS_CONTROL/ex:HAS_VERIFICATION/ex:VERIFIED_BY ?testSection .
}

4. Systems Engineering & Documentation

Q11: Implementation Gaps

Find Hazard Controls that are defined (Derived From) but not yet implemented (no IMPLEMENTED_BY relationship).

SELECT ?control
WHERE {
  ?control ex:DERIVED_FROM ?req .
  FILTER NOT EXISTS { ?control ex:IMPLEMENTED_BY ?design . }
}

Q12: Document Section Hierarchy

Retrieve all sections for a specific document to see the structure of the Software Design or Test Plan.

SELECT ?section
WHERE {
  ex:SoftwareDesignDoc ex:HAS_SECTION ?section .
}

Q13: Orphaned Documents

Identify Documents in the graph that are not linked to any Program, Mission, or Hazard.

SELECT ?doc
WHERE {
  ?doc a ex:Document .
  FILTER NOT EXISTS { ?anyEntity ex:HAS_DOC ?doc . }
}

Q14: Multi-Cause Hazards

Find Hazard Reports that are triggered by more than one Hazard Cause.

SELECT ?hazard (COUNT(?cause) AS ?causeCount)
WHERE {
  ?hazard ex:HAS_CAUSE ?cause .
}
GROUP BY ?hazard
HAVING (COUNT(?cause) > 1)

Q15: Verification Evidence Search

Search for specific verification sections that contain "PASS" or "FAIL" (assuming text/label properties exist).

SELECT ?verif ?docSection
WHERE {
  ?verif ex:VERIFIED_BY ?docSection .
  ?docSection rdfs:label ?label .
  FILTER(CONTAINS(LCASE(?label), "fail"))
}

Would you like me to refine any of these queries to include specific NASA-standard metadata or property types?


Provide a small Python app to run these queries and report the results

This script uses rdflib, which is the standard for managing RDF and SPARQL in Python. It includes a mock dataset based on your schema so you can run it immediately to see the output.

Python SPARQL Runner

from rdflib import Graph, Namespace, Literal, RDF

def run_sparql_demo():
    # 1. Setup Graph and Namespace
    g = Graph()
    EX = Namespace("http://nasa.gov/safety/")
    g.bind("ex", EX)

    # 2. Seed Mock Data (Representing your Schema)
    # Program -> Mission -> Hazard -> Cause -> Control -> Verification -> Doc
    mission = EX.Artemis_II
    hazard = EX.H_001_Propulsion_Failure
    cause = EX.C_001_Software_Glitch
    control = EX.CTL_001_Redundancy_Check
    verif = EX.V_001_Unit_Test
    doc_section = EX.STR_Section_4_2

    g.add((mission, RDF.type, EX.Mission))
    g.add((hazard, RDF.type, EX.Hazard_Report))
    g.add((mission, EX.HAS_HAZARD_REPORT, hazard))
    g.add((hazard, EX.HAS_CAUSE, cause))
    g.add((cause, EX.HAS_CONTROL, control))
    g.add((control, EX.HAS_VERIFICATION, verif))
    g.add((verif, EX.VERIFIED_BY, doc_section))
    
    # Label for search demo
    g.add((doc_section, EX.status, Literal("FAIL: Logic error in redundant switch")))

    # 3. Define Queries
    queries = {
        "Full Safety Chain": """
            SELECT ?hazard ?cause ?control ?verif
            WHERE {
                ?hazard ex:HAS_CAUSE ?cause .
                ?cause ex:HAS_CONTROL ?control .
                ?control ex:HAS_VERIFICATION ?verif .
            }""",
        
        "Verification Evidence Search (FAIL check)": """
            SELECT ?verif ?status
            WHERE {
                ?verif ex:VERIFIED_BY ?section .
                ?section ex:status ?status .
                FILTER(CONTAINS(LCASE(STR(?status)), "fail"))
            }"""
    }

    # 4. Execute and Report
    print("--- NASA S&MA Knowledge Graph Query Report ---\n")
    
    for name, q_text in queries.items():
        print(f"Executing: {name}")
        results = g.query(q_text)
        
        if not results:
            print("  No results found.")
        
        for row in results:
            # Clean up the output by stripping the namespace for readability
            cleaned_row = [str(r).split('/')[-1] for r in row]
            print(f"  > {' | '.join(cleaned_row)}")
        print("-" * 50)

if __name__ == "__main__":
    # You may need to run: pip install rdflib
    run_sparql_demo()

Key Features of this App:

  • Namespace Binding: It binds ex: to a NASA-style URI, making the SPARQL syntax identical to the queries I provided earlier.

  • Traceability: The "Full Safety Chain" query demonstrates how SPARQL traverses the multi-hop relationships defined in your schema.

  • Output Formatting: It strips the long URI prefixes (e.g., http://nasa.gov/safety/) so you just see the entity names like H_001_Propulsion_Failure.

Troubleshooting Tip:

Since you mentioned using pyvis and NetworkX previously, remember that while NetworkX is great for graph algorithms, it doesn't support SPARQL natively. You'll need to export your NetworkX data into an rdflib.Graph (as shown above) to utilize these semantic queries.

Would you like me to write a converter function that maps a NetworkX object directly into this RDF schema?


From <https://gemini.google.com/app/>  Google Gemini 3.0 (Fast)

Comments

Popular posts from this blog

#2892 Example of ChatGPT o1 Prompt

#3105 Federal and State Spending Idiocy