#####################################################################
#####################################################################
## Copyright 2005 Steven R. Newcomb                                ##
##                                                                 ##
## This software is licensed under the Apache License, Version 2.0 ##
## (the "License"); you may not use this file except in compliance ##
## with the License.  Copies of the License are available at       ##
##        http://www.apache.org/licenses/LICENSE-2.0               ##
##                                                                 ##
## Unless required by applicable law or agreed to in writing,      ##
## software distributed under the License is distributed on an "AS ##
## IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either ##
## express or implied.  See the License for the specific language  ##
## governing permissions and limitations under the License.        ##
##                                                                 ##
## Copies of the source code and the license are available at      ##
##        http://www.versavant.org                                 ##
#####################################################################
#####################################################################


#####################################################################
## Implementation of the conferral rule "person name aggregation"
## in Topic Map Application "TMA3", used in versavant/demo.py 
#####################################################################

####################################
cvsRevision = "$Revision: 1.5 $" ##
####################################


import sys

def personNameAggregation1( vsv, conferralRule, **args):

## For each proxy for a subject identified by a
## TMA1,,PersonOrOrganization property, confer an additional proxy for
## that same subject that also has a TMA3,,Person Name List property.
## The value of the TMA1,,PersonOrOrganization property should become
## a list item in the list that is conferred as the value of the
## TMA3,,Person Name List property.
    
    
    topicMap = conferralRule.topicMap  ## make the below code less verbose (and maybe run slightly faster)
    TMA1 = topicMap.TMAs[ args[ 'TMA1Name']]  ## we're getting the other TMA names from the tmaconf.xml file for TMA3
    TMA2 = topicMap.TMAs[ args[ 'TMA2Name']]
    TMA3 = conferralRule.TMA  ## this is the TMA that defines this conferral rule

    personSIPValueList = TMA1.propertyClasses[ 'PersonOrOrganization'].overheadDict.keys()

    for personSIPValue in personSIPValueList:
        personProxyIds = TMA1.propertyClasses[ 'PersonOrOrganization'].overheadDict[ personSIPValue]

        for personProxyId in personProxyIds:
            personProxy = topicMap.proxies[ personProxyId]

            ## get the 'PersonOrOrganization' property
            personPropertyInProxyKey = vsv.catKey( TMA1.TMAName, 'PersonOrOrganization')
            personSIPInstance = personProxy.propertyInstances[ personPropertyInProxyKey]

            ## Don't be triggered by this rule's own output
            conferralRuleTriggerRoleId = vsv.catKey( conferralRule.conferralRuleKey, 'PersonOrOrgName value')
            if personSIPInstance.conferralTriggerAncestry.has_key( conferralRuleTriggerRoleId):
                continue

            triggers = {}  ## new dictionary to work in
            triggers[ 'PersonOrOrgName value'] = personSIPInstance.propertyInstanceKey
            conferredValueSources = {}  ## new dictionary to work in
            conferredValueSources[ 'PersonOrOrgName stuff'] = personSIPInstance.propertyInstanceKey

            if vsv.conferralMayBeSignificant( conferralRule, triggers, conferredValueSources):

                ## new proxy
                newPersonProxy = vsv.newProxyObject( topicMap)

                ## copy the SIP from personSIPInstance to newPersonProxy
                vsv.newPropertyInstance( newPersonProxy,
                                         personSIPInstance.propertyClass.TMA,
                                         personSIPInstance.propertyClass.propertyClassName, 
                                         vsv.versaCopy( personSIPInstance._value),
                                         triggers,
                                         conferredValueSources)
                
                ## add TMA3's way of looking at the same name
                vsv.newPropertyInstance( newPersonProxy,
                                         TMA3,
                                         'Person Name List',
                                         [ personSIPInstance._value],  ## it's a list
                                         triggers,
                                         conferredValueSources)



def personNameAggregation2( vsv, conferralRule, **args):
    ## Triggered by a TMA2,,Person property, confer a TMA3::Person
    ## Name List property that has, as its list items, the names found
    ## in the values of the triggering property's proxy's properties
    ## (its TMA2,,Person property, its TMA2,,Birth Full Name property,
    ## and its TMA2,,Aliases property).

    topicMap = conferralRule.topicMap  ## make the below code less verbose (and maybe run slightly faster)
    TMA1 = topicMap.TMAs[ args[ 'TMA1Name']]  ## we're getting the other TMA names from the tmaconf.xml file for TMA3
    TMA2 = topicMap.TMAs[ args[ 'TMA2Name']]
    TMA3 = conferralRule.TMA  ## this is the TMA that defines this conferral rule

    personSIPValueList = TMA2.propertyClasses[ 'Person'].overheadDict.keys()

    for personSIPValue in personSIPValueList:
        personProxyIds = TMA2.propertyClasses[ 'Person'].overheadDict[ personSIPValue]

        for personProxyId in personProxyIds:
            personProxy = topicMap.proxies[ personProxyId]

            personPropertyInProxyKey = vsv.catKey( TMA2.TMAName, 'Person')
            personSIPInstance = personProxy.propertyInstances[ personPropertyInProxyKey]

            ## Don't be triggered by this rule's own output
            conferralRuleTriggerRoleId = vsv.catKey( conferralRule.conferralRuleKey, 'Person value')

            if personSIPInstance.conferralTriggerAncestry.has_key( conferralRuleTriggerRoleId):
                continue

            triggers = {}  ## new dictionary to work in
            triggers[ 'Person value'] = personSIPInstance.propertyInstanceKey
            conferredValueSources = {}  ## new dictionary to work in
            conferredValueSources[ 'Person stuff'] = personSIPInstance.propertyInstanceKey

            if vsv.conferralMayBeSignificant( conferralRule, triggers, conferredValueSources):

                ## new proxy
                newPersonProxy = vsv.newProxyObject( topicMap)

                ## copy the SIP from personSIPInstance to newPersonProxy
                vsv.newPropertyInstance( newPersonProxy,
                                         personSIPInstance.propertyClass.TMA,
                                         personSIPInstance.propertyClass.propertyClassName, 
                                         vsv.versaCopy( personSIPInstance._value),
                                         triggers,
                                         conferredValueSources)
                
                ## make a list of the names in the various properties of personProxy
                newNameList = []
                newNameList.append( personSIPInstance._value)
                newNameList.append( personProxy.propertyInstances[ vsv.catKey( TMA2.TMAName, 'Birth Full Name')]._value)
                for alias in personProxy.propertyInstances[ vsv.catKey( TMA2.TMAName, 'Aliases')]._value:
                    if alias not in newNameList:
                        newNameList.append( alias)

                ## add TMA3's way of thinking about names
                vsv.newPropertyInstance( newPersonProxy,
                                         TMA3,
                                         'Person Name List',
                                         newNameList,
                                         triggers,
                                         conferredValueSources)
