working on NTriples support in Netbeans 6

In the last couple of days I have been learning about the very powerful Schlieman project. The result of the project is that one can now very simply create language support for NetBeans, and is what has allowed the very quick recent growth of a huge number of languages such as Groovy, Ruby, Prolog, Erlang, and many many others...

I did not take more than a day for me to get the basics for NTriples support done. NTriples is the simplest serialization of RDF. It is extremely explicit, one line per statement.

<SubjectURL> <PredicateURL> <ObjectURL> .
This is RDF at its purest. It is not very humanly writable, but it is easiest to understand. If you download cwm you can transform any rdf/xml into this format. For example you can transform my foaf file into NTriples like this:
hjs@bblfish:0$ cwm --rdf http://bblfish.net/people/henry/card --ntriples | head
    
<http://bblfish.net/people/henry/card> <http://www.iana.org/assignments/relation/alternate> <http://bblfish.net/people/henry/card.n3> .
<http://bblfish.net/people/henry/card> <http://www.iana.org/assignments/relation/alternate> <http://bblfish.net/people/henry/card.rdf> . 
<http://bblfish.net/people/henry/card> <http://xmlns.com/foaf/0.1/maker> <http://bblfish.net/people/henry/card#me> .
<http://bblfish.net/people/henry/card> <http://xmlns.com/foaf/0.1/primaryTopic> <http://bblfish.net/people/henry/card#me> .

So I now have basic syntax highlighting and clickability: i.e. you can control-hover of a URL, then click, and it will fetch a representation of that resource and open it in a browser. There is a lot more that can be done: if the representation were RDF it would be nice if it could be translated into NTriples and opened inside of NetBeans itself...

All of the source code is available under a BSD licence on the so(m)mer project. Download it with subversion from https://sommer.dev.java.net/svn/sommer/trunk. The project is in the misc/Editor directory. Once opened, just right click on the project and choose "Install/Reload in the development IDE". For debugging it is better to do this in the Target Platform IDE. Then you can open any ntriples extension file and get a little syntax highlighting.

The main tokenizer and grammar code itself is in the Ntriples.nbs file. As you can see it is split into token definitions

TOKEN:space:( [" " "\\t"]+ )
TOKEN:comment:( [" " "\\t"]\* "#" [\^ "\\n" "\\r"]\* ["\\n" "\\r"]+ )
TOKEN:bnode:( "_:" ["A"-"Z" "a"-"z" "0"-"9"]+ ) 
TOKEN:absoluteURI:( "<" [\^ "<" ">" " " "\\t"]+ ">" )
TOKEN:qliteral:( "\\"" [\^ "\\"" "\\n" "\\r"]\* "\\"" )
TOKEN:eol:(["\\n" "\\r"]+)
TOKEN:brackets:(".")
TOKEN:type:("\^\^")
and a simple grammar
S = (Triple | BlankLine )\*;
BlankLine = Space <eol>;
Triple =   Space Subject <space> Predicate <space> Object Space "." Space <eol>;
Subject = UriRef | <bnode>;
Predicate = UriRef;
Object = UriRef | <bnode> | Literal ;
Literal =   <qliteral> [ <type,"\^\^"> UriRef ];
UriRef =  <absoluteURI>;
Space = (<space>)\*;

As you can see, this is an incredibly simple grammar for the most powerful of all languages. You can express anything in NTriples, clearly and distinctly, or even fuzzily if you wish to. Of course it is not very practical for human editing as it is. But for machine consumption it is excellent, and it compresses very very well as you can imagine. :-) Making it more human friendly will be the topic of my next blogs.

The Token processing comes first, so one has to be careful there not to create ambiguity for the parser. The reference for Schlieman is currently the Schliemann NBS Language Description Wiki page.

One can then add color information (and one could easily do a lot better than this)
COLOR:bnode: {
    foreground_color: "blue";
}

COLOR:qliteral: {
    foreground_color: "green";
}

COLOR:absoluteUri: {
    foreground_color: "blue";
}

COLOR:type: {
    foreground_color: "red";
}
Then finally I added hyperlink functionality by adding the line
HYPERLINK:absoluteURI: net.java.dev.sommer.editor.ntriples.HyperLink.hyperlink
I then wrote the very simple HyperLink.java class with the hyperlink(Context context) method. This was easy to do by following Geertjan's excellent hints.
public class HyperLink {

    public static Runnable hyperlink(Context context) {
        SyntaxContext scontext = (org.netbeans.api.languages.SyntaxContext) context;
        ASTPath path = scontext.getASTPath();
        ASTToken t = (ASTToken) path.getLeaf();
        
        String url = t.getIdentifier();
        if (url.startsWith("<"))
            url = url.substring(1);
        if (url.endsWith(">"))
            url = url.substring(0,url.length()-1);
        final String cleanUrl = url;
        return new Runnable() {

            public void run() {
                try {
                    org.openide.awt.HtmlBrowser.URLDisplayer.getDefault().showURL(new java.net.URL(cleanUrl));
                } catch (MalformedURLException ex) {
                    Logger.getLogger(HyperLink.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        };
    }
}

You will find Gertjan's blog to be a great source of information on how to get going with Schlieman and NetBeans programming in general. One very useful thing to help with writing a grammar and a parser is a debugging tool of course. To get this you need to open the Tools>Plugins menu and get the Generic Languages Framework Studio plugin. Once it is installed you will find in the Windows > Others menu an AST and Tokens submenu, which when clicked will open two very helpful views on the left of your editor you can see in the main image. This apparently only currently works on the latest developer release of NetBeans, but should be distributes standard with Netbeans beta 2 .

I will next try to add support from Turtle and N3, then it would be nice to add a more intelligent way of displaying linked to RDF files. I can think of quite a lot of different features here...

Comments:

nice work!!! and a i always enjoy a good bblfish read :)

Posted by james on October 03, 2007 at 12:34 AM CEST #

Reading (the entrails of) fish has of time immemorial been an excellent way to predict the future. In this case it augurs very well. :-)

Posted by Henry Story on October 03, 2007 at 03:25 AM CEST #

[Trackback] Talking about templates again... in recent blog entries I&#39;ve written about how the NetBeans module

Posted by Mirror on October 03, 2007 at 06:05 PM CEST #

It would be very nice to see this as a completed netbeans plugin? Especially for n3.

Posted by Dave Pawson on October 31, 2007 at 06:44 AM CET #

Dave,

check out the Turtle support that I also wrote:
http://blogs.sun.com/bblfish/entry/turtle_support_for_netbeans_6

I am busy working on other things, so please anyone feel free to check this out from

https://sommer.dev.java.net/

improve on it, and send the improvements back. I would be happy to add anyone with good ideas to the project... Then we can build a plugin for the rdf languages and have it distributed with NetBeans by default!

Henry

Posted by Henry Story on October 31, 2007 at 07:56 AM CET #

Post a Comment:
Comments are closed for this entry.
About

bblfish

Search

Archives
« April 2014
MonTueWedThuFriSatSun
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
       
Today