示例#1
0
        private static void CreateCBRContext(ICBRContext ctx, string engineName,
                                             string env)
        {
            XMLConfigFile f      = new XMLConfigFile(env);
            ConfigInfo    config = f.GetConfigInfo();

            if (config != null)
            {
                ManagerHelper helper = new ManagerHelper();
                helper.SetEnv(engineName);
                helper.CreateCBRContext(config, ctx);
            }
        }
示例#2
0
        public void CreateCBRContext(ConfigInfo config, ICBRContext ctx)
        {
            //create case retrieve method
            ctx.SetCaseRetrievalMethod((IMethod)CreateInstance(config,
                                                               MappingKey.KEY_CASE_RETRIEVEL_METHOD));
            //create the computing similarity method
            ctx.SetSimilarity((ISimilarity)CreateInstance(config,
                                                          MappingKey.KEY_SIMILARITY));
            //create the case reuse method
            ctx.SetCaseReuseMethod((IMethod)CreateInstance(config,
                                                           MappingKey.KEY_CASE_REUSE_METHOD));
            //create the strategy of case reuse
            ctx.SetCaseReuseStrategy((ICaseReuseStrategy)CreateInstance(config,
                                                                        MappingKey.KEY_CASE_REUSE_STRATEGY));

            //create the case revise method
            ctx.SetCaseReviseMethod((IMethod)CreateInstance(config,
                                                            MappingKey.KEY_CASE_REVISE_METHOD));

            //create the case restore method
            ctx.SetCaseRestoreMethod((IMethod)CreateInstance(config,
                                                             MappingKey.KEY_CASE_RESTORE_METHOD));
            //create the input method of case base
            ctx.SetCaseBaseInput((ICaseBaseInput)CreateInstance(config,
                                                                MappingKey.KEY_CASEBASE_INPUT));
            //create the case base method
            ctx.SetCaseBase((ICaseBase)CreateInstance(config,
                                                      MappingKey.KEY_CASEBASE));
            //init the similarity threhold parameter
            string threhold = (string)CreateParameter(config,
                                                      MappingKey.KEY_PARAM_SIMILARITY_THREHOLD);

            if (threhold != null)
            {
                ctx.SetSimilarityThrehold(Convert.ToDouble(threhold));
            }
            //init the case base input type
            string type = (string)CreateParameter(config,
                                                  MappingKey.KEY_PARAM_CASEBASE_INPUT_TYPE);

            if (type != null)
            {
                ctx.SetCaseBaseInputType(Convert.ToInt32(type));
            }
            //init the case base url
            string url = (string)CreateParameter(config,
                                                 MappingKey.KEY_PARAM_CASEBASE_URL);

            ctx.SetCaseBaseURL(url);
        }
        /// <summary>
        /// compute the similarity of case base's cases and problem case
        ///
        /// </summary>
        /// <param name="cases"></param>
        /// <param name="problem"></param>
        /// <returns></returns>
        public ArrayList ComputeSimilarity(ArrayList cases, Case problem)
        {
            if (_env == null)
            {
                throw new ContextException("environment variable is not set");
            }

            ICBRContext ctx = CBRContextManager.GetCBRContext(_env);

            if (ctx == null)
            {
                throw new ContextException("not set context");
            }

            ISimilarity sim = (ISimilarity)ctx.GetSimilarity();

            if (sim == null)
            {
                throw new ContextException("similarity method is not set");
            }

            ArrayList stats = new ArrayList();
            double    similarityThrehold = ctx.GetSimilarityThrehold();

            for (int i = 0; i < cases.Count; i++)
            {
                Case   solution   = (Case)cases[i];
                double similarity = sim.Compare(problem, solution);
                //continue if the similarity by comparing is lower than the
                //similarity threhold in context setting
                if (similarity < similarityThrehold)
                {
                    continue;
                }
                IStat s = StatFactory.newInstance();
                s.SetCBRCase(solution);
                s.SetCaseSimilarity(similarity);
                //bi-sort similarity
                if (stats.Count <= 0)
                {
                    stats.Add(s);
                    continue;
                }
                SortSimilarity(stats, s);
            }

            return(stats);
        }
