Exemplo n.º 1
0
	def replace_subtree (cls, root_node, side, is_new, cursor):
		actor, actant = PropositionTree.get_actor_and_actant (root_node)
		result_node = PropositionTreeNode ()
		result_node.type = PropositionTreeNodeType.concept
		result_node.side = side
		result_node.concept = TreeNodeConcept ()
		is_memobject = False
		error_text = ""

		if root_node.concept.name == LanguageHelper.translate ("to-have"):
			if actant.concept.name == LanguageHelper.translate ("name"):
				if actor.concept.name == LanguageHelper.translate ("module"):
					child1 = actant.children[0]
					if child1.type == PropositionTreeNodeType.linkage:
						if child1.linkage.name == LanguageHelper.translate ("which"):
							child2 = child1.children[0]
							if child2.type == PropositionTreeNodeType.concept:
								result_node.concept.type = TreeNodeConceptType.module
								result_node.concept.name = child2.concept.name
								result_node.text = result_node.concept.name
								is_memobject = True
				elif actor.concept.name == LanguageHelper.translate ("field"):
					child1 = actant.children[0]
					if child1.type == PropositionTreeNodeType.linkage:
						if child1.linkage.name == LanguageHelper.translate ("which"):
							child2 = child1.children[0]
							if child2.type == PropositionTreeNodeType.code_object:
								if is_new == True:
									result_node.concept.id = MemoryProvider.create_field (child2.text)
									is_memobject = True
									result_node.concept.type = TreeNodeConceptType.field
									result_node.concept.name = "$" + str (result_node.concept.id)
									result_node.text = result_node.concept.name
									ContextProvider.set_field_node (result_node)
								else:
									result_node.concept.id = MemoryProvider.get_field_id (child2.text)
									is_memobject = True
									result_node.concept.type = TreeNodeConceptType.field
									result_node.concept.name = "$" + str (result_node.concept.id)
									result_node.text = result_node.concept.name
				elif actor.concept.name == LanguageHelper.translate ("constant"):
					child1 = actant.children[0]
					if child1.type == PropositionTreeNodeType.linkage:
						if child1.linkage.name == LanguageHelper.translate ("which"):
							child2 = child1.children[0]
							if child2.type == PropositionTreeNodeType.code_object:
								if is_new == True:
									result_node.concept.id = MemoryProvider.create_constant (child2.text)
									is_memobject = True
									result_node.concept.type = TreeNodeConceptType.constant
									result_node.concept.name = "$" + str (result_node.concept.id)
									result_node.text = result_node.concept.name
									ContextProvider.set_constant_node (result_node)
								else:
									result_node.concept.id = MemoryProvider.get_constant_id (child2.text)
									is_memobject = True
									result_node.concept.type = TreeNodeConceptType.constant
									result_node.concept.name = "$" + str (result_node.concept.id)
									result_node.text = result_node.concept.name
				elif actor.concept.name == LanguageHelper.translate ("list"):
					child1 = actant.children[0]
					if child1.type == PropositionTreeNodeType.linkage:
						if child1.linkage.name == LanguageHelper.translate ("which"):
							child2 = child1.children[0]
							if child2.type == PropositionTreeNodeType.code_object:
								if is_new == True:
									result_node.concept.id = MemoryProvider.create_list (child2.text)
									is_memobject = True
									result_node.concept.type = TreeNodeConceptType.memlist
									result_node.concept.name = "$" + str (result_node.concept.id)
									result_node.text = result_node.concept.name
									ContextProvider.set_list_node (result_node)
								else:
									result_node.concept.id = MemoryProvider.get_list_id (child2.text)
									is_memobject = True
									result_node.concept.type = TreeNodeConceptType.memlist
									result_node.concept.name = "$" + str (result_node.concept.id)
									result_node.text = result_node.concept.name
				elif actor.concept.name == LanguageHelper.translate ("trigger"):
					child1 = actant.children[0]
					if child1.type == PropositionTreeNodeType.linkage:
						if child1.linkage.name == LanguageHelper.translate ("which"):
							child2 = child1.children[0]
							if child2.type == PropositionTreeNodeType.code_object:
								if is_new == True:
									result_node.concept.id = TriggerProvider.create_trigger (child2.text)
									result_node.concept.type = TreeNodeConceptType.trigger
									result_node.concept.name = "$" + str (result_node.concept.id)
									result_node.text = result_node.concept.name
									ContextProvider.set_trigger_node (result_node)
				elif actor.concept.name == LanguageHelper.translate ("condition"):
					child1 = actant.children[0]
					if child1.type == PropositionTreeNodeType.linkage:
						if child1.linkage.name == LanguageHelper.translate ("which"):
							child2 = child1.children[0]
							if child2.type == PropositionTreeNodeType.code_object:
								if is_new == True:
									result_node.concept.id = ConditionProvider.create_condition (child2.text)
									result_node.concept.type = TreeNodeConceptType.condition
									result_node.concept.name = "$" + str (result_node.concept.id)
									result_node.text = result_node.concept.name
									ContextProvider.set_condition_node (result_node)
							elif child2.type == PropositionTreeNodeType.concept:
								if is_new == True:
									result_node.concept.id = ConditionProvider.create_condition (child2.text)
									result_node.concept.type = TreeNodeConceptType.condition
									result_node.concept.name = "$" + str (result_node.concept.id)
									result_node.text = result_node.concept.name
									ContextProvider.set_condition_node (result_node)
				elif actor.concept.name == LanguageHelper.translate ("procedure"):
					child1 = actant.children[0]
					if child1.type == PropositionTreeNodeType.linkage:
						if child1.linkage.name == LanguageHelper.translate ("which"):
							child2 = child1.children[0]
							if child2.type == PropositionTreeNodeType.concept:
								database_triad = DatabaseTriad.read (cursor, actant.concept.id, child1.linkage.id, child2.concept.id)
								if database_triad == None:
									error_text = ErrorHelper.get_text (105)
									return None, error_text
								database_sequense1 = DatabaseSequence.read (cursor, 0, 0, database_triad.id)
								if database_sequense1 == None:
									error_text = ErrorHelper.get_text (105)
									return None, error_text
								database_triad = DatabaseTriad.read_by_id (cursor, database_sequense1.left_triad_id)
								if database_triad == None:
									error_text = ErrorHelper.get_text (105)
									return None, error_text
								if database_triad.left_concept_id == root_node.concept.id:
									database_sequense2 = DatabaseSequence.read (cursor, database_sequense1.proposition_id, 0, database_triad.id)
									if database_sequense2 == None:
										error_text = ErrorHelper.get_text (105)
										return None, error_text
									database_triad = DatabaseTriad.read_by_id (cursor, database_sequense2.left_triad_id)
									if database_triad == None:
										error_text = ErrorHelper.get_text (105)
										return None, error_text
									result_node.concept.id = database_triad.left_concept_id
									database_concept = DatabaseConcept.read_by_name (cursor, LanguageHelper.translate ("to-be"))
									if database_concept == None:
										error_text = ErrorHelper.get_text (104)
										return None, error_text
									database_triad1 = DatabaseTriad.read (cursor, result_node.concept.id, 0, database_concept.id)
									if database_triad1 == None:
										error_text = ErrorHelper.get_text (104)
										return None, error_text
									database_triad2 = DatabaseTriad.read (cursor, database_concept.id, 0, actor.concept.id)
									if database_triad2 == None:
										error_text = ErrorHelper.get_text (104)
										return None, error_text
									database_sequense3 = DatabaseSequence.read (cursor, 0, database_triad1.id, database_triad2.id)
									if database_sequense3 == None:
										error_text = ErrorHelper.get_text (104)
										return None, error_text
								else:
									error_text = ErrorHelper.get_text (105)
									return None, error_text
			elif actant.concept.name == LanguageHelper.translate ("class"):
				if actor.concept.name == LanguageHelper.translate ("trigger"):
					child1 = actant.children[0]
					if child1.type == PropositionTreeNodeType.linkage:
						if child1.linkage.name == LanguageHelper.translate ("which"):
							child2 = child1.children[0]
							if child2.type == PropositionTreeNodeType.string:
								result_node.concept.id = TriggerProvider.get_id_by_class (child2.text)
								result_node.concept.type = TreeNodeConceptType.trigger
								result_node.concept.name = "$" + str (result_node.concept.id)
								result_node.text = result_node.concept.name
								is_memobject = True
		if is_memobject != True:
			if result_node.concept.id != 0:
				database_concept = DatabaseConcept.read_by_id (cursor, result_node.concept.id)
				result_node.concept.type = database_concept.type
				result_node.concept.name = database_concept.name
				result_node.text = result_node.concept.name
			else:
				return None, error_text

		return result_node, error_text
