view service/reasoning/graphop.py @ 49:0aeb8d6ea124

cleanup Ignore-this: 5daf1af90686860062cb65a6dfc6526a
author drewp@bigasterisk.com
date Mon, 31 Dec 2012 14:13:22 -0800
parents 571e773c77c3
children 7179284779fd
line wrap: on
line source

import logging
from rdflib import URIRef, ConjunctiveGraph
log = logging.getLogger()

def graphWithoutMetadata(g, ignorePredicates=[]):
    """
    graph filter that removes any statements whose subjects are
    contexts in the graph and also any statements with the given
    predicates
    """
    ctxs = map(URIRef, set(g.contexts())) # weird they turned to strings

    out = ConjunctiveGraph()
    for stmt in g.quads((None, None, None)):
        if stmt[0] not in ctxs and stmt[1] not in ignorePredicates:
            out.addN([stmt])
    return out

def graphEqual(a, b, ignorePredicates=[]):
    """
    compare graphs, omitting any metadata statements about contexts
    (especially modification times) and also any statements using the
    given predicates
    """
    stmtsA = set(graphWithoutMetadata(a, ignorePredicates))
    stmtsB = set(graphWithoutMetadata(b, ignorePredicates))
    if stmtsA == stmtsB:
        return True
    
    if log.getEffectiveLevel() <= logging.INFO:
        lost = stmtsA - stmtsB
        if lost:
            log.info("lost statements:")
            for s in lost:
                log.info("  %s", s)
        new = stmtsB - stmtsA
        if new:
            log.info("new statements:")
            for s in new:
                log.info("  %s", s)
    return False