Skip to content

Commit

Permalink
Implemented fuctionallity for extracting independent equation systems…
Browse files Browse the repository at this point in the history
… from the equation system recieved from the server.
  • Loading branch information
plantblock2 committed May 19, 2022
1 parent 292d667 commit 8df3dcf
Show file tree
Hide file tree
Showing 12 changed files with 1,562 additions and 741 deletions.
1,583 changes: 996 additions & 587 deletions Assets/Scenes/FactorySceneRefactor.unity

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Assets/Scripts/Communication/Knowledge/Fact.cs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ public override bool Equals(System.Object obj)
}
else
{
CogwheelEqsysFact p = (CogwheelEqsysFact)obj;
CogChainEqsysFact p = (CogChainEqsysFact)obj;
return this.CogwheelIds.Equals(p.CogwheelIds);
}
}
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/Gadgets/ChainPlacementToolOld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ void CheckMouseButtons()

// ChainFact newFact = new ChainFact(chnId, cogIds, orientatins, GameState.Facts);
ChainFact newFact = null;
newFact.Representation = newChain;
//newFact.Representation = newChain;
GameState.Facts.Insert(chnId, newFact);
UnityEngine.Debug.Log("Successfully added new ChainFact with backendUri: " + newFact.backendURI);

Expand Down Expand Up @@ -308,7 +308,7 @@ void CheckMouseButtons()

//ChainFact newFact = new ChainFact(chnId, cogIds, orientatins, GameState.Facts);
ChainFact newFact = null;
newFact.Representation = newChain;
//newFact.Representation = newChain;
GameState.Facts.Insert(chnId, newFact);
UnityEngine.Debug.Log("Successfully added new ChainFact with backendUri: " + newFact.backendURI);

Expand Down
4 changes: 3 additions & 1 deletion Assets/Scripts/General/CommunicationEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public class AnimationEvent : UnityEvent<GameObject>{}
public class SimulationEvent : UnityEvent<bool, KnowledgeBasedBehaviour, float> {}

public class EquationSystemEvent : UnityEvent<string[], int, int, string> {}

public class EquationSystemsEvent : UnityEvent<List<string[]>, List<int>, List<int>, List<string>> {}

public static SignalEvent closeUIEvent = new SignalEvent();
public static SignalEvent openUIEvent = new SignalEvent();
public static SignalEvent openPanelEvent = new SignalEvent();
Expand All @@ -25,4 +26,5 @@ public class EquationSystemEvent : UnityEvent<string[], int, int, string> {}
public static SignalEvent generatorOffEvent = new SignalEvent();

public static EquationSystemEvent showEquationSystemEvent = new EquationSystemEvent();
public static EquationSystemsEvent showEquationSystemsEvent = new EquationSystemsEvent();
}
4 changes: 4 additions & 0 deletions Assets/Scripts/General/GameState.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using System;
using System.Collections.Generic;