Exemplo n.º 2
0
def main (single_run, use_ctl, use_dbg, text):
    exit = False
    db = MySQLdb.connect (host="localhost", user="******", passwd="qslbase", db="qslbase", charset="utf8")
    cursor = db.cursor ()
    result = ""
    sys.setdefaultencoding ("utf8")
    SyntaxAnalyzer (cursor)
    semantic_analyzer = SemanticAnalyzer (cursor)
    CodeProvider (cursor)
    CodeStack ()
    ErrorHelper (cursor)
    MemoryProvider ()
    ContextProvider ()
    TriggerProvider ()
    ConditionProvider ()
    DebuggerProvider (use_dbg)

    if single_run == False:
        stdin = os.open ("/tmp/qslime-std-in", os.O_RDONLY | os.O_NONBLOCK)
        stdout = os.open ("/tmp/qslime-std-out", os.O_WRONLY)
    if use_ctl == True:
        ctlout = os.open ("/tmp/qslime-ctl-out", os.O_WRONLY)
        ctlin = open ("/tmp/qslime-ctl-in", "r")
    if use_dbg == True:
        dbgout = os.open ("/tmp/qslime-dbg-out", os.O_WRONLY)
        dbgin = open ("/tmp/qslime-dbg-in", "r")
        DebuggerProvider.dbgin = dbgin
        DebuggerProvider.dbgout = dbgout
    if use_ctl == True:
        text = ctlin.readline ()
        if text != "":
            if SyntaxAnalyzer.analize (text):
                #PropositionTree.print_tree (SyntaxAnalyzer.proposition_tree)
                if semantic_analyzer.analize (SyntaxAnalyzer.proposition_tree, None):
                    #semantic_analyzer.proposition_tree.print_tree ()
                    result += semantic_analyzer.result
                    os.write (ctlout, 'OK.\n')
                else:
                    os.write (ctlout, semantic_analyzer.get_error_text () + '\n')
            else:
                os.write (ctlout, semantic_analyzer.get_error_text () + '\n')
    else:
        if text != "":
            #print text
            if DebuggerProvider.use == True:
                DebuggerProvider.reset ()
                DebuggerProvider.set_single_code_line (text)
                DebuggerProvider.build_debug_data ()
                DebuggerProvider.send_data ()
                DebuggerProvider.receive_data ()
            if SyntaxAnalyzer.analize (text):
                #SyntaxAnalyzer.proposition_tree.print_tree (SyntaxAnalyzer.proposition_tree)
                if semantic_analyzer.analize (SyntaxAnalyzer.proposition_tree, None):
                    #semantic_analyzer.proposition_tree.print_tree ()
                    result += semantic_analyzer.result
                else:
                    return semantic_analyzer.get_error_text ()
            else:
                return SyntaxAnalyzer.get_error_text ()

    while (exit != True):
        code_line = CodeStack.pop ()
        while (code_line != None):
            if code_line.text != "":
                #print code_line.text
                if DebuggerProvider.use == True:
                    if code_line.concept_id != 0 and code_line.id != 0:
                        DebuggerProvider.set_procedure_id (code_line.concept_id)
                        DebuggerProvider.set_line_id (code_line.id)
                    else:
                        DebuggerProvider.set_single_code_line (code_line.text)
                    DebuggerProvider.build_debug_data ()
                    DebuggerProvider.send_data ()
                    DebuggerProvider.receive_data ()
                analized = True
                if code_line.tree == None:
                    analized = SyntaxAnalyzer.analize (code_line.text)
                    tree = SyntaxAnalyzer.proposition_tree
                else:
                    tree = code_line.tree
                if analized:
                    #tree.print_tree (tree)
                    if semantic_analyzer.analize (tree, code_line):
                        #tree.print_tree (tree)
                        if code_line.tree != None:
                            if code_line.tree.is_totally_parsed == False:
                                code_line.tree = semantic_analyzer.proposition_tree
                                code_line.tree.is_totally_parsed = True
                        result += semantic_analyzer.result
                    else:
                        return semantic_analyzer.get_error_text ()
                else:
                    return SyntaxAnalyzer.get_error_text ()
                if CodeStack.is_empty () == True:
                    if CodeStack.inside_procedure == False:
                        if CodeProvider.is_priorities_assigned () == False:
                            CodeProvider.assign_priorities ()
                        TriggerProvider.dispatch_triggers ()
                        ConditionProvider.dispatch_conditions ()
                        CodeStack.sort ()
            else:
                if code_line.prev_line_id == -1 and code_line.concept_id != 0:
                    CodeProvider.execute_procedure (code_line.concept_id)
            code_line = CodeStack.pop ()

            if result != "" and single_run == False:
                #print result
                os.write (stdout, result)
                result = ""
                exit = True

        if single_run == True:
            return result

    os.close (stdin)
    os.close (stdout)
    os.close (ctlin)
    os.close (ctlout)
    os.close (dbgin)
    os.close (dbgout)
