public static object ReadComponent(GameObject gameObject, string entire_name, string st, Type gOType, object obj, ReadSimpleProperty ReadSimpleProperty) { ////Debug.unityLogger.logEnabled = false; string parentName = st.Substring(0, st.IndexOf("^")); string child = st.Substring(st.IndexOf("^") + 1, st.Length - st.IndexOf("^") - 1); MyDebugger.MyDebug("component " + entire_name + " parent " + parentName + " child " + child + " goType " + gOType); if (gOType == typeof(GameObject)) { MyDebugger.MyDebug(gameObject.name + " is the GO"); foreach (Component c in gameObject.GetComponents(typeof(MonoBehaviour))) { if (c.GetType().Name.Equals(parentName)) { MyDebugger.MyDebug("component " + c); if (!child.Contains("^")) { MyDebugger.MyDebug(" of type " + c.GetType()); return(ReadSimpleProperty(child, c.GetType(), c)); } else { return(ReadComposedProperty(gameObject, entire_name, child, c.GetType(), c, ReadSimpleProperty)); } } } } return(null); }
private object[] ReadSimpleArrayProperty(string path, string collectionElementProperty, Type type, object obj, int i, int j) { object[] matrixValue = SensorsUtility.GetArrayProperty(path, collectionElementProperty, type, obj, i, j);//matrixValue[0] is the actual matrix, matrixValue[1] is the Property object[] toReturn = new object[4]; if (matrixValue[0] != null && matrixValue[1] != null) { MyDebugger.MyDebug("READING THE MATRIX!"); FieldOrProperty toRead = (FieldOrProperty)matrixValue[1]; Array matrix = (Array)matrixValue[0]; toReturn[0] = toRead.GetValue(matrix.GetValue(i, j)).GetType(); //Property type of the element of the collection toReturn[1] = matrix.GetLength(0); toReturn[2] = matrix.GetLength(1); toReturn[3] = matrix.GetValue(i, j).GetType(); //Collection Element Type MyDebugger.MyDebug("LENGTH " + toReturn[1] + " " + toReturn[2]); return(toReturn); } else if (matrixValue[0] != null) { toReturn[1] = 0; toReturn[2] = 0; toReturn[3] = matrixValue[0].GetType().GetGenericArguments()[0]; foreach (FieldOrProperty member in ReflectionExecutor.GetFieldsAndProperties(toReturn[3])) { if (member.Name().Equals(currentSubProperty)) { toReturn[0] = member.Type(); return(toReturn); } } } return(null); }
public static object ReadComposedProperty(GameObject gameObject, string entire_name, string st, Type objType, object obj, ReadSimpleProperty ReadSimpleProperty) { ////Debug.unityLogger.logEnabled = false; string parentName = st.Substring(0, st.IndexOf("^")); string child = st.Substring(st.IndexOf("^") + 1, st.Length - st.IndexOf("^") - 1); MemberInfo[] members = objType.GetMember(parentName, SensorsUtility.BindingAttr); MyDebugger.MyDebug("members with name " + parentName + " " + members.Length); if (members.Length == 0) { return(ReadComponent(gameObject, entire_name, st, objType, obj, ReadSimpleProperty)); } FieldOrProperty parentProperty = new FieldOrProperty(members[0]); ///MyDebugger.MyDebug(parentProperty.Name()); object parent = parentProperty.GetValue(obj); Type parentType = parent.GetType(); if (!child.Contains("^")) { return(ReadSimpleProperty(child, parentType, parent)); } else { return(ReadComposedProperty(gameObject, entire_name, child, parentType, parent, ReadSimpleProperty)); } }
// Update is called once per frame void Update() { step++; if (!ready) { return; } List <int> newSizes = new List <int>(); foreach (string property in sizeToTrack.Keys.ToList()) { currentPropertyType = typeForCollectionProperty[property]; foreach (string currentElement in elementForCollectionProperty[property]) { newSizes = new List <int>(); currentSubProperty = currentElement; MyDebugger.MyDebug("checking " + property + " sub " + currentSubProperty + " " + currentPropertyType); object[] result = ReadProperty(property); //MyDebugger.MyDebug(result.Length); //MyDebugger.MyDebug("Collection type "+result[result.Length - 1]); //MyDebugger.MyDebug("Collection length "+result[1]); //MyDebugger.MyDebug("Property type " + result[0]); //MyDebugger.MyDebug("Old size dimensions: " + sizeToTrack[property].Count); for (int i = 0; i < sizeToTrack[property].Count; i++) { newSizes.Add((int)result[i + 1]); //MyDebugger.MyDebug("property " + property + " element " + currentSubProperty); //MyDebugger.MyDebug("new size is "+newSizes[i]+" old one is "+ sizeToTrack[property][i]); if (newSizes[i] > sizeToTrack[property][i]) { if (currentPropertyType.Equals("LIST")) { enlargedList(property, newSizes[i], (Type)result[0], (Type)result[2]); } else if (currentPropertyType.Equals("ARRAY2")) { enlargedArray2(property, i, newSizes[i], (Type)result[0], (Type)result[3]); } } //MyDebugger.MyDebug("Still alive at " + i); } //MyDebugger.MyDebug("New size dimensions: " + newSizes.Count); } sizeToTrack[property] = newSizes; } if (brain.debug) { if (step % 20 == 0 && gameObject.name.Equals("Brain1")) { //Debug.Break(); } } }
public static IEnumerable <IMonoBehaviourSensor> GetSensors(Brain brain, object lockOn) { if (sensorsUpdatedCount(brain) != instantiatedSensorsCount(brain)) { // MyDebugger.MyDebug("I'm waiting since " + sensorsUpdatedCount(brain) + "<>" + instantiatedSensorsCount(brain),brain.debug); MyDebugger.MyDebug(Thread.CurrentThread.Name + " is going to wait"); Monitor.Wait(lockOn); MyDebugger.MyDebug(Thread.CurrentThread.Name + "is going to execute"); } return(getInstantiatedSensors(brain)); }
private IEnumerator pulseOn() { while (true) { yield return(new WaitUntil(() => (bool)reasonerMethod.Invoke(triggerClass, null))); lock (toLock) { solverWaiting = false; MyDebugger.MyDebug("Pulsing in brain"); Monitor.Pulse(toLock); } } }
public object[] ReadProperty(string _path) { object[] toReturn = null; if (!_path.Contains("^")) { MyDebugger.MyDebug("SIMPLE PROPERTY"); toReturn = new object[1]; toReturn[0] = ReadSimpleValueProperty(_path, typeof(GameObject), gameObject); } else { MyDebugger.MyDebug("COMPOSED PROPERTY"); toReturn = (object[])SensorsUtility.ReadComposedProperty(gameObject, _path, _path, typeof(GameObject), gameObject, ReadSimplePropertyMethod); } return(toReturn); }
public List <IMonoBehaviourSensor> generateSensors() { List <IMonoBehaviourSensor> generatedSensors = new List <IMonoBehaviourSensor>(); foreach (SensorConfiguration conf in configurations) { foreach (string property in conf.properties.Distinct()) { MyDebugger.MyDebug("ADDING SENSOR FOR " + property + " TO " + gameObject.name); object[] result = null; Type type = null; currentSubProperty = ""; int currentOperationPerProperty = 0; currentPropertyType = "VALUE";//VALUE, ARRAY2, LIST foreach (SimpleGameObjectsTracker tracker in conf.advancedConf) { if (tracker.propertyName.Equals(property)) { currentPropertyType = tracker.propertyType; foreach (string subproperty in tracker.toSave) { MyDebugger.MyDebug("SUBPROPERTY " + subproperty); currentSubProperty = subproperty; result = ReadProperty(property); configureSensor(result, type, conf, property, currentOperationPerProperty, generatedSensors); } break; } } if (currentPropertyType.Equals("VALUE")) { result = ReadProperty(property); configureSensor(result, type, conf, property, currentOperationPerProperty, generatedSensors); foreach (StringIntPair pair in conf.operationPerProperty) { if (pair.Key.Equals(property)) { currentOperationPerProperty = pair.Value; break; } } } } } ready = true; return(generatedSensors); }
void OnEnable() { myScript = target as Brain; var triggerClass = ScriptableObject.CreateInstance("Trigger"); MethodInfo[] methods = triggerClass.GetType().GetMethods(); foreach (MethodInfo mI in methods) { if (mI.ReturnType == typeof(bool)) { methodsToShow.Add(mI.Name); MyDebugger.MyDebug(mI.Name); } } methodsToShowForReasoner.Add("When Sensors are ready"); methodsToShowForReasoner.AddRange(methodsToShow); methodsToShowForActuators.Add("Always"); methodsToShowForActuators.Add("Never"); methodsToShowForActuators.AddRange(methodsToShow); for (int i = 0; i < methodsToShow.Count; i++) { if (myScript.updateSensorsOn.Equals(methodsToShow[i])) { sensorsUpdateIndex = i; break; } } for (int i = 0; i < methodsToShowForReasoner.Count; i++) { if (myScript.executeReasonerOn.Equals(methodsToShowForReasoner[i])) { reasoningExecutionIndex = i; break; } } for (int i = 0; i < methodsToShowForActuators.Count; i++) { if (myScript.applyActuatorsCondition.Equals(methodsToShowForActuators[i])) { applyActuatorsIndex = i; break; } } }
public override void OnInspectorGUI() { try { if (!objectMode) { base.OnInspectorGUI(); } else { drawObjectProperties(); } }catch (Exception e) { MyDebugger.MyDebug(e.StackTrace); reset(true); } }
private void ReadSimpleListProperty(string path, Type type, object obj) { object[] listValue = SensorsUtility.GetListProperty(path, collectionElementProperty, type, obj, indexes[0]); if (listValue[0] != null && listValue[1] != null) { FieldOrProperty toRead = (FieldOrProperty)listValue[1]; IList list = (IList)listValue[0]; propertyValues.Add((T)Convert.ChangeType(toRead.GetValue(list[indexes[0]]), typeof(T))); } else { MyDebugger.MyDebug("Destroing " + path); SensorsManager.GetInstance().removeSensor(brain, this); Destroy(this); return; } }
internal void deleteConfiguration(string v) { MyDebugger.MyDebug("deleting " + v); int i = 0; for (; i < gOConfigurations.Count; i++) { if (gOConfigurations[i].configurationName.Equals(v)) { break; } } if (i < gOConfigurations.Count) { MyDebugger.MyDebug(gOConfigurations[i].configurationName); onDeleting(gOConfigurations[i]); gOConfigurations.RemoveAt(i); localNames.Remove(v); } manager.delete(v); }
public object[] ReadSimplePropertyMethod(string path, Type type, object obj) { MyDebugger.MyDebug("SWITCHING ON " + path); object[] toReturn = null; if (currentPropertyType.Equals("VALUE")) { MyDebugger.MyDebug("VALUE PROPERTY"); toReturn = new object[1]; toReturn[0] = ReadSimpleValueProperty(path, type, obj); } else if (currentPropertyType.Equals("ARRAY2")) { MyDebugger.MyDebug("ARRAY2 PROPERTY"); toReturn = ReadSimpleArrayProperty(path, currentSubProperty, type, obj, 0, 0); } else if (currentPropertyType.Equals("LIST")) { MyDebugger.MyDebug("LIST PROPERTY"); toReturn = ReadSimpleListProperty(path, currentSubProperty, type, obj, 0); } return(toReturn); }
private IMonoBehaviourSensor addSensor(string confName, string property, int currentOperationPerProperty, Type type) { MyDebugger.MyDebug("ACTUALLY ADDING COMPONENT TO " + gameObject.name); sensorsAdded++; MyDebugger.MyDebug("Added " + sensorsAdded + "sensors to " + gameObject.name); Component component = gameObject.AddComponent(SensorsUtility.actualMonoBehaviourSensor[type]); //MyDebugger.MyDebug("component " + component); component.hideFlags = HideFlags.HideInInspector; IMonoBehaviourSensor sensor = component as IMonoBehaviourSensor; if (!sensor.ready) { sensor.propertyType = currentPropertyType; sensor.path = property; sensor.sensorName = confName; sensor.operationType = currentOperationPerProperty; sensor.brain = brain; if (executeRepeteadly) { sensor.executeRepeteadly = executeRepeteadly; sensor.frequency = frequence; } else { sensor.triggerClass = triggerClass; sensor.updateMethod = updateMethod; } } /*MyDebugger.MyDebug("adding " + sensor.path); * if (sensor.indexes.Count > 0) * { * MyDebugger.MyDebug(" index " + sensor.indexes[0]); * } */ return(sensor); }
private void prepareActuators(List <IMonoBehaviourSensor> sensors, List <SimpleActuator> actuators, MethodInfo[] methods) { foreach (ActuatorConfiguration conf in actuatorsConfigurations) { actuators.Add(new SimpleActuator(conf)); ////MyDebugger.MyDebug(conf.configurationName+" added"); } actuatorsManager.registerActuators(this, actuators); if (!actuatorsManager.applyCoroutinStarted) { StartCoroutine(actuatorsManager.applyActuators()); actuatorsManager.applyCoroutinStarted = true; } foreach (MethodInfo mI in methods) { if (mI.Name.Equals(applyActuatorsCondition)) { MyDebugger.MyDebug("apply actuators on " + mI.Name); applyActuatorsMethod = mI; } } }
override public void OnInspectorGUI() { //MyDebugger.MyDebug("manager "+manager); try { GUILayout.Label(typeOfConfigurator + " Configurator", EditorStyles.boldLabel); chosenGO = configurator.gameObject.name; if (showingConfigurations) { showConfigurations(); if (configuringConfiguration) { tracker.updateDataStructures(null, configuration); } } if (chosingNewConfigurationName) { choseNewConfigurationName(); if (configuringConfiguration && !(configuration is null)) { tracker.updateDataStructures(null, configuration); } } if (configuringConfiguration) { configure(); } }catch (Exception e) { MyDebugger.MyDebug(e.Message); MyDebugger.MyDebug(e.StackTrace); reset(true); } }
protected void addSubProperties(object obj, string name, object objOwner) { if (tracker.ObjectsOwners.ContainsKey(obj) && !obj.GetType().IsValueType&& (!tracker.ObjectsOwners[obj].Key.Equals(objOwner) || !tracker.ObjectsOwners[obj].Value.Equals(name)) || obj.Equals(tracker.GO)) { EditorGUI.BeginDisabledGroup(true); if (obj.Equals(tracker.GO)) { EditorGUILayout.ToggleLeft("This is " + chosenGO + " object", true); } else if (!tracker.ObjectsOwners[obj].Value.Equals(name)) { EditorGUILayout.ToggleLeft("object already listed as " + tracker.ObjectsOwners[obj].Value, true); } else { EditorGUILayout.ToggleLeft("object already listed in an upper level", true); } EditorGUI.EndDisabledGroup(); return; } if (!tracker.ObjectsToggled.ContainsKey(obj)) { tracker.ObjectsToggled.Add(obj, false); } if (!tracker.ObjectsProperties.ContainsKey(obj)) { tracker.updateDataStructures(obj, null, null); } foreach (FieldOrProperty f in tracker.ObjectsProperties[obj].Values) { bool disabled = tracker.ObjectDerivedFromFields[obj][f.Name()] == null; EditorGUI.BeginDisabledGroup(disabled); if (isMappable(f)) { EditorGUILayout.BeginHorizontal(); tracker.ObjectsToggled[f] = EditorGUILayout.ToggleLeft(f.Name(), tracker.ObjectsToggled[f]); addCustomFields(f); if (tracker.ObjectsToggled[f] && !tracker.IsBaseType(f)) { bool configure = GUILayout.Button("Configure Object"); if (configure) { objectMode = true; objectToConfigure = f; helpScroll = new Vector2(0, 0); //MyDebugger.MyDebug("num: "+tracker.basicTypeCollectionsConfigurations.Count); if (!tracker.basicTypeCollectionsConfigurations.ContainsKey(f)) { //MyDebugger.MyDebug("f " + tracker.basicTypeCollectionsConfigurations[f]); MyDebugger.MyDebug("adding simple tracker for " + objectToConfigure.Name() + " that is a " + objectToConfigure.Type()); tracker.basicTypeCollectionsConfigurations.Add(f, new SimpleGameObjectsTracker(objectToConfigure.Type())); } tracker.basicTypeCollectionsConfigurations[f].getBasicProperties(); drawObjectProperties(); } } EditorGUILayout.EndHorizontal(); } else { tracker.ObjectsToggled[f] = EditorGUILayout.Foldout(tracker.ObjectsToggled[f], f.Name()) && !disabled; if (tracker.ObjectsToggled[f]) { EditorGUI.indentLevel++; addSubProperties(tracker.ObjectDerivedFromFields[obj][f.Name()], f.Name(), obj); EditorGUI.indentLevel--; } } EditorGUI.EndDisabledGroup(); } }
protected void addProperties() { using (var h = new EditorGUILayout.VerticalScope()) { using (var scrollView = new EditorGUILayout.ScrollViewScope(mainScroll)) { GameObject gO = tracker.GO; foreach (FieldOrProperty obj in tracker.ObjectsProperties[gO].Values) { bool disabled = tracker.ObjectDerivedFromFields[gO][obj.Name()] == null; EditorGUI.BeginDisabledGroup(disabled); if (isMappable(obj)) { EditorGUILayout.BeginHorizontal(); tracker.ObjectsToggled[obj] = EditorGUILayout.ToggleLeft(obj.Name(), tracker.ObjectsToggled[obj]); addCustomFields(obj); if (tracker.ObjectsToggled[obj] && !tracker.IsBaseType(obj)) { bool configure = GUILayout.Button("Configure Object"); if (configure) { objectMode = true; objectToConfigure = obj; helpScroll = new Vector2(0, 0); if (!tracker.basicTypeCollectionsConfigurations.ContainsKey(obj)) { MyDebugger.MyDebug("adding simple tracker for " + objectToConfigure.Name() + " that is a " + objectToConfigure.Type()); tracker.basicTypeCollectionsConfigurations.Add(obj, new SimpleGameObjectsTracker(objectToConfigure.Type())); } tracker.basicTypeCollectionsConfigurations[obj].getBasicProperties(); drawObjectProperties(); } } EditorGUILayout.EndHorizontal(); } else { tracker.ObjectsToggled[obj] = EditorGUILayout.Foldout(tracker.ObjectsToggled[obj], obj.Name()) && !disabled; if (tracker.ObjectsToggled[obj]) { EditorGUI.indentLevel++; addSubProperties(tracker.ObjectDerivedFromFields[gO][obj.Name()], obj.Name(), gO); EditorGUI.indentLevel--; } } EditorGUI.EndDisabledGroup(); } foreach (Component c in tracker.GOComponents[gO]) { //MyDebugger.MyDebug(c); tracker.ObjectsToggled[c] = EditorGUILayout.Foldout(tracker.ObjectsToggled[c], c.GetType().ToString()); if (tracker.ObjectsToggled[c]) { EditorGUI.indentLevel++; addSubProperties(c, c.GetType().ToString(), gO); EditorGUI.indentLevel--; } } mainScroll = scrollView.scrollPosition; } } }
public void Run() { reason = true; IMapper sensorMapper = mapper.getMapper(typeof(AdvancedSensor)); while (reason) { if (!brain.executeReasonerOn.Equals("When Sensors are ready")) { lock (brain.toLock) { MyDebugger.MyDebug("going to wait for pulse by brain"); brain.solverWaiting = true; Monitor.Wait(brain.toLock); } } try { factsPath = Path.GetTempFileName(); using (StreamWriter fs = new StreamWriter(factsPath, true)) { string toAppend = SensorsManager.GetSensorsMapping(brain); if (!reason) { return; } fs.Write(toAppend); fs.Close(); } } catch (Exception e) { MyDebugger.MyDebug("CAUGHT EXECPTION!!!!"); MyDebugger.MyDebug(e.Message); MyDebugger.MyDebug(e.StackTrace); } Handler handler = new DesktopHandler(new DLV2DesktopService(@".\lib\dlv2.exe")); //With DLV2DesktopService I get a Error during parsing: --> Invalid #show directive: setOnActuator/1--competition-output. //With DLVDesktopService the AS, obviously, are wrongly parsed InputProgram encoding = new ASPInputProgram(); MyDebugger.MyDebug("adding encoding"); encoding.AddFilesPath(Path.GetFullPath(brain.ASPFilePath)); InputProgram facts = new ASPInputProgram(); MyDebugger.MyDebug("adding facts"); facts.AddFilesPath(factsPath); handler.AddProgram(encoding); handler.AddProgram(facts); handler.AddOption(new OptionDescriptor("--filter=setOnActuator/1 ")); stopwatch.Restart(); MyDebugger.MyDebug("starting sync"); Output o = handler.StartSync(); if (!o.ErrorsString.Equals("")) { MyDebugger.MyDebug(o.ErrorsString + " " + o.OutputString); } AnswerSets answers = (AnswerSets)o; stopwatch.Stop(); brain.asSteps++; brain.asTotalMS += stopwatch.ElapsedMilliseconds; MyDebugger.MyDebug("num of AS " + answers.Answersets.Count); if (answers.Answersets.Count > 0) { lock (brain.toLock) { foreach (SimpleActuator actuator in brain.getActuators()) { actuator.parse(answers.Answersets[0]); } brain.setActuatorsReady(true); } } if (!brain.maintainFactFile) { File.Delete(factsPath); } } }
private void configureSensor(object[] result, Type type, SensorConfiguration conf, string property, int currentOperationPerProperty, List <IMonoBehaviourSensor> generatedSensors) { //MyDebugger.MyDebug("configuring sensors for " + property); if (result != null) { type = (Type)result[0]; } if (type != null && currentPropertyType.Equals("VALUE")) { IMonoBehaviourSensor sensor = addSensor(conf.name, property, currentOperationPerProperty, type); //MyDebugger.MyDebug("AND IT'S VALUE"); sensor.done(); generatedSensors.Add(sensor); } else if (type != null) { List <int> currentSize = new List <int>(); if (currentPropertyType.Equals("LIST")) { //MyDebugger.MyDebug("AND IT'S LIST"); currentSize.Add((int)result[1]); for (int i = 0; i < (int)result[1]; i++) { IMonoBehaviourSensor sensor = addSensor(conf.name, property, currentOperationPerProperty, type); sensor.indexes.Add(i); sensor.collectionElementProperty = currentSubProperty; sensor.collectionElementType = ((Type)result[2]).Name; sensor.done(); generatedSensors.Add(sensor); } } else if (currentPropertyType.Equals("ARRAY2")) { MyDebugger.MyDebug("AND IT'S ARRAY2"); currentSize.Add((int)result[1]); currentSize.Add((int)result[2]); for (int i = 0; i < (int)result[1]; i++) { for (int j = 0; j < (int)result[2]; j++) { IMonoBehaviourSensor sensor = addSensor(conf.name, property, currentOperationPerProperty, type); sensor.indexes.Add(i); sensor.indexes.Add(j); sensor.collectionElementProperty = currentSubProperty; sensor.collectionElementType = ((Type)result[3]).Name; sensor.done(); generatedSensors.Add(sensor); } } } if (!sizeToTrack.ContainsKey(property)) { //MyDebugger.MyDebug("property " + property); //MyDebugger.MyDebug("size " + currentSize.Count); sizeToTrack.Add(property, currentSize); typeForCollectionProperty.Add(property, currentPropertyType); elementForCollectionProperty.Add(property, new List <string>()); sensorNameForCollectionProperty.Add(property, conf.name); } elementForCollectionProperty[property].Add(currentSubProperty); } }