Ejemplo n.º 1
0
        public void TestMatchArgValuesWithOptionalTokens()
        {
            ArgumentToken arg1 = new ArgumentToken <int> .Builder().Name("arg1").IsOptional(false).Parser(int.TryParse).Build();

            ArgumentToken arg2 = new ArgumentToken <double> .Builder().Name("arg2").IsOptional(false).Parser(double.TryParse).Build();

            var tokens = new ICommandToken[]
            {
                new VerbToken(new Name("verb0")),
                new OptionWithArgumentToken.Builder().Name("-o2", "--option2").WithArgument(arg1).WithArgument(arg2).Build(),
            };

            var builder = new CommandUsage.Builder().Description("test usage");

            foreach (var token in tokens)
            {
                builder.WithToken(token);
            }

            ICommandUsage usage = builder.Build();

            TokenMatchCollection matchCollection = CommandParser.Match(usage.Tokens, "verb0 -o2 1 2.34");

            int  arg1Value;
            bool arg1Exists = matchCollection.TryGetArgValue(arg1, out arg1Value);

            double arg2Value;
            bool   arg2Exists = matchCollection.TryGetArgValue(arg2, out arg2Value);

            Assert.True(arg1Exists);
            Assert.True(arg2Exists);
            Assert.Equal(1, arg1Value);
            Assert.Equal(2.34, arg2Value);
        }
Ejemplo n.º 2
0
        public override void Execute(ICommandToken commandToken)
        {
            var token = commandToken as AddNodeCommandToken;

            // TODO Test if the connection already exists
            var pos     = mApp.Graph.Nodes.Count == 0 ? new Point2(0, 0) : mApp.Graph.Nodes.Last().Position + new Point2(200, 0);
            var inputs  = ImmutableArray <NodeInput> .Empty;
            var outputs = ImmutableArray <NodeOutput> .Empty;

            var type = ComponentFinder.FindByName(token.Type);

            foreach (var attr in ComponentFinder.GetInputAttributes(type))
            {
                inputs = inputs.Add(new NodeInput(attr.Name, null));
            }
            foreach (var attr in ComponentFinder.GetOutputAttributes(type))
            {
                outputs = outputs.Add(new NodeOutput(attr.Name, false));
            }

            var node = new Node(FindUniqueName(mApp.Graph, token.Type), token.Type, pos,
                                inputs, outputs);

            mApp.SetGraph(mApp.Graph.AddNode(node));
        }
Ejemplo n.º 3
0
        [InlineData("-o2 1", false)]      //partial match
        public void TestTokenIsFullMatch(string input, bool expIsFullMatch)
        {
            ArgumentToken arg1 = new ArgumentToken <int> .Builder().Name("arg1").IsOptional(false).Parser(int.TryParse).Build();

            ArgumentToken arg2 = new ArgumentToken <double> .Builder().Name("arg2").IsOptional(false).Parser(double.TryParse).Build();

            var tokens = new ICommandToken[]
            {
                new OptionWithArgumentToken.Builder().Name("-o2", "--option2").WithArgument(arg1).WithArgument(arg2).Build()
            };
            var builder = new CommandUsage.Builder()
                          .Description("test usage");

            foreach (var token in tokens)
            {
                builder.WithToken(token);
            }

            ICommandUsage usage = builder.Build();

            TokenMatchCollection matchCollection = CommandParser.Match(usage.Tokens, input);

            Assert.Same(usage.Tokens, matchCollection.MatchableTokens);
            Assert.NotEmpty(matchCollection.Matches);

            Assert.Equal(1, matchCollection.Matches.Count());
            ParserTokenMatch match = matchCollection.Matches.First();

            Assert.Equal(expIsFullMatch, match.IsFullMatch);
        }
        public override void Execute(ICommandToken commandToken)
        {
            var token = commandToken as AddConnectionCommandToken;

            // TODO Test if the connection already exists
            mApp.SetGraph(mApp.Graph.AddConnection(token.Connection));
        }
Ejemplo n.º 5
0
        public override void Execute(ICommandToken commandToken)
        {
            var token = commandToken as MoveNodeCommandToken;

            // TODO Test if the connection already exists
            mApp.SetGraph(mApp.Graph.ReplaceNode(token.Node, token.Node.Move(token.NewPosition)));
        }