Exemplo n.º 3
0
    def analize (cls, text):
        word = []
        tokens = []
        prev_letter = ""

        # Разбивка на токены
        i = 0
        while i < len (text):
            letter = text[i]
            if letter == " ":
                if len (word) > 0:
                    token = Token ()
                    token.text = ''.join (word)
                    tokens.append (token)
                    word = []
            elif letter == "(" or \
                 letter == ")" or \
                 letter == ",":
                 #letter == "_":
                if len (word) > 0:
                    token = Token ()
                    token.text = ''.join (word)
                    tokens.append (token)
                    word = []
                token = Token ()
                token.text = letter
                tokens.append (token)
            elif letter == "." or \
                 letter == "_":
                j = i + 1
                if j == len (text):
                    if len (word) > 0:
                        token = Token ()
                        token.text = ''.join (word)
                        tokens.append (token)
                        word = []
                    token = Token ()
                    token.text = letter
                    tokens.append (token)
                while j < len (text):
                    letter = text[j]

                    if letter == " ":
                        break
                    elif letter == ")" or \
                         letter == "(" or \
                         letter == ",":
                        j = j - 1
                        break
                    if i == (j - 1):
                        if letter == " ":
                            if len (word) > 0:
                                token = Token ()
                                token.text = ''.join (word)
                                tokens.append (token)
                                word = []
                            token = Token ()
                            token.text = letter
                            tokens.append (token)
                            break
                        else:
                            word.append (text[i])
                            prev_letter = letter
                            word.append (letter)
                            j += 1
                    else:
                        prev_letter = letter
                        word.append (letter)
                        j += 1
                i = j
            elif letter == "=":
                if prev_letter == " " or \
                   prev_letter == "(":
                    if len (word) > 0:
                        token = Token ()
                        token.text = ''.join (word)
                        tokens.append (token)
                        word = []
                    token = Token ()
                    token.text = letter
                    tokens.append (token)
                else:
                    word.append (letter)
            elif letter == "?":
                if len (word) > 0:
                    token = Token ()
                    token.text = ''.join (word)
                    tokens.append (token)
                    word = []
                if prev_letter not in [" ", "("]:
                    token = Token ()
                    token.text = letter
                    tokens.append (token)
                else:
                    word.append (letter)
            elif letter == "\"":
                i += 1
                while i < len (text):
                    prev_letter = letter
                    letter = text[i]
                    if letter == "\"" and prev_letter != "\\":
                        break
                    else:
                        word.append (letter)
                    i += 1
                token = Token ()
                token.text = ''.join (word)
                token.type = TokenType.string
                tokens.append (token)
                word = []
            else:
                word.append (letter)
            prev_letter = letter
            i += 1

        # Идентификация токенов
        for token in tokens:
            #print token.text
            if token.type == TokenType.string:
                continue
            if token.text.find ('?') == 0 and len (token.text) > 1:
                s = token.text.replace ('?', '')
                query = "SELECT id FROM qsl_linkage WHERE name = \'" + s + "\';"
                cls.__cursor.execute (query)
                row = cls.__cursor.fetchone ()
                if row != None:
                    token.type = TokenType.linkage
                    token.linkage = TokenLinkage ()
                    token.linkage.id = row[0]
                    token.linkage.name = s
                else:
                    cls.__error_text = ErrorHelper.get_text (102, token.text)
                    return False
            elif token.text.find ('%') == 0 and len (token.text) > 1:
                token.type = TokenType.code_object
            elif token.text.find ('*') == 0:
                # Модификатор
                token.type = TokenType.modifier
            elif token.text == "(":
                token.type = TokenType.opening_bracket
            elif token.text == ")":
                token.type = TokenType.closing_bracket
            elif token.text == ",":
                token.type = TokenType.comma
            elif token.text == "_":
                token.type = TokenType.underscore
            elif token.text == ".":
                token.type = TokenType.point
            elif token.text == "?":
                token.type = TokenType.question_mark
            elif token.text == "=":
                token.type = TokenType.equal_sign
            else:
                query = "SELECT id, type FROM qsl_concept WHERE name = \'" + token.text + "\';"
                cls.__cursor.execute (query)
                row = cls.__cursor.fetchone ()
                if row != None:
                    token.type = TokenType.concept
                    token.concept = TokenConcept ()
                    token.concept.id = row[0]
                    token.concept.type = row[1]
                    token.concept.name = token.text
                else:
                    if token.text.isdigit ():
                        token.type = TokenType.number
                    else:
                        cls.__error_text = ErrorHelper.get_text (103, token.text)
                        return False

        node = cls.build_tree (tokens)
        if node != None:
            cls.proposition_tree = PropositionTree ()
            cls.proposition_tree.root_node = node
        else:
            return False

        return True
