All Downloads are FREE. Search and download functionalities are using the official Maven repository.

templates.DefaultModelCompare.vm Maven / Gradle / Ivy

#* @vtlvariable name="packageName" type="java.lang.String" *#
#* @vtlvariable name="ctx" type="org.kevoree.modeling.kotlin.generator.GenerationContext" *#
#* @vtlvariable name="helper" type="org.kevoree.modeling.kotlin.generator.ProcessorHelperClass" *#

package ${packageName}.compare

import java.util.HashMap
import java.util.ArrayList
import org.kevoree.modeling.api.KMFContainer
import org.kevoree.modeling.api.trace.*

/**
 * Created by duke on 26/07/13.
 */


class DefaultModelCompare : org.kevoree.modeling.api.compare.ModelCompare {

override public fun createSequence(): org.kevoree.modeling.api.trace.TraceSequence {
    return ${packageName}.trace.DefaultTraceSequence()
}

override public fun diff(origin: KMFContainer, target: KMFContainer): TraceSequence {
        return createSequence().populate(internal_diff(origin, target, false));
    }

override public fun inter(origin: KMFContainer, target: KMFContainer): TraceSequence {
        return createSequence().populate(internal_diff(origin, target, true));
    }

    private fun internal_diff(origin: KMFContainer, target: KMFContainer, inter: Boolean): List {
        val traces = ArrayList()
        val objectsMap = HashMap()
        traces.addAll((origin as ${ctx.getKevoreeContainerImplFQN()} ).generateDiffTraces(target, inter))
                #if($ctx.jS)
                    for(child in collectAll(origin)){
                #else
                    for(child in origin.containedAllElements()){
                #end
            val childPath = child.path();
            if(childPath != null){
                objectsMap.put(child.path()!!, child as ${ctx.getKevoreeContainerImplFQN()});
            } else {
                throw Exception("Null child path " + child);
            }
        }
            #if($ctx.jS)
                for(child in collectAll(target)){
            #else
                for(child in target.containedAllElements()){
            #end
            val childPath = child.path();
            if(childPath != null){
                if(objectsMap.containsKey(childPath)){
                    if(inter){
                        traces.add(ModelAddTrace(child.eContainer()!!.path()!!, child.getRefInParent()!!, child.path(), child.metaClassName()))
                    }
                    traces.addAll(objectsMap.get(childPath)!!.generateDiffTraces(child, inter))
                    objectsMap.remove(childPath) //drop from to process elements
                } else {
                    if(!inter){
                        traces.add(ModelAddTrace(child.eContainer()!!.path()!!, child.getRefInParent()!!, child.path(), child.metaClassName()))
                        traces.addAll((child as ${ctx.getKevoreeContainerImplFQN()}).generateDiffTraces(child as KMFContainer, true))
                    }
                }
            } else {
                throw Exception("Null child path " + child);
            }
        }
        if(!inter){
            //if diff
            for(diffChild in objectsMap.values()){
                traces.add(ModelAddTrace(diffChild.eContainer()!!.path()!!, diffChild.getRefInParent()!!, (diffChild as KMFContainer).path(), (diffChild as ${ctx.getKevoreeContainer().get()}).metaClassName()))
                traces.addAll((diffChild as ${ctx.getKevoreeContainerImplFQN()}).generateDiffTraces(null, inter))
            }
        }
        return traces;
    }

                #if($ctx.jS)
                    private fun collectAll(obj : KMFContainer) : List {
                        val result = java.util.ArrayList()
                        for(child in obj.containedElementsList()){
                            if(child is ArrayList<*>){
                                for(subChild in (child as ArrayList<*>) ){
                                    var casted = subChild as KMFContainer
                                    result.add(casted)
                                    result.addAll(collectAll(casted))
                                }
                            } else {
                                var casted = child as KMFContainer
                                result.add(casted)
                                result.addAll(collectAll(casted))
                            }
                        }
                        return result
                    }
                #end

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy