Extractors

The extractor classes defined in the package org.tm4j.topicmaps.utils.extractors all implement the Mango library's UnaryFunction interface. This interface defines a single method, fn() which takes a single parameter and returns an Object. The extractor classes in TM4J encapsulate some of the more common operations on topic map data, and also provide some more advanced operations which can be used to abstract topic map data for presentation in a user interface. Some of the extractor methods allow additional parameters to be specified either by calling additional class methods or by using the BinaryFunction interface to pass the additional parameter directly into the fn() method. Check the individual class documentation for details.

Example 7.2. Using Extractors

The following code sample shows how the TM4J extractor classes can be used together to list all of the role players in an association:

  public void listRolePlayers(Association assoc)
  {
    Iterator members = MembersExtractor.fn(assoc);
    while (members.hasNext())
    {
      Member m = (Member)members.next();
      System.out.println(
        "Member: " + TopicNameExtractor.fn(TypesExtractor.fn(m)));
      Syste.out.println("Players:");
      Iterator players =
        TopicNameExtractor.fn(PlayersExtractor.fn(m)).iterator();

      // Adapt.Method() is part of the Mango library. It creates a 
      // UnaryFunction which invokes the named method on the specified
      // object.
      // ForEach applies the UnaryFunction to each item that the iterator
      // visits.
      // So, the following line prints out the names of each role player

      ForEach.execute(players, Adapt.Method(System.out, "println"));
    }
  }

      

Table 7.1. Overview over the Extractors which are part of TM4J

Extractor NameDescription
AssociatedTopicsExtractorAn extractor which returns a list of all topics associated with the input topic with an optionally specified association type and role specs for the the input topic and its associated topic
AssociationExtractorTakes a Member object as input and returns the parent Association object as output
AssociationGroupsExtractorReturns a Map of AssociationGroup objects which represent all the associations of a specific type in which the topic plays a specific role
AssociationNameExtractorUnaryFunction: Returns an arbitrary base name string from the Association's typing topic BinaryFunction: Takes the Association instance as the first parameter, and a Topic instance as the second parameter where the Topic instance MUST be a role player in the Association. The function then returns a base name string from the Association's typing topic that is scoped by the topic that defines the type of the role played by the Topic in the other parameter
BaseNamesExtractorTakes a Topic instance as input and returns the Collection of BaseNames of that Topic as output
BaseTopicExtractorReturns the base topic of the input object if and only if the input object implements the Topic interface. Otherwise null is returned
MembersExtractorTakes an Association instance as input and returns the Collection of Members in that Association as output
OccurrencesExtractor UnaryFunction that retrieves a Collection of Occurrence from a Topic
PlayersExtractorAn extractor which takes a Member instance as input and returns a Collection of the Topics which are role players in that Member instance as Output
RefiedObjectExtractorTakes a Topic instance as input and returns the TopicMapObject which the Topic reifies or null if this Topic does not reify any object in the TMt
ReifyingTopicExtractorTakes a TopicMapObject instance as input and returns the Topic which reifies it or null
ScopeExtractorAn extractor function which takes a ScopedObject instance as input and returns the collection of Topic instances that define the scope of the object as output
SizeExtractorExtracts an integer representing the size of the input object If the input object is a collection, returns the count of the number of objects contained in the collection If the intput object is null, return 0. All other input values will return 1
TopicNameExtractorTakes a Topic as input and returns a String as output
TypeExtractorReturns the topic(s) which type the object specified on the input (output depends on input, see javadoc for details)
VariantNameExtractorTakes a Topic as input and returns a Variant instance as output