public IEnumerable <SubstitutionSet> Unify(IQueryable db, Name perspective, IEnumerable <SubstitutionSet> constraints)
        {
            if (constraints == null || !constraints.Any())
            {
                constraints = new[] { new SubstitutionSet() }
            }
            ;

            if (m_conditions == null)
            {
                return(constraints);
            }

            if (Quantifier == LogicalQuantifier.Existential)
            {
                return(ExistentialEvaluate(db, perspective, constraints));
            }

            return(UniversalEvaluate(db, perspective, constraints));
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return(GetEnumerator());
        }
        private IEnumerable <SubstitutionSet> UniversalEvaluate(IQueryable db, Name perspective, IEnumerable <SubstitutionSet> constraints)
        {
            var lastDomain = BuildDomain(constraints);
            List <SubstitutionSet> sets = new List <SubstitutionSet>(constraints);
            List <SubstitutionSet> aux  = new List <SubstitutionSet>();

            try
            {
                foreach (var c in m_conditions)
                {
                    aux.AddRange(c.Unify(db, perspective, sets));
                    Util.Swap(ref sets, ref aux);
                    aux.Clear();
                    var newDomain = BuildDomain(sets);
                    if (!CompareDomains(lastDomain, newDomain))
                    {
                        RecycleDomain(newDomain);
                        return(aux);
                    }

                    RecycleDomain(lastDomain);
                    lastDomain = newDomain;
                }

                return(sets);
            }
            finally
            {
                RecycleDomain(lastDomain);
            }
        }
        //private Condition()
        //{
        //    this.Id = Guid.NewGuid();
        //}

        public IEnumerable <SubstitutionSet> Unify(IQueryable kb, Name perspective, IEnumerable <SubstitutionSet> constraints)
        {
            if (constraints == null || !constraints.Any())
            {
                constraints = new[] { new SubstitutionSet() }
            }
            ;

            return(CheckActivation(kb, perspective, constraints).Distinct());
        }
Exemple #4
0
        public AppraisalRule Evaluate(IBaseEvent evt, IQueryable kb, Name perspective)
        {
            var auxEvt = evt.EventName.SwapTerms(perspective, Name.SELF_SYMBOL);

            foreach (var possibleAppraisals in this.Rules.Unify(auxEvt))
            {
                //This next for loop is to prevent a problem with using appraisal rules that contain SELF
                //This will replace all the subs with SELF with the name of the perspective
                foreach (var sub in possibleAppraisals.Item2)
                {
                    if (sub.SubValue.Value == Name.SELF_SYMBOL)
                    {
                        sub.SubValue = new ComplexValue(perspective);
                    }
                }
                var substitutions = new[] { possibleAppraisals.Item2 };                 //this adds the subs found in the eventName
                foreach (var appRule in possibleAppraisals.Item1)
                {
                    var finalSubsList = appRule.Conditions.Unify(kb, Name.SELF_SYMBOL, substitutions);

                    //The appraisal will only consider the substitution set that it has the most certainty in
                    var mostCertainSubSet = this.DetermineSubstitutionSetWithMostCertainty(finalSubsList);
                    if (mostCertainSubSet != null)
                    {
                        foreach (var appVariable in appRule.getAppraisalVariables())
                        {
                            appVariable.Value = appVariable.Value.MakeGround(mostCertainSubSet);

                            var minCertainty = mostCertainSubSet.FindMinimumCertainty();

                            if (appVariable.Target != null && appVariable.Target != (Name)"-")
                            {
                                appVariable.Target = appVariable.Target.MakeGround(mostCertainSubSet);
                            }

                            float f;

                            if (float.TryParse(appVariable.Value.ToString(), out f))
                            {
                                var aux = float.Parse(appVariable.Value.ToString()) * minCertainty;
                                appVariable.Value = Name.BuildName(aux);
                            }
                            else
                            {
                                continue;
                            }
                        }

                        return(appRule);
                    }
                }
            }
            return(null);
        }
Exemple #5
0
            protected override IEnumerable <SubstitutionSet> CheckActivation(IQueryable db, Name perspective, IEnumerable <SubstitutionSet> constraints)
            {
                Name realValue = db.AskPossibleProperties(m_value, perspective, null).First().Item1.Value;

                foreach (var pair in m_retriver.Retrieve(db, perspective, constraints))
                {
                    if (CompareValues(pair.Item1.Value, realValue, m_operation))
                    {
                        yield return(pair.Item2);
                    }
                }
            }
