Exemple #1
0
 def to_functional(self,
                   w: Optional[FunctionalWriter] = None
                   ) -> FunctionalWriter:
     """ Return a FunctionalWriter instance with the representation of the OntologyDocument in functional syntax """
     w = w or FunctionalWriter()
     self.add_namespaces(w.g)
     return w.iter([Prefix(ns, uri) for ns, uri in w.g.namespaces()], indent=False).hardbr() +\
            (self.ontology or Ontology())
    def test_join_functional_outputs(self):
        class T1(FunOwlRoot):
            v: IRI

            def __init__(self, v: str) -> None:
                self.v = EX[v]

            def to_functional(self, w: FunctionalWriter) -> FunctionalWriter:
                return w + self.v

        wt = FunctionalWriter()
        wt.bind('ex', EX)
        wt + "The values are:"
        T1("I1").to_functional(wt)
        T1("I2").to_functional(wt)
        T1("I3").to_functional(wt)
        self.assertEqual("The values are: ex:I1 ex:I2 ex:I3", wt.getvalue())
Exemple #3
0
 def to_functional(self, w: Optional[FunctionalWriter]) -> FunctionalWriter:
     """ Return a FunctionalWriter instance with the representation of the ontology in functional syntax """
     if self.version and not self.iri:
         raise ValueError(f"Ontology cannot have a versionIRI ({self.version} without an ontologyIRI")
     w = w or FunctionalWriter()
     return w.func(self, lambda: w.opt(self.iri).opt(self.version).
                   br(bool(self.directlyImportsDocuments) or bool(self.annotations) or bool(self.axioms)).
                   iter(self.directlyImportsDocuments, indent=False).iter(self.annotations, indent=False).
                   iter(self.axioms, indent=False), indent=False)
Exemple #4
0
 def annots(self, w: FunctionalWriter,
            f: Callable[[], FunctionalWriter]) -> FunctionalWriter:
     """
     Emit the declaration of an annotatable function
     :param w: FunctionWriter
     :param f: function to generate post annotation function content
     :return: FUnctionWriter instance
     """
     return w.func(self, lambda: self._add_annotations(w, f))
Exemple #5
0
 def _add_annotations(
         self,
         w: FunctionalWriter,
         f: Callable[[], FunctionalWriter] = None) -> FunctionalWriter:
     """
     Emit annotations at the beginnng of a functional declaration.  On entry, we've emitted "Func(" and
     need to emit zero or more "    Annotation( ... )" entries - one for each annotation
     :param w: FunctionalWriter to append output to
     :param f: function to generate everything within "func" after the annotations
     :return: FunctionalWriter instance
     """
     if self.annotations:
         w.br()
         w.iter(self.annotations, indent=False)
         w.br().indent()
     f()
     if self.annotations:
         w.outdent()
     return w
Exemple #6
0
This is an example ontology that contains all constructs required for the various versions of the Pizza Tutorial
 run by Manchester University 
 (see http://owl.cs.manchester.ac.uk/publications/talks-and-tutorials/protg-owl-tutorial).""",
                lang="en")))
pizza.axioms.append(SubObjectPropertyOf(PIZZA.hasBase, PIZZA.hasIngredient))
pizza.axioms.append(InverseObjectProperties(PIZZA.hasBase, PIZZA.isBaseOf))
pizza.axioms.append(FunctionalObjectProperty(PIZZA.hasBase))
pizza.axioms.append(InverseFunctionalObjectProperty(PIZZA.hasBase))
pizza.axioms.append(ObjectPropertyDomain(PIZZA.hasBase, PIZZA.Pizza))
pizza.axioms.append(ObjectPropertyRange(PIZZA.hasBase, PIZZA.PizzaBase))

print(pizza_doc.to_functional().getvalue())

#%% md

## Ontologies in functional format can be read from files or URL's

#%%
from funowl.converters.functional_converter import to_python
from funowl.writers.FunctionalWriter import FunctionalWriter

