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
def __init__(self): self.converter = Converter() self.parser = self.converter.populate_argparser() self.reset()
class testMlr(unittest.TestCase): @classmethod def setupClass(self): self.converter = Converter() self.graph = self.converter.lomfile2graph(EXAMPLE) def triple_from_n3(self, n3): g = Graph().parse(data=n3, format="n3") return next(g.triples((None, None, None))) def test_not_empty(self): assert (len(self.graph) > 0) def test_has_lang(self): expected = '%s <%s> mlr3:DES0500 "fra-CA".' % (prologue, TEST_ID) assert (len(list(self.graph.triples(self.triple_from_n3(expected))))) def test_has_all_mlr_values(self): triples = list(self.graph.triples((term.URIRef(TEST_ID), None, None))) #sys.stderr.write(`triples`) predicates = set(p for (s, p, o) in triples) missing_predicates = [] for (p, n) in Element_names.iteritems(): if p in Known_Missing: continue if p not in predicates: missing_predicates.append((p, n)) assert not missing_predicates, missing_predicates def test_has_only_mlr_values(self): triples = list(self.graph.triples((term.URIRef(TEST_ID), None, None))) predicates = set(p for (s, p, o) in triples) extra_predicates = [] for p in predicates: if p == term.URIRef('http://www.inria.fr/acacia/corese#graph'): continue if not p in Element_names: extra_predicates.append(p) assert not extra_predicates, extra_predicates def test_codomain(self): wrong_codomain_type = [] for predicate in MLR_codomain.iterkeys(): for s, p, o in self.graph.triples( (None, term.URIRef(predicate), None)): for s2, p2, o2 in self.graph.triples((o, RDF.type, None)): if o2 not in MLR_codomain[predicate]: wrong_codomain_type.append((predicate, o2)) assert not wrong_codomain_type, wrong_codomain_type def test_subobjects_has_only_mlr_values(self): extra_predicates = [] for s, p, o in self.graph.triples((None, RDF.type, None)): if o == MLR1.RC0002: continue assert o in MLR_Subclass_attributes, "Missing type:" + o attributes = MLR_Subclass_attributes[o] for s2, p2, o2 in self.graph.triples((s, None, None)): if RDF.type == p2: continue if not p2 in attributes: extra_predicates.append((o, p2)) assert not extra_predicates, extra_predicates def test_subobjects_has_all_mlr_values(self): missing_types = [] missing_predicates = [] res_by_type = defaultdict(set) pred_by_type = defaultdict(set) for s, p, o in self.graph.triples((None, RDF.type, None)): res_by_type[o].add(s) for type_uri, ressources in res_by_type.iteritems(): for r in ressources: for s, p, o in self.graph.triples((r, None, None)): pred_by_type[type_uri].add(p) for type_uri, predicates in pred_by_type.iteritems(): if type_uri == MLR1.RC0002: continue if type_uri not in MLR_Subclass_attributes: missing_types.append(type_uri) continue attributes = MLR_Subclass_attributes[type_uri] for p, name in attributes.iteritems(): if p in Known_Missing: continue if not p in predicates: missing_predicates.append((type_uri, p, name)) assert not missing_types, missing_types assert not missing_predicates, missing_predicates def test_value_types(self): triples = list(self.graph.triples((None, None, None))) for s, p, o in triples: if p in Type_constraints: assert Type_constraints[p](str(o)) def test_lom_files(self): ex_dir = os.path.join(this_dir, 'data') examples = os.listdir(ex_dir) all_clear = True for example in examples: print(example) graph = self.converter.lomfile2graph(os.path.join(ex_dir, example))
def setupClass(self): self.converter = Converter() self.graph = self.converter.lomfile2graph(EXAMPLE)
def __init__(self): self.converter = Converter() parser = argparse.ArgumentParser(description="Graph Comparison") self.parser = self.converter.populate_argparser(parser) self.reset()
class testMlr(unittest.TestCase): @classmethod def setupClass(self): self.converter = Converter() self.graph = self.converter.lomfile2graph(EXAMPLE) def triple_from_n3(self, n3): g = Graph().parse(data=n3, format="n3") return next(g.triples((None,None,None))) def test_not_empty(self): assert(len(self.graph)>0) def test_has_lang(self): expected = '%s <%s> mlr3:DES0500 "fra-CA".' % (prologue, TEST_ID) assert(len(list(self.graph.triples(self.triple_from_n3(expected))))) def test_has_all_mlr_values(self): triples = list(self.graph.triples((term.URIRef(TEST_ID), None, None))) #sys.stderr.write(`triples`) predicates = set(p for (s, p, o) in triples) missing_predicates = [] for (p, n) in Element_names.iteritems(): if p in Known_Missing: continue if p not in predicates: missing_predicates.append((p, n)) assert not missing_predicates, missing_predicates def test_has_only_mlr_values(self): triples = list(self.graph.triples((term.URIRef(TEST_ID), None, None))) predicates = set(p for (s, p, o) in triples) extra_predicates = [] for p in predicates: if p == term.URIRef('http://www.inria.fr/acacia/corese#graph'): continue if not p in Element_names: extra_predicates.append(p) assert not extra_predicates, extra_predicates def test_codomain(self): wrong_codomain_type = [] for predicate in MLR_codomain.iterkeys(): for s, p, o in self.graph.triples((None, term.URIRef(predicate), None)): for s2, p2, o2 in self.graph.triples((o, RDF.type, None)): if o2 not in MLR_codomain[predicate]: wrong_codomain_type.append((predicate, o2)) assert not wrong_codomain_type, wrong_codomain_type def test_subobjects_has_only_mlr_values(self): extra_predicates = [] for s, p, o in self.graph.triples((None, RDF.type, None)): if o == MLR1.RC0002: continue assert o in MLR_Subclass_attributes, "Missing type:" + o attributes = MLR_Subclass_attributes[o] for s2, p2, o2 in self.graph.triples((s, None, None)): if RDF.type == p2: continue if not p2 in attributes: extra_predicates.append((o, p2)) assert not extra_predicates, extra_predicates def test_subobjects_has_all_mlr_values(self): missing_types = [] missing_predicates = [] res_by_type = defaultdict(set) pred_by_type = defaultdict(set) for s, p, o in self.graph.triples((None, RDF.type, None)): res_by_type[o].add(s) for type_uri, ressources in res_by_type.iteritems(): for r in ressources: for s, p, o in self.graph.triples((r, None, None)): pred_by_type[type_uri].add(p) for type_uri, predicates in pred_by_type.iteritems(): if type_uri == MLR1.RC0002: continue if type_uri not in MLR_Subclass_attributes: missing_types.append(type_uri) continue attributes = MLR_Subclass_attributes[type_uri] for p, name in attributes.iteritems(): if p in Known_Missing: continue if not p in predicates: missing_predicates.append((type_uri, p, name)) assert not missing_types, missing_types assert not missing_predicates, missing_predicates def test_value_types(self): triples = list(self.graph.triples((None, None, None))) for s, p, o in triples: if p in Type_constraints: assert Type_constraints[p](str(o)) def test_lom_files(self): ex_dir = os.path.join(this_dir, 'data') examples = os.listdir(ex_dir) all_clear = True for example in examples: print(example) graph = self.converter.lomfile2graph(os.path.join(ex_dir, example))
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