<s> from __future__ import division <EOL> from vistrails . db . domain import DBWorkflow , DBAdd , DBDelete , DBAction , DBAbstraction , DBModule , DBConnection , DBPort , DBFunction , DBParameter , DBGroup <EOL> from vistrails . db . services . action_chain import getActionChain , getCurrentOperationDict , getCurrentOperations , simplify_ops <EOL> from vistrails . db import VistrailsDBException <EOL> import copy <EOL> import datetime <EOL> import getpass <EOL> import unittest <EOL> import vistrails . core . system <EOL> from itertools import chain <EOL> def update_id_scope ( vistrail ) : <EOL> if hasattr ( vistrail , '<STR_LIT>' ) : <EOL> vistrail . update_id_scope ( ) <EOL> else : <EOL> for action in vistrail . db_actions : <EOL> vistrail . idScope . updateBeginId ( '<STR_LIT:action>' , action . db_id + <NUM_LIT:1> ) <EOL> if action . db_session is not None : <EOL> vistrail . idScope . updateBeginId ( '<STR_LIT>' , action . db_session + <NUM_LIT:1> ) <EOL> for operation in action . db_operations : <EOL> vistrail . idScope . updateBeginId ( '<STR_LIT>' , operation . db_id + <NUM_LIT:1> ) <EOL> if operation . vtType == '<STR_LIT>' or operation . vtType == '<STR_LIT>' : <EOL> vistrail . idScope . updateBeginId ( operation . db_what , <EOL> getNewObjId ( operation ) + <NUM_LIT:1> ) <EOL> if operation . db_data is None : <EOL> if operation . vtType == '<STR_LIT>' : <EOL> operation . db_objectId = operation . db_oldObjId <EOL> vistrail . db_add_object ( operation . db_data ) <EOL> for annotation in action . db_annotations : <EOL> vistrail . idScope . updateBeginId ( '<STR_LIT>' , annotation . db_id + <NUM_LIT:1> ) <EOL> def materializeWorkflow ( vistrail , version ) : <EOL> if vistrail . db_has_action_with_id ( version ) : <EOL> workflow = DBWorkflow ( ) <EOL> performActions ( getActionChain ( vistrail , version ) , <EOL> workflow ) <EOL> workflow . db_id = version <EOL> workflow . db_vistrailId = vistrail . db_id <EOL> return workflow <EOL> elif version == <NUM_LIT:0> : <EOL> return DBWorkflow ( ) <EOL> else : <EOL> raise VistrailsDBException ( "<STR_LIT>" % version ) <EOL> def performAction ( action , workflow ) : <EOL> if action . actionType == '<STR_LIT>' : <EOL> for operation in action . db_operations : <EOL> workflow . db_add_object ( operation . db_data , <EOL> operation . db_parentObjType , <EOL> operation . db_parentObjId ) <EOL> elif action . actionType == '<STR_LIT>' : <EOL> for operation in action . db_operations : <EOL> workflow . db_change_object ( operation . db_data , <EOL> operation . db_parentObjType , <EOL> operation . db_parentObjId ) <EOL> elif action . actionType == '<STR_LIT>' : <EOL> for operation in action . operations : <EOL> workflow . db_delete_object ( operation . db_objectId , <EOL> operation . db_what , <EOL> operation . db_parentObjType , <EOL> operation . db_parentObjId ) <EOL> else : <EOL> msg = "<STR_LIT>" % action . db_actionType <EOL> raise TypeError ( msg ) <EOL> def performDeletes ( deleteOps , workflow ) : <EOL> for operation in deleteOps : <EOL> workflow . db_delete_object ( getOldObjId ( operation ) , operation . db_what , <EOL> operation . db_parentObjType , <EOL> operation . db_parentObjId ) <EOL> def performAdds ( addOps , workflow ) : <EOL> for operation in addOps : <EOL> workflow . db_add_object ( operation . db_data , <EOL> operation . db_parentObjType , <EOL> operation . db_parentObjId ) <EOL> def performActions ( actions , workflow ) : <EOL> performAdds ( getCurrentOperations ( actions ) , workflow ) <EOL> def synchronize ( old_vistrail , new_vistrail , current_action_id ) : <EOL> id_remap = { } <EOL> for action in new_vistrail . db_actions : <EOL> if action . is_new : <EOL> new_action = action . do_copy ( True , old_vistrail . idScope , id_remap ) <EOL> old_vistrail . db_add_action ( new_action ) <EOL> else : <EOL> old_action = old_vistrail . db_actions_id_index [ action . db_id ] <EOL> for annotation in action . db_deleted_annotations : <EOL> if old_action . db_has_annotation_with_id ( annotation . db_id ) : <EOL> old_action . db_delete_annotation ( annotation ) <EOL> else : <EOL> if old_action . db_has_annotation_with_key ( '<STR_LIT>' ) : <EOL> old_annotation = old_action . db_get_annotation_by_key ( '<STR_LIT>' ) <EOL> old_action . db_delete_annotation ( old_annotation ) <EOL> else : <EOL> pass <EOL> for annotation in action . db_annotations : <EOL> if annotation . is_new : <EOL> new_annotation = annotation . do_copy ( True , <EOL> old_vistrail . idScope , <EOL> id_remap ) <EOL> old_action . db_add_annotation ( new_annotation ) <EOL> for tag in new_vistrail . db_deleted_tags : <EOL> if old_vistrail . db_has_tag_with_id ( tag . db_id ) : <EOL> old_vistrail . db_delete_tag ( tag ) <EOL> else : <EOL> pass <EOL> for tag in new_vistrail . db_tags : <EOL> if tag . is_new : <EOL> new_tag = tag . do_copy ( False ) <EOL> try : <EOL> new_tag . db_id = id_remap [ ( DBAction . vtType , new_tag . db_id ) ] <EOL> except KeyError : <EOL> pass <EOL> try : <EOL> old_tag = old_vistrail . db_tags_name_index [ new_tag . db_name ] <EOL> except KeyError : <EOL> old_vistrail . db_delete_tag ( old_tag ) <EOL> try : <EOL> old_tag = old_vistrail . db_tags_id_index [ new_tag . db_id ] <EOL> except KeyError : <EOL> old_vistrail . db_delete_tag ( old_tag ) <EOL> old_vistrail . db_add_tag ( new_tag ) <EOL> new_action_id = id_remap . get ( ( DBAction . vtType , current_action_id ) , current_action_id ) <EOL> old_vistrail . db_currentVersion = new_action_id <EOL> return new_action_id <EOL> def merge ( sb , next_sb , app = '<STR_LIT>' , interactive = False , tmp_dir = '<STR_LIT>' , next_tmp_dir = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> vt = sb . vistrail <EOL> next_vt = next_sb . vistrail <EOL> merge_gui = interactive <EOL> MergeGUI = merge_gui . MergeGUI if merge_gui else False <EOL> skip = <NUM_LIT:0> <EOL> sb . mashups = list ( next_sb . mashups ) <EOL> sb . abstractions = list ( next_sb . abstractions ) <EOL> id_remap = { } <EOL> checkout_key = "<STR_LIT>" <EOL> action_key = checkout_key + app <EOL> annotation_key = action_key + '<STR_LIT>' <EOL> action_annotation_key = action_key + '<STR_LIT>' <EOL> checkinId = <NUM_LIT:0> <EOL> if len ( app ) and next_vt . db_has_annotation_with_key ( action_key ) : <EOL> co = next_vt . db_get_annotation_by_key ( action_key ) <EOL> checkinId = int ( co . _db_value ) <EOL> else : <EOL> actions = [ ] <EOL> actionDict = { } <EOL> for action in vt . db_actions : <EOL> unique = action . _db_user + str ( action . _db_date ) <EOL> copy_no = <NUM_LIT:0> <EOL> while ( unique + str ( copy_no ) ) in actionDict : <EOL> copy_no += <NUM_LIT:1> <EOL> unique = unique + str ( copy_no ) <EOL> actions . append ( unique ) <EOL> actionDict [ unique ] = action <EOL> actionNexts = [ ] <EOL> actionDictNext = { } <EOL> for action in next_vt . db_actions : <EOL> unique = action . _db_user + str ( action . _db_date ) <EOL> copy_no = <NUM_LIT:0> <EOL> while ( unique + str ( copy_no ) ) in actionDictNext : <EOL> copy_no += <NUM_LIT:1> <EOL> unique = unique + str ( copy_no ) <EOL> actionNexts . append ( unique ) <EOL> actionDictNext [ unique ] = action <EOL> while checkinId < len ( actions ) and checkinId < len ( actionNexts ) and actions [ checkinId ] == actionNexts [ checkinId ] : <EOL> checkinId += <NUM_LIT:1> <EOL> if checkinId > <NUM_LIT:0> : <EOL> checkinId = actionDict [ actions [ checkinId - <NUM_LIT:1> ] ] . db_id <EOL> deletekeys = [ action_key , annotation_key , action_annotation_key ] <EOL> for key in deletekeys : <EOL> while vt . db_has_annotation_with_key ( key ) : <EOL> a = vt . db_get_annotation_by_key ( key ) <EOL> vt . db_delete_annotation ( a ) <EOL> mergeAnnotations = True <EOL> if len ( app ) and next_vt . db_has_annotation_with_key ( annotation_key ) : <EOL> co = next_vt . db_get_annotation_by_key ( annotation_key ) <EOL> old_hash = co . _db_value <EOL> mergeAnnotations = ( old_hash != vt . hashAnnotations ( ) ) <EOL> mergeActionAnnotations = True <EOL> if len ( app ) and next_vt . db_has_annotation_with_key ( action_annotation_key ) : <EOL> co = next_vt . db_get_annotation_by_key ( action_annotation_key ) <EOL> old_hash = co . _db_value <EOL> mergeActionAnnotations = ( old_hash != vt . hashActionAnnotations ( ) ) <EOL> for action in next_vt . db_actions : <EOL> if action . _db_id > checkinId : <EOL> new_action = action . do_copy ( True , vt . idScope , id_remap ) <EOL> vt . db_add_action ( new_action ) <EOL> if not mergeAnnotations : <EOL> for annotation in [ a for a in vt . db_annotations ] : <EOL> if not next_vt . db_has_annotation_with_id ( annotation . db_id ) : <EOL> vt . db_delete_annotation ( annotation ) <EOL> for annotation in next_vt . db_annotations : <EOL> if not vt . db_has_annotation_with_id ( annotation . db_id ) : <EOL> new_annotation = annotation . do_copy ( True , vt . idScope , id_remap ) <EOL> vt . db_add_annotation ( new_annotation ) <EOL> else : <EOL> old_annotation = vt . db_get_annotation_by_id ( annotation . db_id ) <EOL> if old_annotation . db_key != annotation . db_key : <EOL> old_annotation . db_key = annotation . db_key <EOL> if old_annotation . db_value != annotation . db_value : <EOL> old_annotation . db_value = annotation . db_value <EOL> else : <EOL> annotations = { } <EOL> for annotation in vt . db_annotations : <EOL> if annotation . db_key not in annotations : <EOL> annotations [ annotation . db_key ] = [ ] <EOL> if annotation . db_value not in annotations [ annotation . db_key ] : <EOL> annotations [ annotation . db_key ] . append ( annotation . db_value ) <EOL> for annotation in next_vt . db_annotations : <EOL> if annotation . db_key not in annotations or annotation . db_value not in annotations [ annotation . db_key ] : <EOL> new_annotation = annotation . do_copy ( True , vt . idScope , id_remap ) <EOL> vt . db_add_annotation ( new_annotation ) <EOL> if not mergeActionAnnotations : <EOL> for annotation in [ a for a in vt . db_actionAnnotations ] : <EOL> if not next_vt . db_has_actionAnnotation_with_id ( annotation . db_id ) : <EOL> vt . db_delete_actionAnnotation ( annotation ) <EOL> if annotation . db_key == '<STR_LIT>' and len ( sb . thumbnails ) > <NUM_LIT:0> : <EOL> thumb = '<STR_LIT:/>' . join ( sb . thumbnails [ <NUM_LIT:0> ] . split ( <EOL> '<STR_LIT:/>' ) [ : - <NUM_LIT:1> ] ) + '<STR_LIT:/>' + annotation . db_value <EOL> if thumb in sb . thumbnails : <EOL> sb . thumbnails . remove ( thumb ) <EOL> for annotation in next_vt . db_actionAnnotations : <EOL> if not vt . db_has_actionAnnotation_with_id ( annotation . db_id ) : <EOL> annotation = annotation . do_copy ( True , vt . idScope , id_remap ) <EOL> vt . db_add_actionAnnotation ( annotation ) <EOL> if annotation . db_key == '<STR_LIT>' and len ( next_sb . thumbnails ) > <NUM_LIT:0> : <EOL> thumb = '<STR_LIT:/>' . join ( next_sb . thumbnails [ <NUM_LIT:0> ] . split ( <EOL> '<STR_LIT:/>' ) [ : - <NUM_LIT:1> ] ) + '<STR_LIT:/>' + annotation . db_value <EOL> if thumb not in sb . thumbnails : <EOL> sb . thumbnails . append ( thumb ) <EOL> else : <EOL> old_annotation = vt . db_get_actionAnnotation_by_id ( annotation . db_id ) <EOL> if old_annotation . db_value != annotation . db_value : <EOL> if annotation . db_key == '<STR_LIT>' and len ( sb . thumbnails ) > <NUM_LIT:0> : <EOL> thumb = '<STR_LIT:/>' . join ( sb . thumbnails [ <NUM_LIT:0> ] . split ( <EOL> '<STR_LIT:/>' ) [ : - <NUM_LIT:1> ] ) + '<STR_LIT:/>' + old_annotation . db_value <EOL> if thumb in sb . thumbnails : <EOL> sb . thumbnails . remove ( thumb ) <EOL> if annotation . db_key == '<STR_LIT>' and len ( next_sb . thumbnails ) > <NUM_LIT:0> : <EOL> thumb = '<STR_LIT:/>' . join ( next_sb . thumbnails [ <NUM_LIT:0> ] . split ( <EOL> '<STR_LIT:/>' ) [ : - <NUM_LIT:1> ] ) + '<STR_LIT:/>' + annotation . db_value <EOL> if thumb not in sb . thumbnails : <EOL> sb . thumbnails . append ( thumb ) <EOL> old_annotation . db_value = annotation . db_value <EOL> old_annotation . db_date = annotation . db_date <EOL> old_annotation . db_user = annotation . db_user <EOL> else : <EOL> oas = { } <EOL> for a in vt . db_actionAnnotations : <EOL> if not a . db_action_id in oas : <EOL> oas [ a . db_action_id ] = { } <EOL> if not a . db_key in oas [ a . db_action_id ] : <EOL> oas [ a . db_action_id ] [ a . db_key ] = [ ] <EOL> oas [ a . db_action_id ] [ a . db_key ] . append ( a ) <EOL> for new_annotation in next_vt . db_actionAnnotations : <EOL> if new_annotation . db_key == '<STR_LIT>' : <EOL> value = int ( new_annotation . db_value ) <EOL> if ( '<STR_LIT:action>' , value ) in id_remap : <EOL> new_annotation . db_value = str ( id_remap [ ( '<STR_LIT:action>' , value ) ] ) <EOL> annotation = new_annotation . do_copy ( True , vt . idScope , id_remap ) <EOL> vt . db_add_actionAnnotation ( annotation ) <EOL> elif new_annotation . db_action_id <= checkinId and new_annotation . db_key in oas [ new_annotation . db_action_id ] : <EOL> old_action = oas [ new_annotation . db_action_id ] <EOL> if new_annotation . db_key == '<STR_LIT>' : <EOL> old_annotation = old_action [ new_annotation . db_key ] [ <NUM_LIT:0> ] <EOL> if old_annotation . db_value != new_annotation . db_value : <EOL> value = old_annotation . db_value + "<STR_LIT>" + new_annotation . db_value <EOL> if interactive : <EOL> if skip == <NUM_LIT:1> : <EOL> pass <EOL> elif skip == <NUM_LIT:2> : <EOL> old_annotation . db_value = new_annotation . db_value <EOL> old_annotation . db_date = new_annotation . db_date <EOL> old_annotation . db_user = new_annotation . db_user <EOL> else : <EOL> v , value = MergeGUI . resolveTags ( <EOL> old_annotation , new_annotation , value ) <EOL> if v == merge_gui . CHOICE_OTHER_ALL : <EOL> skip = <NUM_LIT:1> <EOL> elif v == merge_gui . CHOICE_OTHER : <EOL> pass <EOL> elif v == merge_gui . CHOICE_RESOLVED : <EOL> old_annotation . db_value = value <EOL> old_annotation . db_date = new_annotation . db_date <EOL> old_annotation . db_user = new_annotation . db_user <EOL> pass <EOL> elif v == merge_gui . CHOICE_OWN : <EOL> old_annotation . db_value = new_annotation . db_value <EOL> old_annotation . db_date = new_annotation . db_date <EOL> old_annotation . db_user = new_annotation . db_user <EOL> elif v == merge_gui . CHOICE_OWN_ALL : <EOL> old_annotation . db_value = new_annotation . db_value <EOL> old_annotation . db_date = new_annotation . db_date <EOL> old_annotation . db_user = new_annotation . db_user <EOL> skip = <NUM_LIT:2> <EOL> else : <EOL> old_annotation . db_value = value <EOL> old_annotation . db_date = new_annotation . db_date <EOL> old_annotation . db_user = new_annotation . db_user <EOL> elif new_annotation . db_key == '<STR_LIT>' : <EOL> old_annotation = old_action [ new_annotation . db_key ] [ <NUM_LIT:0> ] <EOL> if new_annotation . db_value != old_annotation . db_value : <EOL> value = ( "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" ) % ( str ( old_annotation . db_date ) , old_annotation . db_user , <EOL> old_annotation . db_value , str ( new_annotation . db_date ) , <EOL> new_annotation . db_user , new_annotation . db_value ) <EOL> if interactive : <EOL> if skip == <NUM_LIT:1> : <EOL> pass <EOL> elif skip == <NUM_LIT:2> : <EOL> old_annotation . db_value = new_annotation . db_value <EOL> old_annotation . db_date = new_annotation . db_date <EOL> old_annotation . db_user = new_annotation . db_user <EOL> else : <EOL> v , value = MergeGUI . resolveNotes ( <EOL> old_annotation , new_annotation , value ) <EOL> if v == merge_gui . CHOICE_OTHER_ALL : <EOL> skip = <NUM_LIT:1> <EOL> elif v == merge_gui . CHOICE_OTHER : <EOL> pass <EOL> elif v == merge_gui . CHOICE_RESOLVED : <EOL> old_annotation . db_value = value <EOL> old_annotation . db_date = new_annotation . db_date <EOL> old_annotation . db_user = new_annotation . db_user <EOL> pass <EOL> elif v == merge_gui . CHOICE_OWN : <EOL> old_annotation . db_value = new_annotation . db_value <EOL> old_annotation . db_date = new_annotation . db_date <EOL> old_annotation . db_user = new_annotation . db_user <EOL> elif v == merge_gui . CHOICE_OWN_ALL : <EOL> old_annotation . db_value = new_annotation . db_value <EOL> old_annotation . db_date = new_annotation . db_date <EOL> old_annotation . db_user = new_annotation . db_user <EOL> skip = <NUM_LIT:2> <EOL> else : <EOL> old_annotation . db_value = value <EOL> old_annotation . db_date = new_annotation . db_date <EOL> old_annotation . db_user = new_annotation . db_user <EOL> elif new_annotation . db_key == '<STR_LIT>' : <EOL> old_annotation = old_action [ new_annotation . db_key ] [ <NUM_LIT:0> ] <EOL> if new_annotation . db_value != old_annotation . db_value : <EOL> if interactive : <EOL> if skip == <NUM_LIT:1> : <EOL> pass <EOL> elif skip == <NUM_LIT:2> : <EOL> thumb = '<STR_LIT:/>' . join ( sb . thumbnails [ <NUM_LIT:0> ] . split ( <EOL> '<STR_LIT:/>' ) [ : - <NUM_LIT:1> ] ) + '<STR_LIT:/>' + old_annotation . db_value <EOL> if thumb in sb . thumbnails : <EOL> sb . thumbnails . remove ( thumb ) <EOL> old_annotation . db_value = new_annotation . db_value <EOL> old_annotation . db_date = new_annotation . db_date <EOL> old_annotation . db_user = new_annotation . db_user <EOL> thumb = '<STR_LIT:/>' . join ( next_sb . thumbnails [ <NUM_LIT:0> ] . split ( <EOL> '<STR_LIT:/>' ) [ : - <NUM_LIT:1> ] ) + '<STR_LIT:/>' + new_annotation . db_value <EOL> if thumb not in sb . thumbnails : <EOL> sb . thumbnails . append ( thumb ) <EOL> else : <EOL> v = MergeGUI . resolveThumbs ( old_annotation , <EOL> new_annotation , tmp_dir , next_tmp_dir ) <EOL> if v == merge_gui . CHOICE_OTHER_ALL : <EOL> skip = <NUM_LIT:1> <EOL> elif v == merge_gui . CHOICE_OTHER : <EOL> pass <EOL> elif v in ( merge_gui . CHOICE_OWN , <EOL> merge_gui . CHOICE_OWN_ALL ) : <EOL> thumb = '<STR_LIT:/>' . join ( sb . thumbnails [ <NUM_LIT:0> ] . split ( <EOL> '<STR_LIT:/>' ) [ : - <NUM_LIT:1> ] ) + '<STR_LIT:/>' + old_annotation . db_value <EOL> if thumb in sb . thumbnails : <EOL> sb . thumbnails . remove ( thumb ) <EOL> old_annotation . db_value = new_annotation . db_value <EOL> old_annotation . db_date = new_annotation . db_date <EOL> old_annotation . db_user = new_annotation . db_user <EOL> thumb = '<STR_LIT:/>' . join ( next_sb . thumbnails [ <NUM_LIT:0> ] . split ( <EOL> '<STR_LIT:/>' ) [ : - <NUM_LIT:1> ] ) + '<STR_LIT:/>' + new_annotation . db_value <EOL> if thumb not in sb . thumbnails : <EOL> sb . thumbnails . append ( thumb ) <EOL> if v == merge_gui . CHOICE_OWN_ALL : <EOL> skip = <NUM_LIT:2> <EOL> else : <EOL> thumb = '<STR_LIT:/>' . join ( sb . thumbnails [ <NUM_LIT:0> ] . split ( <EOL> '<STR_LIT:/>' ) [ : - <NUM_LIT:1> ] ) + '<STR_LIT:/>' + old_annotation . db_value <EOL> if thumb in sb . thumbnails : <EOL> sb . thumbnails . remove ( thumb ) <EOL> old_annotation . db_value = new_annotation . db_value <EOL> old_annotation . db_date = new_annotation . db_date <EOL> old_annotation . db_user = new_annotation . db_user <EOL> thumb = '<STR_LIT:/>' . join ( next_sb . thumbnails [ <NUM_LIT:0> ] . split ( <EOL> '<STR_LIT:/>' ) [ : - <NUM_LIT:1> ] ) + '<STR_LIT:/>' + new_annotation . db_value <EOL> if thumb not in sb . thumbnails : <EOL> sb . thumbnails . append ( thumb ) <EOL> elif new_annotation . db_key == '<STR_LIT>' : <EOL> pass <EOL> else : <EOL> values = [ ] <EOL> for old_annotation in old_action [ new_annotation . db_key ] : <EOL> values . append ( old_annotation . db_value ) <EOL> if new_annotation . db_value not in values : <EOL> annotation = new_annotation . do_copy ( True , vt . idScope , id_remap ) <EOL> vt . db_add_actionAnnotation ( annotation ) <EOL> else : <EOL> annotation = new_annotation . do_copy ( True , vt . idScope , id_remap ) <EOL> vt . db_add_actionAnnotation ( annotation ) <EOL> if annotation . db_key == '<STR_LIT>' : <EOL> thumb = '<STR_LIT:/>' . join ( next_sb . thumbnails [ <NUM_LIT:0> ] . split ( '<STR_LIT:/>' ) [ : - <NUM_LIT:1> ] ) + '<STR_LIT:/>' + annotation . db_value <EOL> if thumb not in sb . thumbnails : <EOL> sb . thumbnails . append ( thumb ) <EOL> if len ( app ) : <EOL> vt . update_checkout_version ( app ) <EOL> def find_data ( what , id , op_dict ) : <EOL> try : <EOL> return op_dict [ ( what , id ) ] . db_data <EOL> except KeyError : <EOL> msg = '<STR_LIT>' % ( what , id ) <EOL> raise KeyError ( msg ) <EOL> def invertOperations ( op_dict , adds , deletes , do_copy = False ) : <EOL> inverse_ops = [ ] <EOL> deletes . reverse ( ) <EOL> for op in deletes : <EOL> data = find_data ( op . db_what , getOldObjId ( op ) , op_dict ) <EOL> if do_copy : <EOL> data = copy . copy ( data ) <EOL> inv_op = DBAdd ( id = - <NUM_LIT:1> , <EOL> what = op . db_what , <EOL> objectId = getOldObjId ( op ) , <EOL> parentObjId = op . db_parentObjId , <EOL> parentObjType = op . db_parentObjType , <EOL> data = data <EOL> ) <EOL> inverse_ops . append ( inv_op ) <EOL> adds . reverse ( ) <EOL> for op in adds : <EOL> inv_op = DBDelete ( id = - <NUM_LIT:1> , <EOL> what = op . db_what , <EOL> objectId = getNewObjId ( op ) , <EOL> parentObjId = op . db_parentObjId , <EOL> parentObjType = op . db_parentObjType , <EOL> ) <EOL> inverse_ops . append ( inv_op ) <EOL> return inverse_ops <EOL> def normalOperations ( adds , deletes , do_copy = False ) : <EOL> new_ops = [ ] <EOL> for op in deletes : <EOL> new_op = DBDelete ( id = - <NUM_LIT:1> , <EOL> what = op . db_what , <EOL> objectId = getOldObjId ( op ) , <EOL> parentObjId = op . db_parentObjId , <EOL> parentObjType = op . db_parentObjType , <EOL> ) <EOL> new_ops . append ( new_op ) <EOL> for op in adds : <EOL> data = op . db_data <EOL> if do_copy : <EOL> data = copy . copy ( op . db_data ) <EOL> new_op = DBAdd ( id = - <NUM_LIT:1> , <EOL> what = op . db_what , <EOL> objectId = getNewObjId ( op ) , <EOL> parentObjId = op . db_parentObjId , <EOL> parentObjType = op . db_parentObjType , <EOL> data = data ) <EOL> new_ops . append ( new_op ) <EOL> return new_ops <EOL> def getPathAsAction ( vistrail , v1 , v2 , do_copy = False ) : <EOL> sharedRoot = getSharedRoot ( vistrail , [ v1 , v2 ] ) <EOL> sharedActionChain = getActionChain ( vistrail , sharedRoot ) <EOL> sharedOperationDict = getCurrentOperationDict ( sharedActionChain ) <EOL> v1Actions = getActionChain ( vistrail , v1 , sharedRoot ) <EOL> v2Actions = getActionChain ( vistrail , v2 , sharedRoot ) <EOL> ( v1AddDict , v1DeleteDict ) = getOperationDiff ( v1Actions , <EOL> sharedOperationDict ) <EOL> ( v2AddDict , v2DeleteDict ) = getOperationDiff ( v2Actions , <EOL> sharedOperationDict ) <EOL> v1Adds = v1AddDict . values ( ) <EOL> v1Adds . sort ( key = lambda x : x . db_id ) <EOL> v1Deletes = v1DeleteDict . values ( ) <EOL> v1Deletes . sort ( key = lambda x : x . db_id ) <EOL> v1InverseOps = invertOperations ( sharedOperationDict , v1Adds , v1Deletes , do_copy ) <EOL> v2Adds = v2AddDict . values ( ) <EOL> v2Adds . sort ( key = lambda x : x . db_id ) <EOL> v2Deletes = v2DeleteDict . values ( ) <EOL> v2Deletes . sort ( key = lambda x : x . db_id ) <EOL> v2Ops = normalOperations ( v2Adds , v2Deletes , do_copy ) <EOL> allOps = v1InverseOps + v2Ops <EOL> simplifiedOps = simplify_ops ( allOps ) <EOL> return DBAction ( id = - <NUM_LIT:1> , <EOL> operations = simplifiedOps , <EOL> ) <EOL> def addAndFixActions ( startDict , actions ) : <EOL> curDict = copy . copy ( startDict ) <EOL> for action in actions : <EOL> new_ops = [ ] <EOL> for op in action . db_operations : <EOL> if op . vtType == '<STR_LIT>' : <EOL> if op . db_parentObjId is None or curDict . has_key ( ( op . db_parentObjType , <EOL> op . db_parentObjId ) ) : <EOL> curDict [ ( op . db_what , op . db_objectId ) ] = op <EOL> new_ops . append ( op ) <EOL> elif op . vtType == '<STR_LIT>' : <EOL> if curDict . has_key ( ( op . db_what , op . db_oldObjId ) ) and ( op . db_parentObjId is None or curDict . has_key ( ( op . db_parentObjType , <EOL> op . db_parentObjId ) ) ) : <EOL> del curDict [ ( op . db_what , op . db_oldObjId ) ] <EOL> curDict [ ( op . db_what , op . db_newObjId ) ] = op <EOL> new_ops . append ( op ) <EOL> elif op . vtType == '<STR_LIT>' : <EOL> if ( op . db_parentObjId is None or <EOL> curDict . has_key ( ( op . db_parentObjType , <EOL> op . db_parentObjId ) ) ) and curDict . has_key ( ( op . db_what , op . db_objectId ) ) : <EOL> del curDict [ ( op . db_what , op . db_objectId ) ] <EOL> new_ops . append ( op ) <EOL> action . db_operations = new_ops <EOL> return curDict <EOL> def fixActions ( vistrail , v , actions ) : <EOL> startingChain = getActionChain ( vistrail , v ) <EOL> startingDict = getCurrentOperationDict ( startingChain ) <EOL> addAndFixActions ( startingDict , actions ) <EOL> def getSharedRoot ( vistrail , versions ) : <EOL> current = copy . copy ( versions ) <EOL> while <NUM_LIT:0> not in current : <EOL> maxId = max ( current ) <EOL> if current . count ( maxId ) == len ( current ) : <EOL> return maxId <EOL> else : <EOL> newId = vistrail . db_get_action_by_id ( maxId ) . db_prevId <EOL> for i , v in enumerate ( current ) : <EOL> if v == maxId : <EOL> current [ i ] = newId <EOL> return <NUM_LIT:0> <EOL> def getOperationDiff ( actions , operationDict ) : <EOL> addDict = { } <EOL> deleteDict = { } <EOL> for action in actions : <EOL> for operation in action . db_operations : <EOL> if operation . vtType == '<STR_LIT>' : <EOL> addDict [ ( operation . db_what , <EOL> operation . db_objectId ) ] = operation <EOL> elif operation . vtType == '<STR_LIT>' : <EOL> if operationDict . has_key ( ( operation . db_what , <EOL> operation . db_objectId ) ) : <EOL> deleteDict [ ( operation . db_what , <EOL> operation . db_objectId ) ] = operation <EOL> elif addDict . has_key ( ( operation . db_what , <EOL> operation . db_objectId ) ) : <EOL> del addDict [ ( operation . db_what , <EOL> operation . db_objectId ) ] <EOL> else : <EOL> pass <EOL> elif operation . vtType == '<STR_LIT>' : <EOL> if operationDict . has_key ( ( operation . db_what , <EOL> operation . db_oldObjId ) ) : <EOL> deleteDict [ ( operation . db_what , <EOL> operation . db_oldObjId ) ] = operation <EOL> elif addDict . has_key ( ( operation . db_what , <EOL> operation . db_oldObjId ) ) : <EOL> del addDict [ ( operation . db_what , operation . db_oldObjId ) ] <EOL> addDict [ ( operation . db_what , <EOL> operation . db_newObjId ) ] = operation <EOL> else : <EOL> msg = "<STR_LIT>" % operation . vtType <EOL> raise TypeError ( msg ) <EOL> return ( addDict , deleteDict ) <EOL> def updateOperationDict ( operationDict , deleteOps , addOps ) : <EOL> for operation in deleteOps : <EOL> if operationDict . has_key ( ( operation . db_what , getOldObjId ( operation ) ) ) : <EOL> del operationDict [ ( operation . db_what , getOldObjId ( operation ) ) ] <EOL> else : <EOL> msg = "<STR_LIT>" + operation <EOL> for operation in addOps : <EOL> operationDict [ ( operation . db_what , getNewObjId ( operation ) ) ] = operation <EOL> return operationDict <EOL> def getObjects ( actions ) : <EOL> objects = { } <EOL> for action in actions : <EOL> for operation in action . db_operations : <EOL> if not objects . has_key ( operation . db_what ) : <EOL> objects [ operation . db_what ] = [ ] <EOL> object = copy . copy ( operation . db_data ) <EOL> objects [ operation . db_what ] . append ( object ) <EOL> return objects <EOL> def getVersionDifferences ( vistrail , versions ) : <EOL> sharedRoot = getSharedRoot ( vistrail , versions ) <EOL> sharedActionChain = getActionChain ( vistrail , sharedRoot ) <EOL> sharedOperationDict = getCurrentOperationDict ( sharedActionChain ) <EOL> vOnlySorted = [ ] <EOL> for v in versions : <EOL> vActions = getActionChain ( vistrail , v , sharedRoot ) <EOL> ( vAddDict , vDeleteDict ) = getOperationDiff ( vActions , <EOL> sharedOperationDict ) <EOL> vOnlyAdds = vAddDict . values ( ) <EOL> vOnlyAdds . sort ( key = lambda x : x . db_id ) <EOL> vOnlyDeletes = vDeleteDict . values ( ) <EOL> vOnlyDeletes . sort ( key = lambda x : x . db_id ) <EOL> vOpDict = copy . copy ( sharedOperationDict ) <EOL> updateOperationDict ( vOpDict , vOnlyDeletes , vOnlyAdds ) <EOL> vOps = vOpDict . values ( ) <EOL> vOps . sort ( key = lambda x : x . db_id ) <EOL> vOnlySorted . append ( ( vOnlyAdds , vOnlyDeletes , vOps ) ) <EOL> sharedOps = sharedOperationDict . values ( ) <EOL> sharedOps . sort ( key = lambda x : x . db_id ) <EOL> return ( sharedOps , vOnlySorted ) <EOL> def heuristicModuleMatch ( m1 , m2 ) : <EOL> """<STR_LIT>""" <EOL> if m1 . db_name == m2 . db_name and m1 . db_namespace == m2 . db_namespace and m1 . db_package == m2 . db_package : <EOL> if m1 . vtType == '<STR_LIT>' : <EOL> m1_desc = None <EOL> m2_desc = None <EOL> if '<STR_LIT>' in m1 . db_annotations_key_index : <EOL> m1_desc = m1 . db_annotations_key_index [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in m2 . db_annotations_key_index : <EOL> m2_desc = m2 . db_annotations_key_index [ '<STR_LIT>' ] <EOL> if not ( m1_desc and m2_desc and m1_desc == m2_desc ) : <EOL> return <NUM_LIT:0> <EOL> m1_functions = copy . copy ( m1 . db_get_functions ( ) ) <EOL> m2_functions = copy . copy ( m2 . db_get_functions ( ) ) <EOL> if len ( m1_functions ) != len ( m2_functions ) : <EOL> return <NUM_LIT:0> <EOL> for f1 in m1_functions [ : ] : <EOL> match = None <EOL> for f2 in m2_functions : <EOL> isMatch = heuristicFunctionMatch ( f1 , f2 ) <EOL> if isMatch == <NUM_LIT:1> : <EOL> match = f2 <EOL> break <EOL> if match is not None : <EOL> m1_functions . remove ( f1 ) <EOL> m2_functions . remove ( f2 ) <EOL> else : <EOL> return <NUM_LIT:0> <EOL> m1_cparams = copy . copy ( m1 . db_get_controlParameters ( ) ) <EOL> m2_cparams = copy . copy ( m2 . db_get_controlParameters ( ) ) <EOL> if len ( m1_cparams ) != len ( m2_cparams ) : <EOL> return <NUM_LIT:0> <EOL> for cp1 in m1_cparams [ : ] : <EOL> match = None <EOL> for cp2 in m2_cparams : <EOL> isMatch = heuristicControlParameterMatch ( cp1 , cp2 ) <EOL> if isMatch == <NUM_LIT:1> : <EOL> match = cp2 <EOL> break <EOL> if match is not None : <EOL> m1_cparams . remove ( cp1 ) <EOL> m2_cparams . remove ( cp2 ) <EOL> else : <EOL> return <NUM_LIT:0> <EOL> m1_annots = copy . copy ( m1 . db_get_annotations ( ) ) <EOL> m2_annots = copy . copy ( m2 . db_get_annotations ( ) ) <EOL> if len ( m1_annots ) != len ( m2_annots ) : <EOL> return <NUM_LIT:0> <EOL> for a1 in m1_annots [ : ] : <EOL> match = None <EOL> for a2 in m2_annots : <EOL> isMatch = heuristicAnnotationMatch ( a1 , a2 ) <EOL> if isMatch == <NUM_LIT:1> : <EOL> match = a2 <EOL> break <EOL> if match is not None : <EOL> m1_annots . remove ( a1 ) <EOL> m2_annots . remove ( a2 ) <EOL> else : <EOL> return <NUM_LIT:0> <EOL> if len ( m1_functions ) == len ( m2_functions ) == len ( m1_cparams ) == len ( m2_cparams ) == len ( m1_annots ) == len ( m2_annots ) == <NUM_LIT:0> : <EOL> return <NUM_LIT:1> <EOL> else : <EOL> return <NUM_LIT:0> <EOL> return - <NUM_LIT:1> <EOL> def heuristicFunctionMatch ( f1 , f2 ) : <EOL> """<STR_LIT>""" <EOL> if f1 . db_name == f2 . db_name : <EOL> f1_parameters = copy . copy ( f1 . db_get_parameters ( ) ) <EOL> f2_parameters = copy . copy ( f2 . db_get_parameters ( ) ) <EOL> if len ( f1_parameters ) != len ( f2_parameters ) : <EOL> return <NUM_LIT:0> <EOL> for p1 in f1_parameters [ : ] : <EOL> match = None <EOL> for p2 in f2_parameters : <EOL> isMatch = heuristicParameterMatch ( p1 , p2 ) <EOL> if isMatch == <NUM_LIT:1> : <EOL> match = p2 <EOL> break <EOL> if match is not None : <EOL> f1_parameters . remove ( p1 ) <EOL> f2_parameters . remove ( match ) <EOL> else : <EOL> return <NUM_LIT:0> <EOL> if len ( f1_parameters ) == len ( f2_parameters ) == <NUM_LIT:0> : <EOL> return <NUM_LIT:1> <EOL> else : <EOL> return <NUM_LIT:0> <EOL> return - <NUM_LIT:1> <EOL> def heuristicParameterMatch ( p1 , p2 ) : <EOL> """<STR_LIT>""" <EOL> if p1 . db_type == p2 . db_type and p1 . db_pos == p2 . db_pos : <EOL> if p1 . db_val == p2 . db_val : <EOL> return <NUM_LIT:1> <EOL> else : <EOL> return <NUM_LIT:0> <EOL> return - <NUM_LIT:1> <EOL> def heuristicControlParameterMatch ( cp1 , cp2 ) : <EOL> """<STR_LIT>""" <EOL> if cp1 . db_name == cp2 . db_name : <EOL> if cp1 . db_value == cp2 . db_value : <EOL> return <NUM_LIT:1> <EOL> else : <EOL> return <NUM_LIT:0> <EOL> return - <NUM_LIT:1> <EOL> def heuristicAnnotationMatch ( a1 , a2 ) : <EOL> """<STR_LIT>""" <EOL> if a1 . db_key == a2 . db_key : <EOL> if a1 . db_value == a2 . db_value : <EOL> return <NUM_LIT:1> <EOL> else : <EOL> return <NUM_LIT:0> <EOL> return - <NUM_LIT:1> <EOL> def heuristicConnectionMatch ( c1 , c2 ) : <EOL> """<STR_LIT>""" <EOL> c1_ports = copy . copy ( c1 . db_get_ports ( ) ) <EOL> c2_ports = copy . copy ( c2 . db_get_ports ( ) ) <EOL> for p1 in c1_ports [ : ] : <EOL> match = None <EOL> for p2 in c2_ports : <EOL> isMatch = heuristicPortMatch ( p1 , p2 ) <EOL> if isMatch == <NUM_LIT:1> : <EOL> match = p2 <EOL> break <EOL> elif isMatch == <NUM_LIT:0> : <EOL> match = p2 <EOL> if match is not None : <EOL> c1_ports . remove ( p1 ) <EOL> c2_ports . remove ( match ) <EOL> else : <EOL> return - <NUM_LIT:1> <EOL> if len ( c1_ports ) == len ( c2_ports ) == <NUM_LIT:0> : <EOL> return <NUM_LIT:1> <EOL> return - <NUM_LIT:1> <EOL> def heuristicPortMatch ( p1 , p2 ) : <EOL> """<STR_LIT>""" <EOL> if p1 . db_moduleId == p2 . db_moduleId : <EOL> return <NUM_LIT:1> <EOL> elif p1 . db_type == p2 . db_type and p1 . db_moduleName == p2 . db_moduleName and p1 . sig == p2 . sig : <EOL> return <NUM_LIT:0> <EOL> return - <NUM_LIT:1> <EOL> def function_sig ( function ) : <EOL> return ( function . db_name , <EOL> [ ( param . db_type , param . db_val ) <EOL> for param in function . db_get_parameters ( ) ] ) <EOL> def getParamChanges ( m1 , m2 , same_vt = True , heuristic_match = True ) : <EOL> paramChanges = [ ] <EOL> m1_functions = m1 . db_get_functions ( ) <EOL> m2_functions = m2 . db_get_functions ( ) <EOL> m1_unmatched = [ ] <EOL> m2_unmatched = [ ] <EOL> if same_vt : <EOL> for f1 in m1_functions : <EOL> f2 = m2 . db_get_function ( f1 . db_id ) <EOL> if f2 is None : <EOL> m1_unmatched . append ( f1 ) <EOL> else : <EOL> if heuristic_match : <EOL> matchValue = heuristicFunctionMatch ( f1 , f2 ) <EOL> if matchValue != <NUM_LIT:1> : <EOL> paramChanges . append ( ( function_sig ( f1 ) , <EOL> function_sig ( f2 ) ) ) <EOL> else : <EOL> paramChanges . append ( ( function_sig ( f1 ) , function_sig ( f2 ) ) ) <EOL> for f2 in m2_functions : <EOL> if m1 . db_get_function ( f2 . db_id ) is None : <EOL> m2_unmatched . append ( f2 ) <EOL> else : <EOL> m1_unmatched . extend ( m1_functions ) <EOL> m2_unmatched . extend ( m2_functions ) <EOL> if len ( m1_unmatched ) + len ( m2_unmatched ) > <NUM_LIT:0> : <EOL> if heuristic_match and len ( m1_unmatched ) > <NUM_LIT:0> and len ( m2_unmatched ) > <NUM_LIT:0> : <EOL> for f1 in m1_unmatched [ : ] : <EOL> matched = False <EOL> matchValue = <NUM_LIT:0> <EOL> for f2 in m2_unmatched : <EOL> matchValue = heuristicFunctionMatch ( f1 , f2 ) <EOL> if matchValue == <NUM_LIT:1> : <EOL> matched = f1 <EOL> break <EOL> elif matchValue == <NUM_LIT:0> : <EOL> matched = f1 <EOL> if matched : <EOL> if matchValue != <NUM_LIT:1> : <EOL> paramChanges . append ( ( function_sig ( f1 ) , <EOL> function_sig ( f2 ) ) ) <EOL> m1_unmatched . remove ( f1 ) <EOL> m2_unmatched . remove ( f2 ) <EOL> for f in m1_unmatched : <EOL> paramChanges . append ( ( function_sig ( f ) , ( None , None ) ) ) <EOL> for f in m2_unmatched : <EOL> paramChanges . append ( ( ( None , None ) , function_sig ( f ) ) ) <EOL> return paramChanges <EOL> def getCParamChanges ( m1 , m2 , same_vt = True , heuristic_match = True ) : <EOL> cparamChanges = [ ] <EOL> m1_cparams = m1 . db_get_controlParameters ( ) <EOL> m2_cparams = m2 . db_get_controlParameters ( ) <EOL> m1_unmatched = [ ] <EOL> m2_unmatched = [ ] <EOL> if same_vt : <EOL> for cp1 in m1_cparams : <EOL> cp2 = m2 . db_get_controlParameter ( cp1 . db_id ) <EOL> if cp2 is None : <EOL> m1_unmatched . append ( cp1 ) <EOL> else : <EOL> if heuristic_match : <EOL> matchValue = heuristicControlParameterMatch ( cp1 , cp2 ) <EOL> if matchValue != <NUM_LIT:1> : <EOL> cparamChanges . append ( ( ( cp1 . db_name , cp1 . db_value ) , <EOL> ( cp2 . db_name , cp2 . db_value ) ) ) <EOL> else : <EOL> cparamChanges . append ( ( ( cp1 . db_name , cp1 . db_value ) , <EOL> ( cp2 . db_name , cp2 . db_value ) ) ) <EOL> for cp2 in m2_cparams : <EOL> if m1 . db_get_controlParameter ( cp2 . db_id ) is None : <EOL> m2_unmatched . append ( cp2 ) <EOL> else : <EOL> m1_unmatched . extend ( m1_cparams ) <EOL> m2_unmatched . extend ( m2_cparams ) <EOL> if len ( m1_unmatched ) + len ( m2_unmatched ) > <NUM_LIT:0> : <EOL> if heuristic_match and len ( m1_unmatched ) > <NUM_LIT:0> and len ( m2_unmatched ) > <NUM_LIT:0> : <EOL> for cp1 in m1_unmatched [ : ] : <EOL> matched = False <EOL> matchValue = <NUM_LIT:0> <EOL> for cp2 in m2_unmatched : <EOL> matchValue = heuristicControlParameterMatch ( cp1 , cp2 ) <EOL> if matchValue == <NUM_LIT:1> : <EOL> matched = cp1 <EOL> break <EOL> elif matchValue == <NUM_LIT:0> : <EOL> matched = cp1 <EOL> if matched : <EOL> if matchValue != <NUM_LIT:1> : <EOL> cparamChanges . append ( ( ( cp1 . db_name , cp1 . db_value ) , <EOL> ( cp2 . db_name , cp2 . db_value ) ) ) <EOL> m1_unmatched . remove ( cp1 ) <EOL> m2_unmatched . remove ( cp2 ) <EOL> for cp in m1_unmatched : <EOL> cparamChanges . append ( ( ( cp . db_name , cp . db_value ) , ( None , None ) ) ) <EOL> for cp in m2_unmatched : <EOL> cparamChanges . append ( ( ( None , None ) , ( cp . db_name , cp . db_value ) ) ) <EOL> return cparamChanges <EOL> def getAnnotationChanges ( m1 , m2 , same_vt = True , heuristic_match = True ) : <EOL> annotChanges = [ ] <EOL> m1_annots = m1 . db_get_annotations ( ) <EOL> m2_annots = m2 . db_get_annotations ( ) <EOL> m1_unmatched = [ ] <EOL> m2_unmatched = [ ] <EOL> if same_vt : <EOL> for a1 in m1_annots : <EOL> a2 = m2 . db_get_annotation ( a1 . db_id ) <EOL> if a2 is None : <EOL> m1_unmatched . append ( a1 ) <EOL> else : <EOL> if heuristic_match : <EOL> matchValue = heuristicAnnotationMatch ( a1 , a2 ) <EOL> if matchValue != <NUM_LIT:1> : <EOL> annotChanges . append ( ( ( a1 . db_key , a1 . db_value ) , <EOL> ( a2 . db_key , a2 . db_value ) ) ) <EOL> else : <EOL> annotChanges . append ( ( ( a1 . db_key , a1 . db_value ) , <EOL> ( a2 . db_key , a2 . db_value ) ) ) <EOL> for a2 in m2_annots : <EOL> if m1 . db_get_annotation ( a2 . db_id ) is None : <EOL> m2_unmatched . append ( a2 ) <EOL> else : <EOL> m1_unmatched . extend ( m1_annots ) <EOL> m2_unmatched . extend ( m2_annots ) <EOL> if len ( m1_unmatched ) + len ( m2_unmatched ) > <NUM_LIT:0> : <EOL> if heuristic_match and len ( m1_unmatched ) > <NUM_LIT:0> and len ( m2_unmatched ) > <NUM_LIT:0> : <EOL> for a1 in m1_unmatched [ : ] : <EOL> matched = False <EOL> matchValue = <NUM_LIT:0> <EOL> for a2 in m2_unmatched : <EOL> matchValue = heuristicAnnotationMatch ( a1 , a2 ) <EOL> if matchValue == <NUM_LIT:1> : <EOL> matched = a1 <EOL> break <EOL> elif matchValue == <NUM_LIT:0> : <EOL> matched = a1 <EOL> if matched : <EOL> if matchValue != <NUM_LIT:1> : <EOL> annotChanges . append ( ( ( a1 . db_key , a1 . db_value ) , <EOL> ( a2 . db_key , a2 . db_value ) ) ) <EOL> m1_unmatched . remove ( a1 ) <EOL> m2_unmatched . remove ( a2 ) <EOL> for cp in m1_unmatched : <EOL> annotChanges . append ( ( ( cp . db_key , cp . db_value ) , ( None , None ) ) ) <EOL> for cp in m2_unmatched : <EOL> annotChanges . append ( ( ( None , None ) , ( cp . db_key , cp . db_value ) ) ) <EOL> return annotChanges <EOL> def getOldObjId ( operation ) : <EOL> if operation . vtType == '<STR_LIT>' : <EOL> return operation . db_oldObjId <EOL> return operation . db_objectId <EOL> def getNewObjId ( operation ) : <EOL> if operation . vtType == '<STR_LIT>' : <EOL> return operation . db_newObjId <EOL> return operation . db_objectId <EOL> def setOldObjId ( operation , id ) : <EOL> if operation . vtType == '<STR_LIT>' : <EOL> operation . db_oldObjId = id <EOL> else : <EOL> operation . db_objectId = id <EOL> def setNewObjId ( operation , id ) : <EOL> if operation . vtType == '<STR_LIT>' : <EOL> operation . db_newObjId = id <EOL> else : <EOL> operation . db_objectId = id <EOL> def getWorkflowDiffCommon ( vistrail , v1 , v2 , heuristic_match = True ) : <EOL> ( sharedOps , vOnlyOps ) = getVersionDifferences ( vistrail , [ v1 , v2 ] ) <EOL> sharedWorkflow = DBWorkflow ( ) <EOL> performAdds ( sharedOps , sharedWorkflow ) <EOL> v1Workflow = DBWorkflow ( ) <EOL> v1Ops = vOnlyOps [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] <EOL> performAdds ( v1Ops , v1Workflow ) <EOL> v2Workflow = DBWorkflow ( ) <EOL> v2Ops = vOnlyOps [ <NUM_LIT:1> ] [ <NUM_LIT:2> ] <EOL> performAdds ( v2Ops , v2Workflow ) <EOL> sharedModuleIds = [ ] <EOL> sharedConnectionIds = [ ] <EOL> sharedFunctionIds = { } <EOL> sharedCParameterIds = { } <EOL> sharedAnnotationIds = { } <EOL> for op in sharedOps : <EOL> if op . what == '<STR_LIT>' or op . what == '<STR_LIT>' or op . what == '<STR_LIT>' : <EOL> sharedModuleIds . append ( getNewObjId ( op ) ) <EOL> elif op . what == '<STR_LIT>' : <EOL> sharedConnectionIds . append ( getNewObjId ( op ) ) <EOL> elif op . what == '<STR_LIT>' : <EOL> sharedFunctionIds [ getNewObjId ( op ) ] = op . db_parentObjId <EOL> elif op . what == '<STR_LIT>' : <EOL> sharedCParameterIds [ getNewObjId ( op ) ] = op . db_parentObjId <EOL> elif op . what == '<STR_LIT>' : <EOL> sharedAnnotationIds [ getNewObjId ( op ) ] = op . db_parentObjId <EOL> vOnlyModules = [ ] <EOL> vOnlyConnections = [ ] <EOL> paramChgModules = { } <EOL> cparamChgModules = { } <EOL> annotChgModules = { } <EOL> for ( vAdds , vDeletes , _ ) in vOnlyOps : <EOL> moduleDeleteIds = [ ] <EOL> connectionDeleteIds = [ ] <EOL> for op in vDeletes : <EOL> if op . what == '<STR_LIT>' or op . what == '<STR_LIT>' or op . what == '<STR_LIT>' : <EOL> moduleDeleteIds . append ( getOldObjId ( op ) ) <EOL> if getOldObjId ( op ) in sharedModuleIds : <EOL> sharedModuleIds . remove ( getOldObjId ( op ) ) <EOL> if paramChgModules . has_key ( getOldObjId ( op ) ) : <EOL> del paramChgModules [ getOldObjId ( op ) ] <EOL> if cparamChgModules . has_key ( getOldObjId ( op ) ) : <EOL> del cparamChgModules [ getOldObjId ( op ) ] <EOL> if annotChgModules . has_key ( getOldObjId ( op ) ) : <EOL> del annotChgModules [ getOldObjId ( op ) ] <EOL> elif op . what == '<STR_LIT>' and ( op . db_parentObjType == '<STR_LIT>' or <EOL> op . db_parentObjType == '<STR_LIT>' or <EOL> op . db_parentObjType == '<STR_LIT>' ) and op . db_parentObjId in sharedModuleIds : <EOL> paramChgModules [ op . db_parentObjId ] = None <EOL> sharedModuleIds . remove ( op . db_parentObjId ) <EOL> elif op . what == '<STR_LIT>' and op . db_parentObjType == '<STR_LIT>' and sharedFunctionIds . has_key ( op . db_parentObjId ) : <EOL> moduleId = sharedFunctionIds [ op . db_parentObjId ] <EOL> if moduleId in sharedModuleIds : <EOL> paramChgModules [ moduleId ] = None <EOL> sharedModuleIds . remove ( moduleId ) <EOL> elif op . what == '<STR_LIT>' and ( op . db_parentObjType == '<STR_LIT>' or <EOL> op . db_parentObjType == '<STR_LIT>' or <EOL> op . db_parentObjType == '<STR_LIT>' ) and op . db_parentObjId in sharedCParameterIds and op . db_parentObjId in sharedModuleIds : <EOL> cparamChgModules [ op . db_parentObjId ] = None <EOL> sharedModuleIds . remove ( op . db_parentObjId ) <EOL> elif op . what == '<STR_LIT>' and ( op . db_parentObjType == '<STR_LIT>' or <EOL> op . db_parentObjType == '<STR_LIT>' or <EOL> op . db_parentObjType == '<STR_LIT>' ) and op . db_parentObjId in sharedAnnotationIds and op . db_parentObjId in sharedModuleIds : <EOL> annotChgModules [ op . db_parentObjId ] = None <EOL> sharedModuleIds . remove ( op . db_parentObjId ) <EOL> elif op . what == '<STR_LIT>' : <EOL> connectionDeleteIds . append ( getOldObjId ( op ) ) <EOL> if getOldObjId ( op ) in sharedConnectionIds : <EOL> sharedConnectionIds . remove ( getOldObjId ( op ) ) <EOL> moduleAddIds = [ ] <EOL> connectionAddIds = [ ] <EOL> for op in vAdds : <EOL> if op . what == '<STR_LIT>' or op . what == '<STR_LIT>' or op . what == '<STR_LIT>' : <EOL> moduleAddIds . append ( getNewObjId ( op ) ) <EOL> elif ( op . what == '<STR_LIT>' and <EOL> ( op . db_parentObjType == '<STR_LIT>' or <EOL> op . db_parentObjType == '<STR_LIT>' or <EOL> op . db_parentObjType == '<STR_LIT>' ) and <EOL> op . db_parentObjId in sharedModuleIds ) : <EOL> paramChgModules [ op . db_parentObjId ] = None <EOL> sharedModuleIds . remove ( op . db_parentObjId ) <EOL> elif op . what == '<STR_LIT>' and op . db_parentObjType == '<STR_LIT>' and sharedFunctionIds . has_key ( op . db_parentObjId ) : <EOL> moduleId = sharedFunctionIds [ op . db_parentObjId ] <EOL> if moduleId in sharedModuleIds : <EOL> paramChgModules [ moduleId ] = None <EOL> sharedModuleIds . remove ( moduleId ) <EOL> elif ( op . what == '<STR_LIT>' and <EOL> ( op . db_parentObjType == '<STR_LIT>' or <EOL> op . db_parentObjType == '<STR_LIT>' or <EOL> op . db_parentObjType == '<STR_LIT>' ) and <EOL> op . db_parentObjId in sharedCParameterIds and <EOL> op . db_parentObjId in sharedModuleIds ) : <EOL> cparamChgModules [ op . db_parentObjId ] = None <EOL> sharedModuleIds . remove ( op . db_parentObjId ) <EOL> elif ( op . what == '<STR_LIT>' and <EOL> ( op . db_parentObjType == '<STR_LIT>' or <EOL> op . db_parentObjType == '<STR_LIT>' or <EOL> op . db_parentObjType == '<STR_LIT>' ) and <EOL> op . db_parentObjId in sharedAnnotationIds and <EOL> op . db_parentObjId in sharedModuleIds ) : <EOL> annotChgModules [ op . db_parentObjId ] = None <EOL> sharedModuleIds . remove ( op . db_parentObjId ) <EOL> elif op . what == '<STR_LIT>' : <EOL> connectionAddIds . append ( getOldObjId ( op ) ) <EOL> vOnlyModules . append ( ( moduleAddIds , moduleDeleteIds ) ) <EOL> vOnlyConnections . append ( ( connectionAddIds , connectionDeleteIds ) ) <EOL> sharedModulePairs = [ ( id , id ) for id in sharedModuleIds ] <EOL> v1Only = vOnlyModules [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> v2Only = vOnlyModules [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> for id in vOnlyModules [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] : <EOL> if id not in vOnlyModules [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] : <EOL> v1Only . append ( id ) <EOL> for id in vOnlyModules [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] : <EOL> if id not in vOnlyModules [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] : <EOL> v2Only . append ( id ) <EOL> sharedConnectionPairs = [ ( id , id ) for id in sharedConnectionIds ] <EOL> c1Only = vOnlyConnections [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> c2Only = vOnlyConnections [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> for id in vOnlyConnections [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] : <EOL> if id not in vOnlyConnections [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] : <EOL> c1Only . append ( id ) <EOL> for id in vOnlyConnections [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] : <EOL> if id not in vOnlyConnections [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] : <EOL> c2Only . append ( id ) <EOL> paramChgModulePairs = [ ( id , id ) for id in paramChgModules . keys ( ) ] <EOL> cparamChgModulePairs = [ ( id , id ) for id in cparamChgModules . keys ( ) ] <EOL> annotChgModulePairs = [ ( id , id ) for id in annotChgModules . keys ( ) ] <EOL> c1Only , c2Only , heuristicConnectionPairs = [ ] , [ ] , [ ] <EOL> if heuristic_match : <EOL> ( heuristicModulePairs , heuristicConnectionPairs , v1Only , v2Only , c1Only , c2Only ) = do_heuristic_diff ( v1Workflow , v2Workflow , v1Only , v2Only , c1Only , c2Only ) <EOL> paramChgModulePairs . extend ( heuristicModulePairs ) <EOL> cparamChgModulePairs . extend ( heuristicModulePairs ) <EOL> annotChgModulePairs . extend ( heuristicModulePairs ) <EOL> allChgModulePairs = list ( set ( chain ( paramChgModulePairs , <EOL> cparamChgModulePairs , <EOL> annotChgModulePairs ) ) ) <EOL> ( heuristicModulePairs , paramChanges , cparam_changes , annot_changes ) = check_params_diff ( v1Workflow , v2Workflow , allChgModulePairs , <EOL> True , heuristic_match ) <EOL> return ( v1Workflow , v2Workflow , <EOL> sharedModulePairs , heuristicModulePairs , v1Only , v2Only , <EOL> paramChanges , cparam_changes , annot_changes , <EOL> sharedConnectionPairs , heuristicConnectionPairs , <EOL> c1Only , c2Only ) <EOL> def do_heuristic_diff ( v1Workflow , v2Workflow , v1_modules , v2_modules , <EOL> v1_connections , v2_connections ) : <EOL> heuristicModulePairs = [ ] <EOL> heuristicConnectionPairs = [ ] <EOL> v1Only = copy . copy ( v1_modules ) <EOL> v2Only = copy . copy ( v2_modules ) <EOL> c1Only = copy . copy ( v1_connections ) <EOL> c2Only = copy . copy ( v2_connections ) <EOL> for m1_id in v1Only [ : ] : <EOL> m1 = v1Workflow . db_get_module ( m1_id ) <EOL> match = None <EOL> for m2_id in v2Only : <EOL> m2 = v2Workflow . db_get_module ( m2_id ) <EOL> isMatch = heuristicModuleMatch ( m1 , m2 ) <EOL> if isMatch == <NUM_LIT:1> : <EOL> match = ( m1_id , m2_id ) <EOL> break <EOL> elif isMatch == <NUM_LIT:0> : <EOL> match = ( m1_id , m2_id ) <EOL> if match is not None : <EOL> v1Only . remove ( match [ <NUM_LIT:0> ] ) <EOL> v2Only . remove ( match [ <NUM_LIT:1> ] ) <EOL> heuristicModulePairs . append ( match ) <EOL> for c1_id in c1Only [ : ] : <EOL> c1 = v1Workflow . db_get_connection ( c1_id ) <EOL> match = None <EOL> for c2_id in c2Only : <EOL> c2 = v2Workflow . db_get_connection ( c2_id ) <EOL> isMatch = heuristicConnectionMatch ( c1 , c2 ) <EOL> if isMatch == <NUM_LIT:1> : <EOL> match = ( c1_id , c2_id ) <EOL> break <EOL> elif isMatch == <NUM_LIT:0> : <EOL> match = ( c1_id , c2_id ) <EOL> if match is not None : <EOL> c1Only . remove ( match [ <NUM_LIT:0> ] ) <EOL> c2Only . remove ( match [ <NUM_LIT:1> ] ) <EOL> heuristicConnectionPairs . append ( match ) <EOL> return ( heuristicModulePairs , heuristicConnectionPairs , v1Only , v2Only , <EOL> c1Only , c2Only ) <EOL> def check_params_diff ( v1Workflow , v2Workflow , paramChgModulePairs , <EOL> same_vt = True , heuristic_match = True ) : <EOL> matched = [ ] <EOL> paramChanges = [ ] <EOL> cparamChanges = [ ] <EOL> annotChanges = [ ] <EOL> for ( m1_id , m2_id ) in paramChgModulePairs : <EOL> m1 = v1Workflow . db_get_module ( m1_id ) <EOL> m2 = v2Workflow . db_get_module ( m2_id ) <EOL> moduleParamChanges = getParamChanges ( m1 , m2 , same_vt , heuristic_match ) <EOL> if len ( moduleParamChanges ) > <NUM_LIT:0> : <EOL> paramChanges . append ( ( ( m1_id , m2_id ) , moduleParamChanges ) ) <EOL> moduleCParamChanges = getCParamChanges ( m1 , m2 , same_vt , <EOL> heuristic_match ) <EOL> if len ( moduleCParamChanges ) > <NUM_LIT:0> : <EOL> cparamChanges . append ( ( ( m1_id , m2_id ) , moduleCParamChanges ) ) <EOL> moduleAnnotChanges = getAnnotationChanges ( m1 , m2 , same_vt , <EOL> heuristic_match ) <EOL> if len ( moduleAnnotChanges ) > <NUM_LIT:0> : <EOL> annotChanges . append ( ( ( m1_id , m2_id ) , moduleAnnotChanges ) ) <EOL> if len ( moduleParamChanges ) == len ( moduleCParamChanges ) == len ( moduleAnnotChanges ) == <NUM_LIT:0> : <EOL> matched . append ( ( m1_id , m2_id ) ) <EOL> return ( matched , paramChanges , cparamChanges , annotChanges ) <EOL> def getWorkflowDiff ( vt_pair_1 , vt_pair_2 , heuristic_match = True ) : <EOL> ( vistrail_1 , v_1 ) = vt_pair_1 <EOL> ( vistrail_2 , v_2 ) = vt_pair_2 <EOL> if vistrail_1 == vistrail_2 : <EOL> return getWorkflowDiffCommon ( vistrail_1 , v_1 , v_2 , heuristic_match ) <EOL> workflow_1 = materializeWorkflow ( vistrail_1 , v_1 ) <EOL> workflow_2 = materializeWorkflow ( vistrail_2 , v_2 ) <EOL> modules_1 = workflow_1 . db_modules_id_index . keys ( ) <EOL> modules_2 = workflow_2 . db_modules_id_index . keys ( ) <EOL> conns_1 = workflow_1 . db_connections_id_index . keys ( ) <EOL> conns_2 = workflow_2 . db_connections_id_index . keys ( ) <EOL> if heuristic_match : <EOL> ( m_matches , c_matches , modules_1 , modules_2 , conns_1 , conns_2 ) = do_heuristic_diff ( workflow_1 , workflow_2 , modules_1 , modules_2 , conns_1 , conns_2 ) <EOL> ( m_matches , param_changes , cparam_changes , annot_changes ) = check_params_diff ( workflow_1 , workflow_2 , <EOL> m_matches , False , <EOL> heuristic_match ) <EOL> return ( workflow_1 , workflow_2 , [ ] , m_matches , modules_1 , modules_2 , <EOL> param_changes , cparam_changes , annot_changes , [ ] , c_matches , conns_1 , conns_2 ) <EOL> return ( workflow_1 , workflow_2 , [ ] , [ ] , modules_1 , modules_2 , [ ] , [ ] , [ ] , [ ] , [ ] , <EOL> conns_1 , conns_2 ) <EOL> class TestDBVistrailService ( unittest . TestCase ) : <EOL> def test_parameter_heuristic ( self ) : <EOL> from vistrails . core . vistrail . module_param import ModuleParam <EOL> param1 = ModuleParam ( id = <NUM_LIT:0> , pos = <NUM_LIT:0> , type = '<STR_LIT>' , val = '<STR_LIT:abc>' ) <EOL> param2 = ModuleParam ( id = <NUM_LIT:1> , pos = <NUM_LIT:0> , type = '<STR_LIT>' , val = '<STR_LIT:abc>' ) <EOL> param3 = ModuleParam ( id = <NUM_LIT:2> , pos = <NUM_LIT:1> , type = '<STR_LIT>' , val = '<STR_LIT:1.0>' ) <EOL> param4 = ModuleParam ( id = <NUM_LIT:3> , pos = <NUM_LIT:0> , type = '<STR_LIT>' , val = '<STR_LIT>' ) <EOL> param5 = ModuleParam ( id = <NUM_LIT:4> , pos = <NUM_LIT:1> , type = '<STR_LIT>' , val = '<STR_LIT:abc>' ) <EOL> assert heuristicParameterMatch ( param1 , param2 ) == <NUM_LIT:1> <EOL> assert heuristicParameterMatch ( param1 , param3 ) == - <NUM_LIT:1> <EOL> assert heuristicParameterMatch ( param1 , param4 ) == <NUM_LIT:0> <EOL> assert heuristicParameterMatch ( param1 , param5 ) == - <NUM_LIT:1> <EOL> def test_function_heuristic ( self ) : <EOL> from vistrails . core . vistrail . module_param import ModuleParam <EOL> from vistrails . core . vistrail . module_function import ModuleFunction <EOL> param1 = ModuleParam ( id = <NUM_LIT:0> , pos = <NUM_LIT:0> , type = '<STR_LIT>' , val = '<STR_LIT:abc>' ) <EOL> param2 = ModuleParam ( id = <NUM_LIT:1> , pos = <NUM_LIT:1> , type = '<STR_LIT>' , val = '<STR_LIT:1.0>' ) <EOL> param3 = ModuleParam ( id = <NUM_LIT:2> , pos = <NUM_LIT:0> , type = '<STR_LIT>' , val = '<STR_LIT:abc>' ) <EOL> param4 = ModuleParam ( id = <NUM_LIT:3> , pos = <NUM_LIT:1> , type = '<STR_LIT>' , val = '<STR_LIT:1.0>' ) <EOL> param5 = ModuleParam ( id = <NUM_LIT:4> , pos = <NUM_LIT:0> , type = '<STR_LIT>' , val = '<STR_LIT:abc>' ) <EOL> param6 = ModuleParam ( id = <NUM_LIT:5> , pos = <NUM_LIT:1> , type = '<STR_LIT>' , val = '<STR_LIT>' ) <EOL> function1 = ModuleFunction ( name = '<STR_LIT>' , parameters = [ param1 , param2 ] ) <EOL> function2 = ModuleFunction ( name = '<STR_LIT>' , parameters = [ param3 , param4 ] ) <EOL> function3 = ModuleFunction ( name = '<STR_LIT>' , parameters = [ param5 , param6 ] ) <EOL> function4 = ModuleFunction ( name = '<STR_LIT>' , parameters = [ param1 , param2 ] ) <EOL> function5 = ModuleFunction ( name = '<STR_LIT>' , parameters = [ param1 ] ) <EOL> assert heuristicFunctionMatch ( function1 , function2 ) == <NUM_LIT:1> <EOL> assert heuristicFunctionMatch ( function1 , function3 ) == <NUM_LIT:0> <EOL> assert heuristicFunctionMatch ( function1 , function4 ) == - <NUM_LIT:1> <EOL> assert heuristicFunctionMatch ( function1 , function5 ) == <NUM_LIT:0> <EOL> def test_module_heuristic ( self ) : <EOL> from vistrails . core . vistrail . module_param import ModuleParam <EOL> from vistrails . core . vistrail . module_function import ModuleFunction <EOL> from vistrails . core . vistrail . module import Module <EOL> param1 = ModuleParam ( id = <NUM_LIT:0> , pos = <NUM_LIT:0> , type = '<STR_LIT>' , val = '<STR_LIT:abc>' ) <EOL> param2 = ModuleParam ( id = <NUM_LIT:1> , pos = <NUM_LIT:1> , type = '<STR_LIT>' , val = '<STR_LIT:1.0>' ) <EOL> param3 = ModuleParam ( id = <NUM_LIT:2> , pos = <NUM_LIT:0> , type = '<STR_LIT>' , val = '<STR_LIT:abc>' ) <EOL> param4 = ModuleParam ( id = <NUM_LIT:3> , pos = <NUM_LIT:1> , type = '<STR_LIT>' , val = '<STR_LIT:1.0>' ) <EOL> param5 = ModuleParam ( id = <NUM_LIT:4> , pos = <NUM_LIT:0> , type = '<STR_LIT>' , val = '<STR_LIT:2>' ) <EOL> param6 = ModuleParam ( id = <NUM_LIT:5> , pos = <NUM_LIT:0> , type = '<STR_LIT>' , val = '<STR_LIT:2>' ) <EOL> function1 = ModuleFunction ( name = '<STR_LIT>' , parameters = [ param1 , param2 ] ) <EOL> function2 = ModuleFunction ( name = '<STR_LIT>' , parameters = [ param3 , param4 ] ) <EOL> function3 = ModuleFunction ( name = '<STR_LIT>' , parameters = [ param5 ] ) <EOL> function4 = ModuleFunction ( name = '<STR_LIT>' , parameters = [ param6 ] ) <EOL> function5 = ModuleFunction ( name = '<STR_LIT>' , parameters = [ param2 , param4 ] ) <EOL> function6 = ModuleFunction ( name = '<STR_LIT>' , parameters = [ param5 ] ) <EOL> module1 = Module ( name = '<STR_LIT>' , functions = [ function1 , function3 ] ) <EOL> module2 = Module ( name = '<STR_LIT>' , functions = [ function2 , function4 ] ) <EOL> module3 = Module ( name = '<STR_LIT>' , functions = [ function1 , function2 ] ) <EOL> module4 = Module ( name = '<STR_LIT>' , functions = [ function5 ] ) <EOL> module5 = Module ( name = '<STR_LIT>' , functions = [ function5 , function6 ] ) <EOL> assert heuristicModuleMatch ( module1 , module2 ) == <NUM_LIT:1> <EOL> assert heuristicModuleMatch ( module1 , module3 ) == - <NUM_LIT:1> <EOL> assert heuristicModuleMatch ( module1 , module4 ) == <NUM_LIT:0> <EOL> assert heuristicModuleMatch ( module1 , module5 ) == <NUM_LIT:0> <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