Ejemplo n.º 6
0
 public object GetDefaultValue(ICommandToken token)
 {
     if (_defaultValues.ContainsKey(token))
     {
         return(_defaultValues[token]);
     }
     return(null);
 }
Ejemplo n.º 7
0
        public bool Change <T>(ICommandToken <T> token, ICommandTrigger <T> trigger)
        {
            var command = _commands[token].Command as ICommand <T>;

            Remove(token);
            Add(trigger, command, token);
            return(true);
        }
Ejemplo n.º 8
0
 public string GetDescription(ICommandToken token)
 {
     if (_descriptions.ContainsKey(token))
     {
         return(_descriptions[token]);
     }
     return(String.Empty);
 }
        public override void Execute(ICommandToken commandToken)
        {
            var token = commandToken as ToggleNodeOutputIsStreamCommandToken;

            // TODO Test if the connection already exists
            var newNodeOutput = new NodeOutput(token.NodeOutput.Name, !token.NodeOutput.IsStream);

            mApp.SetGraph(mApp.Graph.ReplaceNode(token.Node, token.Node.ReplaceOutput(token.NodeOutput, newNodeOutput)));
        }
        public override void Execute(ICommandToken commandToken)
        {
            var token = commandToken as EditNodeInputInitialDataToken;

            // TODO Test if the connection already exists
            var newNodeInput = new NodeInput(token.NodeInput.Name, token.NewInitialData);

            mApp.SetGraph(mApp.Graph.ReplaceNode(token.Node, token.Node.ReplaceInput(token.NodeInput, newNodeInput)));
        }
Ejemplo n.º 11
0
 public Builder WithToken(ICommandToken token, string description = "", object defaultValue = null)
 {
     _tokens.Add(token);
     _descriptions.Add(token, description);
     if (defaultValue != null)
     {
         _defaultValues.Add(token, defaultValue);
     }
     return(this);
 }
Ejemplo n.º 12
0
        public ToolDemo(ICommandManager commandManager, IKeyboard keyboard, IConfig <Config> config)
        {
            CommandA = commandManager.Add(keyboard.OnDown(AK + A),
                                          e => { Logger.LogInformation($"{nameof(ToolDemo)}: Caps+A triggered!!!!!!!"); });
            CommandB = (Caps + B).OnDown(e => Logger.LogWarning("Caps+B pressed!!!"));

            Logger.LogInformation(config.CurrentValue.Option1);
            Logger.LogInformation(config.CurrentValue.Option2.ToString());
            RegisterCommands();
        }
Ejemplo n.º 13
0
 private static string GetOptionalDisplayName(ICommandToken token)
 {
     if (token.IsOptional)
     {
         return($"[{token.DisplayName}]");
     }
     else
     {
         return(token.DisplayName);
     }
 }
Ejemplo n.º 14
0
        public void TestAdjacentTokens(string input, int?prevIdx, int?currentIdx, int?nextIdx)
        {
            var tokens = new ICommandToken[]
            {
                new ArgumentToken <int> .Builder()
                .Name("name")
                .Parser(int.TryParse)
                .IsOptional(false)
                .Build(),
                new VerbToken(new Name("verb1", "alt1")),
                new VerbToken(new Name("verb2", "alt2")),
                new VerbToken(new Name("verb3", "alt3"))
            };
            var builder = new CommandUsage.Builder()
                          .Description("test usage");

            foreach (var token in tokens)
            {
                builder.WithToken(token);
            }

            ICommandUsage usage = builder.Build();

            ICommandToken currentToken = CommandParser.GetCurrentToken(usage, input);
            ICommandToken prevToken    = CommandParser.GetPreviousToken(usage, input);
            ICommandToken nextToken    = CommandParser.GetNextToken(usage, input);

            if (prevIdx.HasValue)
            {
                Assert.Same(tokens[prevIdx.Value], prevToken);
            }
            else
            {
                Assert.Null(prevToken);
            }

            if (currentIdx.HasValue)
            {
                Assert.Same(tokens[currentIdx.Value], currentToken);
            }
            else
            {
                Assert.Null(currentToken);
            }

            if (nextIdx.HasValue)
            {
                Assert.Same(tokens[nextIdx.Value], nextToken);
            }
            else
            {
                Assert.Null(nextToken);
            }
        }