Exemple #6
0
            protected override IEnumerable <SubstitutionSet> CheckActivation(IQueryable db, Name perspective, IEnumerable <SubstitutionSet> constraints)
            {
                var r1 = Property1.Retrieve(db, perspective, constraints).GroupBy(p => p.Item1, p => p.Item2);

                foreach (var g in r1)
                {
                    foreach (var crossPair in Property2.Retrieve(db, perspective, g))
                    {
                        if (CompareValues(g.Key.Value, crossPair.Item1.Value, Operator))
                        {
                            yield return(crossPair.Item2);
                        }
                    }
                }
            }
Exemple #7
0
            public IEnumerable <DynamicPropertyResult> Evaluate(IQueryable kb, Name perspective, SubstitutionSet args2, IEnumerable <SubstitutionSet> constraints)
            {
                var args = ObjectPool <List <Name> > .GetObject();

                try
                {
                    args.AddRange(_parameters.Select(p => args2[p]).Select(v => v == null ? Name.UNIVERSAL_SYMBOL : v.RemoveBoundedVariables(TMP_MARKER)));

                    return(_surogate(new QueryContext(kb, constraints, perspective), args));
                }
                finally
                {
                    args.Clear();
                    ObjectPool <List <Name> > .Recycle(args);
                }
            }
Exemple #8
0
        public IEnumerable <AppraisalRule> Evaluate(IBaseEvent evt, IQueryable kb, Name perspective)
        {
            // Switching the SELF term for the correct name withint the event

            var auxEvt = evt.EventName.SwapTerms(perspective, Name.SELF_SYMBOL);
            var result = new List <AppraisalRule>();

            foreach (var r in this.Rules)
            {
                // Trying to find all possible initial substitutions;
                var initialSubSet = Unifier.Unify(r.EventName, auxEvt);


                if (initialSubSet == null)
                {
                    initialSubSet = new SubstitutionSet();
                }


                if (auxEvt.Match(r.EventName) || initialSubSet.Any())
                {
                    var finalSubSet = r.Conditions.Unify(kb, perspective, new List <SubstitutionSet>()
                    {
                        new SubstitutionSet(initialSubSet)
                    });
                    if (finalSubSet != null)
                    {
                        //TODO: Handle uncertainty in beliefs
                        foreach (var set in finalSubSet)
                        {
                            var a = new AppraisalRule(r);
                            a.EventName.MakeGround(set);
                            foreach (var variable in a.getAppraisalVariables())
                            {
                                variable.Value = variable.Value.MakeGround(set);
                                if (variable.Target != null && variable.Target != (Name)"-")
                                {
                                    variable.Target = variable.Target.MakeGround(set);
                                }
                            }
                            result.Add(a);
                        }
                    }
                }
            }
            return(result);
        }
Exemple #9
0
        public AppraisalRule Evaluate(IBaseEvent evt, IQueryable kb, Name perspective)
        {
            var auxEvt = evt.EventName.SwapTerms(perspective, Name.SELF_SYMBOL);

            foreach (var possibleAppraisals in this.Rules.Unify(auxEvt))
            {
                //This next for loop is to prevent a problem with using appraisal rules that contain SELF
                //This will replace all the subs with SELF with the name of the perspective
                foreach (var sub in possibleAppraisals.Item2)
                {
                    if (sub.SubValue.Value == Name.SELF_SYMBOL)
                    {
                        sub.SubValue = new ComplexValue(perspective);
                    }
                }
                var substitutions = new[] { possibleAppraisals.Item2 };                 //this adds the subs found in the eventName
                foreach (var appRule in possibleAppraisals.Item1)
                {
                    var finalSubsList = appRule.Conditions.Unify(kb, Name.SELF_SYMBOL, substitutions);

                    //The appraisal will only consider the substitution set that it has the most certainty in
                    var mostCertainSubSet = this.DetermineSubstitutionSetWithMostCertainty(finalSubsList);
                    if (mostCertainSubSet != null)
                    {
                        appRule.Desirability     = appRule.Desirability.MakeGround(mostCertainSubSet);
                        appRule.Praiseworthiness = appRule.Praiseworthiness.MakeGround(mostCertainSubSet);
                        if (!appRule.Desirability.IsGrounded || !appRule.Praiseworthiness.IsGrounded)
                        {
                            return(null);
                        }

                        //Modify the appraisal variables based on the certainty of the substitutions
                        var minCertainty = mostCertainSubSet.FindMinimumCertainty();

                        var aux = float.Parse(appRule.Desirability.ToString()) * minCertainty;
                        appRule.Desirability = Name.BuildName(aux);

                        aux = float.Parse(appRule.Praiseworthiness.ToString()) * minCertainty;
                        appRule.Praiseworthiness = Name.BuildName(aux);

                        return(appRule);
                    }
                }
            }
            return(null);
        }
        private IEnumerable <SubstitutionSet> ExistentialEvaluate(IQueryable db, Name perspective, IEnumerable <SubstitutionSet> constraints)
        {
            List <SubstitutionSet> sets = new List <SubstitutionSet>();
            List <SubstitutionSet> aux  = new List <SubstitutionSet>();

            sets.AddRange(constraints.Select(c => new SubstitutionSet(c)));
            foreach (var c in m_conditions)
            {
                aux.AddRange(c.Unify(db, perspective, sets));
                Util.Swap(ref sets, ref aux);
                aux.Clear();
                if (sets.Count == 0)
                {
                    break;
                }
            }



            return(sets);
        }
