public void AddRelations(string data) { if (string.IsNullOrEmpty(data)) { return; } NutritionalFact nutritionalFactData = JsonConvert.DeserializeObject <NutritionalFact>(data); NutritionalFact nutritionalFact = _service.GetById(nutritionalFactData.NutritionalFactId); if (nutritionalFact.FoodStage != null) { FoodStage foodStage = _foodStageService.GetById(nutritionalFact.FoodStageId); nutritionalFact.FoodStage = foodStage; } else if (nutritionalFact.FactDefinition != null) { FactDefinition factDefinition = _factDefinitionService.GetById(nutritionalFact.FactDefinitioId); nutritionalFact.FactDefinition = factDefinition; } else if (nutritionalFact.ValueSource != null) { ValueSource valueSource = _valueSourceService.GetById(nutritionalFact.ValueSourceId); nutritionalFact.ValueSource = valueSource; } _service.Update(nutritionalFact); _service.Complete(); }
public string GetEntityDefinition(JObject entityName) { string entityNameStr = entityName.ToObject <Name>().name; if (NullString(entityNameStr)) { return("false"); } EntityDefinition entity = EntityFacade.GetEntityDefinition(entityNameStr); FactDefinition factDefinition = new FactDefinition(); factDefinition.factName = entityNameStr; factDefinition.fields = new List <FactFieldDefinition>(); foreach (var field in entity.EntityFields) { factDefinition.fields.Add(new FactFieldDefinition() { fieldName = field.FieldName, fieldType = field.FieldTypeStr }); } return(JsonConvert.SerializeObject(factDefinition)); }
protected override void ConvertLine(LuaTextWriter luaWriter, TsvLine line) { var firstField = line.Fields.First(); if (firstField.StartsWith("FACTDEF")) { var def = new FactDefinition(); foreach (var field in line.Fields) { def.AddField(field); } def.Dump(luaWriter); luaWriter.Write("\n"); return; } if (firstField.TryRemovePrefix("DEFAULTVARIABLEVALUE:", out var defVar)) { var(v, value) = defVar.SplitTuple('|'); luaWriter.Write($"SetDefaultVariableValue(\"{v.Value}\", \"{value.Value}\")\n"); return; } if (firstField.StartsWith("FUNCTION:")) { var def = new FunctionDefinition(); foreach (var field in line.Fields) { def.AddField(field); } def.Dump(luaWriter); luaWriter.Write("\n"); return; } if (firstField.StartsWith("FACTSETDEF")) { // Ignoring this one for now return; } base.ConvertLine(luaWriter, line); }
public void Save(ICollection <TOut> classToSave, IKernel kernel) { INutritionalFactService service = kernel.Get <INutritionalFactService>(); IFoodStageService foodStageService = kernel.Get <IFoodStageService>(); IFactDefinitionService factDefinitionService = kernel.Get <IFactDefinitionService>(); ICollection <NutritionalFact> nutritionalFacts = (List <NutritionalFact>)classToSave; foreach (NutritionalFact fact in nutritionalFacts) { FoodStage foodStage = foodStageService.Get(f => f.UsdaRefId == fact.FoodStage.UsdaRefId); if (foodStage == null) { continue; } FactDefinition factDefinition = factDefinitionService.Get(f => f.UsdaNutriRefId == fact.FactDefinition.UsdaNutriRefId); fact.FoodStage = foodStage; fact.FactDefinition = factDefinition; service.Add(fact); } service.Complete(); }
public void Remove(FactDefinition definition) { list.Remove(definition); }
public void Add(FactDefinition definition) { list.Add(definition); }
void OnGUI() { //VIEW for (int i = 0; i < definitions.list.Count; i++) { FactDefinition fd = definitions.list[i]; GUILayout.BeginHorizontal(GUILayout.Width(200)); //Name GUILayout.Label("Name", GUILayout.Width(40)); List <string> names = new List <string> (); try { names = AtomsContainer.Load(AtomListWindow.path).list; } catch (Exception e) { Debug.Log(e.Message); } selectedName = names.IndexOf(fd.name); selectedName = EditorGUILayout.Popup(selectedName, names.ToArray(), GUILayout.Width(50)); if (selectedName == -1) { selectedName = 0; } fd.name = names [selectedName]; //Type GUILayout.Label("Type"); if (fd.factType == "Property") { selectedFactType = 0; } else if (fd.factType == "Relation") { selectedFactType = 1; } selectedFactType = EditorGUILayout.Popup(selectedFactType, factTypes, GUILayout.Width(100)); fd.factType = factTypes[selectedFactType]; GUILayout.Space(20); //Arguments of expression GUILayout.Label("Arguments"); int j; for (j = 0; j < argsTypes.list.Count; j++) { if (fd.argsType == argsTypes.list[j]) { selectedArgType = j; break; } } selectedArgType = EditorGUILayout.Popup(selectedArgType, argsTypes.list.ToArray(), GUILayout.Width(100)); fd.argsType = argsTypes.list[selectedArgType]; //MODEL/CONTROLLER if (GUILayout.Button("Edit", GUILayout.Width(50))) { AddArgumentWindow.ShowWindow(); } GUILayout.Space(20); //Expression GUILayout.Label("Expression"); fd.expression = EditorGUILayout.TextField(fd.expression, GUILayout.Width(300)); definitions.list[i] = fd; GUILayout.Space(20); if (GUILayout.Button("Delete", GUILayout.Width(100))) { definitions.Remove(definitions.list[i]); } GUILayout.EndHorizontal(); } //MODEL/CONTROLLER if (GUILayout.Button("Add Definition", GUILayout.Width(200))) { if (AtomListWindow.atomNames.list.Count > 0) { definitions.Add(new FactDefinition("New Fact")); } else { EditorUtility.DisplayDialog("", "No names in atom names list. Please, fill the list before adding a definition", "OK"); } } }
public bool Contains(FactDefinition definition) { return(list.Contains(definition)); }
//Sense and check facts about object from user definitions public void SenseFactFromDefinition(GameObject obj, List <GameObject> objs, FactDefinition definition) { string factName = definition.name; bool pairSense = false; //whether the object should be compared with other object to sense the fact switch (definition.factType) { case "Property": pairSense = false; break; case "Relation": pairSense = true; break; } //If it is a relation, fact must be evaluated along with every other object in the scene if (pairSense) { foreach (GameObject obj2 in objs) { if (!obj2.Equals(obj)) { bool evaluationResult = false; Atom existingFact = agent.state.facts.Find((Atom fact) => fact.name == factName && fact.terms[0].value.Equals(obj) && fact.terms[1].value.Equals(obj2)); switch (definition.argsType) { case "Position": try { evaluationResult = CSharpExpression.Evaluate <Vector3, Vector3, bool> (definition.expression, obj.transform.position, obj2.transform.position, "arg1", "arg2", null); } catch (Exception e) { UnityEngine.Debug.Log(e.Message); } break; case "Rotation": try { evaluationResult = CSharpExpression.Evaluate <Vector3, Vector3, bool> (definition.expression, obj.transform.rotation.eulerAngles, obj2.transform.rotation.eulerAngles, "arg1", "arg2", null); } catch (Exception e) { UnityEngine.Debug.Log(e.Message); } break; case "Scale": try { evaluationResult = CSharpExpression.Evaluate <Vector3, Vector3, bool> (definition.expression, obj.transform.localScale, obj2.transform.localScale, "arg1", "arg2", null); } catch (Exception e) { UnityEngine.Debug.Log(e.Message); } break; case "Mass": try { Rigidbody rigidBody = obj.GetComponent <Rigidbody> (); Rigidbody rigidBody2 = obj2.GetComponent <Rigidbody> (); if (rigidBody != null && rigidBody2 != null) { evaluationResult = CSharpExpression.Evaluate <float, float, bool> (definition.expression, rigidBody.mass, rigidBody2.mass, "arg1", "arg2", null); } } catch (Exception e) { UnityEngine.Debug.Log(e.Message); } break; case "Self": try { evaluationResult = CSharpExpression.Evaluate <GameObject, GameObject, bool> (definition.expression, obj, obj2, "arg1", "arg2", null); } catch (Exception e) { UnityEngine.Debug.Log(e.Message); } break; default: try { //Get custom argument info string[] customArgument = definition.argsType.Split('/'); string componentName = customArgument[0]; string fieldName = customArgument[1]; //Get arguments from component Component component = obj.GetComponent(GetType(componentName)); Component component2 = obj2.GetComponent(GetType(componentName)); if (component != null) { Type t; object arg1; object arg2; FieldInfo field = component.GetType().GetField(fieldName); if (field == null) { PropertyInfo property = component.GetType().GetProperty(fieldName); arg1 = property.GetValue(component, null); t = property.PropertyType; } else { arg1 = field.GetValue(component); t = field.FieldType; } FieldInfo field2 = component2.GetType().GetField(fieldName); if (field2 == null) { PropertyInfo property = component2.GetType().GetProperty(fieldName); arg2 = property.GetValue(component2, null); } else { arg2 = field.GetValue(component2); } if (t == typeof(string)) { evaluationResult = CSharpExpression.Evaluate <string, string, bool>(definition.expression, (string)arg1, (string)arg2, "arg", null); } else if (t == typeof(int)) { evaluationResult = CSharpExpression.Evaluate <int, int, bool>(definition.expression, (int)arg1, (int)arg2, "arg", null); } else if (t == typeof(float)) { evaluationResult = CSharpExpression.Evaluate <float, float, bool>(definition.expression, (float)arg1, (float)arg2, "arg", null); } else if (t == typeof(double)) { evaluationResult = CSharpExpression.Evaluate <double, double, bool>(definition.expression, (double)arg1, (double)arg2, "arg", null); } else if (t == typeof(bool)) { evaluationResult = CSharpExpression.Evaluate <bool, bool, bool>(definition.expression, (bool)arg1, (bool)arg2, "arg", null); } else if (t == typeof(Vector2)) { evaluationResult = CSharpExpression.Evaluate <Vector2, Vector2, bool>(definition.expression, (Vector2)arg1, (Vector2)arg2, "arg", null); } else if (t == typeof(Vector3)) { evaluationResult = CSharpExpression.Evaluate <Vector3, Vector3, bool>(definition.expression, (Vector3)arg1, (Vector3)arg2, "arg", null); } else if (t == typeof(Color)) { evaluationResult = CSharpExpression.Evaluate <Color, Color, bool>(definition.expression, (Color)arg1, (Color)arg2, "arg", null); } } } catch (Exception e) { UnityEngine.Debug.Log(e.Message); } break; } //If result of expression evaluation is positive, add a positive fact about the object(s) or change sign if it exists yet if (evaluationResult == true) { if (existingFact == null) { Atom newFact = new Atom(factName); newFact.addTerm(new Term(obj)); newFact.addTerm(new Term(obj2)); agent.state.addFact(newFact); } else { existingFact.sign = true; } } //Otherwise, add a negative fact, or change sign if fact exists yet else { if (existingFact == null) { Atom newFact = new Atom(factName, false); newFact.addTerm(new Term(obj)); newFact.addTerm(new Term(obj2)); agent.state.addFact(newFact); } else { existingFact.sign = false; } } } } } //If it is a property, just check object data else { bool evaluationResult = false; Atom existingFact = agent.state.facts.Find((Atom fact) => fact.name == factName && fact.terms[0].value.Equals(obj)); switch (definition.argsType) { case "Position": try { evaluationResult = CSharpExpression.Evaluate <Vector3, bool> (definition.expression, obj.transform.position, "arg", null); } catch (Exception e) { UnityEngine.Debug.Log(e.Message); } break; case "Rotation": try { evaluationResult = CSharpExpression.Evaluate <Vector3, bool> (definition.expression, obj.transform.rotation.eulerAngles, "arg", null); } catch (Exception e) { UnityEngine.Debug.Log(e.Message); } break; case "Scale": try { evaluationResult = CSharpExpression.Evaluate <Vector3, bool> (definition.expression, obj.transform.localScale, "arg", null); } catch (Exception e) { UnityEngine.Debug.Log(e.Message); } break; case "Mass": try { Rigidbody rigidBody = obj.GetComponent <Rigidbody> (); if (rigidBody != null) { evaluationResult = CSharpExpression.Evaluate <float, bool> (definition.expression, rigidBody.mass, "arg", null); } } catch (Exception e) { UnityEngine.Debug.Log(e.Message); } break; default: try { //Get custom argument info string[] customArgument = definition.argsType.Split('/'); string componentName = customArgument[0]; string fieldName = customArgument[1]; //Get argument from component Component component = obj.GetComponent(GetType(componentName)); if (component != null) { Type t; object arg; FieldInfo field = component.GetType().GetField(fieldName); if (field == null) { PropertyInfo property = component.GetType().GetProperty(fieldName); arg = property.GetValue(component, null); t = property.PropertyType; } else { arg = field.GetValue(component); t = field.FieldType; } if (t == typeof(string)) { evaluationResult = CSharpExpression.Evaluate <string, bool>(definition.expression, (string)arg, "arg", null); } else if (t == typeof(int)) { evaluationResult = CSharpExpression.Evaluate <int, bool>(definition.expression, (int)arg, "arg", null); } else if (t == typeof(float)) { evaluationResult = CSharpExpression.Evaluate <float, bool>(definition.expression, (float)arg, "arg", null); } else if (t == typeof(double)) { evaluationResult = CSharpExpression.Evaluate <double, bool>(definition.expression, (double)arg, "arg", null); } else if (t == typeof(bool)) { evaluationResult = CSharpExpression.Evaluate <bool, bool>(definition.expression, (bool)arg, "arg", null); } else if (t == typeof(Vector2)) { evaluationResult = CSharpExpression.Evaluate <Vector2, bool>(definition.expression, (Vector2)arg, "arg", null); } else if (t == typeof(Vector3)) { evaluationResult = CSharpExpression.Evaluate <Vector3, bool>(definition.expression, (Vector3)arg, "arg", null); } else if (t == typeof(Color)) { evaluationResult = CSharpExpression.Evaluate <Color, bool>(definition.expression, (Color)arg, "arg", null); } } } catch (Exception e) { UnityEngine.Debug.Log(e.Message); } break; } //If result of expression evaluation is positive, add a positive fact about the object(s), or change sign to existing if (evaluationResult == true) { if (existingFact == null) { Atom newFact = new Atom(factName); newFact.addTerm(new Term(obj)); agent.state.addFact(newFact); } else { existingFact.sign = true; } } //Otherwise, add a negative fact, or change sign to existing else { if (existingFact == null) { Atom newFact = new Atom(factName, false); newFact.addTerm(new Term(obj)); agent.state.addFact(newFact); } else { existingFact.sign = false; } } } }