Exemplo n.º 4
0
    def build_tree (cls, tokens):
        # Поиск главного понятия действия
        idx = 0
        node = None
        root_node = None
        for token in tokens:
            if token.type == TokenType.concept:
                if token.concept.type == TokenConceptType.action:
                    node = PropositionTreeNode ()
                    node.text = token.text
                    node.type = PropositionTreeNodeType.concept
                    node.side = PropositionTreeNodeSide.center
                    node.concept = TreeNodeConcept ()
                    node.concept.id = token.concept.id
                    node.concept.name = token.concept.name
                    node.concept.type = token.concept.type
                    root_node = node
                    break
            idx += 1

        if node == None:
            cls.__error_text = ErrorHelper.get_text (101)
            return None

        # Обработка левой ветки суждения
        i = idx - 1
        while i >= 0:
            if tokens[i].type == TokenType.equal_sign:
                if tokens[i+1].type == TokenType.concept:
                    node.concept.sublink = True
            else:
                parent_node = node
                node = PropositionTreeNode ()
                node.parent = parent_node
                node.text = tokens[i].text
                if tokens[i].type == TokenType.concept:
                    node.type = PropositionTreeNodeType.concept
                    node.side = PropositionTreeNodeSide.left
                    node.concept = TreeNodeConcept ()
                    node.concept.id = tokens[i].concept.id
                    node.concept.name = tokens[i].concept.name
                    node.concept.type = tokens[i].concept.type
                elif tokens[i].type == TokenType.linkage:
                    node.type = PropositionTreeNodeType.linkage
                    node.side = PropositionTreeNodeSide.left
                    node.linkage = TreeNodeLinkage ()
                    node.linkage.id = tokens[i].linkage.id
                    node.linkage.name = tokens[i].linkage.name
                elif tokens[i].type == TokenType.underscore:
                    node.type = PropositionTreeNodeType.underscore
                elif tokens[i].type == TokenType.number:
                    node.type = PropositionTreeNodeType.number
                elif tokens[i].type == TokenType.string:
                    node.type = PropositionTreeNodeType.string
                #print node.text
                parent_node.children.append (node)
            i -= 1

        # Обработка правой ветки суждения
        i = idx + 1
        node = root_node
        level_inside_brackets = 0
        common_parent = None
        while i < len (tokens):
            if tokens[i].type == TokenType.point:
                pass
            elif tokens[i].type == TokenType.question_mark:
                pass
            elif tokens[i].type == TokenType.opening_bracket:
                level_inside_brackets += 1
                if node.type == PropositionTreeNodeType.concept:
                    if level_inside_brackets > 1:
                        cls.__push_common_parent (common_parent)
                    common_parent = node
                elif node.type == PropositionTreeNodeType.linkage:
                    b = 1
                    subtree_tokens = []
                    j = i + 1
                    while j < len (tokens) and b != 0:
                        if tokens[j].type == TokenType.opening_bracket:
                            b += 1
                        if tokens[j].type == TokenType.closing_bracket:
                            b -= 1
                        if b != 0:
                            subtree_tokens.append (tokens[j])
                        j += 1

                    subtree_rootnode = cls.build_tree (subtree_tokens)
                    subtree_rootnode.concept.subroot = True
                    if node != None:
                        node.children.append (subtree_rootnode)
                    else:
                        return None
                    i = j - 1
            elif tokens[i].type == TokenType.closing_bracket:
                level_inside_brackets -= 1
                common_parent = cls.__pop_common_parent ()
            elif tokens[i].type == TokenType.comma:
                if level_inside_brackets > 0:
                    node = common_parent
            elif tokens[i].type == TokenType.equal_sign:
                pass
            else:
                parent_node = node
                node = PropositionTreeNode ()
                node.parent = parent_node
                node.text = tokens[i].text
                if tokens[i].type == TokenType.concept:
                    node.type = PropositionTreeNodeType.concept
                    node.side = PropositionTreeNodeSide.right
                    node.concept = TreeNodeConcept ()
                    node.concept.id = tokens[i].concept.id
                    node.concept.name = tokens[i].concept.name
                    node.concept.type = tokens[i].concept.type
                elif tokens[i].type == TokenType.linkage:
                    node.type = PropositionTreeNodeType.linkage
                    node.side = PropositionTreeNodeSide.right
                    node.linkage = TreeNodeLinkage ()
                    node.linkage.id = tokens[i].linkage.id
                    node.linkage.name = tokens[i].linkage.name
                elif tokens[i].type == TokenType.underscore:
                    node.type = PropositionTreeNodeType.underscore
                elif tokens[i].type == TokenType.number:
                    node.type = PropositionTreeNodeType.number
                elif tokens[i].type == TokenType.string:
                    node.type = PropositionTreeNodeType.string
                elif tokens[i].type == TokenType.code_object:
                    node.type = PropositionTreeNodeType.code_object
                #print node.text
                parent_node.children.append (node)
            i += 1

        return root_node