Exemple #11
0
        public IEnumerable <BeliefPair> Evaluate2(IQueryable kb, Name property, Name perspective, IEnumerable <SubstitutionSet> constraints)
        {
            if (m_dynamicProperties.Count == 0)
            {
                yield break;
            }

            Name tmpPropertyName = property.ReplaceUnboundVariables(TMP_MARKER);

            var d = m_dynamicProperties.Unify(tmpPropertyName).ToList();

            if (d.Count == 0)
            {
                yield break;
            }

            var results = d.SelectMany(p => p.Item1.Evaluate(kb, perspective, p.Item2, constraints).ToList());

            foreach (var g in results.GroupBy(p => p.Value, p => p.Constraints))
            {
                yield return(Tuples.Create(g.Key, g.Distinct()));
            }
        }
 public IEnumerable<IAction> SelectAction(IQueryable kb, Name perspective)
 {
     return m_actionTendencies.SelectAction(kb, perspective).Select(p => p.Item1);
 }
Exemple #13
0
        private IEnumerable <DynamicPropertyResult> GetEmotionsForEntity(IEmotionalState state,
                                                                         Name emotionName, IQueryable kb, Name perspective, IEnumerable <SubstitutionSet> constraints)
        {
            if (emotionName.IsVariable)
            {
                foreach (var emotion in state.GetAllEmotions())
                {
                    var sub = new Substitution(emotionName, new ComplexValue((Name)emotion.EmotionType));
                    foreach (var c in constraints)
                    {
                        if (c.Conflicts(sub))
                        {
                            continue;
                        }

                        var newConstraints = new SubstitutionSet(c);
                        newConstraints.AddSubstitution(sub);
                        yield return(new DynamicPropertyResult(new ComplexValue(Name.BuildName(emotion.Intensity)), newConstraints));
                    }
                }
            }
            else
            {
                foreach (var resultPair in kb.AskPossibleProperties(emotionName, perspective, constraints))
                {
                    string emotionKey = resultPair.Item1.Value.ToString();
                    var    emotion    = state.GetEmotionsByType(emotionKey).OrderByDescending(e => e.Intensity).FirstOrDefault();
                    float  value      = emotion?.Intensity ?? 0;
                    foreach (var c in resultPair.Item2)
                    {
                        yield return(new DynamicPropertyResult(new ComplexValue(Name.BuildName(value)), c));
                    }
                }
            }
        }
Exemple #14
0
 /// <summary>
 /// Registers an Emotional Appraisal Asset to be used by
 /// this Emotional Decision Making asset.
 /// </summary>
 /// <remarks>
 /// To understand Emotional Appaisal Asset functionalities, please refer to its documentation.
 /// </remarks>
 /// <param name="eaa">The instance of an Emotional Appaisal Asset to regist in this asset.</param>
 public void RegisterKnowledgeBase(IQueryable KB)
 {
     m_kb = KB;
 }
 public AppraisalRule Evaluate(IBaseEvent evt, IQueryable kb, Name perspective)
 {
     foreach (var possibleAppraisals in Rules.Unify(evt.EventName.ApplyPerspective(perspective)))
     {
         var conditions = new[] {possibleAppraisals.Item2};
         foreach (var appraisal in possibleAppraisals.Item1)
         {
             if (appraisal.Conditions.Evaluate(kb,Name.SELF_SYMBOL, conditions))
                 return appraisal;
         }
     }
     return null;
 }
