Example #1
0
 public bool TryConnect(string tag, DState to)
 {
     if (_connections.ContainsKey(tag))
     {
         Debug.Log("Current connention currently added with : " + tag + " to state : " + _connections[tag]);
         return(false);
     }
     if (AutomataManager.automataType == AutomataType.DPDA)
     {
         var allSameInputChar = GetTags(tag[0]);
         if (allSameInputChar != null)
         {
             foreach (var item in allSameInputChar)
             {
                 if (item.machine == tag[1])
                 {
                     return(false);
                 }
             }
         }
     }
     if (AutomataManager.automataType == AutomataType.Turing)
     {
         foreach (var item in _connections)
         {
             if (item.Key[0] == tag[0])
             {
                 return(false);
             }
         }
     }
     _connections.Add(tag, to);
     return(true);
 }
Example #2
0
        public override void Add(Token token)
        {
            switch (_state)
            {
            case DState.Empty:
                SetName(token);
                return;

            case DState.Named:
                if (token.TokenType != TokenType.Control ||
                    !token.TokenString.Equals(":"))
                {
                    throw new InvalidSyntaxException("':' required after defining a variable name", token.Line, token.Position);
                }
                _state = DState.Typing;
                return;

            case DState.Typing:
                SetType(token);
                return;

            case DState.Typed:
                BuildAssignment(token);
                return;

            case DState.Assigning:
                _assignment.Add(token);
                return;
            }
        }
Example #3
0
File: RE2.cs Project: mjibson/junk
        public bool Match(string s)
        {
            if (Method == Methods.NFA)
            {
                var sl = Startlist();

                foreach (var c in s)
                {
                    sl = sl.Step(c);
                }

                return(sl.IsMatch());
            }
            else if (Method == Methods.DFA)
            {
                var ds = Startdstate();

                foreach (var c in s)
                {
                    if (!ds.next.ContainsKey(c))
                    {
                        var sl = ds.l.Step(c);
                        ds.next[c] = DState.Get(sl);
                    }

                    ds = ds.next[c];
                }

                return(ds.l.IsMatch());
            }

            throw new System.ArgumentException("Unknown method type");
        }
 public DoorControl(IDoor door, IAlarm alarm, IEntryNotification entryNotification, IUserValidation userValidation)
 {
     _door              = door;
     _alarm             = alarm;
     _entryNotification = entryNotification;
     _userValidation    = userValidation;
     _doorState         = DState.Closed;
 }
Example #5
0
    public void RemoveConnectionsTo(DState to)
    {
        List <KeyValuePair <string, DState> > items = _connections.Where(kvp => kvp.Value == to).ToList();

        foreach (var item in items)
        {
            _connections.Remove(item.Key);
        }
    }
Example #6
0
 private void BuildAssignment(Token token)
 {
     if (token.TokenType != TokenType.Control || token.TokenString != ":=")
     {
         throw new InvalidSyntaxException($"Expected assignment TokenString or line terminator. Got {token.TokenString}", token.Line, token.Position);
     }
     _assignment = new Assignment(this, this, token.Line, token.Position);
     _state      = DState.Assigning;
 }
Example #7
0
    public DState GetNextState(DState current, char underhead)
    {
        var tags = current.GetTags(underhead);

        if (tags == null)
        {
            return(null);
        }

        return(current.GetNextState(tags[0].GetAllTogether()));
    }
Example #8
0
    public bool CheckInput(string input)
    {
        input += "$";
        if (!DeterministicCheck())
        {
            return(false);
        }

        Stack <char> _stack = new Stack <char>();

        _stack.Push('λ');
        _stack.Push('z');

        DState current = _start;

        for (int i = 0; i < input.Length; i++)
        {
            if (current == null)
            {
                return(false);
            }

            char             inputChar  = input[i];
            List <TagFormat> tagFormats = null;

            tagFormats = current.GetTags(inputChar);
            if (tagFormats == null)
            {
                var list = current.GetTags('λ');
                if (list != null)
                {
                    i--;
                    tagFormats = list;
                }
            }
            if (tagFormats == null)
            {
                return(false);
            }
            foreach (var currentTagFormat in tagFormats.Where(t => t.machine == _stack.Peek()))
            {
                if (_stack.Peek() != 'λ')
                {
                    _stack.Pop();
                }

                PushToStack(_stack, currentTagFormat);

                current = current.GetNextState(currentTagFormat.GetAllTogether());
                break;
            }
        }
        return(current.IsFinal());
    }
Example #9
0
        public void TimelineCanSaveItemsAfterMax()
        {
            var tls = new TimelineStorage <DState>(5);

            for (uint i = 0; i < 11; i++)
            {
                tls[i] = new DState((int)i);
            }
            Assert.Throws <TimelineException>(() => {
                tls[0].Value = 0;
            });
        }
Example #10
0
        public void TimelineCanSaveMaxItems()
        {
            var tls = new TimelineStorage <DState>(50);

            for (uint i = 0; i < 50; i++)
            {
                tls[i] = new DState((int)i * 2);
            }

            Assert.AreEqual(98, tls[49].Value);
            Assert.AreEqual(0, tls[0].Value);
        }
