private void AlwaysValuesNoninertialReleasesShouldBeEmpty(HistoryStatement history)
 {
     history.Always.Should().BeEmpty();
     history.Values.Should().BeEmpty();
     history.NonInertial.Should().BeEmpty();
     history.Releases.Should().BeEmpty();
 }
Exemple #2
0
        public Story(HistoryStatement history)
        {
            this.history = history;
            var fluents            = getAllFluents();
            var nonInertialFluents = getAllNonInertialFluents();

            this.Fluents = fluents.Select(f => new Fluent
            {
                Label      = f,
                IsInertial = !nonInertialFluents.Contains(f)
            }).ToList();

            this.Agents  = getAllAgents();
            this.Actions = getAllActions();

            this.States = AppState.ValidStates(history.Always.Select(a => a.Condition).ToList(), fluents.ToArray()).ToList();
        }
        public override bool Execute(AfterQueryStatement query, Graph.Graph graph, HistoryStatement history)
        {
            if (graph == null)
            {
                throw new ArgumentNullException(nameof(graph));
            }
            query.StateFromCondition = query.StateFromCondition ?? graph.GetStartCondition(history);

            switch (query.Sufficiency)
            {
            case Sufficiency.Necessary:
                return(NecessaryAfter(query, graph, history));

            case Sufficiency.Possibly:
                return(PossiblyAfter(query, graph, history));

            case Sufficiency.Typically:
                return(TypicallyAfter(query, graph, history));
            }
            return(false);
        }
Exemple #4
0
        public override bool Execute(ExecutableQueryStatement query, Graph.Graph graph, HistoryStatement history)
        {
            if (graph == null)
            {
                throw new ArgumentNullException(nameof(graph));
            }

            ExecutableQueryExecutor.actions = query.Actions;
            ExecutableQueryExecutor.history = history;
            ExecutableQueryExecutor.graph   = graph;

            switch (query.Sufficiency)
            {
            case Sufficiency.Necessary:
                return(GetInitialStates().Select(x => ExecuteNecessarySufficiency(0, x)).All(x => x == true));

            case Sufficiency.Possibly:
                return(GetInitialStates().Select(x => ExecutePossibleSufficiency(0, x)).All(x => x == true));

            default:
                throw new InvalidOperationException();
            }
            return(false);
        }
