Skip to content

Commit

Permalink
Updated tests to scalatest.
Browse files Browse the repository at this point in the history
  • Loading branch information
balhoff committed Mar 21, 2016
1 parent 99bd2d3 commit 4944941
Showing 1 changed file with 72 additions and 123 deletions.
195 changes: 72 additions & 123 deletions src/test/scala/org/phenoscape/scowltest/ScowlTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,137 +4,78 @@ import scala.collection.JavaConversions._
import scala.language.postfixOps

import org.phenoscape.scowl._
import org.scalatest._
import org.semanticweb.owlapi.apibinding.OWLManager
import org.semanticweb.owlapi.model.IRI
import org.semanticweb.owlapi.vocab.OWLFacet
import org.semanticweb.owlapi.vocab.SWRLBuiltInsVocabulary
import org.semanticweb.owlapi.vocab.XSDVocabulary

class ScowlTest {
class ScowlTest extends UnitSpec {

val factory = OWLManager.getOWLDataFactory

def testIntersectionOf(): Unit = {
val class1 = Class("http://example.org/class1")
val class2 = Class("http://example.org/class2")
val class3 = Class("http://example.org/class3")
val oneAndTwo = class1 and class2
val longOneAndTwo = factory.getOWLObjectIntersectionOf(class1, class2)
//Assert.assertEquals(longOneAndTwo, oneAndTwo)
val oneAndTwoAndThree = class1 and class2 and class3
val longOneAndTwoAndThree = factory.getOWLObjectIntersectionOf(class1, class2, class3)
//Assert.assertEquals(longOneAndTwoAndThree, oneAndTwoAndThree)
}

def testComplementOf(): Unit = {
val class1 = Class("http://example.org/class1")
val notClass1 = not(class1)
val longNotClass1 = factory.getOWLObjectComplementOf(class1)
//Assert.assertEquals(longNotClass1, notClass1)
}

def testOneOf(): Unit = {
val ind1 = Individual("http://example.org/ind1")
val ind2 = Individual("http://example.org/ind2")
val ind3 = Individual("http://example.org/ind3")
val oneAndTwo = oneOf(ind1, ind2)
val oneAndTwoAndThree = ind1 ~ ind2 ~ ind3
val longOneAndTwo = factory.getOWLObjectOneOf(ind1, ind2)
//Assert.assertEquals(longOneAndTwo, oneAndTwo)
//Assert.assertEquals(factory.getOWLObjectOneOf(ind1), oneOf(ind1))
val class1 = Class("http://example.org/class1")
val class2 = Class("http://example.org/class2")
val class3 = Class("http://example.org/class3")
val ind1 = Individual("http://example.org/ind1")
val ind2 = Individual("http://example.org/ind2")
val ind3 = Individual("http://example.org/ind3")
val prop = ObjectProperty("http://example.org/prop")
val prop1 = ObjectProperty("http://example.org/prop1")
val prop2 = ObjectProperty("http://example.org/prop2")
val prop3 = ObjectProperty("http://example.org/prop3")
val prop4 = ObjectProperty("http://example.org/prop4")
val prop5 = DataProperty("http://example.org/prop5")

"Scowl class expressions" should "equal standard API" in {
(class1 and class2) should equal(factory.getOWLObjectIntersectionOf(class1, class2))
(class1 and class2 and class3) should equal(factory.getOWLObjectIntersectionOf(class1, class2, class3))
(org.phenoscape.scowl.not(class1)) should equal(factory.getOWLObjectComplementOf(class1))
org.phenoscape.scowl.oneOf(ind1, ind2) should equal(factory.getOWLObjectOneOf(ind1, ind2))
(ind1 ~ ind2 ~ ind3) should equal(factory.getOWLObjectOneOf(ind1, ind2, ind3))
(class1 and org.phenoscape.scowl.not(prop some class2)) should equal(factory.getOWLObjectIntersectionOf(class1, factory.getOWLObjectComplementOf(factory.getOWLObjectSomeValuesFrom(prop, class2))))
}

def testComplexExpressions(): Unit = {
val prop = ObjectProperty("http://example.org/prop")
val class1 = Class("http://example.org/class1")
val class2 = Class("http://example.org/class2")
val longClass = factory.getOWLObjectIntersectionOf(class1,
factory.getOWLObjectComplementOf(
factory.getOWLObjectSomeValuesFrom(prop, class2)))
val class1AndNotPropClass2a = class1 and not(prop some class2)
val class1AndNotPropClass2b = class1 and (not(prop some class2))
//Assert.assertEquals(longClass, class1AndNotPropClass2a)
// Assert.assertEquals(longClass, class1AndNotPropClass2b)
}

def testSubClassOf(): Unit = {
val class1 = Class("http://example.org/class1")
val class2 = Class("http://example.org/class2")
val prop = ObjectProperty("http://example.org/prop")
val axiom1 = class1 SubClassOf class2
val longAxiom1 = factory.getOWLSubClassOfAxiom(class1, class2)
//Assert.assertEquals(longAxiom1, axiom1)
}

def testHasSelf(): Unit = {
val loves = ObjectProperty("http://example.org/loves")
"Scowl axioms" should "equal standard API" in {
(class1 SubClassOf class2) should equal(factory.getOWLSubClassOfAxiom(class1, class2))
val Narcissist = Class("http://example.org/Narcissist")
val narcissismAxiom = Narcissist EquivalentTo (loves Self)
val longNarcissismAxiom = factory.getOWLEquivalentClassesAxiom(Narcissist, factory.getOWLObjectHasSelf(loves))
//Assert.assertEquals(longNarcissismAxiom, narcissismAxiom)
}

def testPropertyChains(): Unit = {
val prop1 = ObjectProperty("http://example.org/prop1")
val prop2 = ObjectProperty("http://example.org/prop2")
val prop3 = ObjectProperty("http://example.org/prop3")
val prop4 = ObjectProperty("http://example.org/prop4")
val chainAxiom1 = prop4 SubPropertyChain (prop1 o prop2)
val longChainAxiom1 = factory.getOWLSubPropertyChainOfAxiom(List(prop1, prop2), prop4)
//Assert.assertEquals(longChainAxiom1, chainAxiom1)
val chainAxiom2 = prop4 SubPropertyChain (prop1 o prop2 o prop3)
val longChainAxiom2 = factory.getOWLSubPropertyChainOfAxiom(List(prop1, prop2, prop3), prop4)
//Assert.assertEquals(longChainAxiom2, chainAxiom2)
val loves = ObjectProperty("http://example.org/loves")
(Narcissist EquivalentTo (loves Self)) should equal(factory.getOWLEquivalentClassesAxiom(Narcissist, factory.getOWLObjectHasSelf(loves)))
((ind1 Fact (prop1, ind2)) Annotation (RDFSLabel, AnonymousIndividual("urn:anon:1"))) should equal(factory.getOWLObjectPropertyAssertionAxiom(prop1, ind1, ind2, Set(factory.getOWLAnnotation(factory.getRDFSLabel, factory.getOWLAnonymousIndividual("urn:anon:1")))))
(ind1 SameAs ind2) should equal(factory.getOWLSameIndividualAxiom(ind1, ind2))
(ind1 SameAs (ind2, ind3)) should equal(factory.getOWLSameIndividualAxiom(ind1, ind2, ind3))
(ind1 DifferentFrom ind2) should equal(factory.getOWLDifferentIndividualsAxiom(ind1, ind2))
ind1 DifferentFrom (ind2, ind3) should equal(factory.getOWLDifferentIndividualsAxiom(ind1, ind2, ind3))
}

def testPropertyAssertions(): Unit = {
val rdfsLabel = factory.getRDFSLabel
val prop1 = ObjectProperty("http://example.org/prop1")
val ind1 = Individual("http://example.org/ind1")
val ind2 = Individual("http://example.org/ind2")
ind1 Fact (prop1, ind2) //TODO
(ind1 Fact (prop1, ind2)) Annotation (rdfsLabel, Individual())
"Scowl property chains" should "equal standard API" in {
(prop4 SubPropertyChain (prop1 o prop2)) should equal(factory.getOWLSubPropertyChainOfAxiom(List(prop1, prop2), prop4))
(prop4 SubPropertyChain (prop1 o prop2 o prop3)) should equal(factory.getOWLSubPropertyChainOfAxiom(List(prop1, prop2, prop3), prop4))
}

def testDataPropertiesAndLiterals(): Unit = {
"Scowl data properties and literals" should "equal standard API" in {
val hasAge = DataProperty("http://example.org/has_age")
val countryName = DataProperty("http://example.org/country_name")
val ind1 = Individual("http://example.org/ind1")
ind1 Fact (hasAge, "20" ^^ XSDInteger)
ind1 Fact (hasAge, "20.5" ^^ XSDDouble)
ind1 Fact (hasAge, 20.5)
ind1 Fact (countryName, "Republic of France" @@ "en")
(ind1 Fact (hasAge, "20" ^^ XSDInteger)) should equal(factory.getOWLDataPropertyAssertionAxiom(hasAge, ind1, factory.getOWLLiteral(20)))
(ind1 Fact (hasAge, "20.5" ^^ XSDDouble)) should equal(factory.getOWLDataPropertyAssertionAxiom(hasAge, ind1, factory.getOWLLiteral(20.5)))
(ind1 Fact (hasAge, 20.5)) should equal(factory.getOWLDataPropertyAssertionAxiom(hasAge, ind1, factory.getOWLLiteral(20.5)))
(ind1 Fact (countryName, "Republic of France" @@ "en")) should equal(factory.getOWLDataPropertyAssertionAxiom(countryName, ind1, factory.getOWLLiteral("Republic of France", "en")))
ind1 Fact (countryName, "République française" @@ "fr")
ind1 Annotation (RDFSLabel, "Robespierre" @@ "en")
(ind1 Annotation (RDFSLabel, "Robespierre" @@ "en")) should equal(factory.getOWLAnnotationAssertionAxiom(factory.getRDFSLabel, ind1.getIRI, factory.getOWLLiteral("Robespierre", "en")))
ind1 Annotation (RDFSLabel, "1" ^^ XSDInteger)

ind1 Type (hasAge some (XSDInteger(>(1))))
XSDInteger(>(1), <=(10)) //TODO
(ind1 Type (hasAge some (XSDInteger(org.phenoscape.scowl.>(1))))) should equal(factory.getOWLClassAssertionAxiom(factory.getOWLDataSomeValuesFrom(hasAge, factory.getOWLDatatypeMinExclusiveRestriction(1)), ind1))
(XSDInteger(org.phenoscape.scowl.>(1), org.phenoscape.scowl.<=(10))) should equal(factory.getOWLDatatypeRestriction(factory.getOWLDatatype(XSDVocabulary.INTEGER.getIRI), factory.getOWLFacetRestriction(OWLFacet.MIN_EXCLUSIVE, 1), factory.getOWLFacetRestriction(OWLFacet.MAX_INCLUSIVE, 10)))

val literalNumber = "20" ^^ XSDInteger
val lexicalForm ^^ datatype = literalNumber
datatype should equal(factory.getOWLDatatype(XSDVocabulary.INTEGER.getIRI))
(class1 HasKey (prop1)) should equal(factory.getOWLHasKeyAxiom(class1, prop1))
class1 HasKey (hasAge) should equal(factory.getOWLHasKeyAxiom(class1, hasAge))
(class1 HasKey (prop1, hasAge)) should equal(factory.getOWLHasKeyAxiom(class1, prop1, hasAge))
}

def testKeys(): Unit = {
val prop1 = ObjectProperty("http://example.org/prop1")
val prop2 = ObjectProperty("http://example.org/prop2")
val hasAge = DataProperty("http://example.org/has_age")
val class1 = Class("http://example.org/class1")
class1 HasKey (prop1)
class1 HasKey (hasAge)
class1 HasKey (prop1, hasAge)
}

def testSameAndDifferent(): Unit = {
val ind1 = Individual("http://example.org/ind1")
val ind2 = Individual("http://example.org/ind2")
val ind3 = Individual("http://example.org/ind3")
ind1 SameAs ind2
ind1 SameAs (ind2, ind3)
ind1 DifferentFrom ind2
ind1 DifferentFrom (ind2, ind3)

}

def testExtractors: Unit = {
val class1 = Class("http://example.org/class1")
val class2 = Class("http://example.org/class2")
"Extractors" should "extract the right things" in {
val axioms = Set(
class1 Annotation (RDFSLabel, "cat" @@ "en"),
class1 Annotation (RDFSLabel, "chat" @@ "fr"),
Expand All @@ -144,22 +85,30 @@ class ScowlTest {
val langValuePairs = for {
AnnotationAssertion(_, RDFSLabel, _, value @@ Some(lang)) <- axioms
} yield lang -> value
// Assert.assertEquals(Set("en" -> "cat", "fr" -> "chat"), langValuePairs)
langValuePairs should equal(Set("en" -> "cat", "fr" -> "chat"))
}

def testSWRL: Unit = {
val class1 = Class("http://example.org/class1")
val class2 = Class("http://example.org/class2")
val ind1 = Individual("http://example.org/ind1")
val ind2 = Individual("http://example.org/ind2")
val prop1 = ObjectProperty("http://example.org/prop1")
val prop2 = ObjectProperty("http://example.org/prop2")
val prop3 = DataProperty("http://example.org/prop3")
val res = (class1('x) ^ class1('y)) --> class2('x)
val res2 = class1('x) --> class2('x)
((class1('x) ^ class1('y)) --> class2('x)) Annotation (RDFSLabel, "X and Y rule")
"SWRL syntax" should "equal standard API" in {
(((class1('x) ^ class1('y)) --> class2('x)) Annotation (RDFSLabel, "X and Y rule")) should equal(
factory.getSWRLRule(
Set(
factory.getSWRLClassAtom(class1, factory.getSWRLVariable(IRI.create("urn:swrl#x"))),
factory.getSWRLClassAtom(class1, factory.getSWRLVariable(IRI.create("urn:swrl#y")))),
Set(
factory.getSWRLClassAtom(class2, factory.getSWRLVariable(IRI.create("urn:swrl#x")))),
Set(
factory.getOWLAnnotation(factory.getRDFSLabel, factory.getOWLLiteral("X and Y rule")))))
(prop1(ind1, 'x) ^ class1('x) ^ class2(ind1) ^ class1('y)) --> prop2('x, 'y)
(prop3(ind1, 42) ^ class1(ind1) ^ sameAs(ind1, ind2) ^ differentFrom(ind1, 'x) ^ swrlbAbs('x)) --> prop3(ind1, "text")
((prop5(ind1, 42) ^ class1(ind1) ^ sameAs(ind1, ind2) ^ differentFrom(ind1, 'x) ^ swrlbAbs('x)) --> prop5(ind1, "text")) should equal(
factory.getSWRLRule(
Set(
factory.getSWRLDataPropertyAtom(prop5, factory.getSWRLIndividualArgument(ind1), factory.getSWRLLiteralArgument(factory.getOWLLiteral(42))),
factory.getSWRLClassAtom(class1, factory.getSWRLIndividualArgument(ind1)),
factory.getSWRLSameIndividualAtom(factory.getSWRLIndividualArgument(ind1), factory.getSWRLIndividualArgument(ind2)),
factory.getSWRLDifferentIndividualsAtom(factory.getSWRLIndividualArgument(ind1), factory.getSWRLVariable(IRI.create("urn:swrl#x"))),
factory.getSWRLBuiltInAtom(SWRLBuiltInsVocabulary.ABS.getIRI, List(factory.getSWRLVariable(IRI.create("urn:swrl#x"))))),
Set(
factory.getSWRLDataPropertyAtom(prop5, factory.getSWRLIndividualArgument(ind1), factory.getSWRLLiteralArgument(factory.getOWLLiteral("text"))))))
}

}

0 comments on commit 4944941

Please sign in to comment.