Making types and methods available for custom concepts

I am trying to write a simple language for structures (simple data classes) because I use these often. Each structure has a name and a list of property declarations. For each structure two types are created: an interface and a class. I would like to use these types and their methods in other code. Unfortunately I can't seem to wrap my head around how types are defined and used.

Can somebody give me some pointers on:

- how to specify that a structure concept introduces two types, an interface and a class, in the type system?

- how to specify the methods of the interface and the class?

Regards,

Johan

0
8 comments
Avatar
Permanently deleted user

Hello, Johan.

It seems to me that you are mixing your language for structure with the code which you want to generate. In MPS we usually separate this things. You probably need to have methods in generated code but not in concepts.

In order to create a language you want, you can do the following:

  • Create PropertyDeclaration. It will have name property and type property. Create enumeration data type declaration for your property's type
  • Create StructureDeclaration. It will have name property and property child declaration with type PropertyDeclaration.
  • Create and tune editors for you concepts.

After creating the language you need to start thinking about how to generate code you want from your concept instancens.

P.S. Our structure language is very similar to stuff you want to have. Our concepts have properties, reference and children and we generate classes with getter and setters for them. Take a look at the jetbrains.mps.lang.structure langauge and its generator.

Regards,

Konstantin

0
Avatar
Permanently deleted user

Hi Konstantin,

I already have what you mention and I already generate the correct Java code from my concepts.

I want to be able to use solutions with concept instances from both the base language and my structures language. I need to be able to reference the generated types and methods from my structures language in instances of base language concepts. Here is an example:

structure ServiceConfig { // Structures language: Structure

  String hostName;

  int port;

}

public class Service { // Base language: ClassConcept

  public static Validator validate(ServiceConfig c) {

    ...

    v.notNull(c.getHostName());

    ...

  }

  public Service(ServiceConfig c) {

    ...

  }

}

public class ServiceClient { // Base language: ClassConcept

    ...

    ServiceConfigBean c = new ServiceConfigBean();

    c.setHostName("localhost");

    c.setPort("10000");

    Service s = new Service(c);

    ...

}

I don't want to redo work if you have already done it, but this is a simple enough language to learn MPS.

Regards,

Johan

0
Avatar
Permanently deleted user

Do I understand you right? You want to create a base language which supports concepts from your language as first class objects.

In that case I don't recommend you use methods for properties access. Why using low level getters and setters if you can use them with properties syntax.

I recommend you implement it in the following way:

  • Extend your language from baseLanguage (add baseLanguage to your language's extended languages section).
  • Create StructureType which extends Type from baseLanguage. Create a reference to a structure inside of it. (Take a look at ClassifierType in baseLanguage)
  • Create StructurePropertyAccessOperation and implement IOperation. Take a look at this concept implementors for examples. (for example, ArrayLengthOperation)

Having completed this you will be able to write code like this:

public class ServiceClient { // Base language: ClassConcept

    ...

    structure<Service> c = new structure<Service>();

    c.hostName = "localhost";

    c.p = 10000";

    ...

}



0
Avatar
Permanently deleted user

Okay, that is an improvement. I will try what you suggest.

However I do want to distinguish between an interface and a class in the generated code. The service must use the interface and the service clients must use the class. Should I create two types of structure references?

0
Avatar
Permanently deleted user

Johan,

Probably I don't understand correctly what you want. Where is interface and where is class in your example? You have ServiceBean which is probably generated from your structure concept instance but where is is interface?

0
Avatar
Permanently deleted user

In the final generated code I want to have an interface and a class for my structure. Some code (i.e. the service) must use the interface and some code (i.e. the service client) must use the class.

0
Avatar
Permanently deleted user

If you use approach which I proposed, you can generate StructureType to either interface type or class type depending on whether this code appears in client or server.

Which kind of service do you want to implement this way. I still don't undestand the context of the problem. It appears to me that you can use interface everywhere except places where you instantiate generated class.

0
Avatar
Permanently deleted user

My problem is not particularly tied to services. In general you can state that a single concept can map to multiple types at runtime.

A good example in this case is the old-style EJB structure. I could create an EJB concept for it with generators for the:

- home interface

- remote interface

- implementation class

Other concepts need to be able to reference one of these aspects of the concept. You either use the home interface, the remote interface or the implementation class. And each aspect would have different attributes and methods.

I need something similar. I always split structures into an interface and an implementation. Some classes talk to the interface and others talk to the implementation.

But I have got that working now by using two concepts derived from Type: StructureInterfaceType and StructureImplementationType.

I am now working on the operations. Thanks for your help.

Regards,

Johan

0

Please sign in to leave a comment.