Exemple #16
0
 public IEnumerable <BeliefPair> Evaluate(IQueryable kb, Name perspective, IEnumerable <SubstitutionSet> constraints)
 {
     return(_entry.Evaluate(kb, perspective, _variable, constraints).GroupBy(p => p.Value, p => p.Constraints).Select(g => Tuples.Create(g.Key, g.Distinct())));
 }
 public IEnumerable<Pair<Name, SubstitutionSet>> Retrive(IQueryable db, Name perspective, IEnumerable<SubstitutionSet> constraints)
 {
     int count = constraints.Select(c => c[m_name]).Where(n => n != null).Distinct().Count();
     return constraints.Select(s => Tuples.Create(Name.BuildName(count), s));
 }
Exemple #18
0
            public IEnumerable <Pair <ComplexValue, SubstitutionSet> > Retrieve(IQueryable db, Name perspective, IEnumerable <SubstitutionSet> constraints)
            {
                int count = constraints.Select(c => c[m_name]).Where(n => n != null).Distinct().Count();

                return(constraints.Select(s => Tuples.Create(new ComplexValue(Name.BuildName(count)), s)));
            }
            public IEnumerable<DynamicPropertyResult> Evaluate(IQueryable kb, Name perspective, SubstitutionSet args2, IEnumerable<SubstitutionSet> constraints)
            {
                //var dic = ObjectPool<Dictionary<Name, Name>>.GetObject();
                var args = ObjectPool<List<Name>>.GetObject();

                try
                {
                    //foreach(var s in args2)
                    //{
                    //	var p = s.Variable.RemoveBoundedVariables(TMP_MARKER);
                    //	dic[p] = s.Value;
                    //	if (!s.Value.IsVariable)
                    //		continue;
                    //	dic[s.Value] = p;
                    //}

                    args.AddRange(_parameters.Select(p => args2[p]).Select(v => v == null ? Name.UNIVERSAL_SYMBOL : v.RemoveBoundedVariables(TMP_MARKER)));
                    //args.AddRange(_parameters.Select(p =>
                    //{
                    //	Name v;
                    //	if (dic.TryGetValue(p, out v))
                    //		return v;
                    //	return Name.UNIVERSAL_SYMBOL;
                    //}));

                    return _surogate(new QueryContext(kb, constraints, perspective),args);
                }
                finally
                {
                    args.Clear();
                    ObjectPool<List<Name>>.Recycle(args);

                    //dic.Clear();
                    //ObjectPool<Dictionary<Name, Name>>.Recycle(dic);
                }
            }
Exemple #20
0
 public bool Evaluate(IQueryable db, Name perspective, IEnumerable <SubstitutionSet> constraints)
 {
     return(Unify(db, perspective, constraints).Any());
 }
 public QueryContext(IQueryable kb, IEnumerable<SubstitutionSet> contraints, Name perspective)
 {
     Queryable = kb;
     Constraints = contraints;
     Perspective = perspective;
 }
 protected override IEnumerable<SubstitutionSet> CheckActivation(IQueryable db, Name perspective, IEnumerable<SubstitutionSet> constraints)
 {
     var r1 = Property1.Retrive(db,perspective, constraints).GroupBy(p => p.Item1, p => p.Item2);
     foreach (var g in r1)
     {
         foreach (var crossPair in Property2.Retrive(db,perspective, g))
         {
             if (CompareValues(g.Key, crossPair.Item1, Operator))
                 yield return crossPair.Item2;
         }
     }
 }
Exemple #23
0
 protected abstract IEnumerable <SubstitutionSet> CheckActivation(IQueryable db, Name perspective, IEnumerable <SubstitutionSet> constraints);
Exemple #24
0
 public QueryContext(IQueryable kb, IEnumerable <SubstitutionSet> contraints, Name perspective)
 {
     Queryable   = kb;
     Constraints = contraints;
     Perspective = perspective;
 }