"""
  
   Explode all the RDF tests into files.  

   That is, take everything in all.rdf from
   http://wiki.webont.org/exports/all.rdf
   and put it in files like
   owl-test-suite/New-Feature-ObjectQCR-001/Conclusion.rdf

   (quick and dirty)

      - sandro@w3.org

"""

import os

import rdflib

testNS = "http://www.w3.org/2007/OWL/testOntology#"
TEST = rdflib.Namespace(testNS)

tests = []
identifier = {}

def dirname(test):
    return "/home/sandro/owl-test-suite/"+identifier[test]

def load():

    graph = rdflib.ConjunctiveGraph()

    print "Loading all the test cases..."
    graph.load('/home/sandro/cvs/dev.w3.org/2009/owl-grddl/all.rdf')
    print "Done"
    print

    for (s, o) in graph.subject_objects(TEST['identifier']):
        tests.append(s)
        identifier[s] = o
    print len(tests), "suitable tests found"

def save_all():
    
    for test in tests:
        os.makedirs(dirname(test))
        for (p, o) in graph.predicate_objects(test):
            pp = str(p)
            if pp.startswith(testNS+"rdfXml"):
                form = pp[(len(testNS+"rdfXml")):]
                assert form.endswith('Ontology')
                form = form[0:-8]
                filename = dirname(test)+"/"+form+".rdf"
                f = open(filename, "w")
                f.write(o)
                f.close()
                print filename

load()
save_all()