Exemplo n.º 5
0
    def analize(cls, text):
        word = []
        tokens = []
        prev_letter = ""

        # Разбивка на токены
        i = 0
        while i < len(text):
            letter = text[i]
            if letter == " ":
                if len(word) > 0:
                    token = Token()
                    token.text = ''.join(word)
                    tokens.append(token)
                    word = []
            elif letter == "(" or \
                 letter == ")" or \
                 letter == ",":
                #letter == "_":
                if len(word) > 0:
                    token = Token()
                    token.text = ''.join(word)
                    tokens.append(token)
                    word = []
                token = Token()
                token.text = letter
                tokens.append(token)
            elif letter == "." or \
                 letter == "_":
                j = i + 1
                if j == len(text):
                    if len(word) > 0:
                        token = Token()
                        token.text = ''.join(word)
                        tokens.append(token)
                        word = []
                    token = Token()
                    token.text = letter
                    tokens.append(token)
                while j < len(text):
                    letter = text[j]

                    if letter == " ":
                        break
                    elif letter == ")" or \
                         letter == "(" or \
                         letter == ",":
                        j = j - 1
                        break
                    if i == (j - 1):
                        if letter == " ":
                            if len(word) > 0:
                                token = Token()
                                token.text = ''.join(word)
                                tokens.append(token)
                                word = []
                            token = Token()
                            token.text = letter
                            tokens.append(token)
                            break
                        else:
                            word.append(text[i])
                            prev_letter = letter
                            word.append(letter)
                            j += 1
                    else:
                        prev_letter = letter
                        word.append(letter)
                        j += 1
                i = j
            elif letter == "=":
                if prev_letter == " " or \
                   prev_letter == "(":
                    if len(word) > 0:
                        token = Token()
                        token.text = ''.join(word)
                        tokens.append(token)
                        word = []
                    token = Token()
                    token.text = letter
                    tokens.append(token)
                else:
                    word.append(letter)
            elif letter == "?":
                if len(word) > 0:
                    token = Token()
                    token.text = ''.join(word)
                    tokens.append(token)
                    word = []
                if prev_letter not in [" ", "("]:
                    token = Token()
                    token.text = letter
                    tokens.append(token)
                else:
                    word.append(letter)
            elif letter == "\"":
                i += 1
                while i < len(text):
                    prev_letter = letter
                    letter = text[i]
                    if letter == "\"" and prev_letter != "\\":
                        break
                    else:
                        word.append(letter)
                    i += 1
                token = Token()
                token.text = ''.join(word)
                token.type = TokenType.string
                tokens.append(token)
                word = []
            else:
                word.append(letter)
            prev_letter = letter
            i += 1

        # Идентификация токенов
        for token in tokens:
            #print token.text
            if token.type == TokenType.string:
                continue
            if token.text.find('?') == 0 and len(token.text) > 1:
                s = token.text.replace('?', '')
                query = "SELECT id FROM qsl_linkage WHERE name = \'" + s + "\';"
                cls.__cursor.execute(query)
                row = cls.__cursor.fetchone()
                if row != None:
                    token.type = TokenType.linkage
                    token.linkage = TokenLinkage()
                    token.linkage.id = row[0]
                    token.linkage.name = s
                else:
                    cls.__error_text = ErrorHelper.get_text(102, token.text)
                    return False
            elif token.text.find('%') == 0 and len(token.text) > 1:
                token.type = TokenType.code_object
            elif token.text.find('*') == 0:
                # Модификатор
                token.type = TokenType.modifier
            elif token.text == "(":
                token.type = TokenType.opening_bracket
            elif token.text == ")":
                token.type = TokenType.closing_bracket
            elif token.text == ",":
                token.type = TokenType.comma
            elif token.text == "_":
                token.type = TokenType.underscore
            elif token.text == ".":
                token.type = TokenType.point
            elif token.text == "?":
                token.type = TokenType.question_mark
            elif token.text == "=":
                token.type = TokenType.equal_sign
            else:
                query = "SELECT id, type FROM qsl_concept WHERE name = \'" + token.text + "\';"
                cls.__cursor.execute(query)
                row = cls.__cursor.fetchone()
                if row != None:
                    token.type = TokenType.concept
                    token.concept = TokenConcept()
                    token.concept.id = row[0]
                    token.concept.type = row[1]
                    token.concept.name = token.text
                else:
                    if token.text.isdigit():
                        token.type = TokenType.number
                    else:
                        cls.__error_text = ErrorHelper.get_text(
                            103, token.text)
                        return False

        node = cls.build_tree(tokens)
        if node != None:
            cls.proposition_tree = PropositionTree()
            cls.proposition_tree.root_node = node
        else:
            return False

        return True
