From e293e59dbcf181bce17149f27ec63df35c2892a2 Mon Sep 17 00:00:00 2001 From: Jim Balhoff Date: Fri, 18 Jun 2021 15:50:09 -0400 Subject: [PATCH] Add missing recursion in NNF example. --- README.md | 39 ++++++++++--------- .../scowl/example/ReadMeExamples.scala | 36 ++++++++--------- 2 files changed, 38 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index ef7d41f..206e0ff 100644 --- a/README.md +++ b/README.md @@ -73,25 +73,26 @@ manager.addAxioms(ontology, gcis) ### Using pattern matching extractors to implement negation normal form ```scala -def nnf(given: OWLClassExpression): OWLClassExpression = given match { - case Class(_) => given - case ObjectComplementOf(Class(_)) => given - case ObjectComplementOf(ObjectComplementOf(expression)) => nnf(expression) - case ObjectUnionOf(operands) => ObjectUnionOf(operands.map(nnf)) - case ObjectIntersectionOf(operands) => ObjectIntersectionOf(operands.map(nnf)) - case ObjectComplementOf(ObjectUnionOf(operands)) => ObjectIntersectionOf(operands.map(c => nnf(ObjectComplementOf(c)))) - case ObjectComplementOf(ObjectIntersectionOf(operands)) => ObjectUnionOf(operands.map(c => nnf(ObjectComplementOf(c)))) - case ObjectAllValuesFrom(property, filler) => ObjectAllValuesFrom(property, nnf(filler)) - case ObjectSomeValuesFrom(property, filler) => ObjectSomeValuesFrom(property, nnf(filler)) - case ObjectMinCardinality(num, property, filler) => ObjectMinCardinality(num, property, nnf(filler)) - case ObjectMaxCardinality(num, property, filler) => ObjectMaxCardinality(num, property, nnf(filler)) - case ObjectExactCardinality(num, property, filler) => ObjectExactCardinality(num, property, nnf(filler)) - case ObjectComplementOf(ObjectAllValuesFrom(property, filler)) => ObjectSomeValuesFrom(property, nnf(ObjectComplementOf(filler))) - case ObjectComplementOf(ObjectSomeValuesFrom(property, filler)) => ObjectAllValuesFrom(property, nnf(ObjectComplementOf(filler))) - case ObjectComplementOf(ObjectMinCardinality(num, property, filler)) => ObjectMaxCardinality(math.max(num - 1, 0), property, filler) - case ObjectComplementOf(ObjectMaxCardinality(num, property, filler)) => ObjectMinCardinality(num + 1, property, filler) - case ObjectComplementOf(ObjectExactCardinality(num, property, filler)) => ObjectUnionOf(ObjectMinCardinality(num + 1, property, filler), ObjectMaxCardinality(math.max(num - 1, 0), property, filler)) - } +def nnf(expression: OWLClassExpression): OWLClassExpression = expression match { + case Class(_) => expression + case ObjectComplementOf(Class(_)) => expression + case ObjectComplementOf(ObjectComplementOf(expression)) => nnf(expression) + case ObjectUnionOf(operands) => ObjectUnionOf(operands.map(nnf)) + case ObjectIntersectionOf(operands) => ObjectIntersectionOf(operands.map(nnf)) + case ObjectComplementOf(ObjectUnionOf(operands)) => ObjectIntersectionOf(operands.map(c => nnf(ObjectComplementOf(c)))) + case ObjectComplementOf(ObjectIntersectionOf(operands)) => ObjectUnionOf(operands.map(c => nnf(ObjectComplementOf(c)))) + case ObjectAllValuesFrom(property, filler) => ObjectAllValuesFrom(property, nnf(filler)) + case ObjectSomeValuesFrom(property, filler) => ObjectSomeValuesFrom(property, nnf(filler)) + case ObjectMinCardinality(num, property, filler) => ObjectMinCardinality(num, property, nnf(filler)) + case ObjectMaxCardinality(num, property, filler) => ObjectMaxCardinality(num, property, nnf(filler)) + case ObjectExactCardinality(num, property, filler) => ObjectExactCardinality(num, property, nnf(filler)) + case ObjectComplementOf(ObjectAllValuesFrom(property, filler)) => ObjectSomeValuesFrom(property, nnf(ObjectComplementOf(filler))) + case ObjectComplementOf(ObjectSomeValuesFrom(property, filler)) => ObjectAllValuesFrom(property, nnf(ObjectComplementOf(filler))) + case ObjectComplementOf(ObjectMinCardinality(num, property, filler)) => ObjectMaxCardinality(math.max(num - 1, 0), property, nnf(filler)) + case ObjectComplementOf(ObjectMaxCardinality(num, property, filler)) => ObjectMinCardinality(num + 1, property, nnf(filler)) + case ObjectComplementOf(ObjectExactCardinality(num, property, filler)) => ObjectUnionOf(ObjectMinCardinality(num + 1, property, nnf(filler)), ObjectMaxCardinality(math.max(num - 1, 0), property, nnf(filler))) + case _ => ??? +} ``` ### Using pattern matching extractors in for comprehensions diff --git a/src/main/scala/org/phenoscape/scowl/example/ReadMeExamples.scala b/src/main/scala/org/phenoscape/scowl/example/ReadMeExamples.scala index 182780f..a58863b 100644 --- a/src/main/scala/org/phenoscape/scowl/example/ReadMeExamples.scala +++ b/src/main/scala/org/phenoscape/scowl/example/ReadMeExamples.scala @@ -62,24 +62,24 @@ object ReadMeExamples { * Using pattern matching extractors to implement negation normal form */ def nnf(expression: OWLClassExpression): OWLClassExpression = expression match { - case Class(_) => expression - case ObjectComplementOf(Class(_)) => expression - case ObjectComplementOf(ObjectComplementOf(expression)) => nnf(expression) - case ObjectUnionOf(operands) => ObjectUnionOf(operands.map(nnf)) - case ObjectIntersectionOf(operands) => ObjectIntersectionOf(operands.map(nnf)) - case ObjectComplementOf(ObjectUnionOf(operands)) => ObjectIntersectionOf(operands.map(c => nnf(ObjectComplementOf(c)))) - case ObjectComplementOf(ObjectIntersectionOf(operands)) => ObjectUnionOf(operands.map(c => nnf(ObjectComplementOf(c)))) - case ObjectAllValuesFrom(property, filler) => ObjectAllValuesFrom(property, nnf(filler)) - case ObjectSomeValuesFrom(property, filler) => ObjectSomeValuesFrom(property, nnf(filler)) - case ObjectMinCardinality(num, property, filler) => ObjectMinCardinality(num, property, nnf(filler)) - case ObjectMaxCardinality(num, property, filler) => ObjectMaxCardinality(num, property, nnf(filler)) - case ObjectExactCardinality(num, property, filler) => ObjectExactCardinality(num, property, nnf(filler)) - case ObjectComplementOf(ObjectAllValuesFrom(property, filler)) => ObjectSomeValuesFrom(property, nnf(ObjectComplementOf(filler))) - case ObjectComplementOf(ObjectSomeValuesFrom(property, filler)) => ObjectAllValuesFrom(property, nnf(ObjectComplementOf(filler))) - case ObjectComplementOf(ObjectMinCardinality(num, property, filler)) => ObjectMaxCardinality(math.max(num - 1, 0), property, filler) - case ObjectComplementOf(ObjectMaxCardinality(num, property, filler)) => ObjectMinCardinality(num + 1, property, filler) - case ObjectComplementOf(ObjectExactCardinality(num, property, filler)) => ObjectUnionOf(ObjectMinCardinality(num + 1, property, filler), ObjectMaxCardinality(math.max(num - 1, 0), property, filler)) - case _ => ??? + case Class(_) => expression + case ObjectComplementOf(Class(_)) => expression + case ObjectComplementOf(ObjectComplementOf(expression)) => nnf(expression) + case ObjectUnionOf(operands) => ObjectUnionOf(operands.map(nnf)) + case ObjectIntersectionOf(operands) => ObjectIntersectionOf(operands.map(nnf)) + case ObjectComplementOf(ObjectUnionOf(operands)) => ObjectIntersectionOf(operands.map(c => nnf(ObjectComplementOf(c)))) + case ObjectComplementOf(ObjectIntersectionOf(operands)) => ObjectUnionOf(operands.map(c => nnf(ObjectComplementOf(c)))) + case ObjectAllValuesFrom(property, filler) => ObjectAllValuesFrom(property, nnf(filler)) + case ObjectSomeValuesFrom(property, filler) => ObjectSomeValuesFrom(property, nnf(filler)) + case ObjectMinCardinality(num, property, filler) => ObjectMinCardinality(num, property, nnf(filler)) + case ObjectMaxCardinality(num, property, filler) => ObjectMaxCardinality(num, property, nnf(filler)) + case ObjectExactCardinality(num, property, filler) => ObjectExactCardinality(num, property, nnf(filler)) + case ObjectComplementOf(ObjectAllValuesFrom(property, filler)) => ObjectSomeValuesFrom(property, nnf(ObjectComplementOf(filler))) + case ObjectComplementOf(ObjectSomeValuesFrom(property, filler)) => ObjectAllValuesFrom(property, nnf(ObjectComplementOf(filler))) + case ObjectComplementOf(ObjectMinCardinality(num, property, filler)) => ObjectMaxCardinality(math.max(num - 1, 0), property, nnf(filler)) + case ObjectComplementOf(ObjectMaxCardinality(num, property, filler)) => ObjectMinCardinality(num + 1, property, nnf(filler)) + case ObjectComplementOf(ObjectExactCardinality(num, property, filler)) => ObjectUnionOf(ObjectMinCardinality(num + 1, property, nnf(filler)), ObjectMaxCardinality(math.max(num - 1, 0), property, nnf(filler))) + case _ => ??? } /**