Пример #1
0
class GraphTester(object):
    """Compares MLR graphs obtained from converting LOM to expected graphs.
    The graph tester is stateful.

    1. Receives a LOM fragment and stores it.
    2. Receives a required N3 MLR graph and LOM conversion parameters
        A. Converts the LOM fragment to a converted graph (with parameters.)
        B. Checks that all triples in the N3 graph are found in the converted
           graph. Stores the required graph.
    3. Receives a forbidden N3 MLR graph:
        Checks that all triples in the forbidden graph are either present in
        the required graph or absent from the converted graph.
    """
    MISSING = 1
    UNEXPECTED = 2

    def __init__(self):
        self.converter = Converter()
        self.parser = self.converter.populate_argparser()
        self.reset()

    def normalizeTerm(self, graph, term):
        if isinstance(term, URIRef):
            return graph.normalizeUri(term)
        return term

    def reset(self):
        "Clears the state"
        self.last_lom = None
        self.last_graph = None
        self.last_comparator = None

    def set_lom(self, lom):
        "Sets the LOM fragment."
        self.reset()
        self.last_lom = etree.fromstring(LOM_TEMPLATE % (lom,))

    def process_line(self, format, code, args=None):
        "Process a fragment."
        format = format.lower()
        if format == 'xml':
            self.set_lom(code)
            return None, []
        elif format == 'n3':
            return self.test_n3(code, args)
        elif format == 'rdf-xml':
            return self.test_graph(Graph().parse(data=code, format="xml"), args)
        assert False, 'format should be xml or n3'

    def parse_n3(self, n3):
        "Parse a N3 graph"
        return Graph().parse(data=N3_PREFIXES + n3, format="n3")

    def test_n3(self, n3, args=None):
        "Test that the provided n3 fragment is conformant"
        return self.test_graph(self.parse_n3(n3), args)

    def find_missing(self, expected_graph, obtained_graph):
        "List triples in the expected graph missing from the obtained graph."
        errors = []
        nsm = obtained_graph.namespace_manager
        comparator_eo = GraphCorrespondence(expected_graph, obtained_graph)
        self.last_comparator = comparator_eo
        comparator_eo.identify()
        triples = expected_graph.triples((None, None, None))
        triples = comparator_eo.translate_triple_list(triples)
        for triple in triples:
            if not list(obtained_graph.triples(triple)):
                errors.append(tuple([self.normalizeTerm(nsm, x) for x in triple]))
        return errors

    def find_forbidden(self, forbidden_graph, obtained_graph):
        """List triples in the forbidden graph found in the obtained graph
        and absent from the previous (stored) required graph.
        """
        assert self.last_comparator
        expected_graph = self.last_comparator.source
        errors = []
        nsm = obtained_graph.namespace_manager
        comparator_fo = GraphCorrespondence(forbidden_graph, obtained_graph)
        comparator_fo.identify()
        map_oe = dict(
            (o, e) for (e, o) in
            self.last_comparator.blank_map.iteritems())
        map_fe = dict(
            (f, map_oe.get(o, None)) for f, o in
            comparator_fo.blank_map.iteritems())
        triples = forbidden_graph.triples((None, None, None))
        for triple in triples:
            if list(obtained_graph.triples(comparator_fo.translate_triple(triple))) \
                    and not list(expected_graph.triples(translate_triple(triple, map_fe))):
                errors.append(tuple([self.normalizeTerm(nsm, x) for x in triple]))
        return errors

    def test_graph(self, graph, args=None):
        "Test that the provided graph is conformant"
        assert self.last_lom is not None
        errors = []
        if args and args.lower() == 'forbidden':
            assert(self.last_graph)
            errors = [(self.UNEXPECTED, e) for e in
                      self.find_forbidden(graph, self.last_graph)]
        else:
            if args:
                options = self.parser.parse_args(args.split())
                self.converter.set_options_from_dict(vars(options))
            else:
                self.converter.set_options_from_dict()
            obtained_graph = self.converter.lomxml2graph(self.last_lom)
            self.last_graph = obtained_graph
            errors = [(self.MISSING, e) for e in self.find_missing(graph, obtained_graph)]
        return self.last_graph, errors
Пример #2
0
class GraphTester(object):
    """Compares MLR graphs obtained from converting LOM to expected graphs.
    The graph tester is stateful.

    1. Receives a LOM fragment and stores it.
    2. Receives a required N3 MLR graph and LOM conversion parameters
        A. Converts the LOM fragment to a converted graph (with parameters.)
        B. Checks that all triples in the N3 graph are found in the converted
           graph. Stores the required graph.
    3. Receives a forbidden N3 MLR graph:
        Checks that all triples in the forbidden graph are either present in
        the required graph or absent from the converted graph.
    """
    MISSING = 1
    UNEXPECTED = 2

    def __init__(self):
        self.converter = Converter()
        self.parser = self.converter.populate_argparser()
        self.reset()

    def normalizeTerm(self, graph, term):
        if isinstance(term, URIRef):
            return graph.normalizeUri(term)
        return term

    def reset(self):
        "Clears the state"
        self.last_lom = None
        self.last_graph = None
        self.last_comparator = None

    def set_lom(self, lom):
        "Sets the LOM fragment."
        self.reset()
        self.last_lom = etree.fromstring(LOM_TEMPLATE % (lom, ))

    def process_line(self, format, code, args=None):
        "Process a fragment."
        format = format.lower()
        if format == 'xml':
            self.set_lom(code)
            return None, []
        elif format == 'n3':
            return self.test_n3(code, args)
        elif format == 'rdf-xml':
            return self.test_graph(Graph().parse(data=code, format="xml"),
                                   args)
        assert False, 'format should be xml or n3'

    def parse_n3(self, n3):
        "Parse a N3 graph"
        return Graph().parse(data=N3_PREFIXES + n3, format="n3")

    def test_n3(self, n3, args=None):
        "Test that the provided n3 fragment is conformant"
        return self.test_graph(self.parse_n3(n3), args)

    def find_missing(self, expected_graph, obtained_graph):
        "List triples in the expected graph missing from the obtained graph."
        errors = []
        nsm = obtained_graph.namespace_manager
        comparator_eo = GraphCorrespondence(expected_graph, obtained_graph)
        self.last_comparator = comparator_eo
        comparator_eo.identify()
        triples = expected_graph.triples((None, None, None))
        triples = comparator_eo.translate_triple_list(triples)
        for triple in triples:
            if not list(obtained_graph.triples(triple)):
                errors.append(
                    tuple([self.normalizeTerm(nsm, x) for x in triple]))
        return errors

    def find_forbidden(self, forbidden_graph, obtained_graph):
        """List triples in the forbidden graph found in the obtained graph
        and absent from the previous (stored) required graph.
        """
        assert self.last_comparator
        expected_graph = self.last_comparator.source
        errors = []
        nsm = obtained_graph.namespace_manager
        comparator_fo = GraphCorrespondence(forbidden_graph, obtained_graph)
        comparator_fo.identify()
        map_oe = dict(
            (o, e) for (e, o) in self.last_comparator.blank_map.iteritems())
        map_fe = dict((f, map_oe.get(o, None))
                      for f, o in comparator_fo.blank_map.iteritems())
        triples = forbidden_graph.triples((None, None, None))
        for triple in triples:
            if list(obtained_graph.triples(comparator_fo.translate_triple(triple))) \
                    and not list(expected_graph.triples(translate_triple(triple, map_fe))):
                errors.append(
                    tuple([self.normalizeTerm(nsm, x) for x in triple]))
        return errors

    def test_graph(self, graph, args=None):
        "Test that the provided graph is conformant"
        assert self.last_lom is not None
        errors = []
        if args and args.lower() == 'forbidden':
            assert (self.last_graph)
            errors = [(self.UNEXPECTED, e)
                      for e in self.find_forbidden(graph, self.last_graph)]
        else:
            if args:
                options = self.parser.parse_args(args.split())
                self.converter.set_options_from_dict(vars(options))
            else:
                self.converter.set_options_from_dict()
            obtained_graph = self.converter.lomxml2graph(self.last_lom)
            self.last_graph = obtained_graph
            errors = [(self.MISSING, e)
                      for e in self.find_missing(graph, obtained_graph)]
        return self.last_graph, errors