Active grammar for embedded space ...

Poster Content
nk4um Administrator
Posts: 607
October 22, 2014 08:45

Radu there are many possible patterns. I'm starting to get a clearer idea of your requirements now. Try this:

Create a transparent overlay

import org.netkernel.layer0.nkf.*; import org.netkernel.layer0.urii.ValueSpace; import org.netkernel.module.standard.endpoint.TransparentOverlayImpl;

public class MyOverlay extends TransparentOverlayImpl
{
    publicvoid onRequest(String elementId, INKFRequestContext aContext) throws Exception
    {
        INKFRequest req=aContext.getThisRequest().getIssuableClone();
        ValueSpace space=new ValueSpace(10);
        //get the values from issuing requests here?
        space.put("current:id", 12345, null);
        req.injectDurableRequestScope(space);
        INKFResponseReadOnly responseIn=aContext.issueRequestForResponse(req);
        aContext.createResponseFrom(responseIn);
    }
}

Then instantiate this in your module as such

<overlay>
  <class>MyOverlay&lt;/class&gt;
  <space>
    < !-- put all your stuff inside -->
  &lt;/space&gt;
&lt;/overlay&gt;

Hope this helps, Tony

Like · Post Reply
nk4um User
Posts: 37
October 22, 2014 08:07

Posted by tab (View)
I'm not convinced you need to go along this route. :-) Just when I said dynamic grammar I meant grammars that change at runtime not just new instances of an endpoint or space. The session is a transparent overlay which just reflects all the endpoints on the inside to the outside that is normally a good way to go. Then you can intercept the requests you want and their grammars with their arguments are still intact.

Still if you want to explore this area take a look at the file:

org.netkernel.layer0.meta.impl.ActiveGrammarParser

it contains the code which parses the active grammar syntax and creates a standard grammar including the use of interleave.

But my warning stands: I don't think you are doing anything to unusual from what you have said so a standard approach is going to be much easier. Try using a transparent overlay!

Cheers, Tony

In the Session implementation example each SessionSpace stored some Session values, and there were many Sessions each with its own content.

In my situation I had to store a given id to do some db queries based on that id. So to use a space similar with a Session space, that will store the id which comes from request, created in a transparent overlay (similar with session overlay) and inject that space in the requests seams to be very natural ... In the transparent overlay inner space therefore I could use an syntax to call db services that are not id based (since the id is already stored and used by the session space only).

Now I try to figure out what I don't understand with the pure TransparentOverlay solution. I have a delegated space inside and there are some services that rely on the id, and contains it in their syntax now. The id should be provided so the grammar should be updated with the id on the whole chain of requests which, in my opinion, requires unnecessary boilerplate code and is ugly. If I not pass the id top-down then I must be able to obtain it from somewhere but I have no clue how to do that ... I mean to have a resource like get:current:id and return the id that was provided by the initial request...

Maybe using a scratch?! ...

Pretty confused on this ...

Thanks for your attention and advice, anyway!

Kind regards,

Radu

Like · Post Reply
nk4um Administrator
Posts: 607
October 21, 2014 09:30

I'm not convinced you need to go along this route. :-) Just when I said dynamic grammar I meant grammars that change at runtime not just new instances of an endpoint or space. The session is a transparent overlay which just reflects all the endpoints on the inside to the outside that is normally a good way to go. Then you can intercept the requests you want and their grammars with their arguments are still intact.

Still if you want to explore this area take a look at the file:

org.netkernel.layer0.meta.impl.ActiveGrammarParser

it contains the code which parses the active grammar syntax and creates a standard grammar including the use of interleave.

But my warning stands: I don't think you are doing anything to unusual from what you have said so a standard approach is going to be much easier. Try using a transparent overlay!

Cheers, Tony

Like · Post Reply
nk4um User
Posts: 37
October 21, 2014 08:26

Posted by tab (View)
Hi Radu, firstly decide on what grammar you want, can you describe that with a few examples maybe?

Secondly, varargs in active URI syntax does only work in active grammars. Active grammars though are just a convenience syntax on top of the base standard grammar engine so whatever it can do standard grammars can do with a bit more work.

Third, why are you creating grammars in the constructor of the endpoint? The only reason for doing this is if you are wanting dynamic grammars that would change based on configuration such as in overlays etc.

Cheers, Tony

You know, I just wanted to implement a mechanism that is, in the first phase, a clone of the Session handling one.

So the classes were adapted from the Session implementation.

It's a kind of dynamic endpoint I guess since there is an instance for each session, that's why I used the declaration in constructor ...

I saw that the Session endpoint delegates the job the Session space (I mean for source).

The problem is that my Session space, which is a kind of Session space injected in the Context of the current Request, needs to receive many unknown arguments to pass them further in special cases. For me seams to be very handy to have such a dynamic injected space that solves some requests to session specific resources (even they are referred in scripts by generic names). Besides the number of session spaces is not known before (I mean do declare a bunch of them in a module.xml and try to allocate one of their ids to each new session ...)

So basically I need a path argument and a varargs like collection.

I saw in StandardGrammar that there is an <interleave> feature but it's unclear for me how to emulate using it the varargs ...

I hope that things are more clear now than in the previous post and make sense to you.

Thanks for your attention,

Radu

Like · Post Reply
nk4um Administrator
Posts: 607
October 21, 2014 08:04

Hi Radu, firstly decide on what grammar you want, can you describe that with a few examples maybe?

Secondly, varargs in active URI syntax does only work in active grammars. Active grammars though are just a convenience syntax on top of the base standard grammar engine so whatever it can do standard grammars can do with a bit more work.

Third, why are you creating grammars in the constructor of the endpoint? The only reason for doing this is if you are wanting dynamic grammars that would change based on configuration such as in overlays etc.

Cheers, Tony

Like · Post Reply
nk4um User
Posts: 37
October 20, 2014 18:33Active grammar for embedded space ...

Hi!

I just try to make an embedded space (injected in the request context scope) on the model of Session space.

The first test was successful. Now I'm trying to improve and I encounter some difficulties.

There is one endpoint defined in the injected space with a grammar like (it's Scala code but I think is pretty self-explained):

class RTSessionEndpoint(kernel:IKernel) extends StandardAccessorImpl {
  declareThreadSafe()
  declareName("RTSessionEndpoint")
  val g1:IGrammarGroup=BNFGrammarFactory.createGroup(null)
  BNFGrammarFactory.createText(g1, "cc:")
  val g2:IGrammarGroup=BNFGrammarFactory.createGroup(g1,"path",null)
  BNFGrammarFactory.createRegex(g2, BNFGrammarFactory.BUILTIN_ANYTHING)
  val logger:ILogger = kernel.getLogger
  val g:IIdentifierGrammar=BNFGrammarFactory.createGrammar(g1,logger)
  declareGrammar(g)

In my understanding the grammar is a standard one (with groups).

What I wish is to have a <varargs/> added to the grammar.

Since it is a standard grammar I think it's not possible (since in the doc varargs works only for active grammars).

But I have no clue it it is possible and, in this case, how to define an active one instead.

Any help/idea/tip/advice is welcomed.

Thanks for your understanding,

Radu

Like · Post Reply