File size: 3,795 Bytes
21baa2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

'''
Created on ١٩‏/٠٣‏/٢٠١٠

@Created by: Muhammad Altabba
'''
from .TransitionActions import TransitionActions

class State(object):
    """
     # PyUML: Do not remove this line! # XMI_ID:_qzIH5I35Ed-gg8GOK1TmhA
    """
    '''
    State of Graph
    '''
    Id = None;
#    
#    NextStates = dict();
#    #Mapping between Sates  and Conditions .
#    #Move to a desired state on some conditions.
#    #NextStates[NEXT_STATE] = [[CONDITIONS_LIST_1, ACTIONS_LIST_1], [CONDITIONS_LIST_2, ACTIONS_LIST_2],...]
#    #Note: all CONDITIONS_LIST_1 must be satisfied to do ACTIONS_LIST_1. Or all CONDITIONS_LIST_2 must be satisfied to do ACTIONS_LIST_2.

    Transitions = [];

    IsStart = False;

    IsEnd = False;

    def __init__(self, id = None, transitions = None):
        '''
        Constructor
        '''
        self.Id = id;
        if transitions == None:
            self.Transitions = [];
        self.IsStart = False;    
        self.IsEnd = False;
    
    def NextStateWithActions(self, word, charIndex = None, charIndexForward = True):
        '''
        [charIndexForward] is used to specify the look ahead direction is it forward or backward.
        '''
        
#        print('-- NextStateWithActions...');
        for l in range(len(self.Transitions)):
            conditions = self.Transitions[l].Conditions;
            transitionActions = TransitionActions([], []); 
            transitionActions.Actions = [action for action in self.Transitions[l].Actions.Actions];
            transitionActions.OnInnerIndexes = [i for i in range(len(word.SurfaceFormMorphemes))];
            nextState = self.Transitions[l].NextState;
            numberToConsume = self.Transitions[l].Consume;
            
            conditionSatisfied = True;
#            print('-- Transitions loop...');
            for j in range(len(conditions)):
                if(charIndex == None):
#                    print('-- charIndex == None');
                    result = conditions[j].TestMatch(word, transitionActions, [charIndex]);
                else:
#                    print('-- charIndex != None');
                    if(charIndexForward == True):
#                        print('-- charIndexForward == True');
                        if(charIndex < 0 or charIndex+numberToConsume >= len(word.String)):
                            conditionSatisfied = False;
                            break;
#                        print('-- TestMatch');
                        result = conditions[j].TestMatch(word, transitionActions, range(charIndex, charIndex+numberToConsume));
                    elif(charIndexForward == False):
#                        print('-- charIndexForward == False');
#                        print('-- charIndex = '+str(charIndex));
#                        print('-- numberToConsume = '+str(numberToConsume));
#                        print('-- len(word.String) = '+str(len(word.String)));
                        if(charIndex-numberToConsume+1 < 0 or charIndex+1 > len(word.String)):
                            conditionSatisfied = False;
                            break;
                        
#                        print('-- TestMatch');
                        result = conditions[j].TestMatch(word, transitionActions, range(charIndex-numberToConsume+1, charIndex+1));
                    else:
                        raise Exception('Unexpected value ['+charIndexForward+'] for variable: charIndexForward');
                if result != True:
                    conditionSatisfied = False;
                    break;

                        
                            
            if conditionSatisfied == True:
                return [nextState, transitionActions, numberToConsume];
        return [None, None, None];
        pass