configurationengine/source/plugins/common/ConeRulePlugin/ruleplugin/evals/layer_utils.py
changeset 0 2e8eeb919028
child 3 e7e0ae78773e
equal deleted inserted replaced
-1:000000000000 0:2e8eeb919028
       
     1 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 # All rights reserved.
       
     3 # This component and the accompanying materials are made available
       
     4 # under the terms of "Eclipse Public License v1.0"
       
     5 # which accompanies this distribution, and is available
       
     6 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 #
       
     8 # Initial Contributors:
       
     9 # Nokia Corporation - initial contribution.
       
    10 #
       
    11 # Contributors:
       
    12 #
       
    13 # Description: 
       
    14 #
       
    15 
       
    16 '''
       
    17 Ruleml eval extension to check is passed ref changed on given layer(range).
       
    18 '''
       
    19 
       
    20 from cone.public import api
       
    21 
       
    22 import logging
       
    23 
       
    24 logger = logging.getLogger('cone.ruleplugin.evals.layer_utils')
       
    25 
       
    26 def give_changed_layers(feat):
       
    27     """
       
    28     Returns a list of booleans where True means that feature is changed in that layer. Index is
       
    29     same than in configuration root's get_configuration_by_index(). 
       
    30     """
       
    31     
       
    32     logger.debug('Checking feature: %s' % feat.fqr)
       
    33     
       
    34     root_conf = feat.get_root_configuration()
       
    35     nro_of_layers = len(root_conf.list_configurations())
       
    36     result = [False] * nro_of_layers
       
    37     
       
    38     for i in range(0, nro_of_layers):
       
    39         conf = root_conf.get_configuration_by_index(i)
       
    40         logger.debug("Traversing data from configuration: %s" % conf.get_path())
       
    41         datas = conf._traverse(type=api.Data, filters=[lambda d: d.fqr==feat.fqr])
       
    42         for data in datas:
       
    43             try:
       
    44                 if data.get_value() != None:
       
    45                     logger.debug("Feature '%s' is changed in layer %s with data '%s'" % (feat.fqr, i, data.get_value()))
       
    46                     result[i] = True
       
    47             except Exception, e:
       
    48                 logger.debug("Failed to check Feature '%s' data in layer %s:", (e,i))
       
    49         if result[i] == False:
       
    50             logger.debug("Feature '%s' is not changed in layer: %s" % (feat.fqr, i))
       
    51     logger.debug("Feature '%s' is changed in layers: %s" % (feat.fqr, result))
       
    52     return result
       
    53 
       
    54 def changed_on_last_layer(feat):
       
    55     """
       
    56     Returns True if feature is changed in the last layer.
       
    57     """
       
    58     
       
    59     root_conf = feat.get_root_configuration()
       
    60     conf = root_conf.get_configuration_by_index(-2)#autoconfig layer is ignored 
       
    61     
       
    62     def check(node):
       
    63         if isinstance(node, api.Data) and node.fqr == feat.fqr:
       
    64             return True
       
    65         for obj in node._objects():
       
    66             if check(obj):
       
    67                 return True
       
    68     
       
    69     if check(conf):
       
    70         return True
       
    71     else:
       
    72         return False
       
    73 
       
    74 def changed_on_autoconfig_layer(feat):
       
    75     """
       
    76     Returns True if feature is changed in the autoconfig layer.
       
    77     """
       
    78     
       
    79     root_conf = feat.get_root_configuration()
       
    80     conf = root_conf.get_configuration_by_index(-1) 
       
    81     
       
    82     def check(node):
       
    83         if isinstance(node, api.Data) and node.fqr == feat.fqr:
       
    84             return True
       
    85         for obj in node._objects():
       
    86             if check(obj):
       
    87                 return True
       
    88     
       
    89     if check(conf):
       
    90         return True
       
    91     else:
       
    92         return False
       
    93 
       
    94 def changed_on_layer(feat, layer):
       
    95     """
       
    96     Returns True if feature is changed in the given layer.
       
    97     """
       
    98     try:
       
    99         return give_changed_layers(feat)[layer]
       
   100     except IndexError, e:
       
   101         logger.warning("Given layer is not found: %s" % (layer))
       
   102         return False
       
   103 
       
   104 def changed_on_layers(feat, findex, tindex):
       
   105     """
       
   106     Returns True if feature is changed in layer of given range.
       
   107     """
       
   108     layers = give_changed_layers(feat)
       
   109     
       
   110     if findex == tindex:
       
   111         return changed_on_layer(feat, findex)
       
   112     
       
   113     for i in range(findex, tindex):
       
   114         if i > len(layers):
       
   115             continue
       
   116         if layers != None and layers[i] != None and layers[i]:
       
   117             return True
       
   118     return False
       
   119