Esempio n. 1
0
        /// <summary>
        /// String representation.
        /// </summary>
        /// <returns>String representation.</returns>
        public override string ToString()
        {
            List <string> itemsList = new List <string>();

            if (Predicates != null)
            {
                foreach (var predicateAtom in Predicates)
                {
                    itemsList.Add(predicateAtom.ToString(IdManager.Predicates));
                }
            }
            if (NumericFunctions != null)
            {
                foreach (var numericFunction in NumericFunctions)
                {
                    string functionAtom = numericFunction.Key.ToString(IdManager.Functions);
                    string numericValue = (NumericFunction.IsValueUndefined(numericFunction.Value)) ? "undefined" : numericFunction.Value.ToString(CultureInfo.InvariantCulture);
                    itemsList.Add($"(= {functionAtom} {numericValue})");
                }
            }
            if (ObjectFunctions != null)
            {
                foreach (var objectFunction in ObjectFunctions)
                {
                    string functionAtom = objectFunction.Key.ToString(IdManager.Functions);
                    string constValue   = IdManager.Constants.GetNameFromId(objectFunction.Value);
                    itemsList.Add($"(= {functionAtom} {constValue})");
                }
            }
            return(string.Join(", ", itemsList));
        }
Esempio n. 2
0
 /// <summary>
 /// Defines a new value for the requested function in the state.
 /// </summary>
 /// <param name="function">Requested function.</param>
 /// <param name="assignment">Value to be assigned.</param>
 public void AssignNumericFunction(IAtom function, double assignment)
 {
     if (NumericFunction.IsValueUndefined(assignment))
     {
         if (NumericFunctions != null && NumericFunctions.ContainsKey(function))
         {
             NumericFunctions.Remove(function);
         }
     }
     else
     {
         CheckNumericFunctionsInitialized();
         NumericFunctions[function] = assignment;
     }
 }
        /// <summary>
        /// Checks whether the literal is in the form of a simple numeric function assignment, e.g. (= (numFunc) 8.5),
        /// and in such case returns the pair of numeric function and its assigned value. Otherwise returns null.
        /// </summary>
        /// <returns>Numeric function assignment with its value, if the literal is in such form. Null otherwise.</returns>
        public Tuple <NumericFunction, Number> TryGetNumericFunctionAssignment()
        {
            if (Operator != NumericCompareExpression.RelationalOperator.EQ)
            {
                return(null);
            }

            NumericFunction numFunc = LeftArgument as NumericFunction;
            Number          number  = RightArgument as Number;

            if (numFunc == null || number == null)
            {
                numFunc = RightArgument as NumericFunction;
                number  = LeftArgument as Number;
            }

            return((numFunc == null || number == null) ? null : Tuple.Create(numFunc, number));
        }
Esempio n. 4
0
        /// <summary>
        /// Transforms the numeric expression.
        /// </summary>
        /// <param name="expression">Numeric expression.</param>
        /// <returns>Transformed numeric expression.</returns>
        public INumericExpression Visit(NumericFunction expression)
        {
            IAtom functionAtom = GroundingManager.GroundAtom(expression.FunctionAtom, ExpressionSubstitution);

            if (!ReplacedFunctionAtomsInSubExpression.Contains(functionAtom))
            {
                INumericExpression substitutedValue;
                if (NumericFunctionAssignments.TryGetValue(functionAtom, out substitutedValue))
                {
                    ReplacedFunctionAtomsInSubExpression.Add(functionAtom);
                    ExpressionSubstitution.AddLocalSubstitution(OperatorSubstitution);

                    INumericExpression transformedValue = substitutedValue.Accept(this);

                    ExpressionSubstitution.RemoveLocalSubstitution(OperatorSubstitution);
                    ReplacedFunctionAtomsInSubExpression.Remove(functionAtom);

                    return(transformedValue);
                }
            }

            return(expression.Clone());
        }
