Ruby FAQ for the Protégé Script plugin (55R)
Ruby FAQ summary (55S)
General Questions (561)
Configuration Questions (564)
- How do I make Ruby the Default Language? (565)
- How do I Define a Ruby script to be executed at each startup of the script plugin?} (56M)
- I get an error when I try to use the $kb global variable. (56N)
- How can I upgrade to the latest version of JRuby? (571)
- My Ruby Scripts are really slow. (571)
Ruby Scripting (57C)
- What are some good ways of developing Ruby scripts? (57D)
- How do I tell Ruby where my scripts are? (57M)
- Do you have any example scripts? (5BW)
- Can I use user interface elements like Swing? (5C0)
Back to Protégé Script plugin Main FAQ (55U)
General Questions (55V)
What is Ruby? (55W)
Ruby is a programming language that "combines the pure object-oriented power of the classic OO language Smalltalk with the expressiveness and convenience of a scripting language such as Perl." It is a relatively mature language, known for its clean, intuitive syntax and semantics and transparent, developer friendly programming model. (55X)
How does Ruby Work in Protege? (55Y)
The ProtegeScriptTab implements Ruby by embedding JRuby into Protege using the Bean Scripting Framework (BSF). JRuby is a pure Java implementation of the Ruby language. Currently JRuby supports most but not all Ruby language elements. (55Z)
Although JRuby provides high level and low level support for manipulation of Java objects, the ProtegeScriptTab passes an OWLModel object to JRuby. Using the $kb global variable, you can seamlessly program your knowledgebase using very Ruby-esque constructs. (560)
Is JRuby an Active Project? (562)
Yes, JRuby is a very active Open Source project hosted at SourceForge and has a large user base (but not as huge as for reqular Ruby). (563)
Configuration Questions (566)
How do I make Ruby the Default Scripting Language? (567)
Edit the Protege.lax file in the main installation directory of Protégé and add the following line at the end: (568)
# DEFAULT SCRIPT LANGUAGE # ----------------------- edu.stanford.smi.protege.script.defaultScriptLanguage=jruby (569)
How do I Define a Ruby script to be executed at each startup of the script plugin? (56O)
You can't. Unfortunately, Build 0.26 of the ProtegeScriptTab only executes Python scripts at the startup of the plugin. (56P)
I get an error when I try to use the $kb global variable. (56Q)
The easiest way to see if everything is working with the Ruby language and the Protege knowledgebase is to type in the following one-liner: (56R)
puts $kb (56S)
If you get an exception or some other type of error, it's likely that JRuby is unable to locate it's built-in scripts. JRuby ships with a collection of small but important scripts located in the builtin/ folder of the jruby.jar. Apparently, the Bean Scripting Framework used by the ProtegeScriptTab does not easily allow the configuration of startup variables. Subsequently, JRuby is unable to correctly find it's home jruby.home. (56T)
Fortunately, you can get JRuby working by extracting the builtin/ folder and all of its contents into the ${PROTEGE_HOME} folder. (56U)
How can I upgrade to the latest version of JRuby? (572)
Download the latest version of JRuby from the SourceForge project and drop the jruby.jar into the ProtegeScriptTab plug in folder: ${PROTEGE_HOME}/plugins/edu.stanford.smi.protege.script. (573)
Don't forget about the class path problem. Extract the builtin/ folder and drop that into the ${PROTEGE_HOME} folder. (574)
My Ruby Scripts are really slow. (575)
Version 1.8.3 of the JRuby engine significantly improves the performance of executing scripts that manipulate Java objects such as OWLModel and java.utils. However, version 1.8.3 has not yet been released. So, you have the following options: (576)
- Wait for 1.8.3 release (577)
- Download and build 1.8.3 from the the SourceForge project CVS (578)
- Download a pre-built version 1.8.3 jruby.jar from the jEdit Ruby Editor Plugin. (579)
If you choose to download the 1.8.3 release, upgrade the ProtegeScriptTab as normal. (57B)
Ruby Scripting (57E)
What are some good ways of developing Ruby scripts? (57F)
Here's some of your options for developing Ruby scripts: (57G)
- You can type Ruby language code directly into the ProtegeScriptTab console. This is great for trying out simple ideas. (57H)
- You can load an external script in the ProtegeScriptTab console. ex: load 'startup'. You can write external scripts using: (57I)
- any good text editor. (57J)
- the jEdit Ruby Editor Plugin for the jEdit programmer's editor. (57K)
- the EclipseShell plugin for the insanely cool Eclipse IDE. (slholmes- have not evaluated this yet but I intend to!) (57L)
How do I tell Ruby where my scripts are? (57N)
You can load scripts with the load statement using relative paths, as in: (57O)
load 'html/tree.rb' (57P)
Ruby scripts are loaded relative to the current working folder which you can set using the Dir built-in class, as in: (57Q)
Dir.chdir("C:\\platform\\ProtRubyDev") (57R)
(replace the C:\\platform\\ProtRubyDev in the above example with the absolute path of your development folder.) (57S)
Do you have any example scripts? (5BX)
Yes. Check the ProtegeScriptTabRepositoryRuby pages for some examples. (5BY)
Can I use user interface elements like Swing? (5C1)
Yes. Michael Squillace and Barry Feigenbaum from IBM wrote an excellent JRuby tutorial called alt.lang.jre: Take a shine to JRuby. All of the code written in the article works great in the ProtegeScriptTab. (5C2)