Home
Categories
Dictionary
Glossary
Download
Project Details
Changes Log
What Links Here
FAQ
License

Make copies of services



In some cases, you want to prepare the values of datas for a service in several different contexts, and send one or the other set of values depending on the context.

Create a copy of a service

The ServiceInstance.createCopy() method allows to create copies of publish or event services. The ServiceInstance.invoke() method for the copy will work exactly as the invoke method in the original service, except that the current values of the copied service will be used for the Datas.

This allows to keep several independent set of datas for a service and set the one you need at will.

Example

For example, suppose that you have the following service definition:
        <services>
            <event name="myService">
                <data name="intValue" type="int" />  
                <data name="boolValue" type="bool" />                  
            </event>                         
        </services>
We might want to prepare two set of datas for this service, for example one where the toggle is true, and another when the toggle is false.

We can perform in our module:
      public class MyModule {
        ServiceInstance serviceToggleOff = null;  
        ServiceInstance serviceToggleOn = null;         
       
        public MyModule() {
        }

        public void init(Module module) {
          serviceToggleOff = module.getService("myService").createCopy();
          serviceToggleOn = module.getService("myService").createCopy();       
        }
      }
Then we can prepare values for the datas independently for the two states of a toggle. For example:
      public class MyModule {
        ServiceInstance serviceToggleOff = null;  
        ServiceInstance serviceToggleOn = null;         
       
        public MyModule() {
        }

        public void init(Module module) {
          serviceToggleOff = module.getService("myService").createCopy();
          serviceToggleOn = module.getService("myService").createCopy(); 
       
          serviceToggleOff.getData("intData").setValue(1);      
          serviceToggleOff.getData("boolData").setValue(true);             
       
          serviceToggleOn.getData("intData").setValue(2);      
          serviceToggleOn.getData("boolData").setValue(false);                    
        }
      }
And then send one set or the other depending of the context, for example:

      if (isToggle)  {
         serviceToggleOn.invoke();
      } else {
         serviceToggleOff.invoke();
      }      

Copy the data values from one service to another

The ServiceInstance.copyFromService(ServiceInstance) method allows to copy the data values from one service to the current service.
  • This method will only do something for a ServiceInstance which provide datas. For example if a module subscribe to an event Service, using this method on this service will do nothing and return false
  • This method will only copy datas which are present in both the services and for which the type is compatible
  • This method will copy datas of any type
  • This method does not care if the service used for the copy is providing datas or not. For example, if a module subscribe to the service1 and service1 event services, it is possible to perform service1Instance.copyFromService(service2Instance)

Strict copy

Main Article: types compatibility

The ServiceInstance.copyFromService(ServiceInstance, boolean) allows to specify if the copy is strict (by default the copy is not strict). If the copy is strict, the copy will only copy datas which have compatible types.

Example

For example, suppose that you have the following services definition:
      <services>
         <publish name="service1">  
            <data name="data1" type="int" />         
            <data name="data2" type="float" />     
            <data name="data3" type="bool" />            
         </publish>    
         <publish name="service2" >  
            <data name="data1" type="int" />         
            <data name="data2" type="float" />     
            <data name="data3" type="bool" />                             
         </publish>        
      </services>
The following code will copy the data1 and data2 data values from service2 to service1 when service2 is notified:
      public class MyModule {
        ServiceInstance service1 = null;          
       
        public MyModule() {
        }

        public void init(Module module) {
          service1 = module.getService("service1");    
        }
      
        public void subscribe(ServiceInstance service) {
          if (service.getName().equals("service2")) {
             service1.copyFromService(service);
             service1.invoke();
          }
        }      
      }

See also


Categories: concepts | development

Copyright 2017-2020 Dassault Aviation. All Rights Reserved. Documentation and source under the LGPL v3 licence