Customize default value for the name of an entity

Support questions relating to the actifsource Domain Diagram Editor

Customize default value for the name of an entity

Postby GiUmaTo » Tue Jun 14, 2011 8:50 am

Hello

I'm trying to figure out how to define default values for the name of my entities in a meta-model, in order to speed up the creation of a specific model out of that meta-model.
An example follows, taken from the domain of a company and its partner network (customers, providers, brokers, mediators, ...):
CompanyPartners Diagram.png
Meta-model diagram example of a company and its partner network
CompanyPartners Diagram.png (10.95 KiB) Viewed 91808 times

In one specific model, one instance of a Company (say MyCompany), could have two Partner instances: MyCustomer and MyBroker. For each one of those I would then create a specific instance of order, invoice and contactInfo.

When I start creating them, say for MyCustomer and for the first relationship, contactInfo ...
MyContactInfo Creation.png
Creation of a specific PartnerContactInfo instance for MyCustomer Partner instance
MyContactInfo Creation.png (9.94 KiB) Viewed 91808 times

... I'd like to show a proposed default value like MyCustomerContactInfo, taken from the following "hypothetical" formula:
Code: Select all
PartnerContactInfo.name.defaultValue = Partner.name + "ContactInfo"

Then I would be able to accept the default proposed name, or change it as needed.

Of course the same thing would be done for PartnerOrder instances (with a default name like "MyCustomerOrder"), and for PartnerInvoice (with a default name like "MyBrokerInvoice" for MyBroker partner instance).

Is this possible, whether without or with Java code?

Thanks for your attention
GiUmaTo
 
Posts: 10
Joined: Thu Jun 09, 2011 11:21 pm

Re: Customize default value for the name of an entity

Postby micha » Tue Jun 14, 2011 4:07 pm

Do you know that you can have Resources without names? Just derive the class from Resource instead from NamedResource.

What you want to do might be possible with a ResourceInitializationAspect (written in Java). In must check if this really works in this case, though.

Another possibility is to define a NameAspect for the class (also written in Java). This certainly works but is a bit more work. To do this, the class may not be derived from NamedResource (as NamedResource already implements the NameAspect to take the name from the Resource's "name" attribute). You can browse NamedResource to see how it is defined there. You will need a slight variation of the implementation which takes the name from the owner if it is not set. Note also that you need to define your own "name" Attribute in this case.
micha
 
Posts: 21
Joined: Thu Aug 05, 2010 2:41 pm
Location: Switzerland

Re: Customize default value for the name of an entity

Postby GiUmaTo » Wed Jun 15, 2011 1:40 pm

Could you please provide some more info on either way to accomplish this.

Especially, I'm having troubles in figuring out what to do into my specific Java class implementing INameAspect. What code should I put into the three method to override? (getSimpleName, resolveResourcesByName, addNameChangeListener)
GiUmaTo
 
Posts: 10
Joined: Thu Jun 09, 2011 11:21 pm

Re: Customize default value for the name of an entity

Postby micha » Thu Jun 16, 2011 9:33 am

Basically, the class that you referenece in the model just has to be in the project's java classpath. Have a look at the Literal Aspect Tutorial which describes how to setup the project for it.

Then, your class needs to implement the ch.actifsource.core.model.aspects.INameAspect respectively the ch.actifsource.core.model.aspects.IInitializationAspect.

We will soon release a tutorial introducing into the programmatic low-leval API to access and manipulate actifsource models.
micha
 
Posts: 21
Joined: Thu Aug 05, 2010 2:41 pm
Location: Switzerland

Re: Customize default value for the name of an entity

Postby GiUmaTo » Thu Jun 16, 2011 11:46 am

Yes, I just made it to the class creation point, just by taking "inspiration" from the DateLiteral tutorial.
So now basically I've no more compile errors.

But I have this empty interface implementation and it's not really clear to me what should I put into those methods in order to:
1) get a hold on the current resource I'm working on
2) get it's name attribute
3) change the attribute & "commit" the change

