示例#1
0
    public bool AddProduction(String production)
    {
        String predecessor = "";
        String successor   = "";
        double probability = 0.0;

        ArrayList strictPredecessor = new ArrayList();
        ArrayList lhContext         = new ArrayList();
        ArrayList rhContext         = new ArrayList();
        Stack     booleanExpression = new Stack();

        /*
         *      split the rule down
         *
         *      First check if its Stochastic or not
         */
        production = removeSpaces.Replace(production, "");

        Match stochasticRule = stochastic.Match(production);

        if (stochasticRule.Success)
        {
            predecessor = stochasticRule.Groups[1].Value;
            successor   = stochasticRule.Groups[3].Value;
            probability = Double.Parse(stochasticRule.Groups[2].Value);
        }
        else
        {
            Match nonStochasticRule = notStochastic.Match(production);
            if (nonStochasticRule.Success)
            {
                predecessor = nonStochasticRule.Groups[1].Value;
                successor   = nonStochasticRule.Groups[2].Value;
                probability = 1.0;
            }
            else
            {
                Console.WriteLine("can not parse non Stochastic Production " + production);
                return(false);

                /* I don't know what it is */
            }
        }

        /* now split up the predecessor */
        String[] predecessorSplit = predecessor.Split(new Char[] { ':' });
        predecessor = predecessorSplit[0];
        if (predecessorSplit.Length > 1)
        {
            Console.WriteLine("\tFound boolean test " + predecessorSplit[1]);
            if (predecessorSplit[1] != "*")
            {
                booleanExpression = ExpressionEvaluator.ParseExpression(predecessorSplit[1]);
            }
        }

        /*
         *      now check for context deals with the predecessor in more detail
         */

        predecessorSplit = predecessor.Split(new Char[] { '<', '>' });

        int index = predecessor.IndexOf('<');

        if (index != -1)
        {
            Console.WriteLine("\tFound left hand context " + predecessorSplit[0]);
            lhContext = Rules.ConvertContextToRuleList(predecessorSplit[0]);
            Console.WriteLine("\tFound strict predecessor rule " + predecessorSplit[1]);
            strictPredecessor = Rules.ConvertPredecessorToRuleList(predecessorSplit[1]);
        }
        else
        {
            Console.WriteLine("\tFound strict predecessor rule " + predecessorSplit[0]);
            strictPredecessor = Rules.ConvertPredecessorToRuleList(predecessorSplit[0]);
        }

        index = predecessor.IndexOf('>');

        if (index != -1)
        {
            Console.WriteLine("\tFound right hand context " + predecessorSplit[predecessorSplit.Length - 1]);
            rhContext = Rules.ConvertContextToRuleList(predecessorSplit[predecessorSplit.Length - 1]);
        }

        bool found = false;

        foreach (Production knownProduction in productionList)
        {
            if (knownProduction.IsSameProduction(lhContext, strictPredecessor, rhContext, booleanExpression))
            {
                knownProduction.AddSuccessor(Rules.ConvertSuccessorToRuleList(successor), probability);
                Console.WriteLine("Seen this predecessor before" + predecessor);
                found = true;
                break;
            }
        }

        /* we've not seen this one before so add a new production to the list */
        if (found == false)
        {
            Production newProduction = new Production();
            newProduction.SetPredecessor(lhContext, strictPredecessor, rhContext, booleanExpression);
            newProduction.AddSuccessor(Rules.ConvertSuccessorToRuleList(successor), probability);
            productionList.Add(newProduction);
        }

        return(true);
    }