Example #11
0
        public void TimelineStorageRangeMoveAlong()
        {
            var tls = new TimelineStorage <DState>(2)
            {
                [0] = new DState(0), [1] = new DState(1)
            };

            Assert.AreEqual(0, tls[0].Value);
            tls[2] = new DState(2);
            Assert.Throws <TimelineException>(() => {
                tls[0].Value = 1;
            });
        }
Example #12
0
 private void SetType(Token token)
 {
     if (token.TokenType != TokenType.Name)
     {
         throw new InvalidSyntaxException("Type declaration required", token.Line, token.Position);
     }
     _val = token.TokenString switch
     {
         "int" => new MplInteger(0, token.Line, token.Position),
         "bool" => new MplBoolean(false, token.Line, token.Position),
         "string" => new MplString("", token.Line, token.Position),
         _ => throw new InvalidSyntaxException($"Invalid type declaration: {token.TokenString}", token.Line, token.Position)
     };
     _state = DState.Typed;
 }
Example #13
0
    private IEnumerator DoCheck(string input, Turing turing)
    {
        bool result = false;
        bool isHalt = false;

        if (!turing.DeterministicCheck())
        {
            isHalt = true;
        }

        DState        current = turing.GetStartState();
        StringBuilder tape    = new StringBuilder(input);
        int           head    = 0;

        while (!isHalt)
        {
            if (head >= tape.Length || head < 0)
            {
                if (head < 0)
                {
                    head = 0;
                }
                tape.Insert(head, '□');
            }

            var next = turing.GetNextState(current, tape[head]);

            if (next == null)
            {
                isHalt = true;
                result = current.IsFinal();
            }
            else
            {
                var tagFormat = current.GetTags(tape[head])[0];
                tape[head] = tagFormat.machine;
                head      += tagFormat.machineCommand.ToLower().Contains("r") ? +1 : -1;
                current    = next;
            }
            yield return(null);
        }

        turing.IsAnswerReady = true;
        turing.Result        = result;
        _checking            = null;
    }
Example #14
0
        private void SetName(Token token)
        {
            if (token.TokenType != TokenType.Name)
            {
                throw new InvalidSyntaxException("Expected a name after variable declaration", token.Line, token.Position);
            }
            if (Keywords.Contains(token.TokenString))
            {
                throw new InvalidSyntaxException($"Unable to re-define reserved keyword {token.TokenString}", token.Line, token.Position);
            }
            Definition d = GetDefinition(token.TokenString);

            if (d != null)
            {
                throw new InvalidSyntaxException($"Unable to re-define variable {token.TokenString}", token.Line, token.Position);
            }
            Name   = token.TokenString;
            _state = DState.Named;
        }
Example #15
0
 public void ChangeStatus(int id, Status status)
 {
     if (status == Status.START || status == Status.STARTANDFINAL)
     {
         _start = _states[id];
         foreach (var s in _states)
         {
             if (s.Value.Status == Status.START)
             {
                 s.Value.Status = Status.NORMAL;
                 break;
             }
             if (s.Value.Status == Status.STARTANDFINAL)
             {
                 s.Value.Status = Status.FINAL;
                 break;
             }
         }
     }
     _states[id].Status = status;
 }
Example #16
0
        public void TimelineCanRetrieveItemsInCurrentRange()
        {
            var tls = new TimelineStorage <DState>(5);

            for (uint i = 0; i < 11; i++)
            {
                tls[i] = new DState((int)i);
            }
            Assert.AreEqual(10, tls[10].Value);
            Assert.AreEqual(9, tls[9].Value);
            Assert.AreEqual(8, tls[8].Value);
            Assert.AreEqual(7, tls[7].Value);
            Assert.AreEqual(6, tls[6].Value);
            Assert.Throws <TimelineException>(() => {
                tls[5].Value = 0;
            });
            Assert.Throws <TimelineException>(() => {
                tls[12].Value = 12;
            });
            Assert.DoesNotThrow(() => {
                tls[11] = new DState(11);
            });
        }
Example #17
0
    public bool CheckInput(string inp)
    {
        if (DeterministicCheck() == false)
        {
            return(false);                               //DFA needs a Start and must be deterministic depends on alphabet
        }
        DState current = _start;

        for (int i = 0; i < inp.Length; i++)
        {
            if (current == null)
            {
                Debug.Log("Current Dosent exitst during InputCheck calculation");
                return(false);
            }
            current = current.GetNextState(inp[i].ToString());
        }
        if (current == null)
        {
            return(false);
        }

        return(current.IsFinal());
    }
Example #18
0
File: RE2.cs Project: mjibson/junk
 private DState Startdstate()
 {
     return(DState.Get(Startlist()));
 }
 public void DoorClosed()
 {
     _doorState = DState.Closed;
 }
 public void DoorOpen()
 {
     _doorState = DState.Open;
     _door.Close();
 }