Esempio n. 5
0
 /// <summary>
 /// Evaluates the numeric expression.
 /// </summary>
 /// <param name="expression">Numeric expression.</param>
 public override void Visit(NumericFunction expression)
 {
     ExtractParameters(expression.FunctionAtom);
 }
 /// <summary>
 /// Evaluates the numeric expression.
 /// </summary>
 /// <param name="expression">Numeric expression.</param>
 public override void Visit(NumericFunction expression)
 {
     CheckAndRenameAtom(expression.FunctionAtom);
 }
 /// <summary>
 /// Visits numeric expression.
 /// </summary>
 /// <param name="expression">Numeric expression.</param>
 public override void Visit(NumericFunction expression)
 {
     NumericFunctions.Add(expression);
 }
 /// <summary>
 /// Transforms the numeric expression.
 /// </summary>
 /// <param name="expression">Numeric expression.</param>
 /// <returns>Transformed numeric expression.</returns>
 public INumericExpression Visit(NumericFunction expression)
 {
     return(new NumericFunction(TermsGrounder.Value.GroundAtom(expression.FunctionAtom, Substitution), IdManager));
 }
        /// <summary>
        /// Processes a single CNF literal of operator preconditions.
        /// </summary>
        /// <param name="literal">CNF literal.</param>
        /// <param name="state">Relative state to be applied to.</param>
        private static void ProcessPreconditionLiteral(LiteralCNF literal, IRelativeState state)
        {
            PredicateLiteralCNF predicateLiteral = literal as PredicateLiteralCNF;

            if (predicateLiteral != null)
            {
                if (predicateLiteral.IsNegated)
                {
                    state.AddNegatedPredicate(predicateLiteral.PredicateAtom);
                }
                else
                {
                    state.AddPredicate(predicateLiteral.PredicateAtom);
                }
                return;
            }

            EqualsLiteralCNF equalsLiteral = literal as EqualsLiteralCNF;

            if (equalsLiteral != null)
            {
                ObjectFunctionTerm objFunc   = equalsLiteral.LeftArgument as ObjectFunctionTerm;
                ConstantTerm       constTerm = equalsLiteral.RightArgument as ConstantTerm;

                if (objFunc == null || constTerm == null)
                {
                    objFunc   = equalsLiteral.RightArgument as ObjectFunctionTerm;
                    constTerm = equalsLiteral.LeftArgument as ConstantTerm;
                }

                if (objFunc != null && constTerm != null)
                {
                    if (equalsLiteral.IsNegated)
                    {
                        if (state.GetObjectFunctionValue(objFunc.FunctionAtom) == constTerm.NameId)
                        {
                            state.AssignObjectFunction(objFunc.FunctionAtom, IdManager.InvalidId);
                        }
                    }
                    else
                    {
                        state.AssignObjectFunction(objFunc.FunctionAtom, constTerm.NameId);
                    }
                }
                return;
            }

            NumericCompareLiteralCNF compareLiteral = literal as NumericCompareLiteralCNF;

            if (compareLiteral != null)
            {
                if (compareLiteral.Operator != NumericCompareExpression.RelationalOperator.EQ)
                {
                    return;
                }

                NumericFunction numFunc = compareLiteral.LeftArgument as NumericFunction;
                Number          number  = compareLiteral.RightArgument as Number;

                if (numFunc == null || number == null)
                {
                    numFunc = compareLiteral.RightArgument as NumericFunction;
                    number  = compareLiteral.LeftArgument as Number;
                }

                if (numFunc != null && number != null)
                {
                    if (compareLiteral.IsNegated)
                    {
                        if (state.GetNumericFunctionValue(numFunc.FunctionAtom).Equals(number.Value))
                        {
                            state.AssignNumericFunction(numFunc.FunctionAtom, NumericFunction.DefaultValue);
                        }
                    }
                    else
                    {
                        state.AssignNumericFunction(numFunc.FunctionAtom, number.Value);
                    }
                }
            }
        }