pizza_doc = to_python(
    "https://raw.githubusercontent.com/hsolbrig/funowl/master/tests/data/pizza.owl"
)
w = FunctionalWriter(g=pizza_doc.add_namespaces(Graph()))
for axiom in pizza_doc.ontology.axioms:
    if isinstance(axiom, SubClassOf) and str(
            axiom.subClassExpression) == 'pizza:AmericanHot':
        w.add(axiom)
print(str(w))
Exemple #7
0
 def to_functional2(self, wr: FunctionalWriter) -> FunctionalWriter:
     return wr.hardbr().indent().iter(self.v).outdent()
Exemple #8
0
 def to_functional(self, w: FunctionalWriter) -> FunctionalWriter:
     return w.func(self, lambda: w.br().indent().concat(self.instnum, ':', sep='').outdent().iter(self.foos))
Exemple #9
0
 def to_functional(self, w: FunctionalWriter) -> FunctionalWriter:
     return w.func(self, lambda: w.iter(self.objectPropertyExpressions))
Exemple #10
0
 def setUp(self) -> None:
     self.sw = FunctionalWriter()
     self.sw.bind(None, SCT)
 def to_functional(self, w: FunctionalWriter) -> FunctionalWriter:
     self.list_cardinality(self.dataPropertyExpressions, 'exprs', 2)
     return self.annots(w, lambda: w.iter(self.dataPropertyExpressions))
Exemple #12
0
 def to_functional(self, w: FunctionalWriter) -> FunctionalWriter:
     self.list_cardinality(self.dataRanges, 'dataRange', 2)
     return w.func(self, lambda: w.iter(self.dataRanges))
Exemple #13
0
 def to_functional(self, w: FunctionalWriter) -> FunctionalWriter:
     return w.func(self, lambda:
                   (w + self.datatype).iter(self.restrictions))
Exemple #14
0
 def to_functional(self, w: FunctionalWriter) -> FunctionalWriter:
     return w.concat(self.literal, self.language)
Exemple #15
0
 def to_functional(self, w: FunctionalWriter) -> FunctionalWriter:
     return w.func(self, lambda: w + self.dataRange)
Exemple #16
0
 def to_functional(self, w: FunctionalWriter) -> FunctionalWriter:
     return w.concat(self.literal, '^^', self.datatype)
Exemple #17
0
 def to_functional(self, w: FunctionalWriter) -> FunctionalWriter:
     return w.func(self, lambda: w.iter(self.literal))
