3
|
1 |
#
|
|
2 |
# Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
# All rights reserved.
|
|
4 |
# This component and the accompanying materials are made available
|
|
5 |
# under the terms of "Eclipse Public License v1.0"
|
|
6 |
# which accompanies this distribution, and is available
|
|
7 |
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
#
|
|
9 |
# Initial Contributors:
|
|
10 |
# Nokia Corporation - initial contribution.
|
|
11 |
#
|
|
12 |
# Contributors:
|
|
13 |
#
|
|
14 |
# Description:
|
|
15 |
#
|
|
16 |
'''
|
|
17 |
Legacy RuleML plug-in to support RuleML 1 and 2 after rule engine changes
|
|
18 |
in cone.public.rules. The new RuleML version is 3, and it uses the setting
|
|
19 |
reference format ${MyFeature.MySetting} in the rules.
|
|
20 |
|
|
21 |
The rule engine (rules.py) before the change is contained in this plug-in.
|
|
22 |
|
|
23 |
NOTE THAT THIS PLUG-IN WILL NOT BE MAINTAINED, ALL NEW DEVELOPMENT SHOULD
|
|
24 |
HAPPEN IN THE NEW RULE PLUG-IN. IF NEW FUNCTIONALITY IS REQUIRED IN AN
|
|
25 |
EXISTING RULEML FILE, THE CHANGES SHOULD BE MADE TO THE NEW RULE PLUG-IN
|
|
26 |
AND THE EXISTING RULEML FILE CONVERTED TO RULEML 3.
|
|
27 |
'''
|
|
28 |
|
|
29 |
|
|
30 |
import os
|
|
31 |
import sys
|
|
32 |
import logging
|
|
33 |
import shutil
|
|
34 |
import pkg_resources
|
|
35 |
|
|
36 |
import __init__
|
|
37 |
import re
|
|
38 |
|
|
39 |
from legacyruleplugin import relations, rules
|
|
40 |
from cone.public import exceptions,plugin,utils,api
|
|
41 |
|
|
42 |
class RuleImpl(plugin.ImplBase):
|
|
43 |
"""
|
|
44 |
MakeImpl plugin finds feature references that are configured in a .ruleml file
|
|
45 |
and generate a rule from them
|
|
46 |
"""
|
|
47 |
IMPL_TYPE_ID = 'ruleml'
|
|
48 |
DEFAULT_INVOCATION_PHASE = 'pre'
|
|
49 |
|
|
50 |
def __init__(self, ref, configuration, relation_container):
|
|
51 |
"""
|
|
52 |
Overloading the default constructor
|
|
53 |
"""
|
|
54 |
plugin.ImplBase.__init__(self,ref,configuration)
|
|
55 |
self.relation_container = relation_container
|
|
56 |
|
|
57 |
def list_output_files(self):
|
|
58 |
"""
|
|
59 |
Return a list of output files as an array.
|
|
60 |
"""
|
|
61 |
return []
|
|
62 |
|
|
63 |
def generate(self, context=None):
|
|
64 |
relation_container = self.get_relation_container()
|
|
65 |
relation_container.context = context
|
|
66 |
return relation_container.execute(context)
|
|
67 |
|
|
68 |
def has_tag(self, tags, policy=None):
|
|
69 |
# RuleML should always be executed, regardless of the tags
|
|
70 |
return True
|
|
71 |
|
|
72 |
def get_relation_container(self):
|
|
73 |
return self.relation_container
|
|
74 |
|
|
75 |
def get_refs(self):
|
|
76 |
"""
|
|
77 |
Return a list of all ConfML setting references that affect this
|
|
78 |
implementation. May also return None if references are not relevant
|
|
79 |
for the implementation.
|
|
80 |
"""
|
|
81 |
refs = []
|
|
82 |
relations = self.get_relations()
|
|
83 |
for relation in relations:
|
|
84 |
# get refs from relation return a tuple (left side refs, right side refs)
|
|
85 |
# only the left side refs are the "input" refs
|
|
86 |
refs += relation.get_refs()
|
|
87 |
# If the rules do not have any references return None to disable filter by refs
|
|
88 |
if refs == []:
|
|
89 |
refs = None
|
|
90 |
return refs
|
|
91 |
|
|
92 |
def get_relations(self):
|
|
93 |
return self.relation_container.get_relations()
|
|
94 |
|
|
95 |
class RuleBuiltinsModule(object):
|
|
96 |
pass
|
|
97 |
|
|
98 |
class RulemlRelationContainer(plugin.RelationContainer):
|
|
99 |
"""
|
|
100 |
Relation container for RuleML rules.
|
|
101 |
|
|
102 |
Basically this is a wrapper for rules.RelationContainer that adapts
|
|
103 |
it to the interface of plugin.RelationContainer.
|
|
104 |
"""
|
|
105 |
def __init__(self, configuration, source, rule_list, eval_globals):
|
|
106 |
plugin.RelationContainer.__init__(self, configuration, source=source)
|
|
107 |
self.configuration = configuration
|
|
108 |
self.relation_container = rules.RelationContainerImpl()
|
|
109 |
self.eval_globals = eval_globals
|
|
110 |
self.context = None
|
|
111 |
for rule in rule_list:
|
|
112 |
self.relation_container.add_relation(rule)
|
|
113 |
|
|
114 |
def execute(self, context=None):
|
|
115 |
results = []
|
|
116 |
|
|
117 |
# Create the autoconfig if not done already
|
|
118 |
autoconfig = plugin.get_autoconfig(self.configuration)
|
|
119 |
|
|
120 |
# Register relations etc. to the rule engine.
|
|
121 |
# Due to unit test issues the relations are not registered
|
|
122 |
# in the relations module, but only for the duration of
|
|
123 |
# rule parsing and execution
|
|
124 |
relations.register()
|
|
125 |
try:
|
|
126 |
# Using the configuration to pass the eval globals dict to the
|
|
127 |
# eval expression. The configuration only contains the globals
|
|
128 |
# dict for the duration of the rule execution, so hopefully this
|
|
129 |
# shouldn't mess anything up
|
|
130 |
self._set_builtin_eval_globals()
|
|
131 |
context._eval_expression_globals_dict = self.eval_globals
|
|
132 |
for i, rel in enumerate(self.relation_container):
|
|
133 |
index = i + 1
|
|
134 |
|
|
135 |
# Execute
|
|
136 |
self._execute_relation_and_log_error(rel, self.source, index, context)
|
|
137 |
|
|
138 |
# Collect execution result if supported
|
|
139 |
if hasattr(rel, 'get_execution_result'):
|
|
140 |
result = rel.get_execution_result()
|
|
141 |
if isinstance(result, plugin.RelationExecutionResult):
|
|
142 |
result.source = self.source
|
|
143 |
result.index = index
|
|
144 |
results.append(result)
|
|
145 |
|
|
146 |
del context._eval_expression_globals_dict
|
|
147 |
|
|
148 |
if self.relation_container.has_errors():
|
|
149 |
for error in self.relation_container.get_errors():
|
|
150 |
logging.getLogger('cone.ruleml_relation_container(%s)' % self.source).error(error)
|
|
151 |
|
|
152 |
if self.context:
|
|
153 |
self.context.results += results
|
|
154 |
return results
|
|
155 |
finally:
|
|
156 |
relations.unregister()
|
|
157 |
|
|
158 |
def get_relation_count(self):
|
|
159 |
return len(self.relation_container)
|
|
160 |
|
|
161 |
def get_relations(self):
|
|
162 |
return list(self.relation_container)
|
|
163 |
|
|
164 |
def _set_builtin_eval_globals(self):
|
|
165 |
"""
|
|
166 |
Add built-in attributes into the eval globals dictionary.
|
|
167 |
"""
|
|
168 |
|
|
169 |
builtins = RuleBuiltinsModule()
|
|
170 |
builtins.configuration = self.configuration
|
|
171 |
|
|
172 |
self.eval_globals['ruleml'] = builtins
|
|
173 |
|
|
174 |
class RuleImplReaderBase(plugin.ReaderBase):
|
|
175 |
NAMESPACE = None # Used as a base class, so should have no namespace
|
|
176 |
FILE_EXTENSIONS = ['ruleml']
|
|
177 |
|
|
178 |
def __init__(self, resource_ref, configuration):
|
|
179 |
self.resource_ref = resource_ref
|
|
180 |
self.configuration = configuration
|
|
181 |
|
|
182 |
@classmethod
|
|
183 |
def read_impl(cls, resource_ref, configuration, etree):
|
|
184 |
reader = cls(resource_ref, configuration)
|
|
185 |
|
|
186 |
# Register relations etc. to the rule engine.
|
|
187 |
# Due to unit test issues the relations are not registered
|
|
188 |
# in the relations module, but only for the duration of
|
|
189 |
# rule parsing and execution
|
|
190 |
relations.register()
|
|
191 |
try:
|
|
192 |
rules = reader.parse_rules(resource_ref, etree)
|
|
193 |
eval_globals = reader.parse_eval_globals(etree)
|
|
194 |
lineno = utils.etree.get_lineno(etree)
|
|
195 |
|
|
196 |
# Create an ImplContainer to hold each rule as its own
|
|
197 |
# RuleML implementation
|
|
198 |
main_impl = plugin.ImplContainer(resource_ref, configuration)
|
|
199 |
main_impl.lineno = lineno
|
|
200 |
|
|
201 |
for rule in rules:
|
|
202 |
relation_container = RulemlRelationContainer(
|
|
203 |
configuration = configuration,
|
|
204 |
source = "%s:%d" % (resource_ref, rule.lineno),
|
|
205 |
rule_list = [rule],
|
|
206 |
eval_globals = eval_globals)
|
|
207 |
|
|
208 |
impl = RuleImpl(resource_ref, configuration, relation_container)
|
|
209 |
impl.lineno = rule.lineno
|
|
210 |
rule.implml = impl
|
|
211 |
|
|
212 |
main_impl.append(impl)
|
|
213 |
finally:
|
|
214 |
relations.unregister()
|
|
215 |
|
|
216 |
return main_impl
|
|
217 |
|
|
218 |
class RuleImplReader1(RuleImplReaderBase):
|
|
219 |
NAMESPACE = 'http://www.s60.com/xml/ruleml/1'
|
|
220 |
NAMESPACE_ID = 'ruleml1'
|
|
221 |
ROOT_ELEMENT_NAME = 'ruleml'
|
|
222 |
|
|
223 |
def __init__(self, resource_ref, configuration):
|
|
224 |
RuleImplReaderBase.__init__(self, resource_ref, configuration)
|
|
225 |
|
|
226 |
@classmethod
|
|
227 |
def get_schema_data(cls):
|
|
228 |
return pkg_resources.resource_string('legacyruleplugin', 'xsd/ruleml.xsd')
|
|
229 |
|
|
230 |
def parse_rules(self, ref, etree):
|
|
231 |
rules = []
|
|
232 |
for elem in etree.getiterator("{%s}rule" % self.NAMESPACE):
|
|
233 |
lineno = utils.etree.get_lineno(elem)
|
|
234 |
for rule in relations.RelationFactory.get_relations(self.configuration, elem.text):
|
|
235 |
rule.ref = ref
|
|
236 |
rule.lineno = lineno
|
|
237 |
rules.append(rule)
|
|
238 |
return rules
|
|
239 |
|
|
240 |
def parse_eval_globals(self, etree):
|
|
241 |
return {}
|
|
242 |
|
|
243 |
class RuleImplReader2(RuleImplReaderBase):
|
|
244 |
NAMESPACE = 'http://www.s60.com/xml/ruleml/2'
|
|
245 |
NAMESPACE_ID = 'ruleml2'
|
|
246 |
ROOT_ELEMENT_NAME = 'ruleml'
|
|
247 |
|
|
248 |
def __init__(self, resource_ref, configuration):
|
|
249 |
RuleImplReaderBase.__init__(self, resource_ref, configuration)
|
|
250 |
|
|
251 |
@classmethod
|
|
252 |
def get_schema_data(cls):
|
|
253 |
return pkg_resources.resource_string('legacyruleplugin', 'xsd/ruleml2.xsd')
|
|
254 |
|
|
255 |
def parse_rules(self, ref, etree):
|
|
256 |
rules = []
|
|
257 |
for elem in etree.getiterator("{%s}rule" % self.NAMESPACE):
|
|
258 |
lineno = utils.etree.get_lineno(elem)
|
|
259 |
for rule in relations.RelationFactory.get_relations(self.configuration, self._replace_eval_blocks(elem.text)):
|
|
260 |
rule.ref = ref
|
|
261 |
rule.lineno = lineno
|
|
262 |
rules.append(rule)
|
|
263 |
return rules
|
|
264 |
|
|
265 |
def parse_eval_globals(self, etree):
|
|
266 |
eval_globals = {}
|
|
267 |
for elem in etree.getiterator("{%s}eval_globals" % self.NAMESPACE):
|
|
268 |
text = ""
|
|
269 |
if elem.get('file') != None:
|
|
270 |
self._read_eval_globals_from_file(elem.get('file'), eval_globals)
|
|
271 |
else:
|
|
272 |
try:
|
|
273 |
# Strip surrounding whitespace, otherwise there might be Python
|
|
274 |
# indentation errors
|
|
275 |
text = elem.text.strip()
|
|
276 |
exec(text, eval_globals)
|
|
277 |
except Exception, e:
|
|
278 |
logging.getLogger('cone.ruleml(%s)' % self.resource_ref).warning('Failed to evaluate eval_globals block, exception: %s' % (e))
|
|
279 |
return eval_globals
|
|
280 |
|
|
281 |
def _read_eval_globals_from_file(self, relative_path, eval_globals):
|
|
282 |
# Get the actual path (relative to the current implementation file)
|
|
283 |
base_path = os.path.dirname(self.resource_ref)
|
|
284 |
pyfile_path = os.path.normpath(os.path.join(base_path, relative_path)).replace('\\', '/')
|
|
285 |
# Read the data and execute
|
|
286 |
try:
|
|
287 |
resource = None
|
|
288 |
resource = self.configuration.get_resource(pyfile_path)
|
|
289 |
text = resource.read()
|
|
290 |
exec(text.replace('\r', ''), eval_globals)
|
|
291 |
except Exception, e:
|
|
292 |
logging.getLogger('cone.ruleml(%s)' % self.resource_ref).warning('Cannot import eval file: %s. Exception: %s' % (pyfile_path, e))
|
|
293 |
finally:
|
|
294 |
if resource is not None: resource.close()
|
|
295 |
|
|
296 |
|
|
297 |
@classmethod
|
|
298 |
def _replace_eval_blocks(cls, code):
|
|
299 |
return utils.expand_delimited_tokens(
|
|
300 |
string = code,
|
|
301 |
expander_func = lambda ref, index: '__eval__ %r' % ref,
|
|
302 |
delimiters =('{%', '%}'))
|