コード例 #1
0
        private ITemplateParserContext ReplaceFunctionContext(FunctionNode function)
        {
            Contract.Assume(_currentContext != null);

            TemplateParserDictionaryContext dictionaryContext = new TemplateParserDictionaryContext();
            // TODO: Необходимо добавить учет связи Is-Instance.
            NetworkNode[] stopNodes =
            {
                function.Network.Nodes.GetNode("Персонаж"),
                function.Network.Nodes.GetNode("Положительный персонаж"),
                function.Network.Nodes.GetNode("Отрицательный персонаж"),
                function.Network.Nodes.GetNode("Место"),
                function.Network.Nodes.GetNode("Действие")
            };
            Action<IEnumerable<NetworkNode>, IEnumerable<NetworkNode>, NetworkEdgeType> fillDictionaryAction =
                (functionNodes, contextNodes, contextType) =>
                {
                    foreach (NetworkNode functionNode in functionNodes)
                    {
                        NetworkNode contextNode = contextNodes.FirstOrDefault(
                            node => AreHaveCommonAncestor(functionNode, node, stopNodes));

                        if (contextNode != null &&
                            !functionNodes.Contains(contextNode))
                        {
                            dictionaryContext.Add(contextType, contextNode);
                        }
                        else
                        {
                            dictionaryContext.Add(contextType, functionNode);
                        }
                    }
                };

            // TODO: Пока не уверен, должны ли унаследованные контекстные вершины
            //       также появляться в списке.

            fillDictionaryAction(
                function.Agents,
                _currentContext.ResolvedPersons,
                NetworkEdgeType.Agent);

            fillDictionaryAction(
                function.Recipients,
                _currentContext.ResolvedPersons,
                NetworkEdgeType.Recipient);

            fillDictionaryAction(
                function.Locatives,
                _currentContext.ResolvedLocatives,
                NetworkEdgeType.Locative);

            fillDictionaryAction(
                function.Actions,
                _currentContext.ResolvedActions,
                NetworkEdgeType.Action);

            //fillDictionaryAction(function.Recipients, _currentContext.Persons, NetworkEdgeType.Recipient);
            //fillDictionaryAction(function.Locatives, _currentContext.Locatives, NetworkEdgeType.Locative);
            //fillDictionaryAction(function.Actions, _currentContext.Actions, NetworkEdgeType.Action);

            return dictionaryContext;
        }
コード例 #2
0
        private void GetChildContextNode(
			IDictionary<FunctionNode, TemplateParserResult> parserResults,
			TaleNode currentTaleNode,
			FunctionNode baseFunctionNode,
			NetworkEdgeType edgeType,
			TemplateParserDictionaryContext newParserContext)
        {
            TemplateParserResult baseFunctionNodeParserResult = parserResults[baseFunctionNode];
            var currentTaleContextNodes = new List<NetworkNode>();
            var baseTaleContextNodes = baseFunctionNode.OutgoingEdges.GetEdges(edgeType).Select(edge => edge.EndNode);
            bool found = false;

            foreach (FunctionNode functionNode in currentTaleNode.Functions)
            {
                currentTaleContextNodes.AddRange(functionNode.OutgoingEdges.GetEdges(edgeType).Select(edge => edge.EndNode));
            }

            foreach (NetworkNode baseTaleContextNode in baseTaleContextNodes)
            {
                if (currentTaleContextNodes.Any(node => node.IsInherit(baseTaleContextNode, false)))
                {
                    NetworkNode currentNode = null;

                    while (true)
                    {
                        NetworkEdge isAEdge = baseTaleContextNode.OutgoingEdges.GetEdge(NetworkEdgeType.IsA);

                        if (isAEdge != null)
                        {
                            currentNode = isAEdge.StartNode;

                            NetworkNode childNode = currentTaleContextNodes.FirstOrDefault(node => node == currentNode || node.BaseNode == currentNode);

                            if (childNode != null)
                            {
                                found = true;
                                newParserContext.Add(edgeType, childNode);
                                break;
                            }
                        }

                        if (isAEdge == null &&
                            currentNode != null)
                        {
                            found = true;
                            newParserContext.Add(edgeType, currentNode);
                            break;
                        }
                    }
                }
            }

            if (!found)
            {
                newParserContext.Add(edgeType, baseTaleContextNodes);
            }
        }