public ByReleasesIf(Action action, AgentsList agents, Fluent fluent, LogicExpression condition) { Action = action; Agents = agents; Fluent = fluent; Condition = condition; }
public ByCausesIf(Action action, AgentsList agents, LogicExpression result, LogicExpression condition) { A = action; G = agents; Pi = condition; Alpha = result; }
public AgentsControllerUnitTest() { var log = LogManager.GetCurrentClassLogger(); var agentList = new AgentsList(); _controller = new AgentsController(agentList, log); _agentInfo = new AgentInfo( new Random().Next(1, 9999999), new Uri($"http:\\\\192.168.{new Random().Next(0,3)}.{new Random().Next(0,254)}:5000")); }
public List <AgentsList> AgentsGroups() { AgentsList agents; if (Agent is null) { agents = new AgentsList(); } else { agents = new AgentsList(Agent.ToList()); } foreach (Expression ex in this) { if (ex as ByCausesIf != null) { var temp = ex as ByCausesIf; agents.AddRange(temp.G); } else if (ex as ByReleasesIf != null) { var temp = ex as ByReleasesIf; agents.AddRange(temp.Agents); } else if (ex as ObservableAfter != null) { var temp = ex as ObservableAfter; agents.AddRange(temp.Instructions.SelectMany(x => x.Item2)); } else if (ex as After != null) { var temp = ex as After; agents.AddRange(temp.Instructions.SelectMany(x => x.Item2)); } } agents = new AgentsList(agents.Distinct().ToList()); var result = new List <AgentsList>(); for (int i = 0; i < Math.Pow(2, agents.Count); i++) { var binary = Convert.ToString(i, 2).PadLeft(agents.Count, '0').Select(x => x == '1' ? true : false).ToArray(); AgentsList temp = new AgentsList(); for (int j = 0; j < agents.Count; j++) { if (binary[j]) { temp.Add(agents[j]); } } result.Add(temp); } return(result); }
public static AgentsList GetAgentList(ParserState state) { AgentsList al = new AgentsList(); Token open = state.PopToken(); if (open.Name != "[") { open.ThrowException("Expected '[' at the beginning of agents list."); } bool correctSyntax = false; Token close = state.PeepToken(); if (close == null) { open.ThrowException("Expected empty list of agents '[]' or agent name."); } if (close.Name == "]") { state.PopToken(); return(al); } while (state.TokenList.Count > 0) { Token t = state.PopToken(); if (state.Agent.ContainsKey(t.Name)) { al.Add(new Agent(t.Name)); } else { t.ThrowException("Agent name doesn't exist."); } if (state.TokenList.Count == 0) { return(null); } Token x = state.PopToken(); if (x.Name == "]") { correctSyntax = true; break; } if (x.Name != ",") { x.ThrowException("',' should separate agents' names."); } } if (correctSyntax) { return(al); } return(null); }
public AgentsController(AgentsList agentList, ILogger logger) { _logger = logger; try { _agentList = agentList; } catch (Exception e) { _logger.Error(e); } }
private static Instruction GetInstructions(ParserState state, Token previous) { Instruction inst = new Instruction(); do { if (state.PeepToken().Name == ",") { state.PopToken(); } Token open = state.PopToken(); if (open == null || open.Name != "(") { previous.ThrowException("Expected program format: (A1,G1),(A2,G2),...,(An,Gn)"); } Token a = state.PopToken(); if (a == null) { open.ThrowException("Expected action"); } if (a.Name == ")") { return(inst); } if (state.Action.ContainsKey(a.Name) == false) { open.ThrowException("Expected action"); } Token comma = state.PopToken(); if (comma == null || comma.Name != ",") { a.ThrowException("Comma should separate an action and a set of agents"); } MultiAgentLanguageModels.Action a1 = new MultiAgentLanguageModels.Action(a.Name); AgentsList g = GetAgentList(state); Token close = state.PopToken(); if (close == null || close.Name != ")") { a.ThrowException("Expected )"); } inst.Add(new Tuple <MultiAgentLanguageModels.Action, AgentsList>(a1, g)); } while (state.PeepToken() != null && state.PeepToken().Name == ","); return(inst); }
public AgentsList GetAgents() { List <AgentDTO> agents = new List <AgentDTO>(); foreach (var item in ctx.Agents) { AgentDTO buff = new AgentDTO() { Id = item.Id, Endpoint = item.Endpoint, OsType = item.OsType, AgentVersion = item.AgentVersion, IsEnabled = item.IsEnabled }; agents.Add(buff); } AgentsList agentsList = new AgentsList(); agentsList.Agents = agents; return(agentsList); }
public static void ParseKeyword(ParserState state, Token firstToken) { switch (firstToken.Name) { case "initially": LogicElement le = EntryC1(state); Initially st = new Initially(le); state.Expression.Add(st); break; case "noninertial": ParseNoninertial(state, firstToken); break; case "by": Token action = state.TokenList[state.TokenList.Count - 1]; state.TokenList.RemoveAt(state.TokenList.Count - 1); AgentsList al = GetAgentList(state); if (al == null) { firstToken.ThrowException("Expected ']' at the end of agents list."); } Token t = state.PopToken(); if (t == null) { firstToken.ThrowException("Expected 'causes' or 'releases'."); } LogicElement result = null; if (t.Name == "releases") { Token fT = state.PopToken(); if (fT == null) { firstToken.ThrowException("Expected fluent after release."); } else if (!state.Fluent.ContainsKey(fT.Name)) { firstToken.ThrowException("Attempting to use undeclared fluent."); } result = state.Fluent[fT.Name]; } else if (t.Name == "causes") { result = EntryC1(state); } else { t.ThrowException("Expected 'causes' or 'releases'."); } if (t.Name == "releases" && (result is Fluent) == false) { t.ThrowException("Expected fluent after release."); } LogicElement condition = null; Token if_token = state.PeepToken(); if (if_token != null && if_token.Name == "if") { state.PopToken(); condition = EntryC1(state); } if (t.Name == "causes") { if (condition == null) { state.Expression.Add(new MultiAgentLanguageModels.Expressions.ByCauses( new MultiAgentLanguageModels.Action(action.Name), al, result)); } else { state.Expression.Add(new MultiAgentLanguageModels.Expressions.ByCausesIf( new MultiAgentLanguageModels.Action(action.Name), al, result, condition)); } } if (t.Name == "releases") { if (condition == null) { state.Expression.Add(new MultiAgentLanguageModels.Expressions.ByReleases( new MultiAgentLanguageModels.Action(action.Name), al, (Fluent)result)); state.Expression.Add(new MultiAgentLanguageModels.Expressions.ByCauses( new MultiAgentLanguageModels.Action(action.Name), al, new Or(result, new Not(result)))); } else { state.Expression.Add(new MultiAgentLanguageModels.Expressions.ByReleasesIf( new MultiAgentLanguageModels.Action(action.Name), al, (Fluent)result, condition)); state.Expression.Add(new MultiAgentLanguageModels.Expressions.ByCausesIf( new MultiAgentLanguageModels.Action(action.Name), al, new Or(result, new Not(result)), condition)); } } break; case "causes": MultiAgentLanguageModels.Action act = new MultiAgentLanguageModels.Action(state.TokenList[state.TokenList.Count - 1].Name); state.TokenList.RemoveAt(state.TokenList.Count - 1); LogicElement effect = EntryC1(state); Token if_exp = state.PeepToken(); if (if_exp != null && if_exp.Name == "if") { state.PopToken(); LogicElement con = EntryC1(state); state.Expression.Add(new CausesIf(act, effect, con)); } else { state.Expression.Add(new Causes(act, effect)); } break; case "releases": MultiAgentLanguageModels.Action act1 = new MultiAgentLanguageModels.Action(state.TokenList[state.TokenList.Count - 1].Name); state.TokenList.RemoveAt(state.TokenList.Count - 1); Token eff1 = state.PopToken(); if (eff1 == null) { firstToken.ThrowException("Expected fluent after release."); } else if (!state.Fluent.ContainsKey(eff1.Name)) { firstToken.ThrowException("Attempting to use undeclared fluent."); } Token if_expr = state.PeepToken(); if (if_expr != null && if_expr.Name == "if") { state.PopToken(); LogicElement con = EntryC1(state); state.Expression.Add(new ReleasesIf(act1, state.Fluent[eff1.Name], con)); state.Expression.Add(new CausesIf(act1, new Or(state.Fluent[eff1.Name], new Not(state.Fluent[eff1.Name])), con)); } else { state.Expression.Add(new Releases(act1, state.Fluent[eff1.Name])); state.Expression.Add(new Causes(act1, new Or(state.Fluent[eff1.Name], new Not(state.Fluent[eff1.Name])))); } break; case "if": firstToken.ThrowException("Unexpected 'if' token."); break; case "impossible": Token token = state.PopToken(); if (token == null) { firstToken.ThrowException("Expected action name."); } if (!state.Action.ContainsKey(token.Name)) { token.ThrowException("Unknown action name."); } MultiAgentLanguageModels.Action ac = new MultiAgentLanguageModels.Action(token.Name); Token key = state.PopToken(); if (key == null) { firstToken.ThrowException("Expected 'by' or 'if' token."); } AgentsList agentsList = null; if (key.Name == "by") { agentsList = GetAgentList(state); Token cond_st = state.PeepToken(); if (cond_st == null || cond_st.Name != "if") { state.Expression.Add(new ImpossibleBy(ac, agentsList)); } else { state.PopToken(); LogicElement c = EntryC1(state); state.Expression.Add(new ImpossibleByIf(ac, agentsList, c)); } } else if (key.Name == "if") { //Token cond_st = state.PopToken(); //if (cond_st == null || cond_st.Name != "if") //key.ThrowException("Expected if after the list of agents."); LogicElement c = EntryC1(state); state.Expression.Add(new ImpossibleIf(ac, c)); } else { firstToken.ThrowException("Expected 'by' or 'if' token."); } break; case "always": LogicElement cond = EntryC1(state); state.Expression.Add(new Always(cond)); break; case "not": Token act2 = state.TokenList[state.TokenList.Count - 1]; MultiAgentLanguageModels.Action actt = new MultiAgentLanguageModels.Action(act2.Name); state.TokenList.RemoveAt(state.TokenList.Count - 1); Token by = state.PopToken(); if (by == null || by.Name != "by") { firstToken.ThrowException("Expected 'by' after 'not'."); } AgentsList agents = GetAgentList(state); Token if_st = state.PeepToken(); if (if_st != null && if_st.Name == "if") { state.PopToken(); condition = EntryC1(state); foreach (Agent a in agents) { state.Expression.Add(new ImpossibleByIf(actt, new AgentsList() { a }, condition)); Output.Print($"{actt.Name} not by {a.Name} under cond {condition.ToString()}"); } } else { foreach (Agent a in agents) { state.Expression.Add(new ImpossibleBy(actt, new AgentsList() { a })); } } break; case "after": LogicElement observable = EntryC1(state); Token aft = state.PopToken(); if (aft == null || aft.Name != "after") { firstToken.ThrowException("Expected 'after' after logic expression."); } Instruction instr = GetInstructions(state, aft); After after_exp = new After(observable, instr); state.Expression.Add(after_exp); break; case "observable": LogicElement obs = EntryC1(state); Token after = state.PopToken(); if (after == null || after.Name != "after") { firstToken.ThrowException("Expected 'after' after logic expression."); } Instruction inst = GetInstructions(state, after); ObservableAfter obsAfter = new ObservableAfter(obs, inst); state.Expression.Add(obsAfter); break; } }
public ImpossibleByIf(Action action, AgentsList agents, LogicExpression condition) : base(action, agents, new False(), condition) { }
public ImpossibleBy(Action action, AgentsList agents) : base(action, agents, new False(), new True()) { }
public NecessaryEngaged(AgentsList agents, Instruction instructions) : base(agents, instructions, new True()) { }
public NecessaryEngagedFrom(AgentsList agents, Instruction instructions, LogicExpression condition) { QueriedAgents = agents; Instructions = instructions; Condition = condition; }
public NotByIf(Action action, AgentsList agents, LogicExpression condition) { Action = action; Agents = agents; Condition = condition; }
public ByCauses(Action action, AgentsList agents, LogicExpression result) : base(action, agents, result, new True()) { }
public PossiblyEngaged(AgentsList agents, Instruction instructions) : base(agents, instructions, new True()) { }
public ByReleases(Action action, AgentsList agents, Fluent fluent) : base(action, agents, fluent, new True()) { }
public NotBy(Action action, AgentsList agents) : base(action, agents, new True()) { }
public static Query ParseQuery(List <Token> tokenList, ParserState story) { ParserState state = new ParserState(tokenList); state.Action = story.Action; state.Agent = story.Agent; state.Noninertial = story.Noninertial; state.Fluent = story.Fluent; if (tokenList.Count == 0) { throw new Exception("Empty query"); } Token first = state.PopToken(); if (first.Name == "necessary" || first.Name == "possibly") { Token t = state.PopToken(); Token next = state.PeepToken(); if (t == null) { first.ThrowException("Expected: executable, agents list or logic expression."); } if (t.Name == "executable") // necessary executable { if (state.PeepToken() == null) { t.ThrowException("Expected program."); } Instruction inst = GetInstructions(state, t); Token from = state.PopToken(); if (from == null) { if (first.Name == "necessary") { return(new NecessaryExecutable(inst)); } else { return(new PossiblyExecutable(inst)); } } if (from.Name != "from") { t.ThrowException("Expected from after program."); } LogicElement cond = EntryC1(state); if (first.Name == "necessary") { return(new NecessaryExecutableFrom(inst, cond)); } else { return(new PossiblyExecutableFrom(inst, cond)); } } else if (state.Agent.ContainsKey(next.Name)) // necessary engaged { state.TokenList.Insert(0, t); AgentsList agents = GetAgentList(state); Token engaged = state.PopToken(); if (engaged == null || engaged.Name != "engaged") { t.ThrowException("Expected engaged after agents list."); } Token in_token = state.PopToken(); if (in_token == null || in_token.Name != "in") { t.ThrowException("Expected in after engaged."); } Instruction inst = GetInstructions(state, in_token); Token from = state.PopToken(); if (from == null) { if (first.Name == "necessary") { return(new NecessaryEngaged(agents, inst)); } else { return(new PossiblyEngaged(agents, inst)); } } if (from.Name != "from") { t.ThrowException("Expected from after action list."); } LogicElement cond = EntryC1(state); if (first.Name == "necessary") { return(new NecessaryEngagedFrom(agents, inst, cond)); } else { return(new PossiblyEngagedFrom(agents, inst, cond)); } } else if (next != null && (state.Fluent.ContainsKey(next.Name) || state.Noninertial.ContainsKey(next.Name) || next.Name == "(" || next.Name == "~")) // necessary value { state.TokenList.Insert(0, t); LogicElement result = EntryC1(state); Token after = state.PopToken(); if (after == null || after.Name != "after") { t.ThrowException("Expected 'after' after result."); } Instruction inst = GetInstructions(state, t); Token from = state.PopToken(); if (from == null) { if (first.Name == "necessary") { return(new NecessaryAfter(inst, result)); } else { return(new PossiblyAfter(inst, result)); } } if (from.Name != "from") { t.ThrowException("Expected from after program."); } LogicElement cond = EntryC1(state); if (first.Name == "necessary") { return(new NecessaryAfterFrom(inst, result, cond)); } else { return(new PossiblyAfterFrom(inst, result, cond)); } } else { throw new Exception("Incorrect query."); } } else { first.ThrowException("Expected 'necessary' or 'possibly'."); } return(null); }
private HashSet <string> New(ExpressionsList expressions, State from, State to, AgentsList agents, Action action) { //find all fluents that differs var diff = to.Values.Where(x => !from.Values.Contains(x)).Select(x => x.Key); //except noninertial ones diff = diff.Except(expressions.Noninertial.Select(x => x.Fluent.Name)); //add fluents from release statements diff = diff.Concat(expressions.Releases.Where( x => x.Action.Equals(action) && agents.HasSubset(x.Agents) && x.Condition.EvaluateLogicExpression().Any(e => from.Values.HasSubset(e))).Select(x => x.Fluent.Name)); return(new HashSet <string>(diff)); //maybe except should be at the end of the query? }