Exemplo n.º 6
0
    def build_tree(cls, tokens):
        # Поиск главного понятия действия
        idx = 0
        node = None
        root_node = None
        for token in tokens:
            if token.type == TokenType.concept:
                if token.concept.type == TokenConceptType.action:
                    node = PropositionTreeNode()
                    node.text = token.text
                    node.type = PropositionTreeNodeType.concept
                    node.side = PropositionTreeNodeSide.center
                    node.concept = TreeNodeConcept()
                    node.concept.id = token.concept.id
                    node.concept.name = token.concept.name
                    node.concept.type = token.concept.type
                    root_node = node
                    break
            idx += 1

        if node == None:
            cls.__error_text = ErrorHelper.get_text(101)
            return None

        # Обработка левой ветки суждения
        i = idx - 1
        while i >= 0:
            if tokens[i].type == TokenType.equal_sign:
                if tokens[i + 1].type == TokenType.concept:
                    node.concept.sublink = True
            else:
                parent_node = node
                node = PropositionTreeNode()
                node.parent = parent_node
                node.text = tokens[i].text
                if tokens[i].type == TokenType.concept:
                    node.type = PropositionTreeNodeType.concept
                    node.side = PropositionTreeNodeSide.left
                    node.concept = TreeNodeConcept()
                    node.concept.id = tokens[i].concept.id
                    node.concept.name = tokens[i].concept.name
                    node.concept.type = tokens[i].concept.type
                elif tokens[i].type == TokenType.linkage:
                    node.type = PropositionTreeNodeType.linkage
                    node.side = PropositionTreeNodeSide.left
                    node.linkage = TreeNodeLinkage()
                    node.linkage.id = tokens[i].linkage.id
                    node.linkage.name = tokens[i].linkage.name
                elif tokens[i].type == TokenType.underscore:
                    node.type = PropositionTreeNodeType.underscore
                elif tokens[i].type == TokenType.number:
                    node.type = PropositionTreeNodeType.number
                elif tokens[i].type == TokenType.string:
                    node.type = PropositionTreeNodeType.string
                #print node.text
                parent_node.children.append(node)
            i -= 1

        # Обработка правой ветки суждения
        i = idx + 1
        node = root_node
        level_inside_brackets = 0
        common_parent = None
        while i < len(tokens):
            if tokens[i].type == TokenType.point:
                pass
            elif tokens[i].type == TokenType.question_mark:
                pass
            elif tokens[i].type == TokenType.opening_bracket:
                level_inside_brackets += 1
                if node.type == PropositionTreeNodeType.concept:
                    if level_inside_brackets > 1:
                        cls.__push_common_parent(common_parent)
                    common_parent = node
                elif node.type == PropositionTreeNodeType.linkage:
                    b = 1
                    subtree_tokens = []
                    j = i + 1
                    while j < len(tokens) and b != 0:
                        if tokens[j].type == TokenType.opening_bracket:
                            b += 1
                        if tokens[j].type == TokenType.closing_bracket:
                            b -= 1
                        if b != 0:
                            subtree_tokens.append(tokens[j])
                        j += 1

                    subtree_rootnode = cls.build_tree(subtree_tokens)
                    subtree_rootnode.concept.subroot = True
                    if node != None:
                        node.children.append(subtree_rootnode)
                    else:
                        return None
                    i = j - 1
            elif tokens[i].type == TokenType.closing_bracket:
                level_inside_brackets -= 1
                common_parent = cls.__pop_common_parent()
            elif tokens[i].type == TokenType.comma:
                if level_inside_brackets > 0:
                    node = common_parent
            elif tokens[i].type == TokenType.equal_sign:
                pass
            else:
                parent_node = node
                node = PropositionTreeNode()
                node.parent = parent_node
                node.text = tokens[i].text
                if tokens[i].type == TokenType.concept:
                    node.type = PropositionTreeNodeType.concept
                    node.side = PropositionTreeNodeSide.right
                    node.concept = TreeNodeConcept()
                    node.concept.id = tokens[i].concept.id
                    node.concept.name = tokens[i].concept.name
                    node.concept.type = tokens[i].concept.type
                elif tokens[i].type == TokenType.linkage:
                    node.type = PropositionTreeNodeType.linkage
                    node.side = PropositionTreeNodeSide.right
                    node.linkage = TreeNodeLinkage()
                    node.linkage.id = tokens[i].linkage.id
                    node.linkage.name = tokens[i].linkage.name
                elif tokens[i].type == TokenType.underscore:
                    node.type = PropositionTreeNodeType.underscore
                elif tokens[i].type == TokenType.number:
                    node.type = PropositionTreeNodeType.number
                elif tokens[i].type == TokenType.string:
                    node.type = PropositionTreeNodeType.string
                elif tokens[i].type == TokenType.code_object:
                    node.type = PropositionTreeNodeType.code_object
                #print node.text
                parent_node.children.append(node)
            i += 1

        return root_node