Custom Space with generics

23 views
Skip to first unread message

Stefano Tedeschi

unread,
Apr 6, 2023, 2:01:50 PM4/6/23
to sarl
Hi everyone!

I'm facing an issue while trying to develop a custom agent space.
In particular I'd like to leverage Java generics bu I cannot figure out how to instantiate such spaces by means of a corresponding space specification.

I'd like to instantiate special spaces devised for propagating only specific events.

I defined the space, by using Java generics, as follows:

interface MySpace<T extends Event> extends OpenEventSpace

and the corresponding implementation as follows:

class MySpaceImpl<T extends Event> extends OpenLocalEventSpace implements MySpace<T>

I then defined a generic space specification:

class MySpaceSpecification<T extends Failure> implements SpaceSpecification<MySpace<T>>

The create method should return an instance of ExceptionSpaceImpl<T>.

Unfortunately, I cannot manage to make the defaultContext.getOrCreateSpaceWithSpec
method work with generics in any way.
Do you, by chance, have any insight on what may I be missing?

Thank you very much in advance!
Best wishes,

Stefano

Stéphane Galland

unread,
Apr 7, 2023, 3:35:59 PM4/7/23
to sarl
Dear Stefano.

Could you gives the cause of problem. Is it an exception, a compilation error, or simply the fact that getOrCreateSpaceWithSpec does not create/return the space.

Have you implemented your code using pure Java or SARL syntax?

There is an Java issue (and consequently SARL syntax issue) related to the usage of the Class in getOrCreateSpaceWithSpec when the class is generic.
We should rethink this part of the API in this case. Nevertheless, the following code seems to work (even if it is not perfect from coding point of view) 




interface MySpace<T extends Event> extends OpenEventSpace {

def doSomething

}

class MySpaceImpl<T extends Event> extends OpenLocalEventSpace implements MySpace<T> {

def doSomething {
}

}

class MySpaceSpecification<T extends Failure> implements SpaceSpecification<MySpace<T>> {

def create(arg0 : SpaceID, arg1 : Object*) : MySpace<T> {
var sid : SpaceID = null
return new MySpaceImpl<T>(sid, null, null)
}

}

agent MyTestAgent {
uses DefaultContextInteractions
on Initialize {
val id : UUID = null
val type : Class = typeof(MySpaceSpecification);
var ^space = defaultContext.getOrCreateSpaceWithSpec(type, id) as MySpace<Failure>
}
}

Stefano Tedeschi

unread,
Apr 10, 2023, 3:06:07 PM4/10/23
to sarl
Dear Stéphane,

thank you very much for your answer!

Indeed it's a compilation/syntax issue. I used the SARL syntax, but the same happens in pure Java.

I managed to make the code provided by you work.
However, my issue is that I'd like to istantiate tifferent types of MySpace, by using the generics feature (e.g. MySpace<MyEvent>, MySpace<MyOtherEvent>, ...).
I've tried to use something like:

val type : Class = typeof(MySpaceSpecification<MyEvent>);

but the compiler does not seem to like it.

Otherwise, the generics feature is not used in the end.

I hope I have better explained the issue.

Thank you very much again for any edvice.
Happy (late) Easter!
Best,

Stefano

Stéphane Galland

unread,
Apr 10, 2023, 3:40:32 PM4/10/23
to sarl
Hi Stefano.

Indeed, the typeof operator has a strict syntax that accepts only types without generics. "typeof(T)" is equivalent to "T.class" in Java. So that it is not possible to specify a generic.
It is mostly an issue related to the "Java" type specification that is also used by SARL.

Could you try:

val type = typeof(MySpaceSpecification) as Class<MySpaceSpecification<MyEvent>>

or 

val type : Class<MySpaceSpecification<MyEvent>> = typeof(MySpaceSpecification)

The variable should be of the correct type (Class<MySpaceSpecification<MyEvent>>).

Stéphane.

Stefano Tedeschi

unread,
Apr 11, 2023, 11:00:43 AM4/11/23
to sarl
Dear Stéphane,

thank you very much.

Unfortuantely, if I try in both ways, the compuler signals a casting issue.

It says that it cannot cast from Class<MySpaceSpecification> to Class<MySpaceSpecification<MyEvent>>. Indeed, it seems reasonable to me.

Would you maybe have some other idea on how to instantiate spaces with generics?

Thank you a lot again.
Best regards,

Stefano

Stéphane Galland

unread,
Apr 11, 2023, 11:42:34 AM4/11/23
to sarl
Dear Stefano.

There is something to add in the to-do list of SARL type cheker.

The followin solution is not very efficient; but it may work :

val type = (typeof(MySpaceSpecification) as Class) as Class<MySpaceSpecification<MyEvent>>

Stefano Tedeschi

unread,
Apr 11, 2023, 12:35:12 PM4/11/23
to sarl
Hi Stéphane,

unfortunately the very same error occurs: Cannot cast from Class<MySpaceSpecification> to Class<MySpecification<MyEvent>>

Anyway don't worry! I don't want to bother you too much :)

Thank you again for your help.
Best regards,

Stefano

Stefano Tedeschi

unread,
Apr 11, 2023, 12:51:16 PM4/11/23
to sarl
Hi again,

I think I have found a workaround:

val type = new MySpaceSpecification.class as Class<MySpecification<MyEvent>>

It is not beautiful to create a new class object, but it seems to work.
I'll test it in the next few days.

Thank you again very much for your help!
Best wishes,

Stefano
Reply all
Reply to author
Forward
0 new messages