Exemple #18
0
class ClassAxiomsTestCase(TestBase):
    def setUp(self) -> None:
        self.sw = FunctionalWriter()
        self.sw.bind(None, SCT)

    def test_subclass_of(self):
        # print(SubClassOf(Annotation(RDFS.comment, "Just a test"), ))
        pass

    def test_equivalentclasses(self):
        self.assertEqual(
            """EquivalentClasses(
    <http://snomed.info/id/303394007>
    <http://snomed.info/id/45189000>
    <http://snomed.info/id/609096000>
)""",
            str(
                EquivalentClasses(SCT['303394007'], SCT['45189000'],
                                  SCT['609096000']).to_functional(self.sw)))

        with self.assertRaises(ValueError,
                               msg="at least 2 arguments are required"):
            str(EquivalentClasses(SCT['303394007']).to_functional(self.sw))

        # Taken from SNOMED CT
        self.assertEqual(
            """EquivalentClasses(
    <http://snomed.info/id/303394007>
        ObjectIntersectionOf(
        <http://snomed.info/id/45189000>
            ObjectSomeValuesFrom( <http://snomed.info/id/609096000>     ObjectIntersectionOf(
            ObjectSomeValuesFrom( <http://snomed.info/id/260686004> <http://snomed.info/id/129397003> )
            ObjectSomeValuesFrom( <http://snomed.info/id/363700003> <http://snomed.info/id/52988006> )
            ObjectSomeValuesFrom( <http://snomed.info/id/405813007> <http://snomed.info/id/69695003> )
    ) )
    )
)""",
            str(
                EquivalentClasses(
                    SCT['303394007'],
                    ObjectIntersectionOf(
                        SCT['45189000'],
                        ObjectSomeValuesFrom(
                            SCT['609096000'],
                            ObjectIntersectionOf(
                                ObjectSomeValuesFrom(SCT['260686004'],
                                                     SCT['129397003']),
                                ObjectSomeValuesFrom(SCT['363700003'],
                                                     SCT['52988006']),
                                ObjectSomeValuesFrom(
                                    SCT['405813007'],
                                    SCT['69695003']))))).to_functional(
                                        self.sw.reset())))

    def test_disjointclasses(self):
        self.assertEqual(
            """DisjointClasses(
    <http://snomed.info/id/303394007>
    <http://snomed.info/id/45189000>
    <http://snomed.info/id/609096000>
)""",
            str(
                DisjointClasses(SCT['303394007'], SCT['45189000'],
                                SCT['609096000']).to_functional(self.sw)))

    def test_disjointunion(self):
        self.assertEqual(
            """DisjointUnion( <http://snomed.info/id/12345>
    <http://snomed.info/id/303394007>
    <http://snomed.info/id/45189000>
    <http://snomed.info/id/609096000>
)""",
            str(
                DisjointUnion(SCT['12345'], SCT['303394007'], SCT['45189000'],
                              SCT['609096000']).to_functional(
                                  self.sw.reset())))
        with self.assertRaises(ValueError,
                               msg="Have to have at least 2 expressions"):
            DisjointUnion(SCT['12345'],
                          SCT['303394007']).to_functional(self.sw)

    def test_haskey(self):
        self.assertEqual(
            '''HasKey( <http://snomed.info/id/12345> (
    <http://snomed.info/id/23456>
    <http://snomed.info/id/23457>
) (
    <http://snomed.info/id/23458>
    <http://snomed.info/id/23459>
) )''',
            str(
                HasKey(SCT['12345'], ObjectPropertyExpression(SCT['23456']),
                       ObjectPropertyExpression(SCT['23457']),
                       DataPropertyExpression(SCT['23458']),
                       DataPropertyExpression(SCT['23459'])).to_functional(
                           self.sw.reset())))
 def to_functional(self, w: FunctionalWriter) -> FunctionalWriter:
     return w.func(self, lambda: w.concat((self.prefixName or '') + ':', '=',
                                          URIRef(str(self.fullIRI)).n3(), sep=' '))
Exemple #20
0
 def to_functional(self, w: FunctionalWriter) -> FunctionalWriter:
     self.list_cardinality(self.objectPropertyExpressions, 'expressions', 2,
                           2)
     return w.func(
         self, lambda: w + self.objectPropertyExpressions[0] + self.
         objectPropertyExpressions[1])
 def to_functional(self, w: FunctionalWriter) -> FunctionalWriter:
     return w.iter(self.as_prefixes(), indent=False)
Exemple #22
0
 def to_functional(self, wr: FunctionalWriter) -> FunctionalWriter:
     return wr.iter(self.v1)
