package tudelft.healthpsychology.traumaontologies.owltree; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import java.io.File; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; import org.junit.Before; import org.junit.Test; import org.semanticweb.owlapi.apibinding.OWLManager; import org.semanticweb.owlapi.model.OWLOntology; import org.semanticweb.owlapi.model.OWLOntologyCreationException; import org.semanticweb.owlapi.model.OWLOntologyManager; import tudelft.healthpsychology.traumaontologies.answerstate.Property; import tudelft.healthpsychology.traumaontologies.questiontypes.TypedQuestion; import tudelft.utilities.junit.GeneralTests; import tudelft.utilities.tree.TreeNode; public class OwlOntologyNodeTest extends GeneralTests { private static final String MEUBEL = "Meubel"; private static final File ONTO_FILE = new File( "src/main/resources/Child Sexual Abuse.owl"); private OWLOntology ontology; private OwlTreeReasoner reasoner; private OwlOntologyNode node1, node1a, node2, node3; @Before public void before() throws OWLOntologyCreationException { OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); ontology = manager.loadOntologyFromOntologyDocument(ONTO_FILE); reasoner = new OwlTreeReasoner(ontology); node1 = new OwlOntologyNode(reasoner.getObject("Bed"), reasoner); node1a = new OwlOntologyNode(reasoner.getObject("Bed"), reasoner); node2 = new OwlOntologyNode(reasoner.getObject("Kast"), reasoner); node3 = new OwlOntologyNode(reasoner.getObject("Meubel"), reasoner); } @Override public List> getGeneralTestData() { return Arrays.asList(Arrays.asList(node1, node1a), Arrays.asList(node2), Arrays.asList(node3)); } @Override public List getGeneralTestStrings() { return Arrays.asList("Node\\[\\<.*.owl#Bed\\>\\]", "Node\\[\\<.*.owl#Kast\\>\\]", "Node\\[\\<.*.owl#Meubel\\>\\]"); } @Test public void smokeTest() { new OwlOntologyNode(reasoner.getObject("Bed"), reasoner); } @Test public void smokeTest1() throws OWLOntologyCreationException { OwlOntologyNode.fromFile("Object", ONTO_FILE); } @Test public void getAttrTest() { OwlOntologyNode node = new OwlOntologyNode(reasoner.getObject("Bed"), reasoner); List attris = node.getAttribute(); assertEquals(3, attris.size()); System.out.println(attris); } @Test public void getMeubelQuestionsTest() { OwlOntologyNode node = new OwlOntologyNode(reasoner.getObject("Meubel"), reasoner); List attris = node.getAttribute(); System.out.println(attris); assertEquals(2, attris.size()); // Beschrijving_voorwerp and // Locatie_meubel Set questions = attris.stream() .map(attri -> attri.getQuestionType().getQuestion()) .collect(Collectors.toSet()); System.out.println(questions); assertTrue(questions.contains( "Kan je beschrijven waar in de ruimte dit meubel stond?")); assertTrue(questions.contains( "Beschrijf nu hoe dit voorwerp eruit zag, en waarom je je deze is bijgebleven.")); } @Test public void getPersoonAttrTest() { OwlOntologyNode node = new OwlOntologyNode( reasoner.getObject("Persoon"), reasoner); List attris = node.getAttribute(); assertEquals(2, attris.size()); Set questions = attris.stream() .map(attri -> attri.getQuestionType().getQuestion()) .collect(Collectors.toSet()); assertTrue(questions.contains("Wat is het geslacht van [Naam]?")); assertTrue(questions .contains("Geef de naam van een persoon op de locatie.")); } @Test(expected = IllegalArgumentException.class) public void getNonexistingTest() { new OwlOntologyNode(reasoner.getObject("Nonsense"), reasoner); } @Test public void getChildTest() { OwlOntologyNode node = new OwlOntologyNode(reasoner.getObject(MEUBEL), reasoner); OwlOntologyNode child = node.getChild("Bed"); assertNotNull(child); } @Test public void getNonsenseChildTest() { OwlOntologyNode node = new OwlOntologyNode(reasoner.getObject(MEUBEL), reasoner); OwlOntologyNode child = node.getChild("Nonsense"); assertNull(child); } @Test public void getLabelTest() { OwlOntologyNode node = new OwlOntologyNode(reasoner.getObject(MEUBEL), reasoner); assertEquals(MEUBEL, node.getLabel()); } @Test public void getChildrenTest() { OwlOntologyNode node = new OwlOntologyNode(reasoner.getObject(MEUBEL), reasoner); List>> childs = node .getChildren(); System.out.println(childs); assertTrue("Meubel does not have the expected children", childs.toString().matches( ".*Node.*Bureau.*Node.*Tafel.*Node.*Kast.*Node.*Stoel.*Node.*Bed.*")); } @Test public void getChildrenDepth1Test() { OwlOntologyNode node = new OwlOntologyNode( reasoner.getObject("Persoon"), reasoner); List>> childs = node .getChildren(1); System.out.println(childs); assertEquals(2, childs.size()); assertTrue("Familie does not have the expected children", childs.toString().matches(".*Node.*Jijzelf.*Node.*Anderen.*")); } @Test public void getChildrenDepth2Test() { OwlOntologyNode node = new OwlOntologyNode( reasoner.getObject("Persoon"), reasoner); List>> childs = node .getChildren(2); System.out.println(childs); assertEquals(6, childs.size()); assertTrue("Familie does not have the expected children", childs.toString().matches( ".*Node.*Jijzelf.*Node.*Leraar.*Node.*Collega.*Node.*Vriend.*Node.*Geestelijke.*")); } @Test public void getParentTest() { OwlOntologyNode node = new OwlOntologyNode( reasoner.getObject("Anderen"), reasoner); assertEquals( new OwlOntologyNode(reasoner.getObject("Persoon"), reasoner), node.getParent()); } @Test public void getParentOfRootTest() { OwlOntologyNode node = new OwlOntologyNode( reasoner.getObject("Type_Locatie"), reasoner); assertEquals(null, node.getParent()); } @Test public void ParkStateE2Etest() throws OWLOntologyCreationException { OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); OWLOntology ontology = manager.loadOntologyFromOntologyDocument( new File("src/main/resources/Child Sexual Abuse.owl")); OwlTreeReasoner reas = new OwlTreeReasoner(ontology); OwlOntologyNode park = new OwlOntologyNode("Park", reas); assertEquals(Collections.EMPTY_LIST, park.getChildren()); } @Test public void getCollegaProperties() { OwlOntologyNode node = new OwlOntologyNode( reasoner.getObject("Collega"), reasoner); List attris = node.getAttribute(); System.out.println(attris); assertEquals(4, attris.size()); // Beschrijving_voorwerp and // Locatie_meubel Property relatieCollega = attris.stream() .filter(prop -> "Relatie_met_jou_collega" .equals(prop.getQuestionType().getId())) .findFirst().get(); System.out.println(relatieCollega.getQuestionType().getQuestion()); Map naamIsPiet = new HashMap<>(); naamIsPiet.put("\\[Naam\\]", "Piet"); TypedQuestion substiRelatie = relatieCollega.getQuestionType() .substitute(naamIsPiet); System.out.println(substiRelatie.getQuestion()); assertEquals( "Beschrijf nu jou relatie tot Piet. Bij welke baan hebben jullie elkaar leren kennen, en hoe was de relatie tussen jullie?", substiRelatie.getQuestion()); } }