-- @path FeatureModel=/DetectionAndroidMalwareByICC/metamodels/FeatureModel.ecore
-- @path ASEC=/DetectionAndroidMalwareByICC/metamodels/ASEC.ecore

module CreateFeatureModel;
create OUT : FeatureModel from IN : ASEC;

helper context OclAny def : get_all_Static_Receiver:  Sequence(ASEC!BroadcastReceiver) =
	ASEC!BroadcastReceiver.allInstances()->select(c | c.Source=#XML);

helper context OclAny def : get_all_Dynamic_Receiver:  Sequence(ASEC!BroadcastReceiver) =
	ASEC!BroadcastReceiver.allInstances()->select(c | c.Source=#JAVA);

helper context OclAny def : get_all_IntentFilter_for_activity:  Sequence(ASEC!intentFilter) =
	ASEC!intentFilter.allInstances()->select(c | c.refImmediateComposite().oclIsTypeOf(ASEC!Activity) );

helper context OclAny def : get_all_IntentFilter_for_activity1:  Sequence(ASEC!intentFilter) =
	ASEC!intentFilter.allInstances();

rule ApplicationPolicyFile2ICCPatterns {
      from 
	  s:ASEC!ApplicationPolicyFile
	  to
	  t:FeatureModel!ICCPatterns
	  (
	  Package <- s.Package,
	  AppComps <- s.app,
	--  IntentFilterAS <- thisModule.get_all_IntentFilter_for_activity->collect(c | thisModule.CreateIntentFilterForActivityService(c))
	  IntentFilterAS <- thisModule.get_all_IntentFilter_for_activity1->collect(c | thisModule.CreateIntentFilterForActivityService(c))
	  )
}


--for Create Component:
rule CreateComponent{
	from
	    s:ASEC!Application
	to
	   t:FeatureModel!Component
	   (
	   	NameOfComps <- s.Components-> flatten()-> collect(c | thisModule.Createcomponentname(c)),
	   --	NameOfCompRS <- s.Components-> flatten()-> collect(c | thisModule.ComponentNameOfReceiverStatic(c))
	   			
			NameOfCompRS <- s.Components-> flatten()-> select(c | c.oclIsKindOf(ASEC!BroadcastReceiver))->collect(c | thisModule.ComponentNameOfReceiverStatic(c)),
		--	NameOfCompRS <- s.Components-> flatten()-> select(c | c.oclIsKindOf(ASEC!BroadcastReceiver))->flatten()->select(c | c.),
		NameOfCompRS <- thisModule.get_all_Static_Receiver->collect(c | thisModule.ComponentNameOfReceiverStatic(c)),
		NameOfCompRD <- thisModule.get_all_Dynamic_Receiver->collect(c | thisModule.ComponentNameReceiverDynamic(c))
		
		
			--NameOfCompRD <-s.Components-> flatten()-> select(c | c.oclIsKindOf(ASEC!BroadcastReceiver))->collect(c | thisModule.ComponentNameReceiverDynamic(c))
	   
	   )
}
--component_name(activity/service/provider)
lazy rule Createcomponentname{
     from
s:ASEC!Component
to
t:FeatureModel!ComponentNameForActivityServiceProvider
(
		 Name <- ' : '+ s.name,
		 Type <- if s.oclIsKindOf(ASEC!Activity) then
		          #Activity
				  else
				  	if s.oclIsKindOf(ASEC!Service) then
					#Service
					else
						if s.oclIsKindOf(ASEC!ContentProvider) then
						#provider
						else
							OclUndefined
						endif
					endif
				 endif
)
}

lazy rule ComponentNameOfReceiverStatic{
     from
s:ASEC!BroadcastReceiver (s.Source= #XML)
to
t:FeatureModel!ComponentNameOfReceiverStatic
(
		 Name <- ' : '+ s.name,
		RegisterMode <- #Static,
	 Type <- #Receiver
		             
		
)
}

lazy rule ComponentNameReceiverDynamic{
     from
s:ASEC!BroadcastReceiver (s.Source= #JAVA)
to
t:FeatureModel!ComponentNameReceiverDynamic
(
		 Name <- ' : '+ s.name,
		RegisterMode <- #Dynamic,
						
		 Type <- #Receiver
		             
		
)
}

lazy rule CreateIntentFilterForActivityService{
     from
s:ASEC!intentFilter 
to
t:FeatureModel!IntentFilterForActivityService
(
	TypeOwnedComp <- if s.refImmediateComposite().oclIsTypeOf(ASEC!Activity) then
	                      #Activity
						  else
						  	if s.refImmediateComposite().oclIsTypeOf(ASEC!Service) then
						      #Service
						     else
						  	   if s.refImmediateComposite().oclIsTypeOf(ASEC!BroadcastReceiver) then
							    #Receiver
								else
									OclUndefined
						       endif
							 endif
							 endif,
	Actions <- s.actions->flatten()->collect(c | thisModule.CreateActionString())
				 		  	
	
		             
		
)
}

lazy rule CreateActionString{
     from
s:ASEC!Action
to
t:FeatureModel!ActionString
(
	--Name <- s.name
	--Name <- s.name.toString()
		             
		
)
}

