forked from owlcs/owlapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add control flag to structure walking to switch between not walking a…
…nnotations, walking ontology annotations only, and walking all annotations. The second case is the default if no flag is supplied, and provides the same behavior as before If ontology annotations are walked, recursive annotations will not be processed. The current annotation will be set before the callback is run. Annotations on an object are processed after the visitor callback, but before any fields specific to the object are walked; thus a literal in annotation on a data property assertion will be handed to the callback before the literal in the assertion itself. In a separate change, constructors are added to OWLObjectWalker to allow lists of objects to be supplied instead of just sets. Since the set is copied into a list before walking, this avoids having to turn a list of objects into a set prior to walking.
- Loading branch information
Showing
3 changed files
with
183 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
contract/src/test/java/org/semanticweb/owlapi/util/OWLObjectWalkerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package org.semanticweb.owlapi.util; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Set; | ||
import javax.annotation.Nonnull; | ||
import static org.junit.Assert.assertEquals; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.semanticweb.owlapi.api.test.baseclasses.TestBase; | ||
import org.semanticweb.owlapi.model.AddOntologyAnnotation; | ||
import org.semanticweb.owlapi.model.OWLAnnotation; | ||
import org.semanticweb.owlapi.model.OWLAnnotationProperty; | ||
import org.semanticweb.owlapi.model.OWLObject; | ||
import org.semanticweb.owlapi.model.OWLObjectVisitor; | ||
import org.semanticweb.owlapi.model.OWLOntology; | ||
import static org.semanticweb.owlapi.util.StructureWalker.AnnotationWalkingControl.DONT_WALK_ANNOTATIONS; | ||
import static org.semanticweb.owlapi.util.StructureWalker.AnnotationWalkingControl.WALK_ANNOTATIONS; | ||
import static org.semanticweb.owlapi.util.StructureWalker.AnnotationWalkingControl.WALK_ONTOLOGY_ANNOTATIONS_ONLY; | ||
|
||
/** | ||
* Created by ses on 8/15/15. | ||
*/ | ||
public class OWLObjectWalkerTest extends TestBase { | ||
|
||
private OWLAnnotation world; | ||
private OWLAnnotation cruelWorld; | ||
private OWLAnnotationProperty ap; | ||
private OWLAnnotation goodbye; | ||
private OWLAnnotation hello; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
this.ap = df.getOWLAnnotationProperty(iri("ap")); | ||
this.cruelWorld = df.getOWLAnnotation(ap, df.getOWLLiteral("cruel world")); | ||
this.goodbye = df.getOWLAnnotation(ap, df.getOWLLiteral("goodbye"), singleton(cruelWorld)); | ||
this.world = df.getOWLAnnotation(ap, df.getOWLLiteral("world")); | ||
this.hello = df.getOWLAnnotation(ap, df.getOWLLiteral("hello"), singleton(world)); | ||
} | ||
|
||
@Test | ||
public void testWalkAnnotations() throws Exception { | ||
OWLOntology o = getOwlOntology(); | ||
List<OWLAnnotation> emptyAnnotationList = Collections.emptyList(); | ||
checkWalkWithFlags(o, DONT_WALK_ANNOTATIONS, emptyAnnotationList); | ||
checkWalkWithFlags(o, WALK_ONTOLOGY_ANNOTATIONS_ONLY, Arrays.asList(hello)); | ||
checkWalkWithFlags(o, WALK_ANNOTATIONS, Arrays.asList(hello, world, goodbye, cruelWorld)); | ||
} | ||
|
||
private void checkWalkWithFlags(OWLOntology o, StructureWalker.AnnotationWalkingControl walkFlag, List<OWLAnnotation> expected) { | ||
final List<OWLAnnotation> visitedAnnotations = new ArrayList<>(); | ||
|
||
OWLObjectVisitor visitor = new OWLObjectVisitorAdapter() { | ||
@Override | ||
public void visit(OWLAnnotation node) { | ||
visitedAnnotations.add(node); | ||
} | ||
}; | ||
|
||
Set<? extends OWLObject> ontologySet = Collections.singleton(o); | ||
OWLObjectWalker<? extends OWLObject> walker; | ||
if (walkFlag == WALK_ONTOLOGY_ANNOTATIONS_ONLY) { | ||
walker = new OWLObjectWalker<>(ontologySet); | ||
} else { | ||
walker = new OWLObjectWalker<>(ontologySet, walkFlag); | ||
} | ||
walker.walkStructure(visitor); | ||
assertEquals(expected, visitedAnnotations); | ||
} | ||
|
||
@Nonnull | ||
private OWLOntology getOwlOntology() { | ||
OWLOntology o = getOWLOntology("foo"); | ||
m.applyChange(new AddOntologyAnnotation(o, hello)); | ||
addAxiom(o, df.getOWLDeclarationAxiom(ap, singleton(goodbye))); | ||
return o; | ||
} | ||
} |