Java: Protege Frames: How to get a distinct list of all possible Slots within a Project.

If for whatever reason you might need this list. All you have to do is loop through the Cls’s, then loop through the Slots of the Cls’s, using the method .getOwnSlots()

More Protege Frames how-to’s here http://mantascode.com/?p=507

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
 
import edu.stanford.smi.protege.model.Cls;
import edu.stanford.smi.protege.model.KnowledgeBase;
import edu.stanford.smi.protege.model.Project;
 
//This Program will output a complete list of distinct slots within a Protege Frames Project file.
 
public class jokke
{
      private static final String PROJECT_FILE_NAME = "C:\\MC\\RadLex.pprj";
      public static void main(String[] args)
      {
            HashSet idsLoaded = new HashSet();
            Collection errors = new ArrayList();
        Project project = new Project(PROJECT_FILE_NAME, errors);
        KnowledgeBase kb = project.getKnowledgeBase();
        ArrayList distinctSlotList = new ArrayList();
        //get Class iterator
        Iterator radClsIter = kb.getClses().iterator();
        //loop through each class
        while ( radClsIter.hasNext())
        {
            //get Cls object
            Cls currentClass = (Cls) radClsIter.next();
            //String for a comma deliminated list of slots
            String slotsString = currentClass.getOwnSlots().toString();
            String [] clsSlotComponents = slotsString.split(",");
            //loop through each slot
            for ( int i = 0 ; i <  clsSlotComponents.length; i++ )
            {
                  //check to see if particular slot already exists in ArrayList
                  if ( distinctSlotList.contains(clsSlotComponents[i].trim().replaceAll("\\[|\\]", "")))
                  {}
                  else
                  {
                        distinctSlotList.add(clsSlotComponents[i].trim().replaceAll("\\[|\\]", ""));
                  }
            }
        }
 
        Iterator iterator = distinctSlotList.iterator();
        while(iterator.hasNext())
        {
            System.out.println(iterator.next());
        }
      }
}

Leave a Reply

Your email address will not be published. Required fields are marked *