示例#4
0
        public bool Startup()
        {
            if (_env == null ||
                CBRContextManager.GetCBRContext(_name) == null)
            {
                System.Console.WriteLine("environment is not set or context is null");
                return(false);
            }

            _ctx = CBRContextManager.GetCBRContext(_name);
            if (_problem == null)
            {
                System.Console.WriteLine("problem is not set");
                return(false);
            }
            _ctx.SetCurrentCase(_problem);
            #region only for test
            if (Version.DEBUG)
            {
                System.Console.WriteLine("=====context detail=====");
                System.Console.WriteLine("Reasoning Engine startup successfully!");
                System.Console.WriteLine("problem case is:");
                for (int i = 0; i < _problem.GetFeatures().Count; i++)
                {
                    Feature f = (Feature)_problem.GetFeatures()[i];
                    System.Console.WriteLine("\t" + f.GetFeatureName() + ":" + f.GetFeatureValue());
                }
                System.Console.WriteLine("context is:");
                System.Console.WriteLine("case base\t" + _ctx.GetCaseBase().ToString());
                System.Console.WriteLine("case base input\t" + _ctx.GetCaseBaseInput().ToString());
                System.Console.WriteLine("case base input type\t" + _ctx.GetCaseBaseInputType().ToString());
                System.Console.WriteLine("case base url\t" + _ctx.GetCaseBaseURL().ToString());
                System.Console.WriteLine("case restore method\t" + _ctx.GetCaseRestoreMethod().ToString());
                System.Console.WriteLine("case retrieval method\t" + _ctx.GetCaseRetrievalMethod().ToString());
                System.Console.WriteLine("case reuse method\t" + _ctx.GetCaseReuseMethod().ToString());
                System.Console.WriteLine("case reuse strategy\t" + _ctx.GetCaseReuseStrategy().ToString());
                System.Console.WriteLine("case revise method\t" + _ctx.GetCaseReviseMethod().ToString());
                System.Console.WriteLine("current case\t" + _ctx.GetCurrentCase().ToString());
                System.Console.WriteLine("similarity\t" + _ctx.GetSimilarity().ToString());
                System.Console.WriteLine("similarity threhold\t" + _ctx.GetSimilarityThrehold().ToString());
                System.Console.WriteLine("=====end of context detail=====");
            }
            #endregion
            return(true);
        }
示例#5
0
    public static void Load(string engineName, string env)
    {
        bool isExist = File.Exists(env);

        if (isExist)
        {
            ICBRContext ctx = CBRContextFactory.newInstance();
            if (ctx != null)
            {
                CreateCBRContext(ctx, engineName, env);
                _ctxs.Add(engineName, ctx);
            }
        }
        else
        {
            throw new ContextException("context env " + env
                                       + "is not existed");
        }
    }
示例#6
0
        public Case Reuse(ArrayList stats)
        {
            if (stats == null || stats.Count <= 0)
            {
                return(null);
            }

            if (_env == null)
            {
                throw new ContextException("environment variable is not set");
            }

            ICBRContext ctx = CBRContextManager.GetCBRContext(_env);

            if (ctx == null)
            {
                throw new ContextException("context is not set");
            }
            Case problem = ctx.GetCurrentCase();
            Case c       = ((IStat)(stats[0])).GetCBRCase();

            if (c != null && problem != null)
            {
                ArrayList features = c.GetFeatures();
                for (int i = 0; i < features.Count; i++)
                {
                    Feature f = (Feature)features[i];
                    //if the feature is the key of problem
                    if (f.GetIsKey())
                    {
                        problem.ModifyFeature(f.GetFeatureName(),
                                              f.GetFeatureType(), f.GetFeatureValue(),
                                              f.GetWeight(), f.GetIsKey(),
                                              f.GetIsIndex());
                    }
                }

                return(problem);
            }
            return(null);
        }