/**
*
*/
public static class GameState
{
public static bool ServerRunning = false;
Expand Down
5 changes: 5 additions & 0 deletions Assets/Scripts/Refactor/Simulation/Interaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
using System.Collections.Generic;
using UnityEngine;

/**
* class that represents Interactions between simulated objects
* interactions have their own id
* interactions have a associated Fact representing them on the MMT side
*/
public class Interaction
{
protected int id;
Expand Down
511 changes: 380 additions & 131 deletions Assets/Scripts/Refactor/Simulation/KnowlegeBasedSimulationRefactor.cs

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions Assets/Scripts/Refactor/Simulation/SimulatedObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
using System.Collections.Generic;
using UnityEngine;

/**
* simulated objects are all the objects that are either affecting the knowledge based simulation
* or objects that are to be 'simulated' by the knowledge based simulation
* They have
* an id
* a Fact representing the Object on the MMT side
* a GameObject representing the Object in the GameWorld
* a List of 'values of interest' representong the aspects of the object
* that the kowledge based simulation is sopposed to determine
*/
public abstract class SimulatedObject
{
protected int id;
Expand Down Expand Up @@ -44,6 +54,10 @@ public List<ValueOfInterest> getValuesOfInterest()
return valuesOfInterest;
}

/*
* communicate the results of the knowledge based simulation to the object
* and have the object reflect these results in the game world
*/
public abstract void applyValuesOfInterest(Dictionary<ValueOfInterest, float> input);

public abstract void unapplyValuesOfInterest();
Expand Down
48 changes: 30 additions & 18 deletions Assets/Scripts/Refactor/Simulation/Simulation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,46 +46,47 @@ public GearboxSimulation()

public override void startSimulation()
{

List<SimulatedObject> simCogwheels = simulatedObjects.Where(simObj => simObj.GetType().Equals(typeof(SimulatedCogwheel))).ToList();
List<SimulatedObject> simChains = simulatedObjects.Where(simObj => simObj.GetType().Equals(typeof(SimulatedChain))).ToList();
List<SimulatedObject> simShafts = simulatedObjects.Where(simObj => simObj.GetType().Equals(typeof(SimulatedShaft))).ToList();
List<SimulatedObject> simMotors = simulatedObjects.Where(simObj => simObj.GetType().Equals(typeof(SimulatedMotor))).ToList();

List<CogwheelCogwheelInteraction> cogCogInteractions = interactions
//gather the different types of interactions that are relevant for the gearbox-simulation from the list of interactions
List<CogwheelCogwheelInteraction> cogCogInteractions = this.interactions
.Where(interaction => interaction.GetType().Equals(typeof(CogwheelCogwheelInteraction)))
.Cast<CogwheelCogwheelInteraction>().ToList();
Debug.Log("cogCogInteractions count: " + cogCogInteractions.Count);
List<CogwheelChainInteraction> cogChainInteractions = interactions
List<CogwheelChainInteraction> cogChainInteractions = this.interactions
.Where(interaction => interaction.GetType().Equals(typeof(CogwheelChainInteraction)))
.Cast<CogwheelChainInteraction>().ToList();
Debug.Log("cogChainInteractions count: " + cogChainInteractions.Count);
List<ShaftCogwheelInteraction> shaftCogInteractions = interactions
List<ShaftCogwheelInteraction> shaftCogInteractions = this.interactions
.Where(interaction => interaction.GetType().Equals(typeof(ShaftCogwheelInteraction)))
.Cast<ShaftCogwheelInteraction>().ToList();
Debug.Log("shaftCogInteractions count: " + shaftCogInteractions.Count);
List<MotorShaftInteraction> motorShaftInteractions = interactions
List<MotorShaftInteraction> motorShaftInteractions = this.interactions
.Where(interaction => interaction.GetType().Equals(typeof(MotorShaftInteraction)))
.Cast<MotorShaftInteraction>().ToList();
Debug.Log("motorShaftInteractions count: " + motorShaftInteractions.Count);

List<Fact> facts = getExistingFacts();
List<ValueOfInterest> valuesOfInterest = simulatedObjects.Select(simObj => simObj.getValuesOfInterest()).ToList().SelectMany(i => i).ToList();

//create a new equation-system fact(and therefore add it to the server)
//(this equation-system fact 'is created' using the different interactions of our gearbox
// and gets simplified by the server into a list of equations that define how the different objects making up the gearbox
// are supposed to behave in relation to each other)
int eqsysId = GameState.simulationHandler.getNextId();
GameState.simulationHandler.activeSimAddEqsys();
new GearboxEqsys2Fact(eqsysId, cogCogInteractions, cogChainInteractions, shaftCogInteractions, motorShaftInteractions);


//retrieve a list of all simplified facts in the servers situation-space
List<SimplifiedFact> sfacts = listSimplifiedFacts();

Dictionary<ValueOfInterest, float> newlyDiscoveredVoiMap = KnowlegeBasedSimulationRefactor.knowledgeBasedSimulation(facts, valuesOfInterest, sfacts);
//take the server response and calcualte from it the values of interest
//(the result is a dictionary mapping the Values of interest to their calculated values)
List<ValueOfInterest> valuesOfInterest = simulatedObjects.Select(simObj => simObj.getValuesOfInterest()).ToList().SelectMany(i => i).ToList();
Dictionary<ValueOfInterest, float> newlyDiscoveredVoiMap = KnowlegeBasedSimulationRefactor.knowledgeBasedSimulation(valuesOfInterest, sfacts);

//Debug info
foreach (KeyValuePair<ValueOfInterest, float> voiVal in newlyDiscoveredVoiMap)
{
Debug.Log(voiVal.Key.getName() + ": " + voiVal.Value);
}



//apply the results of the knowlege-based-simulation to the simulated objects
//(and by extension to the game objects representing them in the game world)
foreach (SimulatedObject simObj in simulatedObjects)
{
Dictionary<ValueOfInterest, float> valueOfIntrestValues = new Dictionary<ValueOfInterest, float>();
Expand All @@ -109,6 +110,7 @@ public override void stopSimulation()
}
}

/*
private List<Fact> getExistingFacts()
{
List<Fact> existingFacts = new List<Fact>();
Expand All @@ -122,19 +124,27 @@ private List<Fact> getExistingFacts()
return existingFacts;
}
*/


/*
* method for retrieving a list of all simplified facts within the servers situation-space
*/
private static List<SimplifiedFact> listSimplifiedFacts()
{
//send hhtp request to get the list of simplified facts from the server
UnityWebRequest request = UnityWebRequest.Get(GameSettings.ServerAdress + "/fact/list");
request.method = UnityWebRequest.kHttpVerbGET;
AsyncOperation op = request.SendWebRequest();
//wait for the servers answer
while (!op.isDone) { }
//handle potentioal errors
if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError)
{
Debug.LogWarning(request.error);
return null;
}
//process the response
else
{
string response = request.downloadHandler.text;
Expand All @@ -143,11 +153,13 @@ private static List<SimplifiedFact> listSimplifiedFacts()
}
}

/*
private void debugLogList(List<GameObject> input)
{
foreach (object i in input)
{
Debug.Log(i);
}
}
*/
}
6 changes: 5 additions & 1 deletion Assets/Scripts/Refactor/Simulation/ValueOfInterest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
using UnityEngine;
using static JSONManager;

