Skip to content

Commit

Permalink
fix necessary engaged + new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkadiusz Ryszewski committed Jun 11, 2019
1 parent c4d76b1 commit 2720f6d
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 47 deletions.
4 changes: 2 additions & 2 deletions ModelsTests/ForOtherTeam/AlchemyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public void NecessaryEngagedFilemonInBrew()
Query q = Parser.ParseQuery(Tokenizer.Tokenize(query), _parserState);
var result = q.Solve(_parserState.Story);

Assert.False(result);
Assert.True(result);
}

[Test]
Expand All @@ -165,7 +165,7 @@ public void NecessaryEngagedBercikInBrew()
Query q = Parser.ParseQuery(Tokenizer.Tokenize(query), _parserState);
var result = q.Solve(_parserState.Story);

Assert.False(result);
Assert.True(result);
}

[Test]
Expand Down
5 changes: 2 additions & 3 deletions ModelsTests/ForOtherTeam/ElevatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,15 @@ public void NecessaryEngaged_Serwisant_In_Repair_True()
Assert.That(result, Is.True);
}


[Test]
public void PossiblyEngaged_Tomek_In_Repair_False()
public void PossiblyEngaged_Tomek_In_Repair_True()
{
var query = "necessary [tomek] engaged in (repair, [tomek])";

Query q = Parser.ParseQuery(Tokenizer.Tokenize(query), _parserState);
var result = q.Solve(_parserState.Story);

Assert.That(result, Is.False);
Assert.True(result);
}

#endregion
Expand Down
4 changes: 2 additions & 2 deletions ModelsTests/ForOtherTeam/PickUpTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,14 @@ public void PossiblyEngaged_Jack_In_PickUp_True()
}

[Test]
public void NecessaryEngaged_JackAndBob_In_PickUp_False()
public void NecessaryEngaged_JackAndBob_In_PickUp_True()
{
var query = "necessary [jack, bob] engaged in (pickUp, [jack, bob])";

Query q = Parser.ParseQuery(Tokenizer.Tokenize(query), _parserState);
var result = q.Solve(_parserState.Story);

Assert.That(result, Is.False);
Assert.True(result);
}

#endregion
Expand Down
63 changes: 62 additions & 1 deletion ModelsTests/NecessaryEngegedTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ necessary [g2] engaged in (A1, [g1, g2, g3]),(A2, [g2, g3, g4])

var result = q.Solve(expressions);

Assert.IsFalse(result);
Assert.IsTrue(result);
}

[Test]
Expand Down Expand Up @@ -192,5 +192,66 @@ necessary [h] engaged in (buypaper, [g])
Assert.IsFalse(result);
}

[Test]
public void Test_BP_withNotBy()
{
string story = @"
Fluent hasA
Fluent hasB
Agent g
Agent h
Action buypaper
buypaper by [g, h] causes [hasA || hasB]
impossible buypaper by [h]
";
var tokens = Tokenizer.Tokenize(story);
var parserState = Parser.Parse(tokens);
var expressions = parserState.Story;

string query = @"
necessary [h] engaged in (buypaper, [g, h])
";

Query q = Parser.ParseQuery(
Tokenizer.Tokenize(query),
parserState);

var result = q.Solve(expressions);

Assert.IsTrue(result);
}

[Test]
public void Test_3()
{
string story = @"
Fluent a
Fluent b
Agent g
Agent h
Action A1
Action A2
initially [~b]
A1 by [g, h] releases a if [~b]
A2 by [h] causes [b] if [a]
";
var tokens = Tokenizer.Tokenize(story);
var parserState = Parser.Parse(tokens);
var expressions = parserState.Story;

string query = @"
necessary [h] engaged in (A1, [g, h]), (A2, [h]) from [~b && a]
";

Query q = Parser.ParseQuery(
Tokenizer.Tokenize(query),
parserState);

var result = q.Solve(expressions);

Assert.IsTrue(result);
}

}
}
2 changes: 0 additions & 2 deletions ModelsTests/PossiblyEngegedTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ possibly [h] engaged in (buypaper, [g])

var result = q.Solve(expressions);

// TODO: update docummentation - Arek
Assert.IsTrue(result);
}

Expand Down Expand Up @@ -195,7 +194,6 @@ possibly [h] engaged in (buypaper, [g])

var result = q.Solve(expressions);

// TODO: fix - Arek
Assert.IsFalse(result);
}

Expand Down
44 changes: 7 additions & 37 deletions MultiAgentLanguageModels/Queries/NecessaryEngagedFrom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public override bool Solve(ExpressionsList expressions)
//could be done prettier but it's easier to read
List<bool> resultsForEachInitiallState = new List<bool>();

//for each initial state
//for each initiall state
foreach (var initialState in initialStates)
{
HashSet<State> currentStates = new HashSet<State>();
Expand All @@ -51,66 +51,36 @@ public override bool Solve(ExpressionsList expressions)
}
}
}
var necessarilyEngagesAgents = new List<bool>();
//now we iterate through instructions
for (int i = 0; i < Instructions.Count; i++)
{
var action = Instructions[i].Item1;
var agents = Instructions[i].Item2;
QueriedAgents.ForEach(queriedAgent =>
{
if (!agents.Contains(queriedAgent)) agents.Add(queriedAgent);
});
QueriedAgents.ForEach(queriedAgent => agents.Remove(queriedAgent));

HashSet<State> newCurrentStates = new HashSet<State>();
var engagesAgentsInEachState = true;
//for each state in current states we want to move forward in graph
foreach (var currentState in currentStates)
{
var triple = new Triple(action, currentState, agents);

//if we can find good edge in graph
//from currentState, specific action and agents group then
if (res.ContainsKey(triple))
{
//add all next states to the newCurrentStates
res[triple].ToList().ForEach(s => newCurrentStates.Add(s));
}

if (!CheckAllActionsEngageAgents(res, triple))
else
{
engagesAgentsInEachState = false;
resultsForEachInitiallState.Add(false);
}
}
necessarilyEngagesAgents.Add(engagesAgentsInEachState);
// do it again for new action and agents group
//do it again for new action and agents group
currentStates = newCurrentStates;
}
resultsForEachInitiallState.Add(currentStates.Count != 0 && necessarilyEngagesAgents.Any(x => x));
resultsForEachInitiallState.Add(currentStates.Count != 0);
}
return resultsForEachInitiallState.All(x => x);
}

private bool CheckAllActionsEngageAgents(Dictionary<Triple, HashSet<State>> res, Triple triple)
{
var action = triple.Item1;
var state = triple.Item2;
var agents = triple.Item3;
var amountOfActions = res.Where(t =>
{
var resAction = t.Key.Item1;
var resState = t.Key.Item2;
return resAction.Equals(action) && resState.Equals(state);
}).Count();
var amountOfActionsExecutedByAnyOfQueriedAgents = res.Where(t =>
{
var resAction = t.Key.Item1;
var resState = t.Key.Item2;
var resAgents = t.Key.Item3;
var actionEngagesAnyOfAgents = resAgents.Any(agent => QueriedAgents.Contains(agent));
return resAction.Equals(action) && resState.Equals(state) && actionEngagesAnyOfAgents;
}).Count();
return amountOfActionsExecutedByAnyOfQueriedAgents == amountOfActions;
return resultsForEachInitiallState.All(x => !x);
}
}

Expand Down

0 comments on commit 2720f6d

Please sign in to comment.