|
| 1 | +import cppl_cpp_python_bridge as cppl_bridge |
| 2 | + |
| 3 | +error = cppl_bridge.error |
| 4 | + |
| 5 | +def init(): |
| 6 | + cppl_bridge.init() |
| 7 | + |
| 8 | +def cleanup(): |
| 9 | + cppl_bridge.cleanup() |
| 10 | + |
| 11 | +class PolicyDefinition(object): |
| 12 | + """docstring for PolicyDefinition""" |
| 13 | + def __init__(self, policyDefinitionPath, functionHandlerPath): |
| 14 | + super(PolicyDefinition, self).__init__() |
| 15 | + self.pd_id = cppl_bridge.read_policy_definition_from_file( policyDefinitionPath, functionHandlerPath) |
| 16 | + |
| 17 | + def get_id(self): |
| 18 | + return self.pd_id |
| 19 | + |
| 20 | + def __del__(self): |
| 21 | + cppl_bridge.policy_definition_free( self.get_id() ) |
| 22 | + |
| 23 | +class NodeParameters(object): |
| 24 | + """docstring for NodeParameters""" |
| 25 | + def __init__(self, nodeParametersPath, nodeRuntimeParametersPath, policyDefinition): |
| 26 | + # sanity checks |
| 27 | + if type(policyDefinition) != PolicyDefinition: |
| 28 | + raise TypeError |
| 29 | + |
| 30 | + super(NodeParameters, self).__init__() |
| 31 | + self.np_id = cppl_bridge.read_node_parameters_from_file( nodeParametersPath, nodeRuntimeParametersPath, policyDefinition.get_id() ) |
| 32 | + |
| 33 | + def get_id(self): |
| 34 | + return self.np_id |
| 35 | + |
| 36 | + def __del__(self): |
| 37 | + cppl_bridge.node_parameters_free( self.get_id() ) |
| 38 | + |
| 39 | +class Policy(object): |
| 40 | + """docstring for Policy""" |
| 41 | + def __init__(self, policyPath): |
| 42 | + super(Policy, self).__init__() |
| 43 | + self.string = cppl_bridge.read_policy_from_file( policyPath ) |
| 44 | + |
| 45 | + def get_string(self): |
| 46 | + return self.string |
| 47 | + |
| 48 | + def compress_to_file(self, destPath, policyDefinition): |
| 49 | + # sanity checks |
| 50 | + if type(policyDefinition) != PolicyDefinition: |
| 51 | + raise TypeError |
| 52 | + |
| 53 | + ret = cppl_bridge.compress_policy_to_file(self.get_string(), policyDefinition.get_id(), destPath) |
| 54 | + |
| 55 | + # def __del__(self): |
| 56 | + # # Nothing todo as only a string is passed from C++ to Python |
| 57 | + # cppl_bridge.compressed_cppl_free( self.get_id() ) |
| 58 | + |
| 59 | +class CompressedPolicy(object): |
| 60 | + """docstring for CompressedPolicy""" |
| 61 | + def __init__(self, compressedPolicyPath, policyDefinition): |
| 62 | + # sanity checks |
| 63 | + if type(policyDefinition) != PolicyDefinition: |
| 64 | + raise TypeError |
| 65 | + |
| 66 | + super(CompressedPolicy, self).__init__() |
| 67 | + self.ccppl_id = cppl_bridge.read_compressed_cppl_from_file( compressedPolicyPath, policyDefinition.get_id() ) |
| 68 | + self.policyDefinition = policyDefinition |
| 69 | + |
| 70 | + def get_id(self): |
| 71 | + return self.ccppl_id |
| 72 | + |
| 73 | + def __del__(self): |
| 74 | + cppl_bridge.compressed_cppl_free( self.get_id() ) |
| 75 | + |
| 76 | + def evaluate(self, nodeParameters, with_reason=True): |
| 77 | + # sanity checks |
| 78 | + if type( nodeParameters ) != NodeParameters: |
| 79 | + raise TypeError |
| 80 | + |
| 81 | + ret_eval = cppl_bridge.evaluate(self.get_id(), nodeParameters.get_id() ) |
| 82 | + |
| 83 | + reason = None |
| 84 | + if with_reason: |
| 85 | + reason = cppl_bridge.get_reason( self.get_id(), self.policyDefinition.get_id() ) |
| 86 | + |
| 87 | + return ret_eval, reason |
0 commit comments