/**
*
*
*/
public class ValueOfInterest
{
private string name;
private Fact relevantFact;
private Fact relevantFact; //Fact representing the Objec to which this value of interest applies
private string relevantValue; //MMTURI

public ValueOfInterest(string name)
Expand Down
111 changes: 111 additions & 0 deletions Assets/Scripts/Refactor/UI/EquationsDisplayPanel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class EquationsDisplayPanel : MonoBehaviour
{
public GameObject equationsContent;
public GameObject equationsPrefab;
public GameObject rankAText;
public GameObject solutionsText;
public GameObject rankAbText;
public GameObject NumberEquationSystemsText;
public GameObject CurrentEquationSystemText;

private int currentEquationSytem;
private List<string[]> equationsList;
private List<int> ARanksList;
private List<int> AbRanksList;
private List<string> numbersOfsolutions;

// Start is called before the first frame update
void Start()
{
//KnowledgeBasedSimulationPanel should not be visible on startup
Deactivate();

CommunicationEvents.showEquationSystemsEvent.AddListener(showEquationSystems);
}

public void showEquationSystems(List<string[]> equationsList, List<int> ARanksList, List<int> AbRanksList, List<string> numbersOfsolutions)
{
Debug.Log("showEquationSystems called");
this.gameObject.SetActive(true);

this.equationsList = equationsList;
this.ARanksList = ARanksList;
this.AbRanksList = AbRanksList;
this.numbersOfsolutions = numbersOfsolutions;

this.currentEquationSytem = 0;

if (equationsList.Count > 0)
{
NumberEquationSystemsText.GetComponentInChildren<TMP_Text>().text = "Number of Equation Systems: " + equationsList.Count.ToString();
CurrentEquationSystemText.GetComponentInChildren<TMP_Text>().text = "current Equation System: " + "1";

showEquationSystem(equationsList[0], ARanksList[0], AbRanksList[0], numbersOfsolutions[0]);
}

}

private void showEquationSystem(string[] equations, int rankA, int rankAb, string numberOfsolutions)
{

//Clear previous content
foreach (Transform child in equationsContent.transform)
{
GameObject.Destroy(child.gameObject);
}

//Create an object for each equation
foreach (string equation in equations)
{
GameObject equationObj = Instantiate(equationsPrefab);
equationObj.transform.SetParent(equationsContent.transform, false);
equationObj.GetComponentInChildren<TMP_Text>().text = equation;
}

rankAText.GetComponentInChildren<TMP_Text>().text = "Rank(A): " + rankA.ToString();
rankAbText.GetComponentInChildren<TMP_Text>().text = "Rank(A|b): " + rankAb.ToString();
solutionsText.GetComponentInChildren<TMP_Text>().text = "Solutions: " + numberOfsolutions;
}

public void showNextEquationSystem()
{
if (this.currentEquationSytem + 1 < equationsList.Count)
{
this.currentEquationSytem++;

CurrentEquationSystemText.GetComponentInChildren<TMP_Text>().text = "current Equation System: "
+ (this.currentEquationSytem + 1);

showEquationSystem(equationsList[this.currentEquationSytem], ARanksList[this.currentEquationSytem],
AbRanksList[this.currentEquationSytem], numbersOfsolutions[this.currentEquationSytem]);
}
}

public void showPreviousEquationSystem()
{
if (this.currentEquationSytem - 1 >= 0)
{
this.currentEquationSytem--;

CurrentEquationSystemText.GetComponentInChildren<TMP_Text>().text = "current Equation System: "
+ (this.currentEquationSytem + 1);

showEquationSystem(equationsList[this.currentEquationSytem], ARanksList[this.currentEquationSytem],
AbRanksList[this.currentEquationSytem], numbersOfsolutions[this.currentEquationSytem]);
}
}




public void Deactivate()
{
this.gameObject.SetActive(false);
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/Refactor/UI/EquationsDisplayPanel.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8df3dcf

Please sign in to comment.