Search Mailing List Archives

Limit search to: Subject & Body Subject Author
Sort by: Reverse Sort
Limit to: All This Week Last Week This Month Last Month
Select Date Range     through    

[protege-discussion] beanshell scripting in protege

Jonathan Carter jonathan.carter at
Thu Nov 13 09:02:34 PST 2008

Thomas is right in his response. You have to be careful as everything  
is a Frame in Protege (classes and instances)
The API does make sense, as you say, and is predictable. Everything  
that you can do with the GUI, you can do via the API.
To get or set a slot use something like using Python syntax (although  
the API calls are as per Java:

// Get all the instances of a class (Cls in the API) called 'Class_Name'
anInstList = kb.getCls("Class_Name").getDirectInstances()

// For each instance in the list you can get values of slots, e.g.  
slot called 'slot_name'
for anInstance in anInstList:
	aName = anInstance.getDirectOwnSlotValue(kb.getSlot("slot_name"))

the Direct in there makes sure that you get the instances of this  
class / slot and not any subclasses or slot values of subclasses. You  
may wish to use getOwnSlotValue() or getInstances() to also return all  
subclasses or overridden slot values

To set the slot value:

// For a given Instance, with a String slot:
anInstance.setOwnSlotValue(kb.getSlot("slot_name"), "My new slot value")

Hope this helps.

I've got used to restarting Protege when my script fails. I find that  
on some exceptions, the print() function stops working reliably.  
Restarting Protege resolves it all, though.


Jonathan Carter - Head of Technical Architecture
Enterprise Architecture Solutions Ltd

Assess your EA maturity at:

On 13 Nov 2008, at 14:46, Jason Sachs wrote:

> OK, I found the API at and  
> have gotten some very preliminary tasks done like creating a new  
> instance of a class and examining/setting its slots. I'm guessing  
> what I have in mind should be fairly straightforward, just a matter  
> of getting to know the API & it looks fairly predictable. (and not  
> "squirrely" like the Win32 API where sometimes I feel like I'm going  
> in circles around a hidden room... >:( but that's a whole other  
> ballgame)
> is the appropriate way to get/set a slot value like this:
> s = kb.getSlot("title");
> print(i.getOwnSlotValue(s)); // i is an instance
> i.setOwnSlotValue(s,"this is the title");
> It seems strange to me to get slots from the kb object rather than  
> the i object. Frames have a getOwnSlots() method but that returns a  
> collection of slots which you have to iterate through one by one (at  
> least in beanshell). It seems like there ought to be  
> Frame.getOwnSlotByName() and Frame.hasOwnSlotByName() functions.
> also a request (and I'm not quite sure how the Protege bindings  
> interact with the BSF features): in beanshell you can type clear()  
> or reassign the "kb" variable and you lose access to the knowledge  
> base object. ("Doctor, it hurts when I stab myself!" "Well stop  
> doing it!") I suspect it is possible to declare this variable as non- 
> modifiable (there's definitely a way to do this for objects exposed  
> to Javascript) and that would be helpful.
> Of course what I was really trying to do was reset the scripting  
> environment; there doesn't seem to be any way to do this other than  
> reconfigure the project to remove the scripting tab, then add it  
> back. A "reset" or "clear" button would be a lot of help.
> _______________________________________________
> protege-discussion mailing list
> protege-discussion at
> Instructions for unsubscribing:

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the protege-discussion mailing list