Skip to content

Commit

Permalink
Merge pull request #111 from phenoscape/issue-89
Browse files Browse the repository at this point in the history
Add missing recursion in NNF example.
  • Loading branch information
balhoff authored Jun 18, 2021
2 parents 8f03289 + e293e59 commit f61b914
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 37 deletions.
39 changes: 20 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 18 additions & 18 deletions src/main/scala/org/phenoscape/scowl/example/ReadMeExamples.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 _ => ???
}

/**
Expand Down

0 comments on commit f61b914

Please sign in to comment.