@@ -402,6 +402,48 @@ def test_store():
402402 }
403403
404404
405+ def test_update_context ():
406+ """Test update_context()."""
407+ from tripper import HUME , OWL , Namespace
408+ from tripper .datadoc import get_context
409+ from tripper .datadoc .dataset import update_context
410+
411+ EX = Namespace ("http://example.com/" )
412+ sources = {
413+ "@context" : {
414+ "ex" : str (EX ),
415+ "hume" : str (HUME ),
416+ },
417+ "@graph" : [
418+ {
419+ # Instances are not added to context
420+ "@id" : "ex:instr" ,
421+ "@type" : "hume:Device" ,
422+ },
423+ {
424+ # Not added to context, since there is no @type
425+ "@id" : "ex:instr2" ,
426+ },
427+ {
428+ "@id" : "ex:MyDevice" ,
429+ "skos:prefLabel" : "MyDevice" ,
430+ "subClassOf" : "hume:Device" ,
431+ },
432+ ],
433+ }
434+ context = get_context (default_theme = None )
435+ update_context (sources , context )
436+ c = context .get_context_dict ()
437+ assert "instr" not in c
438+ assert "instr2" not in c
439+ assert "MyDevice" in c
440+ assert c ["MyDevice" ] == {"@id" : EX .MyDevice , "@type" : OWL .Class }
441+ assert c ["Device" ] == {"@id" : HUME .Device , "@type" : OWL .Class }
442+
443+ # TODO: add tests for what happens if there is mismatch between
444+ # previously added context and updated_context...
445+
446+
405447def test_infer_restriction_types ():
406448 """Test infer_restriction_types()."""
407449 from tripper import DCTERMS , HUME , RDFS , Namespace
@@ -426,7 +468,7 @@ def test_infer_restriction_types():
426468 "http://example.org#A" : {
427469 DCTERMS .creator : "some" ,
428470 DCTERMS .hasPart : "value" ,
429- DCTERMS .issued : "value" ,
471+ # DCTERMS.issued: "value",
430472 }
431473 }
432474
@@ -452,7 +494,7 @@ def test_infer_restriction_types():
452494 "@id" : "ex:MyDevice" ,
453495 # "@type": "owl:Class",
454496 "subClassOf" : HUME .Device ,
455- "hasPart" : HUME .MeasuringInstrument ,
497+ "hasPart" : [ HUME .MeasuringInstrument , "ex:MyDevice" ] ,
456498 },
457499 ],
458500 }
@@ -577,6 +619,13 @@ def test_update_restrictions():
577619 "@type" : HUME .Device ,
578620 "isDefinedBy" : HUME .MeasuringInstrument ,
579621 },
622+ {
623+ # An individial relating to two classes and an individual.
624+ # Should be converted to an existential restriction.
625+ "@id" : "ex:instr3" ,
626+ "@type" : HUME .Device ,
627+ "hasPart" : [HUME .MeasuringInstrument , "MyDevice" , "ex:instr" ],
628+ },
580629 {
581630 # A class relating to a class.
582631 # Should be converted to an existential restriction.
@@ -586,63 +635,68 @@ def test_update_restrictions():
586635 "@id" : "ex:MyDevice" ,
587636 # "@type": "owl:Class",
588637 "subClassOf" : HUME .Device ,
638+ "label" : "MyDevice" ,
589639 "hasPart" : HUME .MeasuringInstrument ,
590640 },
641+ {
642+ # A class relating to two classes
643+ "@id" : "ex:MyDevice2" ,
644+ "@type" : "owl:Class" ,
645+ "subClassOf" : HUME .Device ,
646+ "label" : "MyDevice2" ,
647+ "hasPart" : [HUME .MeasuringInstrument , "MyDevice" ],
648+ },
649+ # TODO: for completeness, add tests for individual
650+ # relating to one individual and individual related to a
651+ # list of individuals
591652 ],
592653 }
593654 r6 = deepcopy (d6 )
594655 update_restrictions (r6 , ctx )
595- assert r6 == {
596- "@context" : {
597- "MeasuringInstrument" : {
598- "@id" : "https://w3id.org/emmo/hume#MeasuringInstrument" ,
599- "@type" : "owl:Class" ,
600- }
601- },
602- "@graph" : [
603- {
604- "@id" : "ex:instr" ,
605- "@type" : "https://w3id.org/emmo/hume#Device" ,
606- "isDefinedBy" : "https://w3id.org/emmo/hume#MeasuringSystem" ,
607- },
656+ res6 = {d ["@id" ]: d for d in r6 ["@graph" ]}
657+ assert res6 ["ex:instr" ] == {
658+ "@id" : "ex:instr" ,
659+ "@type" : "https://w3id.org/emmo/hume#Device" ,
660+ "isDefinedBy" : "https://w3id.org/emmo/hume#MeasuringSystem" ,
661+ }
662+ assert res6 ["ex:instr2" ] == {
663+ "@id" : "ex:instr2" ,
664+ "@type" : [
665+ "https://w3id.org/emmo/hume#Device" ,
608666 {
609- "@id" : "ex:instr2" ,
610- "@type" : [
611- "https://w3id.org/emmo/hume#Device" ,
612- {
613- "@type" : "owl:Restriction" ,
614- "owl:onProperty" : {
615- "@id" : (
616- "http://www.w3.org/2000/01/rdf-schema#"
617- "isDefinedBy"
618- )
619- },
620- "owl:someValuesFrom" : {
621- "@id" : (
622- "https://w3id.org/emmo/hume#MeasuringInstrument"
623- )
624- },
625- },
626- ],
667+ "@type" : "owl:Restriction" ,
668+ "owl:onProperty" : {
669+ "@id" : "http://www.w3.org/2000/01/rdf-schema#isDefinedBy" ,
670+ },
671+ "owl:someValuesFrom" : {
672+ "@id" : "https://w3id.org/emmo/hume#MeasuringInstrument" ,
673+ },
627674 },
675+ ],
676+ }
677+ assert res6 ["ex:instr3" ] == {
678+ # WRONG! Should be converted to restrictions
679+ "@id" : "ex:instr3" ,
680+ "@type" : "https://w3id.org/emmo/hume#Device" ,
681+ "hasPart" : [
682+ "https://w3id.org/emmo/hume#MeasuringInstrument" ,
683+ "MyDevice" ,
684+ "ex:instr" ,
685+ ],
686+ }
687+ assert res6 ["ex:MyDevice" ] == {
688+ "@id" : "ex:MyDevice" ,
689+ "subClassOf" : [
690+ "https://w3id.org/emmo/hume#Device" ,
628691 {
629- "@id" : "ex:MyDevice" ,
630- "subClassOf" : [
631- "https://w3id.org/emmo/hume#Device" ,
632- {
633- "@type" : "owl:Restriction" ,
634- "owl:onProperty" : {
635- "@id" : "http://purl.org/dc/terms/hasPart"
636- },
637- "owl:someValuesFrom" : {
638- "@id" : (
639- "https://w3id.org/emmo/hume#MeasuringInstrument"
640- )
641- },
642- },
643- ],
692+ "@type" : "owl:Restriction" ,
693+ "owl:onProperty" : {"@id" : "http://purl.org/dc/terms/hasPart" },
694+ "owl:someValuesFrom" : {
695+ "@id" : "https://w3id.org/emmo/hume#MeasuringInstrument"
696+ },
644697 },
645698 ],
699+ "label" : "MyDevice" ,
646700 }
647701
648702
0 commit comments