(I think I've found the method on how to get the resource's parent node).
GiUmaTo
 
Posts: 10
Joined: Thu Jun 09, 2011 11:21 pm

Re: Customize default value for the name of an entity

Postby micha » Thu Jun 16, 2011 3:36 pm

Okay, the InitializationAspect does not work for what you want because the parent is only set after the aspect gets invoked. So, you indeed need a NameAspect.

The most important things about the api:
  • INode is a reference to a Resource (or a Literal). INodes are untyped.
  • There is a Select class to make queries and an Update class to make changes
micha
 
Posts: 21
Joined: Thu Aug 05, 2010 2:41 pm
Location: Switzerland

Re: Customize default value for the name of an entity

Postby GiUmaTo » Wed Jun 22, 2011 8:18 am

Hello. I'm working through some kind of simple demo to try to solve my issue.
I managed to create some custom MyNamedResource Class , that extends Resource (not NamedResource), that has an aspect[NameAspect] and that has a custom name property.

The NameAspect is implemented by some custom MyNameAspectImpl java class, that extends the core AbstractNameByAttributeAspect - in order to get some basic functioning - and that implements INameAspect. In the java class I also reused (copied) some code from the core NamedResourceAspect implementation, and stuffed in some ch.actifsource.util.Assert calls to see what's happening. At the moment, the class returns a dummy constant in getSimpleName.

