def setup_top_concepts(rdf, mark_top_concepts):
    """Determine the top concepts of each concept scheme and mark them using
    hasTopConcept/topConceptOf."""

    for cs in sorted(rdf.subjects(RDF.type, SKOS.ConceptScheme)):
        for conc in sorted(rdf.subjects(SKOS.inScheme, cs)):
            if (conc, RDF.type, SKOS.Concept) not in rdf:
                continue  # not a Concept, so can't be a top concept
            # check whether it's a top concept
            broader = rdf.value(conc, SKOS.broader, None, any=True)
            if broader is None:  # yes it is a top concept!
                if (cs, SKOS.hasTopConcept, conc) not in rdf and \
                   (conc, SKOS.topConceptOf, cs) not in rdf:
                    if mark_top_concepts:
                        logging.info(
                            "Marking loose concept %s "
                            "as top concept of scheme %s", conc, cs)
                        rdf.add((cs, SKOS.hasTopConcept, conc))
                        rdf.add((conc, SKOS.topConceptOf, cs))
                    else:
                        logging.debug(
                            "Not marking loose concept %s as top concept "
                            "of scheme %s, as mark_top_concepts is disabled",
                            conc, cs)