Exemple #5
0
        public override bool Execute(AgentInQueryStatement query, Stories.Graph.Graph graph, HistoryStatement history)
        {
            if (graph == null)
            {
                throw new ArgumentNullException(nameof(graph));
            }
            // czy występuje jawnie w ścieżce
            #region
            var story = new Story(history);
            var nonExisitngActions = query.Actions.Select(p => p.Action).Except(story.Actions).ToArray();
            var actors             = new HashSet <string>(story.Agents.Concat(query.Actions.Select(p => p.Agent).Concat(new[] { query.Agent })));
            actors.ExceptWith(new string[] { null });
            foreach (var actor in actors)
            {
                foreach (var action in nonExisitngActions)
                {
                    foreach (var vertex in graph.Vertexes)
                    {
                        var edge = new Edge(vertex, vertex, true, action, actor);
                        vertex.EdgesIncoming.Add(edge);
                        vertex.EdgesOutgoing.Add(edge);
                    }
                }
            }
            #endregion
            //if (query.Actions.Any(p => p.Agent == query.Agent))
            //    return true;

            // poszukiwanie krawędzi null, którą tylko on może spełnić z warunków początkowych
            var vertices = graph.Vertexes.ToList();
            foreach (var val in history.Values.Where(x => x.Actions.Count == 0))
            {
                vertices = vertices.FindVerticesSatisfyingCondition(val.Condition).ToList();
            }
            query.Actions.Reverse();
            if (query.Sufficiency == Sufficiency.Necessary)
            {
                var search = new necessarySearch();

                vertices.ForEach(p => search.Search(p, query, query.Actions.Count, new List <Edge>()));

                return(search.started && search.result);
            }
            if (query.Sufficiency == Sufficiency.Possibly)
            {
                var search = new possibleSearch();
                vertices.ForEach(p => search.Search(p, query, query.Actions.Count, new List <Edge>()));

                return(search.started && search.result);
            }

            return(false);
        }
 public bool Execute(QueryStatement query, Graph.Graph graph, HistoryStatement history)
 => Execute(query as T, graph, history);
 public abstract bool Execute(T query, Graph.Graph graph, HistoryStatement history);
        private static bool TypicallyAfter(AfterQueryStatement query, Graph.Graph graph, HistoryStatement history)
        {
            var startVertices = graph.Vertexes.FindVerticesSatisfyingCondition(query.StateFromCondition).ToList();

            foreach (var startVertex in startVertices)
            {
                var possibleFinalVertices = FollowProgramTypically(query.Actions, startVertex, 0);

                if (!possibleFinalVertices.All(v => v.State.EvaluateCondition(query.StateToCondition)))
                {
                    return(false);
                }
            }
            return(true);
        }
        public static ConditionExpression GetStartCondition(this Stories.Graph.Graph graph, HistoryStatement history)
        {
            ConditionExpression cond = new ConditionConstant(true);
            var vertices             = graph.Vertexes.ToList();

            foreach (var val in history.Values.Where(x => x.Actions.Count == 0))
            {
                cond = new ConditionOperation()
                {
                    Left = cond, Right = val.Condition, Operation = OperationType.And
                };
            }
            return(cond);
        }
 public static bool Execute <T>(this T st, Stories.Graph.Graph graph, HistoryStatement history)
     where T : QueryStatement
 {
     return(Execute(st).Execute(st, graph, history));
 }
        private static IEnumerable <Vertex> ApplyNotInitiallyValueStatements(Graph.Graph graph, HistoryStatement history, IEnumerable <Vertex> vertices)
        {
            foreach (var val in history.Values.Where(x => x.Actions.Count != 0))
            {
                var valuePaths = vertices.SelectMany(x => x.EdgesOutgoing);
                for (int i = 0; i < val.Actions.Count - 1; i++)
                {
                    // wybranie wszystkich krawędzi odpowiadajacych danej parze (actor, action) z value staatement
                    valuePaths = valuePaths.Where(y => y.Action == val.Actions[i].Action && y.Actor == val.Actions[i].Agent).Select(x => x.To)
                                 .SelectMany(x => x.EdgesOutgoing);
                    if (valuePaths.Count() == 0)
                    {
                        break;
                    }
                }
                // krawędzie spełniajace warunek ostatniej pary (actor, action) z value staatement
                valuePaths = valuePaths.Where(y => y.Action == val.Actions[val.Actions.Count - 1].Action && y.Actor == val.Actions[val.Actions.Count - 1].Agent);

                var edgeList = valuePaths.ToList();
                for (int i = 0; i < edgeList.Count; i++)
                {
                    // znalezienie wierzchołka, do którego przeniesiemy się po zaaplikowaniu value statements
                    var vertexState = edgeList[i].To.State.Values.ToDictionary(x => x.Key, x => x.Value);
                    foreach (var fluent in val.Condition.ExtractFluentsValues())
                    {
                        vertexState[fluent.Key] = fluent.Value;
                    }
                    var vertexEvaluatingValueStatement = graph.Vertexes.Where(v => v.State.Values.All(x => vertexState[x.Key] == x.Value)).ToList();
                    if (vertexEvaluatingValueStatement.Count > 1)
                    {
                        throw new InvalidOperationException("vertexEvaluatingValueStatement length is grater than 1.");
                    }

                    // sprawdzić czy wszystkie drogi prowadzące do wierzchołka spełniają value statement
                    // TODO
                    if (edgeList[i].To.EdgesIncoming.All(x => valuePaths.Contains(x)) && val.IsObservable == false)
                    {
                        edgeList[i].To = vertexEvaluatingValueStatement.First();
                    }
                    else
                    {
                        vertexEvaluatingValueStatement.ForEach(
                            x =>
                        {
                            var actionEdge = edgeList[i].From.EdgesOutgoing.FirstOrDefault(
                                y => y.IsTypical == edgeList[i].IsTypical && edgeList[i].Action == y.Action && edgeList[i].Actor == y.Actor);
                            if (actionEdge == null)
                            {
                                edgeList[i].From.EdgesOutgoing.Add(new Edge(edgeList[i].From, x, edgeList[i].IsTypical, edgeList[i].Action, edgeList[i].Actor));
                            }
                            else
                            {
                                actionEdge.To = x;
                            }
                        });
                    }
                }
            }
            return(vertices);
        }
        private static IEnumerable <Vertex> BuildGraph(IEnumerable <Vertex> startVertices, Graph.Graph graph, HistoryStatement history)
        {
            var vertices = startVertices;

            //apply initially value statements
            foreach (var val in history.Values.Where(x => x.Actions.Count == 0))
            {
                vertices = vertices.FindVerticesSatisfyingCondition(val.Condition).ToList();
            }

            vertices = DropUnreachableEdges(vertices);

            // zaaplikowanie value statements
            vertices = ApplyNotInitiallyValueStatements(graph, history, vertices);

            //zaaplikowanie zdan releases
            vertices = ApplyReleasesStatements(graph, history, vertices);
            return(vertices);
        }
        private static IEnumerable <Vertex> ApplyReleasesStatements(Graph.Graph graph, HistoryStatement history, IEnumerable <Vertex> vertices)
        {
            foreach (var release in history.Releases)
            {
                var edges = vertices.SelectMany(x => x.EdgesOutgoing.Where(y => y.Action == release.Action &&
                                                                           (release.Agents == null || release.Agents.Contains(y.Actor)))).ToList();

                for (int i = 0; i < edges.Count; i++)
                {
                    var edge = edges[i];
                    if (edge.From.State.EvaluateCondition(release.Condition))
                    {
                        var state = edge.To.State.Values.ToDictionary(x => x.Key, x => x.Value);
                        state[release.Fluent] = !state[release.Fluent];
                        var vertex = graph.Vertexes.FirstOrDefault(v => v.State.Values.All(x => state[x.Key] == x.Value));
                        if (vertex != null)
                        {
                            edge.From.EdgesOutgoing.Add(new Edge(edge.From, vertex, edge.IsTypical, edge.Action, edge.Actor));
                        }
                    }
                }
            }
            return(vertices);
        }
        public override bool Execute(AccessibleQueryStatement query, Graph.Graph graph, HistoryStatement history)
        {
            if (graph == null)
            {
                throw new ArgumentNullException(nameof(graph));
            }

            query.StateFromCondition = query.StateFromCondition ?? graph.GetStartCondition(history);
            var startVertices = graph.Vertexes.FindVerticesSatisfyingCondition(query.StateFromCondition).ToList();

            startVertices = BuildGraph(startVertices, graph, history).ToList();

            switch (query.Sufficiency)
            {
            case Sufficiency.Necessary:
                return(ExecuteNecessarySufficiency(startVertices, query.StateToCondition));

            case Sufficiency.Possibly:
                return(ExecutePossiblySufficiency(startVertices, query.StateToCondition));

            case Sufficiency.Typically:
                return(ExecuteTypicallySufficiency(startVertices, query.StateToCondition));

            default:
                throw new InvalidOperationException();
            }
            return(false);
        }