All in all, I can see some INameAspect methods being called, and when the project gets processed I can see in the console that an entity of MyNamedResource Class shows up in the log with my dummy constant name. But:

  • While I'm creating that instance in my model, the name property is empty and it's up to me to fill it. There seem to be no relation with the simple name String returned and the possibility to get some default value for the name.
  • Even after I manually named the entity (which I'd like to avoid), I cannot find a way to use my dummy returned String in a template, e.g. use it to name a generated java file and/or java class. That name only shows up at some point in the output console while processing the project.

After also noting what you said about having unnamed resources, what I'd like is the following:

  • To create some custom Class for my metamodel, that has some property/attribute/field. This is not necessarily the name property.
  • While authoring templates for this Class, the actual value of this property should be available in the filename area, as well as in the template body area.
  • While instantiating entities of this Class in my model, the property should be pre-filled with some default value. I can modify that value or leave it as it is. The default should ideally be calculated taking into account the owner entity. At the minimum, it should be calculated in some accessible java code.

Sorry for the length, I'm trying to be clear on my needs. Do you have any advice on this?

Thanks again.
GiUmaTo
 
Posts: 10
Joined: Thu Jun 09, 2011 11:21 pm

Re: Customize default value for the name of an entity

Postby micha » Wed Jun 22, 2011 10:06 am

GiUmaTo wrote:Hello. I'm working through some kind of simple demo to try to solve my issue.
I managed to create some custom MyNamedResource Class , that extends Resource (not NamedResource), that has an aspect[NameAspect] and that has a custom name property.

The NameAspect is implemented by some custom MyNameAspectImpl java class, that extends the core AbstractNameByAttributeAspect - in order to get some basic functioning - and that implements INameAspect. In the java class I also reused (copied) some code from the core NamedResourceAspect implementation, and stuffed in some ch.actifsource.util.Assert calls to see what's happening. At the moment, the class returns a dummy constant in getSimpleName.


This all seems good.

All in all, I can see some INameAspect methods being called, and when the project gets processed I can see in the console that an entity of MyNamedResource Class shows up in the log with my dummy constant name. But:

  • While I'm creating that instance in my model, the name property is empty and it's up to me to fill it. There seem to be no relation with the simple name String returned and the possibility to get some default value for the name.
  • Even after I manually named the entity (which I'd like to avoid), I cannot find a way to use my dummy returned String in a template, e.g. use it to name a generated java file and/or java class. That name only shows up at some point in the output console while processing the project.



In your name aspect implementation you can check if your name attribute is present, and if not return a default (or based on the parent's name as your originial request was when I understand correctly). The resource's name is used to display in the navigators, in the dialogs, in the resource editor to refer to the resource etc. The resource's name can be accessed in the api through Select.simpleName(). In the template editor, there is currently no direct way to access this name. All you can access directly are the properties of a resource. You can, however, write a java function that returns the name via the Select function:

Code: Select all

   /**
     * Gives the Resource's name as defined by its NameAspect
     */
    public static java.lang.String simpleName(final ch.actifsource.core.javamodel.IResource resource) {
      /* Begin Protected Region [[xxxxx]] */
      return Select.simpleName(resource.getReadJobExecutor(), resource.getResource());
      /* End Protected Region   [[xxxxxx]] */
    }

We will add this as a Builtin function in the 4.7 release of actifsource.

After also noting what you said about having unnamed resources, what I'd like is the following:

  • To create some custom Class for my metamodel, that has some property/attribute/field. This is not necessarily the name property.
  • While authoring templates for this Class, the actual value of this property should be available in the filename area, as well as in the template body area.
  • While instantiating entities of this Class in my model, the property should be pre-filled with some default value. I can modify that value or leave it as it is. The default should ideally be calculated taking into account the owner entity. At the minimum, it should be calculated in some accessible java code.


I suggest that you make the property optional in the model. Then, in the template, make a new function for the resource which checks whether the property is defined, and if so, returns its value, otherwise calculates the property as desired. Note that the function can take arguments from the contexts. Likely, the owner is already in the context so you can easily pass it to the function. Otherwise, you can use the selectToMe* function from the generated java classes to traverse relations in reverse direction.

This is probably what serves your original request. The name of a resource is special because it is used outside the templates for various purposes and therefore is more difficult to customize. Template functions get used only in the templates.
micha
 
Posts: 21
Joined: Thu Aug 05, 2010 2:41 pm
Location: Switzerland

Re: Customize default value for the name of an entity

Postby GiUmaTo » Mon Jun 27, 2011 9:58 am

Hi. Here's my solution that will do for the moment:

  • use "normal" NamedResources entities in my meta-model
  • bind each one of them to a custom InitializationAspect
  • custom InitializationAspect implementation finds the 'name' property of NamedResource and writes a constant string to it, which is a suffix peculiar of each entity (say ContactInfo, Order, Invoice)
  • 'name' property is accessed within templates to give name to files and java classes

Doing so, I save myself some typing, as the suffix is already there, but of course I'm loosing the chance to get also the bit of name coming from the owner entity (say Customer, Broker, ...).

The next step will be the following:

  • use a custom base entity Class derived from Resource
  • bind it to a custom NameAspect implementation
  • have a custom property that lets the author assign a name 'manually' if needed
  • in NameAspect implementation, build up the simpleName (the special one, used by actifsource UI and console) starting from 3 bits of info:
    • the 'manual choice' (which takes precedence over all),
    • the constant suffix for each type of entity (ContactInfo, Invoice, ...)
    • the owner entity name (Customer, Broker, ...). These last two are returned concatenated if there's no manual override.
  • create a template function to let the templates have access to the simpleName, as you suggested in a previous post
  • in templates, use the accessed simpleName to give names to files and classes

Thanks for your help, I appreciated.
Cheers
GiUmaTo
 
Posts: 10
Joined: Thu Jun 09, 2011 11:21 pm

Re: Customize default value for the name of an entity

Postby micha » Mon Jun 27, 2011 11:08 am

Hello GiUmaTo

Thank you for sharing your solution with us!
micha
 
Posts: 21
Joined: Thu Aug 05, 2010 2:41 pm
Location: Switzerland


Return to Domain Diagram Editor

Who is online

Users browsing this forum: No registered users and 0 guests

cron