#region Copyright
// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (C) 2015 Ian Horswill
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in the
// Software without restriction, including without limitation the rights to use, copy,
// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// --------------------------------------------------------------------------------------------------------------------
#endregion
using System;
using UnityEngine;
namespace Prolog
{
public static class UnityExtensionMethods
{
///
/// Returns the parent GameObject of this GameObject
///
/// The GameObject to get the parent of
/// The parent GameObject
public static GameObject GetParent(this GameObject o)
{
return o.transform.parent.gameObject;
}
///
/// Gets the KnowledgeBase attached to this component's GameObject.
///
/// The KnowledgeBase attached to component's GameObjec
public static KnowledgeBase KnowledgeBase(this Component component)
{
return component.GetComponent().KnowledgeBase;
}
///
/// Gets the KnowledgeBase attached to this GameObject.
///
/// The KnowledgeBase attached to component's GameObjec
public static KnowledgeBase KnowledgeBase(this GameObject gameObject)
{
return gameObject.GetComponent().KnowledgeBase;
}
///
/// Adds ASSERTION to GAMEOBJECT's knowledgebase.
///
/// Object whose knowledge base it should be added to
/// Assertion to add
public static void Assert(this GameObject gameObject, Structure assertion)
{
gameObject.KnowledgeBase().AssertZ(assertion);
}
///
/// Adds assertion to GAMEOBJECT's knowledgebase.
///
/// Object whose knowledge base it should be added to
/// Functor (i.e. predicate) of the assertion
/// Arguments to the functor
public static void Assert(this GameObject gameObject, Symbol functor, params object[] args)
{
gameObject.KnowledgeBase().AssertZ(new Structure(functor, args));
}
///
/// Adds assertion to GAMEOBJECT's knowledgebase.
///
/// Object whose knowledge base it should be added to
/// Functor (i.e. predicate) of the assertion
/// Arguments to the functor
public static void Assert(this GameObject gameObject, string functor, params object[] args)
{
gameObject.KnowledgeBase().AssertZ(new Structure(functor, args));
}
///
/// Adds ASSERTION to COMPONENT's knowledgebase.
///
/// Object whose knowledge base it should be added to
/// Assertion to add
public static void Assert(this Component component, Structure assertion)
{
component.KnowledgeBase().AssertZ(assertion);
}
///
/// Adds assertion to component's knowledgebase.
///
/// Object whose knowledge base it should be added to
/// Functor (i.e. predicate) of the assertion
/// Arguments to the functor
public static void Assert(this Component component, Symbol functor, params object[] args)
{
component.KnowledgeBase().AssertZ(new Structure(functor, args));
}
///
/// Adds assertion to component's knowledgebase.
///
/// Object whose knowledge base it should be added to
/// Functor (i.e. predicate) of the assertion
/// Arguments to the functor
public static void Assert(this Component component, string functor, params object[] args)
{
component.KnowledgeBase().AssertZ(new Structure(functor, args));
}
///
/// True if goal is provable within this GameObject's knowledge base.
///
/// GameObject whose KB should be queried.
/// Term to try to prove.
/// True if the goal is provable.
public static bool IsTrue(this GameObject gameObject, object goal)
{
return gameObject.KnowledgeBase().IsTrue(goal, gameObject);
}
///
/// True if functor(args) is provable within this GameObject's knowledge base.
///
/// GameObject whose KB should be queried.
/// Functor of the goal
/// Arguments of the goal
///
public static bool IsTrue(this GameObject gameObject, string functor, params object[] args)
{
return gameObject.KnowledgeBase().IsTrue(new Structure(functor, args), gameObject);
}
///
/// True if functor(args) is provable within this Component's GameObject's knowledge base.
///
/// GameObject whose KB should be queried.
/// Functor of the goal
/// Arguments of the goal
///
public static bool IsTrue(this Component component, string functor, params object[] args)
{
return component.KnowledgeBase().IsTrue(new Structure(functor, args), component);
}
///
/// True if goal is provable within this Component's GameObject's knowledge base.
///
/// GameObject whose KB should be queried.
/// Term to try to prove.
/// True if the goal is provable.
public static bool IsTrue(this Component component, object goal)
{
return component.KnowledgeBase().IsTrue(goal, component);
}
///
/// True if goal is provable within this GameObject's knowledge base.
///
/// GameObject whose KB should be queried.
/// Term to try to prove.
/// True if the goal is provable.
public static bool IsTrueParsed(this GameObject gameObject, string goal)
{
return gameObject.KnowledgeBase().IsTrue(ISOPrologReader.Read(goal), gameObject);
}
///
/// True if goal is provable within this Component's GameObject's knowledge base.
///
/// GameObject whose KB should be queried.
/// Term to try to prove.
/// True if the goal is provable.
public static bool IsTrueParsed(this Component component, string goal)
{
return component.KnowledgeBase().IsTrue(ISOPrologReader.Read(goal), component);
}
///
/// Finds the value of result in the first solution when proving goal within this GameObject's knowledge base.
///
/// GameObject whose KB should be queried.
/// Value of the variable to solve for
/// Constraint on the value of the variable
/// Value found for the variable.
public static object SolveFor(this GameObject gameObject, LogicVariable result, object goal)
{
return gameObject.KnowledgeBase().SolveFor(result, goal, gameObject, gameObject);
}
///
/// Finds the value of result in the first solution when proving goal within this Component's GameObject's knowledge base.
///
/// GameObject whose KB should be queried.
/// Value of the variable to solve for
/// Functor of the goal.
/// Arguments for the goal.
/// Value found for the variable.
public static object SolveFor(this GameObject gameObject, LogicVariable result, string functor, params object[] args)
{
return gameObject.SolveFor(result, new Structure(functor, args));
}
///
/// Given argument Variable:Constraint, finds the value of Variable in the first solution to Constraint when proved against this GameObject's knowledge base.
///
/// GameObject whose KB should be queried.
/// String of the form "Variable:Constraint"
/// Value found for the variable.
public static object SolveForParsed(this GameObject gameObject, string variableAndConstraint)
{
var colonExpression = ISOPrologReader.Read(variableAndConstraint) as Structure;
if (colonExpression == null || !colonExpression.IsFunctor(Symbol.Colon, 2))
throw new ArgumentException("Arguent to SolveFor(string) must be of the form Var:Goal.");
return gameObject.SolveFor((LogicVariable)colonExpression.Argument(0), colonExpression.Argument(1));
}
///
/// Finds the value of result in the first solution when proving goal within this Component's GameObject's knowledge base.
///
/// Component whose KB should be queried.
/// Value of the variable to solve for
/// Constraint on the value of the variable
/// Value found for the variable.
public static object SolveFor(this Component component, LogicVariable result, object goal)
{
return component.KnowledgeBase().SolveFor(result, goal, component, component.gameObject);
}
///
/// Finds the value of result in the first solution when proving goal within this Component's GameObject's knowledge base.
///
/// Component whose KB should be queried.
/// Value of the variable to solve for
/// Functor of the goal.
/// Arguments for the goal.
/// Value found for the variable.
public static object SolveFor(this Component component, LogicVariable result, string functor, params object[] args)
{
return component.SolveFor(result, new Structure(functor, args));
}
///
/// Given argument Variable:Constraint, finds the value of Variable in the first solution to Constraint when proved against this Component's GameObject's knowledge base.
///
/// Component whose KB should be queried.
/// String of the form "Variable:Constraint"
/// Value found for the variable.
public static object SolveForParsed(this Component component, string variableAndConstraint)
{
var colonExpression = ISOPrologReader.Read(variableAndConstraint) as Structure;
if (colonExpression == null || !colonExpression.IsFunctor(Symbol.Colon, 2))
throw new ArgumentException("Arguent to SolveFor(string) must be of the form Var:Goal.");
return component.SolveFor((LogicVariable)colonExpression.Argument(0), colonExpression.Argument(1));
}
}
}