static IEnumerable <bool> attack3(object X, object N, object Arg3) { Variable Y = new Variable(); foreach (bool l in new ListPair(Y, new Variable()).unify(Arg3)) { if ((int)YP.getValue(X) == (int)Y.getValue() + (int)YP.getValue(N)) { yield return(false); } if ((int)YP.getValue(X) == (int)Y.getValue() - (int)YP.getValue(N)) { yield return(false); } } Variable Ys = new Variable(); Variable N1 = new Variable(); foreach (bool l1 in new ListPair(new Variable(), Ys).unify(Arg3)) { foreach (bool l2 in N1.unify((int)YP.getValue(N) + 1)) { foreach (bool l3 in attack3(X, N1, Ys)) { yield return(false); } } } }
IEnumerator ChooseBehavior() { while (true) { yield return(new WaitForSeconds(behaviorDelay)); //Debug.Log("Choosing behavior"); Variable deadAgent = new Variable(); Variable whenVar = new Variable(); Variable whereVar = new Variable(); //Debug.Log("AgentBehavior" + info.agentName); int deadCount = 0; bool killerFound = false; try { foreach (bool l1 in YP.matchDynamic(info.agentId, Atom.a("dead"), new object[] { deadAgent, whenVar, whereVar })) { Debug.Log(string.Format("{0} KNOWS {1} HAS DIED IN {2} at {3}", info.agentName, deadAgent.getValue(), whereVar.getValue(), whenVar.getValue())); deadCount++; } } catch (System.Exception e) { Debug.Log(e); } Variable killerAgent = new Variable(); try { foreach (bool l1 in YP.matchDynamic(info.agentId, Atom.a("killed"), new object[] { killerAgent, deadAgent })) { Debug.Log(string.Format("{0} KNOWS {1} HAS KILLED {2}", info.agentName, killerAgent.getValue(), deadAgent.getValue())); killerFound = true; } } catch (System.Exception e) { Debug.Log(e); } if (deadCount == 3 || killerFound) { StartNewBehavior("RunAwayBehavior"); //StopCoroutine(thinkingBehavior); } } }
static IEnumerable <bool> queens3(object UnplacedQs, object SafeQs, Variable Qs) { ListPair UnplacedQsListPair = YP.getValue(UnplacedQs) as ListPair; if (UnplacedQsListPair != null) { Variable UnplacedQs1 = new Variable(); Variable Q = new Variable(); foreach (bool l1 in selectq(Q, UnplacedQsListPair, UnplacedQs1)) { if (!(SafeQs is ListPair && hasAttack((int)Q.getValue(), (ListPair)SafeQs))) { foreach (bool l2 in queens3(UnplacedQs1, new ListPair(Q, SafeQs), Qs)) { yield return(false); } } } } else { foreach (bool l1 in Qs.unify(SafeQs)) { yield return(false); } } }
// // // End of Context API implementation // // // determine the type from the VariableFacade or use the value object internal virtual int determineType(Variable var, Object value) { int type = VariableType.UNKNOWN; if (var is VariableFacade && ((VariableFacade)var).Variable == null) { if (value is ValueType) { type = VariableType.NUMBER; } else if (value is Boolean) { type = VariableType.BOOLEAN; } else { type = VariableType.STRING; } } else { type = var.getValue().getType(); } return(type); }
/* returns a string consisting of formatted member names and values */ public virtual Object lookupMembers(Object o) { Variable var = null; Value val = null; Variable[] mems = null; try { var = resolveToVariable(o); if (var != null) { val = var.getValue(); } else { val = resolveToValue(o); } mems = val.getMembers(Session); } catch (NullReferenceException) { throw new NoSuchVariableException(o); } catch (PlayerDebugException) { throw new NoSuchVariableException(o); // not quite right... } System.Text.StringBuilder sb = new System.Text.StringBuilder(); // [mmorearty] experimenting with hierarchical display of members String[] classHierarchy = val.getClassHierarchy(false); if (classHierarchy != null && Session.getPreference(SessionManager.PREF_HIERARCHICAL_VARIABLES) != 0) { for (int c = 0; c < classHierarchy.Length; ++c) { String classname = classHierarchy[c]; sb.Append(m_newline + "(Members of " + classname + ")"); //$NON-NLS-1$ //$NON-NLS-2$ for (int i = 0; i < mems.Length; ++i) { if (classname.Equals(mems[i].getDefiningClass())) { sb.Append(m_newline + " "); //$NON-NLS-1$ } } } } else { for (int i = 0; i < mems.Length; i++) { sb.Append(m_newline + " "); //$NON-NLS-1$ } } return(sb.ToString()); }
// // START-FOLVisitor public object visitVariable(Variable var, object arg) { // All variables will be marked with an * identity.Append("*"); ICollection <int> positions = varPositions.Get(var.getValue()); if (null == positions) { positions = CollectionFactory.CreateQueue <int>(); varPositions.Put(var.getValue(), positions); } positions.Add(noVarPositions); noVarPositions++; clauseVarCounts[currentLiteral]++; return(var); }
public override MathTreeNodeValue eval(Dictionary <string, Variable> variableMap) { if (!variableMap.ContainsKey(this.value)) { throw new ArgumentException("変数" + this.value + "が見つかりません。"); } Variable variable = variableMap[this.value]; return(variable.getValue()); }
// // Statics for formatting stuff // /// <summary> Formatting function for variable</summary> public static void appendVariable(System.Text.StringBuilder sb, Variable v) { //sb.append('\''); String name = v.getName(); sb.Append(name); //sb.append('\''); sb.Append(" = "); //$NON-NLS-1$ appendVariableValue(sb, v.getValue(), name); //appendVariableAttributes(sb, v); }
// START-FOLVisitor public Object visitVariable(Variable var, Object arg) { // All variables will be marked with an * identity.Append("*"); List<int> positions = null; if (varPositions.ContainsKey(var.getValue())) { positions = varPositions[var.getValue()]; } if (null == positions) { positions = new List<int>(); varPositions.Add(var.getValue(), positions); } positions.Add(noVarPositions); noVarPositions++; clauseVarCounts[currentLiteral]++; return var; }
/// <summary> The magic conversion function which provides a mapping /// from any object into a long! /// </summary> public static long toLong(Object o) { long value = 0; try { if (o is ValueType) { value = Convert.ToInt64(((ValueType)o)); } else if (o is Boolean) { value = (((Boolean)o))?1:0; } else if (o is Variable) { Variable var = (Variable)o; if (var.getValue().getType() == VariableType.BOOLEAN) { return((bool)var.getValue().ValueAsObject?1:0); } if (var.getValue().getType() == VariableType.NULL) { return(0); } return(var.getValue().ValueAsObject != null?var.getValue().Id : 0); } else { value = (long)Double.Parse(o.ToString()); } } catch (FormatException n) { throw n; } return(value); }
/// <summary> Performs a search for a member with the given name using the /// given id as the parent variable. /// /// If a match is found then, we return the parent variable of /// the member that matched. The proto chain is optionally traversed. /// /// No exceptions are thrown /// </summary> internal virtual Value locateParentForNamed(int id, String name, bool traverseProto) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); Variable var = null; Value val = null; try { var = memberNamed(id, name); // see if we need to traverse the proto chain while (var == null && traverseProto) { // first attempt to get __proto__, then resolve name Variable proto = memberNamed(id, "__proto__"); //$NON-NLS-1$ sb.Append("__proto__"); //$NON-NLS-1$ if (proto == null) { traverseProto = false; } else { id = proto.getValue().Id; var = memberNamed(id, name); if (var == null) { sb.Append('.'); } } } } catch (NoSuchVariableException) { // don't worry about this one, it means variable with id couldn't be found } catch (NullReferenceException) { // probably no session } // what we really want is the parent not the child variable if (var != null) { pushName(sb.ToString()); val = Session.getValue(id); } return(val); }
public IExpressionType ExpressionImportFromFormatToModel(IFormat expression) { List <IExpressionType> operands = new List <IExpressionType>(); Context ctx = Context.getInstance(); if (expression.getOperands() != null) { foreach (var operand in expression.getOperands()) { operands.Add(ExpressionImportFromFormatToModel(operand)); } } switch (expression.getType()) { case Types.Variable: Variable newVar = new Variable(expression.getValue()); ctx.addVariable(newVar.getValue(), ctx.getCurrPath(), null); return(newVar); case Types.Number: return(new Number(Convert.ToDouble(expression.getValue()))); case Types.VarDefinition: Equation ne = new Equation(operands, false); ctx.changeVariable(((Variable)operands[0]).getValue(), ctx.getCurrPath(), ne); return(ne); case Types.SuspendedVarDefinition: Equation nde = new Equation(operands, true); ctx.changeVariable(((Variable)operands[0]).getValue(), ctx.getCurrPath(), nde); return(nde); case Types.List: return(new MathList(operands)); case Types.Interval: return(new Interval(operands)); case Types.FuncExpression: return(new Function(expression.getType(), operands, expression.getOptions().getValue())); default: return(new Function(expression.getType(), operands, "")); } }
public void assign(Object par0, Value par1) { Variable var = lookup(par0) as Variable; if (var != null) { int type = var.getValue().getType(); if (type == VariableType_.BOOLEAN || type == VariableType_.NUMBER || type == VariableType_.STRING) { var.setValue(session, par1.getType(), par1.getValueAsString()); } else { throw new NotSupportedException(TextHelper.GetString("Error.NoScalar")); } } else { throw new NoSuchVariableException(System.String.Format(TextHelper.GetString("Error.NoSuchVariable"), par0)); } }
static void Main(string[] args) { Console.WriteLine("Return a list of 2 elements:"); Variable List = new Variable(); foreach (bool l1 in makeList("a", "b", List)) { Console.WriteLine("List = " + List.getValue()); } Console.WriteLine("Unify two lists:"); Variable Second = new Variable(); foreach (bool l1 in makeList("x", Second, new ListPair("x", new ListPair("y", Atom.NIL)))) { Console.WriteLine("The second element is " + Second.getValue()); } Console.WriteLine("\nPress Enter to finish."); Console.ReadLine(); }
public IExpressionType doVariable(Variable v) { Context ctx = Context.getInstance(); Computator comp = new Computator(); IExpressionType varDef = ctx.getVariableValue(v.getValue(), ctx.getCurrPath()); if (varDef == null) { return(v); } if (varDef.getType() == Types.Number) { return(varDef); } if (varDef.getType() == Types.Variable) { return(varDef.doOperation(comp)); } varDef = (Equation)varDef.doOperation(comp); return(varDef.getOperands()[1]); }
public Object lookupMembers(Object par0) { String name = "?"; Value val = null; if (par0 is Value) { val = (Value)par0; } if (par0 is Variable) { Variable var0 = (Variable)par0; name = var0.getName(); val = var0.getValue(); } if (val != null) { int type = val.getType(); if (type == VariableType_.MOVIECLIP || type == VariableType_.OBJECT) { String ret = name + " = " + FormatValue(val) + "\r\n"; foreach (Variable v in val.getMembers(session)) { ret += " " + v.getName() + " = " + FormatValue(v.getValue()) + "\r\n"; } return(ret); } return(new String(name + " = " + val.getValueAsString())); } //NoSuchVariableException throw new NotImplementedException(); }
public VariableNode(Variable variable) : base(variable.getName()) { m_Variable = variable; m_Value = variable.getValue(); }
static void Main(string[] args) { YP.assertFact(Atom.a("brother"), new object[] { Atom.a("Hillary"), Atom.a("Hugh") }); YP.assertFact(Atom.a("brother"), new object[] { Atom.a("Hillary"), Atom.a("Tony") }); YP.assertFact(Atom.a("brother"), new object[] { Atom.a("Bill"), Atom.a("Roger") }); Variable Brother = new Variable(); Console.WriteLine("Using dynamic assert:"); foreach (bool l1 in YP.matchDynamic (Atom.a("brother"), new object[] { Atom.a("Hillary"), Brother })) { Console.WriteLine("Hillary has brother " + Brother.getValue() + "."); } string prologCode = "uncle(Person, Uncle) :- \n" + " parent(Person, Parent), \n" + " brother(Parent, Uncle). \n"; Console.WriteLine("\n// Compiled code:"); compileAndWrite(prologCode); prologCode = ":- import('', [parent/2]). \n" + "uncle(Person, Uncle) :- \n" + " parent(Person, Parent), \n" + " brother(Parent, Uncle). \n"; Console.WriteLine("// Calling an imported function:"); compileAndWrite(prologCode); prologCode = "parent('Chelsea', 'Hillary'). \n" + "parent('Chelsea', 'Bill'). \n" + "uncle(Person, Uncle) :- \n" + " parent(Person, Parent), \n" + " brother(Parent, Uncle). \n"; Console.WriteLine("// Calling a locally-defined function:"); compileAndWrite(prologCode); prologCode = ":- import('', [parent/2]). \n" + "uncle(Person, Uncle) :- \n" + " Goal = parent(Person, Parent), \n" + " Goal, \n" + " brother(Parent, Uncle). \n"; Console.WriteLine("// Calling a dynamic goal:"); compileAndWrite(prologCode); Console.WriteLine("Calling compiled code having a dynamic goal:"); Variable Person = new Variable(); Variable Uncle = new Variable(); foreach (bool l1 in uncle(Person, Uncle)) { Console.WriteLine(Person.getValue() + " has uncle " + Uncle.getValue() + "."); } Console.WriteLine("\nPress Enter to finish."); Console.ReadLine(); }
public virtual Value getValue() { return(m_var.getValue()); }
static void Main(string[] args) { Console.WriteLine("Find relations:"); Variable Brother = new Variable(); foreach (bool l1 in brother("Hillary", Brother)) { Console.WriteLine("Hillary has brother " + Brother.getValue() + "."); } Console.WriteLine("Check if it is square:"); foreach (bool l1 in squaredRectangle(10, 10)) { Console.WriteLine("10 by 10 rectangle is square."); } Console.WriteLine("Make it square:"); Variable Width = new Variable(); Variable Height = new Variable(); foreach (bool l1 in Width.unify(10)) { foreach (bool l2 in squaredRectangle(Width, Height)) { Console.WriteLine("A square of width " + Width.getValue() + " has height " + Height.getValue() + "."); } } Console.WriteLine("Make it square before we know the width:"); foreach (bool l1 in squaredRectangle(Width, Height)) { foreach (bool l2 in Width.unify(10)) { Console.WriteLine("A square of width " + Width.getValue() + " has height " + Height.getValue() + "."); } } Console.WriteLine("Get one match:"); foreach (bool l1 in anyBrother("Hillary", Brother)) { Console.WriteLine("Hillary has a brother " + Brother.getValue() + "."); } foreach (bool l1 in anyBrother("Bill", Brother)) { Console.WriteLine("Bill has a brother " + Brother.getValue() + "."); } Console.WriteLine("Use cut for negation:"); foreach (bool l1 in noBrother("Hillary")) { Console.WriteLine("Hillary has no brother."); } foreach (bool l1 in noBrother("Chelsea")) { Console.WriteLine("Chelsea has no brother."); } Console.WriteLine("\nPress Enter to finish."); Console.ReadLine(); }
// determine the type from the VariableFacade or use the value object internal virtual int determineType(Variable var, Object value) { int type = VariableType.UNKNOWN; if (var is VariableFacade && ((VariableFacade) var).Variable == null) { if (value is ValueType) type = VariableType.NUMBER; else if (value is Boolean) type = VariableType.BOOLEAN; else type = VariableType.STRING; } else type = var.getValue().getType(); return type; }
/// <summary> All the really good stuff about finding where name exists goes here! /// /// If name is not null, then it implies that we use the existing /// m_current to find a member of m_current. If m_current is null /// Then we need to probe variable context points attempting to locate /// name. When we find a match we set the m_current to this context /// /// If name is null then we simply return the current context. /// </summary> internal virtual int determineContext(String name) { long id = Value.UNKNOWN_ID; // have we already resolved our context... if (m_current != null) { Object value; if (m_current is Variable) { value = ((Variable)m_current).getValue().ValueAsObject; } else if (m_current is Value) { value = ((Value)m_current).ValueAsObject; } else { value = m_current; } id = ArithmeticExp.toLong(value); } // nothing to go on, so we're done else if (name == null) { } // use the name and try and resolve where we are... else { // Each stack frame has a root variable under (BASE_ID-depth) // where depth is the depth of the stack. // So we query for our current stack depth and use that // as the context for our base computation int baseId = Value.BASE_ID; int depth = ((Int32)m_cache.get_Renamed(DebugCLI.DISPLAY_FRAME_NUMBER)); baseId -= depth; // obtain data about our current state Variable contextVar = null; Value contextVal = null; Value val = null; // look for 'name' starting from local scope if ((val = locateParentForNamed(baseId, name, false)) != null) { } // get the this pointer, then look for 'name' starting from that point else if (((contextVar = locateForNamed(baseId, "this", false)) != null) && (setName("this") && (val = locateParentForNamed(contextVar.getValue().Id, name, true)) != null)) //$NON-NLS-1$ { } // now try to see if 'name' exists off of _root else if (setName("_root") && (val = locateParentForNamed(Value.ROOT_ID, name, true)) != null) //$NON-NLS-1$ { } // now try to see if 'name' exists off of _global else if (setName("_global") && (val = locateParentForNamed(Value.GLOBAL_ID, name, true)) != null) //$NON-NLS-1$ { } // now try off of class level, if such a thing can be found else if (((contextVal = locate(Value.GLOBAL_ID, CurrentPackageName, false)) != null) && (setName("_global." + CurrentPackageName) && (val = locateParentForNamed(contextVal.Id, name, true)) != null)) //$NON-NLS-1$ { } // if we found it then stake this as our context! if (val != null) { id = val.Id; pushName(name); lockName(); } } return((int)id); }