Working with Facets

The following code shows how to work with facets programmatically.  It shows how to access both system and user defined facets and how to do overrides.

----------------------------------------------------------------------

package test;

import java.util.*;

import edu.stanford.smi.protege.model.*;
import edu.stanford.smi.protege.util.*;

/**
 * This example shows how to work with system and user defined facets programmatically.
 * 
 * @author Ray Fergerson <fergerson@smi.stanford.edu>
 */
public class FacetsExample {

    public static void main(String[] args) {
        Collection errors = new ArrayList();
        Project project = Project.createNewProject(null, errors);
        dumpErrors(errors);

        KnowledgeBase kb = project.getKnowledgeBase();
        playWithSystemFacet(kb);
        playWithUserDefinedFacet(kb);
        System.out.println("Done");
    }

    private static void dumpErrors(Collection errors) {
        if (!errors.isEmpty()) {
            Iterator i = errors.iterator();
            while (i.hasNext()) {
                Object o = i.next();
                System.out.println("Error: " + o);
            }
            System.exit(-1);
        }
    }

    private static void playWithSystemFacet(KnowledgeBase kb) {
        Collection rootClses = kb.getRootClses();
        Cls cls = kb.createCls(null, rootClses);
        Slot slot = kb.createSlot(null);
        slot.setValueType(ValueType.INTEGER);
        Facet numericMinimumFacet = kb.getFacet(Model.Facet.NUMERIC_MINIMUM);
        assertTrue(numericMinimumFacet != null);
        Slot associatedSlot = numericMinimumFacet.getAssociatedSlot();
        playWithFacet(cls, slot, numericMinimumFacet, associatedSlot);
    }

    /**
     * Same stuff as above but with a user defined facet.  In order to get a user defined facet we first have to 
     * define a metaslot and add a template slot to it.  This template slot becomes the "associated class" for the facet
     * which the system creates for us.
     */
    private static void playWithUserDefinedFacet(KnowledgeBase kb) {
        Cls defaultMetaSlot = kb.getDefaultSlotMetaCls();
        Collection parents = CollectionUtilities.createCollection(defaultMetaSlot);
        Cls mySlotMetaSlot = kb.createCls(null, parents);
        Slot slotAssociatedWithFacet = kb.createSlot(null);
        mySlotMetaSlot.addDirectTemplateSlot(slotAssociatedWithFacet);
        Facet myFacet = kb.createFacet(null);
        slotAssociatedWithFacet.setAssociatedFacet(myFacet);

        Collection rootClses = kb.getRootClses();
        Cls cls = kb.createCls(null, rootClses);
        Slot slot = kb.createSlot(null, mySlotMetaSlot); // an instance of the new metaslot
        slot.setValueType(ValueType.INTEGER);
        playWithFacet(cls, slot, myFacet, slotAssociatedWithFacet);
    }

    private static void playWithFacet(Cls cls, Slot slot, Facet facet, Slot associatedSlot) {
        // set the numeric minimum on the slot  This is the "top-level facet value"
        Integer topLevelValueSet = new Integer(3);
        slot.setOwnSlotValue(associatedSlot, topLevelValueSet);
        cls.addDirectTemplateSlot(slot);

        // now the "facet value at the class" (the real facet value) equal to the top-level value.
        Object facetValue = cls.getTemplateFacetValue(slot, facet);
        assertTrue(facetValue.equals(topLevelValueSet));

        // now set the facet at the class
        Integer facetAtClassValueSet = new Integer(5);
        cls.setTemplateFacetValue(slot, facet, facetAtClassValueSet);
        Object facetAtClassValue = cls.getTemplateFacetValue(slot, facet);
        assertTrue(facetAtClassValue.equals(facetAtClassValueSet));
    }

    private static void assertTrue(boolean b) {
        if (!b) {
            System.err.println("assert failed");
            new Throwable().printStackTrace();
            System.exit(-1);
        }
    }
}