Exemple #23
0
class ClassAxiomsTestCase(TestBase):
    def setUp(self) -> None:
        self.sw = FunctionalWriter()
        self.sw.bind(None, SCT)

    def test_equivalentclasses(self):
        self.assertEqual(
            """EquivalentClasses(
    :303394007
    :45189000
    :609096000
)""",
            str(
                EquivalentClasses(SCT['303394007'], SCT['45189000'],
                                  SCT['609096000']).to_functional(self.sw)))

        with self.assertRaises(ValueError,
                               msg="at least 2 arguments are required"):
            str(EquivalentClasses(SCT['303394007']).to_functional(self.sw))

        # Taken from SNOMED CT
        self.assertEqual(
            """EquivalentClasses(
    :303394007
        ObjectIntersectionOf(
        :45189000
            ObjectSomeValuesFrom( :609096000     ObjectIntersectionOf(
            ObjectSomeValuesFrom( :260686004 :129397003 )
            ObjectSomeValuesFrom( :363700003 :52988006 )
            ObjectSomeValuesFrom( :405813007 :69695003 )
    ) )
    )
)""",
            str(
                EquivalentClasses(
                    SCT['303394007'],
                    ObjectIntersectionOf(
                        SCT['45189000'],
                        ObjectSomeValuesFrom(
                            SCT['609096000'],
                            ObjectIntersectionOf(
                                ObjectSomeValuesFrom(SCT['260686004'],
                                                     SCT['129397003']),
                                ObjectSomeValuesFrom(SCT['363700003'],
                                                     SCT['52988006']),
                                ObjectSomeValuesFrom(
                                    SCT['405813007'],
                                    SCT['69695003']))))).to_functional(
                                        self.sw.reset())))

    def test_oio(self):
        """ Bug: ObjectIntersectionOf ends up being a single argument to ObjectSomeValuesOf """
        self.assertEqual(
            """ObjectIntersectionOf(
    :45189000
        ObjectSomeValuesFrom( :609096000     ObjectUnionOf(
        :1
        :2
    ) )
)""",
            str(
                ObjectIntersectionOf(
                    SCT['45189000'],
                    ObjectSomeValuesFrom(SCT['609096000'],
                                         ObjectUnionOf(
                                             SCT['1'],
                                             SCT['2']))).to_functional(
                                                 self.sw.reset())))

    def test_disjointclasses(self):
        self.assertEqual(
            """DisjointClasses(
    :303394007
    :45189000
    :609096000
)""",
            str(
                DisjointClasses(SCT['303394007'], SCT['45189000'],
                                SCT['609096000']).to_functional(self.sw)))

    def test_disjointunion(self):
        self.assertEqual(
            """DisjointUnion( :12345
    :303394007
    :45189000
    :609096000
)""",
            str(
                DisjointUnion(SCT['12345'], SCT['303394007'], SCT['45189000'],
                              SCT['609096000']).to_functional(
                                  self.sw.reset())))
        with self.assertRaises(ValueError,
                               msg="Have to have at least 2 expressions"):
            DisjointUnion(SCT['12345'],
                          SCT['303394007']).to_functional(self.sw)

    def test_haskey(self):
        self.assertEqual(
            '''HasKey( :12345 (
    :23456
    :23457
) (
    :23458
    :23459
) )''',
            str(
                HasKey(SCT['12345'], ObjectPropertyExpression(SCT['23456']),
                       ObjectPropertyExpression(SCT['23457']),
                       DataPropertyExpression(SCT['23458']),
                       DataPropertyExpression(SCT['23459'])).to_functional(
                           self.sw.reset())))
Exemple #24
0
 def to_functional(self, wr: FunctionalWriter) -> FunctionalWriter:
     return wr.iter(self.v, f=lambda e:  wr.hardbr() + e)
Exemple #25
0
 def to_functional(self, w: FunctionalWriter) -> FunctionalWriter:
     return w.func(self, lambda: w.func(self.v, lambda: self.v.to_functional(w), indent=False))
Exemple #26
0
 def setUpClass(cls) -> None:
     cls.w = FunctionalWriter()
     cls.wa = FunctionalWriter()
     cls.wa.bind('a', A)
Exemple #27
0
 def to_functional(self, w: FunctionalWriter) -> FunctionalWriter:
     return w.concat(str(self), sep='')
Exemple #28
0
 def to_functional(self, w: FunctionalWriter) -> FunctionalWriter:
     return w.func(self, lambda: (w + self.iri))
Exemple #29
0
 def test_base_declaration(self):
     decl = Declaration(Class(PIZZA.American))
     w = FunctionalWriter()
     self.assertEqual(
         "Declaration( Class( <http://www.co-ode.org/ontologies/pizza/pizza.owl#American> ) )",
         str(decl.to_functional(w)))