view service/reasoning/graphop.py @ 963:2decd45addf4

notes on pins Ignore-this: 1fced0c832715c5fbd16011563e7120a darcs-hash:20150120053811-312f9-cbcd7801cd4a454a3ae4a05c5534cfb0a9cbafb8
author drewp <drewp@bigasterisk.com>
date Mon, 19 Jan 2015 21:38:11 -0800
parents 4ae49c6adecb
children 0f2ac014d9ea
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("graph A only:")
            for s in lost:
                log.info("  %s", s)
        new = stmtsB - stmtsA
        if new:
            log.info("graph B only:")
            for s in new:
                log.info("  %s", s)
    return False