Ejemplo n.º 15
0
        public override void Execute(ICommandToken commandToken)
        {
            var token = commandToken as PlayCommandToken;

            var scheduler = new Scheduler(mApp.Graph);

            try {
                scheduler.Run();
            } catch (Exception ex) {
                MessageBox.Show("Error: " + ex.Message, "Scheduler error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Ejemplo n.º 16
0
        public override void Execute(ICommandToken commandToken)
        {
            var token = commandToken as UndoRedoCommandToken;

            if (token.IsUndoNotRedo)
            {
                mApp.Undo();
            }
            else
            {
                mApp.Redo();
            }
        }
Ejemplo n.º 17
0
        internal void Remove <T>(ICommandToken <T> token)
        {
            var command = _commands[token].Command as Command <T>;
            var trigger = _commands[token].Trigger as ICommandTrigger <T>;

            _commands.Remove(token);
            Debug.Assert(command != null, nameof(command) + " != null");
            Debug.Assert(trigger != null, nameof(trigger) + " != null");
            trigger.CanExecute -= command.CanExecute;
            command.CanExecute  = null;
            trigger.Execute    -= command.Execute;
            command.Execute     = null;
            trigger.OnRemove(command);
        }
Ejemplo n.º 18
0
        public void StartCommand(ICommandToken token)
        {
            //1. Realize the command
            Func <CommandBase> factory;

            if (!mTokenTypeToFactory.TryGetValue(token.GetType(), out factory))
            {
                // Command not registered
                return;
            }

            //2. Run the command
            var command = factory();

            command.Execute(token);
        }
Ejemplo n.º 19
0
        [InlineData("verb0 verb4 -o1 -o2 1 2.34 -o3", 0, 4)]                    //All tokens present, wrong order
        public void TestMatchWithOptionalTokensWithArgs(string input, params int[] expectedMatchingIndexes)
        {
            ArgumentToken arg1 = new ArgumentToken <int> .Builder().Name("arg1").IsOptional(false).Parser(int.TryParse).Build();

            ArgumentToken arg2 = new ArgumentToken <double> .Builder().Name("arg2").IsOptional(false).Parser(double.TryParse).Build();

            var tokens = new ICommandToken[]
            {
                new VerbToken(new Name("verb0")),
                new StandAloneOptionToken(new Name("-o1", "--option1")),
                new OptionWithArgumentToken.Builder().Name("-o2", "--option2").WithArgument(arg1).WithArgument(arg2).Build(),
                new StandAloneOptionToken(new Name("-o3", "--option3")),
                new VerbToken(new Name("verb4")),
            };
            var builder = new CommandUsage.Builder()
                          .Description("test usage");

            foreach (var token in tokens)
            {
                builder.WithToken(token);
            }

            ICommandUsage usage = builder.Build();

            TokenMatchCollection matchCollection = CommandParser.Match(usage.Tokens, input);

            Assert.Same(usage.Tokens, matchCollection.MatchableTokens);
            if (expectedMatchingIndexes.Length > 0)
            {
                Assert.NotEmpty(matchCollection.Matches);

                Assert.Equal(expectedMatchingIndexes.Length, matchCollection.Matches.Count());

                //Ensure all that are expected are there
                foreach (var expectedMatchingIndex in expectedMatchingIndexes)
                {
                    Assert.True(matchCollection.Matches.Any(x => x.TokenIdx == expectedMatchingIndex));
                }
            }
            else
            {
                Assert.Empty(matchCollection.Matches);
            }
        }
Ejemplo n.º 20
0
        public string Translate(ICommandToken commandToken)
        {
            switch (commandToken?.CommandTokenType)
            {
            case CommandTokenType.PlaceArmiesResponse:
                return(TranslatePlaceArmiesResponse((PlaceArmiesResponseToken)commandToken));

            case CommandTokenType.AttackResponse:
                return(TranslateAttackResponse((AttackResponseToken)commandToken));

            case CommandTokenType.PickStartingRegionsResponse:
                return(TranslatePickStartingRegions((PickStartingRegionsResponseToken)commandToken));

            case null:
                return(null);

            default:
                throw new ArgumentOutOfRangeException(nameof(commandToken));
            }
        }
Ejemplo n.º 21
0
        public TokenMatchResult(ICommandToken token, string matchedTokensText, string fullMatchText, MatchOutcome matchOutcome, int charsMatched, int tokensMatched)
        {
            if (matchedTokensText == null)
            {
                throw new ArgumentNullException(nameof(matchedTokensText));
            }
            if (fullMatchText == null)
            {
                throw new ArgumentNullException(nameof(fullMatchText));
            }
            if (charsMatched < 0 || charsMatched > matchedTokensText.Length)
            {
                throw new ArgumentOutOfRangeException($"{nameof(charsMatched)} should be <= {nameof(matchedTokensText)}.Length");
            }
            ArgumentValues = new ValueBag <ArgumentToken>();

            Token             = token;
            MatchedTokensText = matchedTokensText;
            FullMatchText     = fullMatchText;
            MatchOutcome      = matchOutcome;
            CharsMatched      = charsMatched;
            TokensMatched     = tokensMatched;
        }
Ejemplo n.º 22
0
        [InlineData("1234 verb1 verb2 verb3 verb4", 3)] //Too many tokens
        public void TestMatch(string input, int?expectedMatchIdx)
        {
            var tokens = new ICommandToken[]
            {
                new ArgumentToken <int> .Builder()
                .Name("name")
                .Parser(int.TryParse)
                .IsOptional(false)
                .Build(),
                new VerbToken(new Name("verb1", "alt1")),
                new VerbToken(new Name("verb2", "alt2")),
                new VerbToken(new Name("verb3", "alt3"))
            };
            var builder = new CommandUsage.Builder()
                          .Description("test usage");

            foreach (var token in tokens)
            {
                builder.WithToken(token);
            }

            ICommandUsage usage = builder.Build();

            TokenMatchCollection matchCollection = CommandParser.Match(usage.Tokens, input);

            Assert.Same(usage.Tokens, matchCollection.MatchableTokens);
            if (expectedMatchIdx.HasValue)
            {
                Assert.NotEmpty(matchCollection.Matches);
                Assert.Same(tokens[expectedMatchIdx.Value], matchCollection.Matches.Last(x => x.MatchOutcome == Enums.MatchOutcome.Full).Token);
                Assert.Equal(expectedMatchIdx, matchCollection.Matches.Where(x => x.MatchOutcome == Enums.MatchOutcome.Full).Max(x => x.TokenIdx));
            }
            else
            {
                Assert.Empty(matchCollection.Matches);
            }
        }
Ejemplo n.º 23
0
        public override void Execute(ICommandToken commandToken)
        {
            var token = commandToken as SaveCommandToken;

            FbpWriter.Write(mApp.Graph, mApp.CurrentFbpFullFileName);
        }
Ejemplo n.º 24
0
 public ICommandToken Execute(ICommandToken commandToken)
 {
     // dynamically dispatch for the token
     return(Execute((dynamic)commandToken));
 }
Ejemplo n.º 25
0
 public bool IsDisabled <T>(ICommandToken <T> token)
 {
     return(_commands[token].Command.Disabled);
 }
Ejemplo n.º 26
0
 public void DisableEnable <T>(ICommandToken <T> token, bool disable)
 {
     _commands[token].Command.Disabled = disable;
 }
Ejemplo n.º 27
0
 private ICommandToken <TArgs> Add <TArgs>(ICommandTrigger <TArgs> trigger, ICommand <TArgs> command, ICommandToken <TArgs> token = null)
 {
     trigger.CanExecute += command.CanExecute;
     trigger.Execute    += command.Execute;
     trigger.OnAdd(command);
     token ??= new CommandToken <TArgs>(this);
     _commands.Add(token, new Entry(trigger, command));
     return(token);
 }
Ejemplo n.º 28
0
 public virtual bool HasToken(ICommandToken token)
 {
     return(_matchCollection.Matches.Any(x => x.IsFullMatch && x.Token.Equals(token)));
 }
Ejemplo n.º 29
0
        public override void Execute(ICommandToken commandToken)
        {
            var token = commandToken as RemoveNodeCommandToken;

            mApp.SetGraph(mApp.Graph.RemoveNode(token.Node));
        }
Ejemplo n.º 30
0
 public abstract void Execute(ICommandToken token);