1 """
2 Boolean geometry utilities.
3
4 """
5
6 from fabmetheus_utilities.geometry.geometry_utilities.evaluate_elements import setting
7 from fabmetheus_utilities.vector3 import Vector3
8 from fabmetheus_utilities import archive
9 from fabmetheus_utilities import euclidean
10 import math
11 import os
12 import sys
13 import traceback
14
15
16 __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
17 __credits__ = 'Art of Illusion <http://www.artofillusion.org/>'
18 __date__ = '$Date: 2008/02/05 $'
19 __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
20
21
22 globalModuleFunctionsDictionary = {}
23
24
26 'Add quote word and remainder if the word starts with a quote character or dollar sign, otherwise add the word.'
27 if len(word) < 2:
28 evaluatorWords.append(word)
29 return
30 firstCharacter = word[0]
31 if firstCharacter == '$':
32 dotIndex = word.find('.', 1)
33 if dotIndex > -1:
34 evaluatorWords.append(word[: dotIndex])
35 evaluatorWords.append(word[dotIndex :])
36 return
37 if firstCharacter != '"' and firstCharacter != "'":
38 evaluatorWords.append(word)
39 return
40 nextQuoteIndex = word.find(firstCharacter, 1)
41 if nextQuoteIndex < 0 or nextQuoteIndex == len(word) - 1:
42 evaluatorWords.append(word)
43 return
44 nextQuoteIndex += 1
45 evaluatorWords.append(word[: nextQuoteIndex])
46 evaluatorWords.append(word[nextQuoteIndex :])
47
49 'Add prefixed key values to dictionary.'
50 for key in keys:
51 dictionary[key.lstrip('_')] = value
52
54 'Add to vector3 paths recursively.'
55 if vector3Lists.__class__ == Vector3 or vector3Lists.__class__ .__name__ == 'Vector3Index':
56 paths.append([ vector3Lists ])
57 return
58 path = []
59 for vector3List in vector3Lists:
60 if vector3List.__class__ == list:
61 addToPathsRecursively(paths, vector3List)
62 elif vector3List.__class__ == Vector3:
63 path.append(vector3List)
64 if len(path) > 0:
65 paths.append(path)
66
68 'Get the evaluated dictionary.'
69 value = getEvaluatedValueObliviously(key, xmlElement)
70 if value == None:
71 valueString = str(xmlElement.attributeDictionary[key])
72 print('Warning, addValueToEvaluatedDictionary in evaluate can not get a value for:')
73 print(valueString)
74 evaluatedDictionary[key + '__Warning__'] = 'Can not evaluate: ' + valueString.replace('"', ' ').replace( "'", ' ')
75 else:
76 evaluatedDictionary[key] = value
77
79 'Add vector3 to xml element.'
80 xmlElement.attributeDictionary[key] = '[%s,%s,%s]' % (vector3.x, vector3.y, vector3.z)
81
91
106
121
123 'Evaluate the expression value from the numeric and operation evaluators.'
124 for negativeIndex in xrange( - len(evaluators), - 1 ):
125 evaluatorIndex = negativeIndex + len(evaluators)
126 evaluators[evaluatorIndex].executeLeftOperation( evaluators, evaluatorIndex, operationLevel )
127
129 'Execute the nextEvaluator arguments.'
130 if evaluator.value == None:
131 print('Warning, executeNextEvaluatorArguments in evaluate can not get a evaluator.value for:')
132 print(evaluatorIndex)
133 print(evaluators)
134 print(evaluator)
135 return
136 nextEvaluator.value = evaluator.value(*nextEvaluator.arguments)
137 del evaluators[evaluatorIndex]
138
140 'Evaluate the expression value from the numeric and operation evaluators.'
141 for negativeIndex in xrange(1 - len(evaluators), - 1):
142 evaluatorIndex = negativeIndex + len(evaluators)
143 evaluators[evaluatorIndex].executePairOperation(evaluators, evaluatorIndex, operationLevel)
144
148
150 'Evaluate the expression value.'
151 bracketBeginIndex = None
152 for negativeIndex in xrange( - len(evaluators), 0 ):
153 bracketEndIndex = negativeIndex + len(evaluators)
154 evaluatorEnd = evaluators[ bracketEndIndex ]
155 evaluatorWord = evaluatorEnd.word
156 if evaluatorWord in ['(', '[', '{']:
157 bracketBeginIndex = bracketEndIndex
158 elif evaluatorWord in [')', ']', '}']:
159 if bracketBeginIndex == None:
160 print('Warning, bracketBeginIndex in evaluateBrackets in evaluate is None.')
161 print('This may be because the brackets are not balanced.')
162 print(evaluators)
163 del evaluators[ bracketEndIndex ]
164 return
165 evaluators[ bracketBeginIndex ].executeBracket(bracketBeginIndex, bracketEndIndex, evaluators)
166 evaluators[ bracketBeginIndex ].word = None
167 return True
168 return False
169
171 'Get the bracket values and delete the evaluator.'
172 evaluatedExpressionValueEvaluators = getBracketEvaluators(bracketBeginIndex, bracketEndIndex, evaluators)
173 bracketValues = []
174 for evaluatedExpressionValueEvaluator in evaluatedExpressionValueEvaluators:
175 bracketValues.append( evaluatedExpressionValueEvaluator.value )
176 del evaluators[ bracketBeginIndex + 1: bracketEndIndex + 1 ]
177 return bracketValues
178
180 'Get key with capitalized suffix.'
181 if prefix == '' or prefix.endswith('.'):
182 return prefix + suffix
183 return prefix + suffix[:1].upper()+suffix[1:]
184
186 'Get split line for evaluators.'
187 if getIsQuoted(value):
188 return [value]
189 for dictionaryKey in dictionary.keys():
190 value = value.replace(dictionaryKey, ' ' + dictionaryKey + ' ')
191 dictionarySplitWords = []
192 for word in value.split():
193 dictionarySplitWords.append(word)
194 return dictionarySplitWords
195
197 'Get the bracket end index and convert the equation value evaluators into a string.'
198 evaluator = evaluators[evaluatorIndex]
199 if evaluator.__class__ != EvaluatorValue:
200 return bracketEndIndex
201 if not evaluator.word.startswith('equation.'):
202 return bracketEndIndex
203 if evaluators[ evaluatorIndex + 1 ].word != ':':
204 return bracketEndIndex
205 valueBeginIndex = evaluatorIndex + 2
206 equationValueString = ''
207 for valueEvaluatorIndex in xrange( valueBeginIndex, len(evaluators) ):
208 valueEvaluator = evaluators[ valueEvaluatorIndex ]
209 if valueEvaluator.word == ',' or valueEvaluator.word == '}':
210 if equationValueString == '':
211 return bracketEndIndex
212 else:
213 evaluators[ valueBeginIndex ] = EvaluatorValue( equationValueString )
214 valueDeleteIndex = valueBeginIndex + 1
215 del evaluators[ valueDeleteIndex : valueEvaluatorIndex ]
216 return bracketEndIndex - valueEvaluatorIndex + valueDeleteIndex
217 equationValueString += valueEvaluator.word
218 return bracketEndIndex
219
227
229 'Get the evaluated dictionary by copyKeys.'
230 evaluatedDictionary = {}
231 for key in xmlElement.attributeDictionary.keys():
232 if key in copyKeys:
233 evaluatedDictionary[key] = xmlElement.attributeDictionary[key]
234 else:
235 addValueToEvaluatedDictionary(evaluatedDictionary, key, xmlElement)
236 return evaluatedDictionary
237
239 'Get the evaluated dictionary.'
240 evaluatedDictionary = {}
241 for key in xmlElement.attributeDictionary.keys():
242 if key in evaluationKeys:
243 addValueToEvaluatedDictionary(evaluatedDictionary, key, xmlElement)
244 return evaluatedDictionary
245
247 'Evaluate the expression value.'
248 try:
249 return getEvaluatedExpressionValueBySplitLine( getEvaluatorSplitWords(value), xmlElement )
250 except:
251 print('Warning, in getEvaluatedExpressionValue in evaluate could not get a value for:')
252 print(value)
253 traceback.print_exc(file=sys.stdout)
254 return None
255
257 'Evaluate the expression value.'
258 evaluators = []
259 for wordIndex, word in enumerate(words):
260 nextWord = ''
261 nextWordIndex = wordIndex + 1
262 if nextWordIndex < len(words):
263 nextWord = words[nextWordIndex]
264 evaluator = getEvaluator(evaluators, nextWord, word, xmlElement)
265 if evaluator != None:
266 evaluators.append(evaluator)
267 while getBracketsExist(evaluators):
268 pass
269 evaluatedExpressionValueEvaluators = getEvaluatedExpressionValueEvaluators(evaluators)
270 if len( evaluatedExpressionValueEvaluators ) > 0:
271 return evaluatedExpressionValueEvaluators[0].value
272 return None
273
275 'Evaluate the expression value from the numeric and operation evaluators.'
276 for evaluatorIndex, evaluator in enumerate(evaluators):
277 evaluator.executeCenterOperation(evaluators, evaluatorIndex)
278 for negativeIndex in xrange(1 - len(evaluators), 0):
279 evaluatorIndex = negativeIndex + len(evaluators)
280 evaluators[evaluatorIndex].executeRightOperation(evaluators, evaluatorIndex)
281 executeLeftOperations(evaluators, 200)
282 for operationLevel in [80, 60, 40, 20, 15]:
283 executePairOperations(evaluators, operationLevel)
284 executeLeftOperations(evaluators, 13)
285 executePairOperations(evaluators, 12)
286 for negativeIndex in xrange(-len(evaluators), 0):
287 evaluatorIndex = negativeIndex + len(evaluators)
288 evaluators[evaluatorIndex].executePairOperation(evaluators, evaluatorIndex, 10)
289 for evaluatorIndex in xrange(len(evaluators) - 1, -1, -1):
290 evaluators[evaluatorIndex].executePairOperation(evaluators, evaluatorIndex, 0)
291 return evaluators
292
300
302 'Get the evaluated float by keys.'
303 for key in keys:
304 defaultValue = getEvaluatedFloat(defaultValue, key, xmlElement)
305 return defaultValue
306
308 'Get the evaluated int.'
309 if xmlElement == None:
310 return None
311 if key in xmlElement.attributeDictionary:
312 try:
313 return getIntFromFloatString(getEvaluatedValueObliviously(key, xmlElement))
314 except:
315 print('Warning, could not evaluate the int.')
316 print(key)
317 print(xmlElement.attributeDictionary[key])
318 return defaultValue
319
321 'Get the evaluated int by keys.'
322 for key in keys:
323 defaultValue = getEvaluatedInt(defaultValue, key, xmlElement)
324 return defaultValue
325
333
335 'Get the evaluated string.'
336 if xmlElement == None:
337 return defaultValue
338 if key in xmlElement.attributeDictionary:
339 return str(getEvaluatedValueObliviously(key, xmlElement))
340 return defaultValue
341
343 'Get the evaluated value.'
344 if xmlElement == None:
345 return defaultValue
346 if key in xmlElement.attributeDictionary:
347 return getEvaluatedValueObliviously(key, xmlElement)
348 return defaultValue
349
351 'Get the evaluated value.'
352 value = str(xmlElement.attributeDictionary[key]).strip()
353 if key == 'id' or key == 'name' or key == 'tags':
354 return value
355 return getEvaluatedLinkValue(value, xmlElement)
356
358 'Get the evaluator.'
359 if word in globalSplitDictionary:
360 return globalSplitDictionary[word](word, xmlElement)
361 firstCharacter = word[: 1]
362 if firstCharacter == "'" or firstCharacter == '"':
363 if len(word) > 1:
364 if firstCharacter == word[-1]:
365 return EvaluatorValue(word[1 : -1])
366 if firstCharacter == '$':
367 return EvaluatorValue(word[1 :])
368 dotIndex = word.find('.')
369 functions = xmlElement.getXMLProcessor().functions
370 if dotIndex > -1 and len(word) > 1:
371 if dotIndex == 0 and word[1].isalpha():
372 return EvaluatorAttribute(word, xmlElement)
373 if dotIndex > 0:
374 untilDot = word[: dotIndex]
375 if untilDot in globalModuleEvaluatorDictionary:
376 return globalModuleEvaluatorDictionary[untilDot](word, xmlElement)
377 if len(functions) > 0:
378 if untilDot in functions[-1].localDictionary:
379 return EvaluatorLocal(word, xmlElement)
380 if firstCharacter.isalpha() or firstCharacter == '_':
381 if len(functions) > 0:
382 if word in functions[-1].localDictionary:
383 return EvaluatorLocal(word, xmlElement)
384 wordElement = xmlElement.getXMLElementByImportID(word)
385 if wordElement != None:
386 if wordElement.localName == 'class':
387 return EvaluatorClass(word, wordElement)
388 if wordElement.localName == 'function':
389 return EvaluatorFunction(word, wordElement)
390 return EvaluatorValue(word)
391 return EvaluatorNumeric(word, xmlElement)
392
394 'Get split words for evaluators.'
395 if value.startswith('='):
396 value = value[len('=') :]
397 if len(value) < 1:
398 return []
399 global globalDictionaryOperatorBegin
400 uniqueQuoteIndex = 0
401 word = ''
402 quoteString = None
403 quoteDictionary = {}
404 for characterIndex in xrange(len(value)):
405 character = value[characterIndex]
406 if character == '"' or character == "'":
407 if quoteString == None:
408 quoteString = ''
409 elif quoteString != None:
410 if character == quoteString[: 1]:
411 uniqueQuoteIndex = getUniqueQuoteIndex(uniqueQuoteIndex, value)
412 uniqueToken = getTokenByNumber(uniqueQuoteIndex)
413 quoteDictionary[uniqueToken] = quoteString + character
414 character = uniqueToken
415 quoteString = None
416 if quoteString == None:
417 word += character
418 else:
419 quoteString += character
420 beginSplitWords = getDictionarySplitWords(globalDictionaryOperatorBegin, word)
421 global globalSplitDictionaryOperator
422 evaluatorSplitWords = []
423 for beginSplitWord in beginSplitWords:
424 if beginSplitWord in globalDictionaryOperatorBegin:
425 evaluatorSplitWords.append(beginSplitWord)
426 else:
427 evaluatorSplitWords += getDictionarySplitWords(globalSplitDictionaryOperator, beginSplitWord)
428 for evaluatorSplitWordIndex, evaluatorSplitWord in enumerate(evaluatorSplitWords):
429 for quoteDictionaryKey in quoteDictionary.keys():
430 if quoteDictionaryKey in evaluatorSplitWord:
431 evaluatorSplitWords[evaluatorSplitWordIndex] = evaluatorSplitWord.replace(quoteDictionaryKey, quoteDictionary[quoteDictionaryKey])
432 evaluatorTransitionWords = []
433 for evaluatorSplitWord in evaluatorSplitWords:
434 addQuoteWord(evaluatorTransitionWords, evaluatorSplitWord)
435 return evaluatorTransitionWords
436
438 'Get list from a bracketed string.'
439 if not getIsBracketed( bracketedString ):
440 return None
441 bracketedString = bracketedString.strip().replace('[', '').replace(']', '').replace('(', '').replace(')', '')
442 if len( bracketedString ) < 1:
443 return []
444 splitLine = bracketedString.split(',')
445 floatList = []
446 for word in splitLine:
447 evaluatedFloat = euclidean.getFloatFromValue(word)
448 if evaluatedFloat != None:
449 floatList.append( evaluatedFloat )
450 return floatList
451
453 'Get float lists by paths.'
454 floatListLists = []
455 for path in paths:
456 floatListList = []
457 for point in path:
458 floatListList.append( point.getFloatList() )
459 return floatListLists
460
462 'Get the creation evaluator plugins if the xmlElement is from the creation evaluator.'
463 if getEvaluatedBoolean( False, '_fromCreationEvaluator', xmlElement ):
464 return getMatchingPlugins( namePathDictionary, xmlElement )
465 return []
466
468 'Get keys for repository.'
469 repositoryClass = repository.__class__
470 if repositoryClass == list or repositoryClass == tuple:
471 return range(len(repository))
472 if repositoryClass == dict:
473 return repository.keys()
474 return None
475
477 'Get the int from the string.'
478 floatString = str(value).strip()
479 if floatString == '':
480 return None
481 dotIndex = floatString.find('.')
482 if dotIndex < 0:
483 return int(value)
484 return int( round( float(floatString) ) )
485
487 'Determine if the word is bracketed.'
488 if len(word) < 2:
489 return False
490 firstCharacter = word[0]
491 lastCharacter = word[-1]
492 if firstCharacter == '(' and lastCharacter == ')':
493 return True
494 return firstCharacter == '[' and lastCharacter == ']'
495
497 'Determine if the word is quoted.'
498 if len(word) < 2:
499 return False
500 firstCharacter = word[0]
501 lastCharacter = word[-1]
502 if firstCharacter == '"' and lastCharacter == '"':
503 return True
504 return firstCharacter == "'" and lastCharacter == "'"
505
507 'Get the local attribute value string with augmented assignment.'
508 augmentedStatements = '+= -= *= /= %= **='.split()
509 for augmentedStatement in augmentedStatements:
510 if valueString.startswith(augmentedStatement):
511 return key + augmentedStatement[: -1] + valueString[len(augmentedStatement) :]
512 return valueString
513
515 'Get the plugins whose names are in the attribute dictionary.'
516 matchingPlugins = []
517 namePathDictionaryCopy = namePathDictionary.copy()
518 for key in xmlElement.attributeDictionary:
519 dotIndex = key.find('.')
520 if dotIndex > - 1:
521 keyUntilDot = key[: dotIndex]
522 if keyUntilDot in namePathDictionaryCopy:
523 pluginModule = archive.getModuleWithPath( namePathDictionaryCopy[ keyUntilDot ] )
524 del namePathDictionaryCopy[ keyUntilDot ]
525 if pluginModule != None:
526 matchingPlugins.append( pluginModule )
527 return matchingPlugins
528
530 'Get the next childNode index.'
531 for childNodeIndex, childNode in enumerate( xmlElement.parentNode.childNodes ):
532 if childNode == xmlElement:
533 return childNodeIndex + 1
534 return len( xmlElement.parentNode.childNodes )
535
537 'Get path from prefix and xml element.'
538 if key not in xmlElement.attributeDictionary:
539 return defaultPath
540 word = str(xmlElement.attributeDictionary[key]).strip()
541 evaluatedLinkValue = getEvaluatedLinkValue(word, xmlElement)
542 if evaluatedLinkValue.__class__ == list:
543 return getPathByList(evaluatedLinkValue)
544 xmlElementObject = getXMLElementObject(evaluatedLinkValue)
545 if xmlElementObject == None:
546 return defaultPath
547 return xmlElementObject.getPaths()[0]
548
560
562 'Get path from prefix and xml element.'
563 if len(path) < 2:
564 print('Warning, bug, path is too small in evaluate in setPathByPrefix.')
565 return
566 pathByKey = getPathByKey([], getCapitalizedSuffixKey(prefix, 'path'), xmlElement)
567 if len( pathByKey ) < len(path):
568 for pointIndex in xrange( len( pathByKey ) ):
569 path[pointIndex] = pathByKey[pointIndex]
570 else:
571 path = pathByKey
572 path[0] = getVector3ByPrefix(path[0], getCapitalizedSuffixKey(prefix, 'pathStart'), xmlElement)
573 path[-1] = getVector3ByPrefix(path[-1], getCapitalizedSuffixKey(prefix, 'pathEnd'), xmlElement)
574 return path
575
577 'Get paths by key.'
578 if key not in xmlElement.attributeDictionary:
579 return defaultPaths
580 word = str(xmlElement.attributeDictionary[key]).strip()
581 evaluatedLinkValue = getEvaluatedLinkValue(word, xmlElement)
582 if evaluatedLinkValue.__class__ == dict or evaluatedLinkValue.__class__ == list:
583 convertToPaths(evaluatedLinkValue)
584 return getPathsByLists(evaluatedLinkValue)
585 xmlElementObject = getXMLElementObject(evaluatedLinkValue)
586 if xmlElementObject == None:
587 return defaultPaths
588 return xmlElementObject.getPaths()
589
596
598 'Get the number of poygon sides.'
599 return int(math.ceil(math.sqrt(0.5 * radius * math.pi * math.pi / setting.getPrecision(xmlElement))))
600
602 'Get the number of poygon sides, with a minimum of three.'
603 return max(getSidesBasedOnPrecision(radius, xmlElement), 3)
604
609
626
628 'Determine if the word starts with round or square brackets.'
629 return word.startswith('{') or word.startswith('=') or word.startswith('(') or word.startswith('[')
630
632 'Get token by number.'
633 return '_%s_' % number
634
650
665
682
684 'Get uniqueQuoteIndex.'
685 uniqueQuoteIndex += 1
686 while getTokenByNumber(uniqueQuoteIndex) in word:
687 uniqueQuoteIndex += 1
688 return uniqueQuoteIndex
689
691 'Get unique token.'
692 uniqueString = '@#!'
693 for character in uniqueString:
694 if character not in word:
695 return character
696 uniqueNumber = 0
697 while True:
698 for character in uniqueString:
699 uniqueToken = character + str(uniqueNumber)
700 if uniqueToken not in word:
701 return uniqueToken
702 uniqueNumber += 1
703
716
730
743
745 'Get vector3 from multiplier, prefix and xml element.'
746 if multiplier == 0.0:
747 return vector3
748 oldMultipliedValueVector3 = vector3 * multiplier
749 vector3ByPrefix = getVector3ByPrefix(oldMultipliedValueVector3.copy(), prefix, xmlElement)
750 if vector3ByPrefix == oldMultipliedValueVector3:
751 return vector3
752 return vector3ByPrefix / multiplier
753
759
779
785
787 'Get vector3 from xml element.'
788 vector3 = Vector3(
789 getEvaluatedFloat(0.0, 'x', xmlElement),
790 getEvaluatedFloat(0.0, 'y', xmlElement),
791 getEvaluatedFloat(0.0, 'z', xmlElement))
792 return getVector3ByPrefix(vector3, 'cartesian', xmlElement)
793
795 'Get new vector3 if the original vector3 is none.'
796 if vector3 == None:
797 return Vector3()
798 return vector3
799
801 'Get vector3 lists recursively.'
802 if len(floatLists) < 1:
803 return Vector3()
804 firstElement = floatLists[0]
805 if firstElement.__class__ == Vector3:
806 return floatLists
807 if firstElement.__class__ != list:
808 return getVector3ByFloatList(floatLists, Vector3())
809 vector3ListsRecursively = []
810 for floatList in floatLists:
811 vector3ListsRecursively.append(getVector3ListsRecursively(floatList))
812 return vector3ListsRecursively
813
815 'Get the visible objects.'
816 visibleObjects = []
817 for archivableObject in archivableObjects:
818 if archivableObject.getVisible():
819 visibleObjects.append(archivableObject)
820 return visibleObjects
821
823 'Get the xml element by key.'
824 if key not in xmlElement.attributeDictionary:
825 return None
826 word = str(xmlElement.attributeDictionary[key]).strip()
827 evaluatedLinkValue = getEvaluatedLinkValue(word, xmlElement)
828 if evaluatedLinkValue.__class__.__name__ == 'XMLElement':
829 return evaluatedLinkValue
830 print('Warning, could not get XMLElement in getXMLElementByKey in evaluate for:')
831 print(key)
832 print(evaluatedLinkValue)
833 print(xmlElement)
834 return None
835
837 'Get XMLElementObject.'
838 if evaluatedLinkValue.__class__.__name__ != 'XMLElement':
839 print('Warning, could not get XMLElement in getXMLElementObject in evaluate for:')
840 print(evaluatedLinkValue)
841 return None
842 if evaluatedLinkValue.xmlObject == None:
843 print('Warning, evaluatedLinkValue.xmlObject is None in getXMLElementObject in evaluate for:')
844 print(evaluatedLinkValue)
845 return None
846 return evaluatedLinkValue.xmlObject
847
849 'Get the xml elements by key.'
850 if key not in xmlElement.attributeDictionary:
851 return []
852 word = str(xmlElement.attributeDictionary[key]).strip()
853 evaluatedLinkValue = getEvaluatedLinkValue(word, xmlElement)
854 if evaluatedLinkValue.__class__.__name__ == 'XMLElement':
855 return [evaluatedLinkValue]
856 if evaluatedLinkValue.__class__ == list:
857 return evaluatedLinkValue
858 print('Warning, could not get XMLElements in getXMLElementsByKey in evaluate for:')
859 print(key)
860 print(evaluatedLinkValue)
861 print(xmlElement)
862 return []
863
865 'Get any new elements and process the archivable.'
866 if xmlElement == None:
867 return
868 xmlElement.xmlObject = archivableClass()
869 xmlElement.xmlObject.setToXMLElement(xmlElement)
870 xmlElement.getXMLProcessor().processChildNodes(xmlElement)
871
873 'Process the xml element condition.'
874 xmlProcessor = xmlElement.getXMLProcessor()
875 if xmlElement.xmlObject == None:
876 xmlElement.xmlObject = ModuleXMLElement(xmlElement)
877 if xmlElement.xmlObject.conditionSplitWords == None:
878 return
879 if len(xmlProcessor.functions ) < 1:
880 print('Warning, the (in) element is not in a function in processCondition in evaluate for:')
881 print(xmlElement)
882 return
883 if int( getEvaluatedExpressionValueBySplitLine( xmlElement.xmlObject.conditionSplitWords, xmlElement ) ) > 0:
884 xmlProcessor.functions[-1].processChildNodes(xmlElement)
885 else:
886 xmlElement.xmlObject.processElse(xmlElement)
887
891
893 'Set the attribute dictionary to the arguments.'
894 for argumentIndex, argument in enumerate(arguments):
895 xmlElement.attributeDictionary[argumentNames[argumentIndex]] = argument
896
898 'Evaluate the function statement and delete the evaluators.'
899 function.localDictionary = {'_arguments' : arguments}
900 if len(arguments) > 0:
901 firstArgument = arguments[0]
902 if firstArgument.__class__ == dict:
903 function.localDictionary = firstArgument
904 return
905 if 'parameters' not in function.xmlElement.attributeDictionary:
906 return
907 parameters = function.xmlElement.attributeDictionary['parameters'].strip()
908 if parameters == '':
909 return
910 parameterWords = parameters.split(',')
911 for parameterWordIndex, parameterWord in enumerate(parameterWords):
912 strippedWord = parameterWord.strip()
913 keyValue = KeyValue().getByEqual(strippedWord)
914 if parameterWordIndex < len(arguments):
915 function.localDictionary[keyValue.key] = arguments[parameterWordIndex]
916 else:
917 strippedValue = keyValue.value
918 if strippedValue == None:
919 print('Warning there is no default parameter in getParameterValue for:')
920 print(strippedWord)
921 print(parameterWords)
922 print(arguments)
923 print( function.xmlElement.attributeDictionary )
924 else:
925 strippedValue = strippedValue.strip()
926 function.localDictionary[keyValue.key.strip()] = strippedValue
927 if len(arguments) > len(parameterWords):
928 print('Warning there are too many initializeFunction parameters for:')
929 print( function.xmlElement.attributeDictionary )
930 print(parameterWords)
931 print(arguments)
932
943
944
946 'Class to get equation results.'
948 'Initialize.'
949 self.localDictionary = {}
950 self.xmlElement = xmlElement
951 self.xmlProcessor = xmlElement.getXMLProcessor()
952
954 'Get the string representation of this Class.'
955 return str(self.__dict__)
956
962
964 'Process childNodes if shouldReturn is false.'
965 for childNode in xmlElement.childNodes:
966 if self.shouldReturn:
967 return
968 self.xmlProcessor.processXMLElement(childNode)
969
970
972 'Class to get class results.'
977
979 'Get return value without deleting last function.'
980 self.returnValue = None
981 self.shouldReturn = False
982 self.xmlProcessor.functions.append(self)
983 self.processChildNodes(self.xmlElement)
984 return self.returnValue
985
986
988 'Class to hold class attributes and functions.'
990 'Initialize.'
991 self.functionDictionary = xmlElement.xmlObject.functionDictionary
992 self.selfDictionary = {}
993 for variable in xmlElement.xmlObject.variables:
994 self.selfDictionary[variable] = None
995
997 'Get the accessible attribute.'
998 if attributeName in self.selfDictionary:
999 return self.selfDictionary[attributeName]
1000 if attributeName in self.functionDictionary:
1001 function = self.functionDictionary[attributeName]
1002 function.classObject = self
1003 return function.getReturnValueByArguments
1004 return None
1005
1007 'Get the string representation of this Class.'
1008 return str(self.__dict__)
1009
1011 'Set the accessible attribute.'
1012 if attributeName in self.selfDictionary:
1013 self.selfDictionary[attributeName] = value
1014
1015
1017 'Base evaluator class.'
1019 'Set value to none.'
1020 self.value = None
1021 self.word = word
1022
1024 'Get the string representation of this Class.'
1025 return str(self.__dict__)
1026
1027 - def executeBracket( self, bracketBeginIndex, bracketEndIndex, evaluators ):
1028 'Execute the bracket.'
1029 pass
1030
1032 'Execute operator which acts on the center.'
1033 pass
1034
1035 - def executeDictionary(self, dictionary, evaluators, keys, evaluatorIndex, nextEvaluator):
1036 'Execute the dictionary.'
1037 del evaluators[evaluatorIndex]
1038 enumeratorKeys = euclidean.getEnumeratorKeys(dictionary, keys)
1039 if enumeratorKeys.__class__ == list:
1040 nextEvaluator.value = []
1041 for enumeratorKey in enumeratorKeys:
1042 if enumeratorKey in dictionary:
1043 nextEvaluator.value.append(dictionary[enumeratorKey])
1044 else:
1045 print('Warning, key in executeKey in Evaluator in evaluate is not in for:')
1046 print(enumeratorKey)
1047 print(dictionary)
1048 return
1049 if enumeratorKeys in dictionary:
1050 nextEvaluator.value = dictionary[enumeratorKeys]
1051 else:
1052 print('Warning, key in executeKey in Evaluator in evaluate is not in for:')
1053 print(enumeratorKeys)
1054 print(dictionary)
1055
1057 'Execute the function.'
1058 pass
1059
1060 - def executeKey(self, evaluators, keys, evaluatorIndex, nextEvaluator):
1061 'Execute the key index.'
1062 if self.value.__class__ == str:
1063 self.executeString(evaluators, keys, evaluatorIndex, nextEvaluator)
1064 return
1065 if self.value.__class__ == list:
1066 self.executeList(evaluators, keys, evaluatorIndex, nextEvaluator)
1067 return
1068 if self.value.__class__ == dict:
1069 self.executeDictionary(self.value, evaluators, keys, evaluatorIndex, nextEvaluator)
1070 return
1071 getAccessibleDictionaryFunction = getattr(self.value, '_getAccessibleDictionary', None)
1072 if getAccessibleDictionaryFunction != None:
1073 self.executeDictionary(getAccessibleDictionaryFunction(), evaluators, keys, evaluatorIndex, nextEvaluator)
1074 return
1075 if self.value.__class__.__name__ != 'XMLElement':
1076 return
1077 del evaluators[evaluatorIndex]
1078 enumeratorKeys = euclidean.getEnumeratorKeys(self.value.attributeDictionary, keys)
1079 if enumeratorKeys.__class__ == list:
1080 nextEvaluator.value = []
1081 for enumeratorKey in enumeratorKeys:
1082 if enumeratorKey in self.value.attributeDictionary:
1083 nextEvaluator.value.append(getEvaluatedExpressionValue(self.value.attributeDictionary[enumeratorKey], self.value))
1084 else:
1085 print('Warning, key in executeKey in Evaluator in evaluate is not in for:')
1086 print(enumeratorKey)
1087 print(self.value.attributeDictionary)
1088 return
1089 if enumeratorKeys in self.value.attributeDictionary:
1090 nextEvaluator.value = getEvaluatedExpressionValue(self.value.attributeDictionary[enumeratorKeys], self.value)
1091 else:
1092 print('Warning, key in executeKey in Evaluator in evaluate is not in for:')
1093 print(enumeratorKeys)
1094 print(self.value.attributeDictionary)
1095
1097 'Execute operator which acts from the left.'
1098 pass
1099
1100 - def executeList(self, evaluators, keys, evaluatorIndex, nextEvaluator):
1101 'Execute the key index.'
1102 del evaluators[evaluatorIndex]
1103 enumeratorKeys = euclidean.getEnumeratorKeys(self.value, keys)
1104 if enumeratorKeys.__class__ == list:
1105 nextEvaluator.value = []
1106 for enumeratorKey in enumeratorKeys:
1107 intKey = euclidean.getIntFromValue(enumeratorKey)
1108 if self.getIsInRange(intKey):
1109 nextEvaluator.value.append(self.value[intKey])
1110 else:
1111 print('Warning, key in executeList in Evaluator in evaluate is not in for:')
1112 print(enumeratorKey)
1113 print(self.value)
1114 return
1115 intKey = euclidean.getIntFromValue(enumeratorKeys)
1116 if self.getIsInRange(intKey):
1117 nextEvaluator.value = self.value[intKey]
1118 else:
1119 print('Warning, key in executeList in Evaluator in evaluate is not in for:')
1120 print(enumeratorKeys)
1121 print(self.value)
1122
1124 'Operate on two evaluators.'
1125 pass
1126
1128 'Execute operator which acts from the right.'
1129 pass
1130
1131 - def executeString(self, evaluators, keys, evaluatorIndex, nextEvaluator):
1132 'Execute the string.'
1133 del evaluators[evaluatorIndex]
1134 enumeratorKeys = euclidean.getEnumeratorKeys(self.value, keys)
1135 if enumeratorKeys.__class__ == list:
1136 nextEvaluator.value = ''
1137 for enumeratorKey in enumeratorKeys:
1138 intKey = euclidean.getIntFromValue(enumeratorKey)
1139 if self.getIsInRange(intKey):
1140 nextEvaluator.value += self.value[intKey]
1141 else:
1142 print('Warning, key in executeString in Evaluator in evaluate is not in for:')
1143 print(enumeratorKey)
1144 print(self.value)
1145 return
1146 intKey = euclidean.getIntFromValue(enumeratorKeys)
1147 if self.getIsInRange(intKey):
1148 nextEvaluator.value = self.value[intKey]
1149 else:
1150 print('Warning, key in executeString in Evaluator in evaluate is not in for:')
1151 print(enumeratorKeys)
1152 print(self.value)
1153
1155 'Determine if the keyIndex is in range.'
1156 if keyIndex == None:
1157 return False
1158 return keyIndex >= -len(self.value) and keyIndex < len(self.value)
1159
1160
1162 'Class to add two evaluators.'
1164 'Operate on two evaluators.'
1165 if operationLevel == 20:
1166 self.executePair(evaluators, evaluatorIndex)
1167
1169 'Add two evaluators.'
1170 leftIndex = evaluatorIndex - 1
1171 rightIndex = evaluatorIndex + 1
1172 if leftIndex < 0:
1173 print('Warning, no leftKey in executePair in EvaluatorAddition for:')
1174 print(evaluators)
1175 print(evaluatorIndex)
1176 print(self)
1177 del evaluators[evaluatorIndex]
1178 return
1179 if rightIndex >= len(evaluators):
1180 print('Warning, no rightKey in executePair in EvaluatorAddition for:')
1181 print(evaluators)
1182 print(evaluatorIndex)
1183 print(self)
1184 del evaluators[evaluatorIndex]
1185 return
1186 rightValue = evaluators[rightIndex].value
1187 evaluators[leftIndex].value = self.getOperationValue(evaluators[leftIndex].value, evaluators[rightIndex].value)
1188 del evaluators[ evaluatorIndex : evaluatorIndex + 2 ]
1189
1191 'Get evaluatedValues.'
1192 if enumerable.__class__ == dict:
1193 evaluatedValues = {}
1194 for key in keys:
1195 evaluatedValues[key] = self.getOperationValue(value, enumerable[key])
1196 return evaluatedValues
1197 evaluatedValues = []
1198 for key in keys:
1199 evaluatedValues.append(self.getOperationValue(value, enumerable[key]))
1200 return evaluatedValues
1201
1203 'Get operation value.'
1204 leftKeys = getKeys(leftValue)
1205 rightKeys = getKeys(rightValue)
1206 if leftKeys == None and rightKeys == None:
1207 return self.getValueFromValuePair(leftValue, rightValue)
1208 if leftKeys == None:
1209 return self.getEvaluatedValues(rightValue, rightKeys, leftValue)
1210 if rightKeys == None:
1211 return self.getEvaluatedValues(leftValue, leftKeys, rightValue)
1212 leftKeys.sort(reverse=True)
1213 rightKeys.sort(reverse=True)
1214 if leftKeys != rightKeys:
1215 print('Warning, the leftKeys are different from the rightKeys in getOperationValue in EvaluatorAddition for:')
1216 print('leftValue')
1217 print(leftValue)
1218 print(leftKeys)
1219 print('rightValue')
1220 print(rightValue)
1221 print(rightKeys)
1222 print(self)
1223 return None
1224 if leftValue.__class__ == dict or rightValue.__class__ == dict:
1225 evaluatedValues = {}
1226 for leftKey in leftKeys:
1227 evaluatedValues[leftKey] = self.getOperationValue(leftValue[leftKey], rightValue[leftKey])
1228 return evaluatedValues
1229 evaluatedValues = []
1230 for leftKey in leftKeys:
1231 evaluatedValues.append(self.getOperationValue(leftValue[leftKey], rightValue[leftKey]))
1232 return evaluatedValues
1233
1235 'Add two values.'
1236 return leftValue + rightValue
1237
1238
1240 'Class to compare two evaluators.'
1242 'Operate on two evaluators.'
1243 if operationLevel == 15:
1244 self.executePair(evaluators, evaluatorIndex)
1245
1247 'Compare two values.'
1248 return leftValue == rightValue
1249
1253
1254
1256 'Class to subtract two evaluators.'
1258 'Minus the value to the right.'
1259 leftIndex = evaluatorIndex - 1
1260 rightIndex = evaluatorIndex + 1
1261 leftValue = None
1262 if leftIndex >= 0:
1263 leftValue = evaluators[leftIndex].value
1264 if leftValue != None:
1265 return
1266 rightValue = evaluators[rightIndex].value
1267 if rightValue == None:
1268 print('Warning, can not minus.')
1269 print( evaluators[rightIndex].word )
1270 else:
1271 evaluators[rightIndex].value = self.getNegativeValue(rightValue)
1272 del evaluators[evaluatorIndex]
1273
1275 'Minus the value to the right.'
1276 if operationLevel == 200:
1277 self.executeLeft(evaluators, evaluatorIndex)
1278
1287
1289 'Minus value.'
1290 return -value
1291
1293 'Subtract two values.'
1294 return leftValue - rightValue
1295
1296
1298 'Class to compare two evaluators.'
1300 'Operate on two evaluators.'
1301 if operationLevel == 12:
1302 self.executePair(evaluators, evaluatorIndex)
1303
1305 'And two values.'
1306 return leftValue and rightValue
1307
1311
1312
1314 'Class to handle an attribute.'
1318
1345
1346
1348 'Class to evaluate a string.'
1349 - def executeBracket(self, bracketBeginIndex, bracketEndIndex, evaluators):
1350 'Execute the bracket.'
1351 for evaluatorIndex in xrange(bracketEndIndex - 3, bracketBeginIndex, - 1):
1352 bracketEndIndex = getEndIndexConvertEquationValue(bracketEndIndex, evaluatorIndex, evaluators)
1353 evaluatedExpressionValueEvaluators = getBracketEvaluators(bracketBeginIndex, bracketEndIndex, evaluators)
1354 self.value = {}
1355 for evaluatedExpressionValueEvaluator in evaluatedExpressionValueEvaluators:
1356 keyValue = evaluatedExpressionValueEvaluator.value
1357 self.value[keyValue.key] = keyValue.value
1358 del evaluators[bracketBeginIndex + 1: bracketEndIndex + 1]
1359
1360
1362 'Class to evaluate a string.'
1364 'Set value to none.'
1365 self.arguments = []
1366 self.value = None
1367 self.word = word
1368
1369 - def executeBracket( self, bracketBeginIndex, bracketEndIndex, evaluators ):
1370 'Execute the bracket.'
1371 self.arguments = getBracketValuesDeleteEvaluator(bracketBeginIndex, bracketEndIndex, evaluators)
1372 if len( self.arguments ) < 1:
1373 return
1374 if len( self.arguments ) > 1:
1375 self.value = self.arguments
1376 else:
1377 self.value = self.arguments[0]
1378
1380 'Evaluate the statement and delete the evaluators.'
1381 previousIndex = evaluatorIndex - 1
1382 if previousIndex < 0:
1383 return
1384 evaluators[ previousIndex ].executeFunction( evaluators, previousIndex, self )
1385
1386
1388 'Class to evaluate a string.'
1389 - def executeBracket( self, bracketBeginIndex, bracketEndIndex, evaluators ):
1392
1394 'Evaluate the statement and delete the evaluators.'
1395 previousIndex = evaluatorIndex - 1
1396 if previousIndex < 0:
1397 return
1398 if self.value.__class__ != list:
1399 return
1400 evaluators[ previousIndex ].executeKey( evaluators, self.value, previousIndex, self )
1401
1402
1404 'Class evaluator class.'
1406 'Set value to none.'
1407 self.value = None
1408 self.word = word
1409 self.xmlElement = xmlElement
1410
1412 'Execute the function.'
1413 if self.xmlElement.xmlObject == None:
1414 self.xmlElement.xmlObject = FunctionVariable(self.xmlElement)
1415 nextEvaluator.value = ClassObject(self.xmlElement)
1416 initializeFunction = None
1417 if '_init' in self.xmlElement.xmlObject.functionDictionary:
1418 function = self.xmlElement.xmlObject.functionDictionary['_init']
1419 function.classObject = nextEvaluator.value
1420 setFunctionLocalDictionary(nextEvaluator.arguments, function)
1421 function.getReturnValue()
1422 del evaluators[evaluatorIndex]
1423
1424
1426 'Class to join two evaluators.'
1428 'Operate on two evaluators.'
1429 if operationLevel != 0:
1430 return
1431 previousIndex = evaluatorIndex - 1
1432 if previousIndex < 0:
1433 evaluators[evaluatorIndex].value = None
1434 return
1435 if evaluators[previousIndex].word == ',':
1436 evaluators[evaluatorIndex].value = None
1437 return
1438 del evaluators[evaluatorIndex]
1439
1440
1442 'Class to join two evaluators.'
1444 'Operate on two evaluators.'
1445 if operationLevel != 80:
1446 return
1447 leftIndex = evaluatorIndex - 1
1448 if leftIndex < 0:
1449 del evaluators[evaluatorIndex]
1450 return
1451 rightIndex = evaluatorIndex + 1
1452 if rightIndex >= len(evaluators):
1453 del evaluators[ leftIndex : rightIndex ]
1454 return
1455 leftValue = evaluators[leftIndex].value
1456 rightValue = evaluators[rightIndex].value
1457 if leftValue.__class__ == rightValue.__class__ and (leftValue.__class__ == list or rightValue.__class__ == str):
1458 evaluators[leftIndex].value = leftValue + rightValue
1459 del evaluators[ evaluatorIndex : evaluatorIndex + 2 ]
1460 return
1461 if leftValue.__class__ == list and rightValue.__class__ == int:
1462 if rightValue > 0:
1463 originalList = leftValue[:]
1464 for copyIndex in xrange( rightValue - 1 ):
1465 leftValue += originalList
1466 evaluators[leftIndex].value = leftValue
1467 del evaluators[ evaluatorIndex : evaluatorIndex + 2 ]
1468 return
1469 if leftValue.__class__ == dict and rightValue.__class__ == dict:
1470 leftValue.update(rightValue)
1471 evaluators[leftIndex].value = leftValue
1472 del evaluators[ evaluatorIndex : evaluatorIndex + 2 ]
1473 return
1474 del evaluators[ leftIndex : evaluatorIndex + 2 ]
1475
1476
1478 'Class to join two evaluators.'
1480 'Operate on two evaluators.'
1481 if operationLevel != 10:
1482 return
1483 leftEvaluatorIndex = evaluatorIndex - 1
1484 if leftEvaluatorIndex < 0:
1485 print('Warning, leftEvaluatorIndex is less than zero in EvaluatorDictionary for:')
1486 print(self)
1487 print(evaluators)
1488 return
1489 rightEvaluatorIndex = evaluatorIndex + 1
1490 if rightEvaluatorIndex >= len(evaluators):
1491 print('Warning, rightEvaluatorIndex too high in EvaluatorDictionary for:')
1492 print(rightEvaluatorIndex)
1493 print(self)
1494 print(evaluators)
1495 return
1496 evaluators[rightEvaluatorIndex].value = KeyValue(evaluators[leftEvaluatorIndex].value, evaluators[rightEvaluatorIndex].value)
1497 del evaluators[ leftEvaluatorIndex : rightEvaluatorIndex ]
1498
1499
1501 'Class to divide two evaluators.'
1503 'Operate on two evaluators.'
1504 if operationLevel == 40:
1505 self.executePair(evaluators, evaluatorIndex)
1506
1508 'Divide two values.'
1509 return leftValue / rightValue
1510
1511
1513 'Element evaluator class.'
1515 'Set value to none.'
1516 self.value = None
1517 self.word = word
1518 self.xmlElement = xmlElement
1519
1521 'Execute operator which acts on the center.'
1522 dotIndex = self.word.find('.')
1523 if dotIndex < 0:
1524 print('Warning, EvaluatorElement in evaluate can not find the dot for:')
1525 print(functionName)
1526 print(self)
1527 return
1528 attributeName = self.word[dotIndex + 1 :]
1529 moduleName = self.word[: dotIndex]
1530 if moduleName in globalModuleFunctionsDictionary:
1531 self.value = globalModuleFunctionsDictionary[moduleName](attributeName, self.xmlElement)
1532 return
1533 pluginModule = None
1534 if moduleName in globalElementNameSet:
1535 pluginModule = archive.getModuleWithPath(archive.getElementsPath(moduleName))
1536 if pluginModule == None:
1537 print('Warning, EvaluatorElement in evaluate can not get a pluginModule for:')
1538 print(moduleName)
1539 print(self)
1540 return
1541 getAccessibleAttributeFunction = pluginModule._getAccessibleAttribute
1542 globalModuleFunctionsDictionary[moduleName] = getAccessibleAttributeFunction
1543 self.value = getAccessibleAttributeFunction(attributeName, self.xmlElement)
1544
1548
1549
1551 'Class to evaluate a string.'
1553 'Set value to zero.'
1554 self.value = False
1555 self.word = word
1556
1557
1559 'Function evaluator class.'
1561 'Set value to none.'
1562 self.value = None
1563 self.word = word
1564 self.xmlElement = xmlElement
1565
1567 'Execute the function.'
1568 if self.xmlElement.xmlObject == None:
1569 if 'return' in self.xmlElement.attributeDictionary:
1570 value = self.xmlElement.attributeDictionary['return']
1571 self.xmlElement.xmlObject = getEvaluatorSplitWords(value)
1572 else:
1573 self.xmlElement.xmlObject = []
1574 self.function = Function(self.xmlElement )
1575 setFunctionLocalDictionary(nextEvaluator.arguments, self.function)
1576 nextEvaluator.value = self.function.getReturnValue()
1577 del evaluators[evaluatorIndex]
1578
1579
1581 'Fundamental evaluator class.'
1610
1614
1615
1617 'Class to compare two evaluators.'
1619 'Compare two values.'
1620 return leftValue >= rightValue
1621
1622
1624 'Class to compare two evaluators.'
1626 'Compare two values.'
1627 return leftValue > rightValue
1628
1629
1631 'Class to compare two evaluators.'
1633 'Compare two values.'
1634 return leftValue <= rightValue
1635
1636
1638 'Class to compare two evaluators.'
1640 'Compare two values.'
1641 return leftValue < rightValue
1642
1643
1645 'Class to get a local variable.'
1647 'Execute operator which acts on the center.'
1648 functions = self.xmlElement.getXMLProcessor().functions
1649 if len(functions) < 1:
1650 print('Warning, there are no functions in EvaluatorLocal in evaluate for:')
1651 print(self.word)
1652 return
1653 attributeKeywords = self.word.split('.')
1654 self.value = functions[-1].localDictionary[attributeKeywords[0]]
1655 for attributeKeyword in attributeKeywords[1 :]:
1656 self.value = self.value._getAccessibleAttribute(attributeKeyword)
1657
1658
1660 'Class to modulo two evaluators.'
1662 'Modulo two values.'
1663 return leftValue % rightValue
1664
1665
1667 'Class to multiply two evaluators.'
1669 'Multiply two values.'
1670 return leftValue * rightValue
1671
1672
1674 'Class to evaluate None.'
1676 'Set value to none.'
1677 self.value = None
1678 self.word = str(word)
1679
1680
1682 'Class to compare two evaluators.'
1684 'Minus the value to the right.'
1685 if operationLevel == 13:
1686 self.executeLeft(evaluators, evaluatorIndex)
1687
1689 'Minus value.'
1690 return not value
1691
1692
1694 'Class to compare two evaluators.'
1696 'Compare two values.'
1697 return leftValue != rightValue
1698
1699
1701 'Class to evaluate a string.'
1703 'Set value.'
1704 self.value = None
1705 self.word = word
1706 try:
1707 if '.' in word:
1708 self.value = float(word)
1709 else:
1710 self.value = int(word)
1711 except:
1712 print('Warning, EvaluatorNumeric in evaluate could not get a numeric value for:')
1713 print(word)
1714 print(xmlElement)
1715
1716
1718 'Class to compare two evaluators.'
1720 'Or two values.'
1721 return leftValue or rightValue
1722
1723
1725 'Class to power two evaluators.'
1727 'Operate on two evaluators.'
1728 if operationLevel == 60:
1729 self.executePair(evaluators, evaluatorIndex)
1730
1732 'Power of two values.'
1733 return leftValue ** rightValue
1734
1735
1737 'Class to handle self.'
1739 'Execute operator which acts on the center.'
1740 functions = self.xmlElement.getXMLProcessor().functions
1741 if len(functions) < 1:
1742 print('Warning, there are no functions in executeCenterOperation in EvaluatorSelf in evaluate for:')
1743 print(self.xmlElement)
1744 return
1745 function = functions[-1]
1746 attributeKeywords = self.word.split('.')
1747 self.value = function.classObject
1748 for attributeKeyword in attributeKeywords[1 :]:
1749 self.value = self.value._getAccessibleAttribute(attributeKeyword)
1750
1751
1753 'Class to evaluate a string.'
1755 'Set value to true.'
1756 self.value = True
1757 self.word = word
1758
1759
1761 'Class to evaluate a string.'
1763 'Set value to none.'
1764 self.value = word
1765 self.word = str(word)
1766
1767
1769 'Class to get equation results.'
1771 'Initialize.'
1772 self.localDictionary = {}
1773 self.evaluatorSplitLine = xmlElement.xmlObject
1774 self.xmlElement = xmlElement
1775 self.xmlProcessor = xmlElement.getXMLProcessor()
1776
1778 'Get return value without deleting last function.'
1779 self.returnValue = None
1780 self.xmlProcessor.functions.append(self)
1781 if len(self.evaluatorSplitLine) < 1:
1782 self.shouldReturn = False
1783 self.processChildNodes(self.xmlElement)
1784 else:
1785 self.returnValue = getEvaluatedExpressionValueBySplitLine(self.evaluatorSplitLine, self.xmlElement)
1786 return self.returnValue
1787
1788
1790 'Class to hold class functions and variable set.'
1792 'Initialize.'
1793 self.functionDictionary = {}
1794 self.variables = []
1795 self.processClass(xmlElement)
1796
1798 'Add to variables.'
1799 setLocalAttribute(xmlElement)
1800 keySplitLine = xmlElement.xmlObject.key.split('.')
1801 if len(keySplitLine) == 2:
1802 if keySplitLine[0] == 'self':
1803 variable = keySplitLine[1]
1804 if variable not in self.variables:
1805 self.variables.append(variable)
1806
1808 'Add class to FunctionVariable.'
1809 for childNode in xmlElement.childNodes:
1810 self.processFunction(childNode)
1811 if 'parentNode' in xmlElement.attributeDictionary:
1812 self.processClass(xmlElement.getXMLElementByImportID(xmlElement.attributeDictionary['parentNode']))
1813
1815 'Add function to function dictionary.'
1816 if xmlElement.localName != 'function':
1817 return
1818 idKey = xmlElement.attributeDictionary['id']
1819 if idKey in self.functionDictionary:
1820 return
1821 self.functionDictionary[idKey] = ClassFunction(xmlElement)
1822 for childNode in xmlElement.childNodes:
1823 self.processStatement(childNode)
1824
1826 'Add self statement to variables.'
1827 if xmlElement.localName == 'statement':
1828 self.addToVariableSet(xmlElement)
1829 for childNode in xmlElement.childNodes:
1830 self.processStatement(childNode)
1831
1832
1834 'Class to hold a key value.'
1835 - def __init__(self, key=None, value=None):
1836 'Get key value.'
1837 self.key = key
1838 self.value = value
1839
1841 'Get the string representation of this KeyValue.'
1842 return str(self.__dict__)
1843
1845 'Get by character.'
1846 dotIndex = line.find( character )
1847 if dotIndex < 0:
1848 self.key = line
1849 self.value = None
1850 return self
1851 self.key = line[: dotIndex]
1852 self.value = line[dotIndex + 1 :]
1853 return self
1854
1858
1862
1863
1865 'Class to get the in attribute, the index name and the value name.'
1867 'Initialize.'
1868 self.conditionSplitWords = None
1869 self.elseElement = None
1870 if 'condition' in xmlElement.attributeDictionary:
1871 self.conditionSplitWords = getEvaluatorSplitWords( xmlElement.attributeDictionary['condition'] )
1872 else:
1873 print('Warning, could not find the condition attribute in ModuleXMLElement in evaluate for:')
1874 print(xmlElement)
1875 return
1876 if len( self.conditionSplitWords ) < 1:
1877 self.conditionSplitWords = None
1878 print('Warning, could not get split words for the condition attribute in ModuleXMLElement in evaluate for:')
1879 print(xmlElement)
1880 nextIndex = getNextChildIndex(xmlElement)
1881 if nextIndex >= len( xmlElement.parentNode.childNodes ):
1882 return
1883 nextXMLElement = xmlElement.parentNode.childNodes[ nextIndex ]
1884 lowerLocalName = nextXMLElement.localName.lower()
1885 if lowerLocalName != 'else' and lowerLocalName != 'elif':
1886 return
1887 xmlProcessor = xmlElement.getXMLProcessor()
1888 if lowerLocalName not in xmlProcessor.namePathDictionary:
1889 return
1890 self.pluginModule = archive.getModuleWithPath( xmlProcessor.namePathDictionary[ lowerLocalName ] )
1891 if self.pluginModule == None:
1892 return
1893 self.elseElement = nextXMLElement
1894
1896 'Process the else statement.'
1897 if self.elseElement != None:
1898 self.pluginModule.processElse( self.elseElement)
1899
1900
1901 globalCreationDictionary = archive.getGeometryDictionary('creation')
1902 globalDictionaryOperatorBegin = {
1903 '||' : EvaluatorConcatenate,
1904 '==' : EvaluatorEqual,
1905 '>=' : EvaluatorGreaterEqual,
1906 '<=' : EvaluatorLessEqual,
1907 '!=' : EvaluatorNotEqual,
1908 '**' : EvaluatorPower }
1909 globalModuleEvaluatorDictionary = {}
1910 globalFundamentalNameSet = set(archive.getPluginFileNamesFromDirectoryPath(archive.getFundamentalsPath()))
1911 addPrefixDictionary(globalModuleEvaluatorDictionary, globalFundamentalNameSet, EvaluatorFundamental)
1912 globalElementNameSet = set(archive.getPluginFileNamesFromDirectoryPath(archive.getElementsPath()))
1913 addPrefixDictionary(globalModuleEvaluatorDictionary, globalElementNameSet, EvaluatorElement)
1914 globalModuleEvaluatorDictionary['self'] = EvaluatorSelf
1915 globalSplitDictionaryOperator = {
1916 '+' : EvaluatorAddition,
1917 '{' : EvaluatorBracketCurly,
1918 '}' : Evaluator,
1919 '(' : EvaluatorBracketRound,
1920 ')' : Evaluator,
1921 '[' : EvaluatorBracketSquare,
1922 ']' : Evaluator,
1923 ',' : EvaluatorComma,
1924 ':' : EvaluatorDictionary,
1925 '/' : EvaluatorDivision,
1926 '>' : EvaluatorGreater,
1927 '<' : EvaluatorLess,
1928 '%' : EvaluatorModulo,
1929 '*' : EvaluatorMultiplication,
1930 '-' : EvaluatorSubtraction }
1931 globalSplitDictionary = getSplitDictionary()
1932