示例#7
0
        public Case Reuse(System.Collections.ArrayList stats)
        {
            if (stats == null || stats.Count <= 0)
            {
                return(null);
            }
            if (_env == null)
            {
                throw new ContextException("environment variable is not set");
            }

            ICBRContext ctx = CBRContextManager.GetCBRContext(_env);

            if (ctx == null)
            {
                throw new ContextException("ctx is not set");
            }

            Case problem = ctx.GetCurrentCase();

            if (problem == null)
            {
                throw new ContextException(
                          "interface ICBRContext's GetCurrentCase return null");
            }
            ArrayList list = problem.GetFeatures();

            for (int i = 0; i < list.Count; i++)
            {
                Feature f = (Feature)list[i];
                if (f.GetIsKey() && TypeHandle.IsNumericFeature(f))
                {
                    //compute the mean value of features
                    f.SetFeatureValue(ComputeMeanValue(stats,
                                                       f.GetFeatureName()));
                }
            }
            return(problem);
        }
示例#8
0
        public string ParseConditions(string src)
        {
            if (src == null ||
                src.Trim().Length <= 0)
            {
                return(null);
            }
            string      dst = src;
            ICBRContext ctx = CBRContextManager.GetCBRContext(_env);
            Case        c   = ctx.GetCurrentCase();

            ArrayList features = c.GetFeatures();

            foreach (Feature f in features)
            {
                string name = TOKEN_SHARP + f.GetFeatureName() + TOKEN_SHARP;
                if (f.GetFeatureValue() != null)
                {
                    dst = dst.Replace(name, f.GetFeatureValue().ToString());
                }
            }
            return(dst);
        }
        public object Execute(object obj)
        {
            if (_env == null)
            {
                throw new ContextException("environment variable is not set");
            }

            ICBRContext ctx = CBRContextManager.GetCBRContext(_env);

            if (ctx == null)
            {
                //throw NoContextException
                throw new ContextException("not set the context");
            }

            ICaseBase caseBase = ctx.GetCaseBase();

            if (caseBase == null)
            {
                //throw exception
                throw new ContextException("not set casebase");
            }

            ArrayList cases = caseBase.GetCases((Case)obj);

            if (cases == null || cases.Count <= 0)
            {
                if (Version.DEBUG)
                {
                    System.Console.WriteLine("not found cases matched");
                }
                return(null);
            }

            //compute the similarity and sort by similarity ascending
            return(ComputeSimilarity(cases, (Case)obj));
        }
示例#10
0
    public ArrayList RetrievalCases_partial(Case problem, ArrayList testingcases)
    {
        if (_env == null)
        {
            throw new ContextException("environment variable is not set");
        }
        ICBRContext ctx = CBRContextManager.GetCBRContext(_env);

        if (ctx == null)
        {
            throw new ContextException("context is not set");
        }

        IMethod m = ctx.GetCaseRetrievalMethod();

        if (m == null)
        {
            throw new ContextException(
                      "context's GetCaseRetrievalMethod is not set");
        }
        ctx.SetMatchedCase((ArrayList)m.Execute_partial(problem, testingcases));

        return((ArrayList)m.Execute_partial(problem, testingcases));
    }
    /// <summary>
    /// find case from case base
    /// return null if not suitable case
    /// otherwise retrurn case list
    /// note that only *.ocbr and db type are supported
    /// </summary>
    /// <param name="c"></param>
    /// <returns></returns>
    public ArrayList GetCases(Case c)
    {
        if (_env == null)
        {
            System.Console.WriteLine("environment is not set");
            return(null);
        }

        ICBRContext ctx = CBRContextManager.GetCBRContext(_env);

        if (ctx == null)
        {
            throw new ContextException("context is null");
        }

        int    type = ctx.GetCaseBaseInputType();
        string url  = ctx.GetCaseBaseURL();

        if (type == CaseBaseInputType.TYPE_FILE)
        {
            IOCBRFile file = OCBRFileFactory.newInstance(url);
            return(file.GetCases());
        }
        else if (type == CaseBaseInputType.TYPE_DB)
        {
            IDb db = DbFactory.newInstance(url);
            db.SetEnv(_env);                    //set the conditions rules

            return(db.GetCases());
        }
        else
        {
            System.Console.WriteLine("input case base type unsupported");
        }
        return(null);
    }