Example #1
0
 public static bool operation_message(AdminShell.Operation op, i40LanguageAutomaton auto)
 {
     // inputVariable property value = text
     foreach (var v in op.inputVariable)
     {
         if (v.value.submodelElement is AdminShell.Property)
         {
             var p = v.value.submodelElement as AdminShell.Property;
             auto.getMessages.Add(p.value);
             Console.WriteLine("operation message: " + p.idShort + " = " + p.value);
         }
     }
     return(true);
 }
Example #2
0
        public static bool operation_checkCollection(AdminShell.Operation op, i40LanguageAutomaton auto)
        {
            // inputVariable property checkType: isEmpty, isNotEmpty;
            // inputVariable reference collection proposal

            if (auto.name == debugAutomaton)
            {
                int i = 0; // set breakpoint here to debug specific automaton
            }

            if (op.inputVariable.Count != 2 && op.outputVariable.Count != 0)
            {
                return(false);
            }

            AdminShell.Property checkType = null;
            AdminShell.SubmodelElementCollection refCollection = null;

            foreach (var input in op.inputVariable)
            {
                var inputRef = input.value.submodelElement;
                if (inputRef is AdminShell.Property)
                {
                    checkType = (inputRef as AdminShell.Property);
                    continue;
                }
                if (!(inputRef is AdminShell.ReferenceElement))
                {
                    return(false);
                }
                var refElement = Program.env[0].AasEnv.FindReferableByReference((inputRef as AdminShell.ReferenceElement).value);
                if (refElement is AdminShell.SubmodelElementCollection)
                {
                    refCollection = refElement as AdminShell.SubmodelElementCollection;
                }
            }

            int count = refCollection.value.Count;

            switch (checkType.idShort)
            {
            case "isEmpty":
                return(count == 0);

            case "isNotEmpty":
                return(count != 0);
            }

            return(false);
        }
Example #3
0
        public static bool operation_clear(AdminShell.Operation op, i40LanguageAutomaton auto)
        {
            // outputVariables are references to collections
            // alle elements will be removed from collections

            if (auto.name == debugAutomaton)
            {
                int i = 0; // set breakpoint here to debug specific automaton
            }

            if (op.outputVariable.Count == 0)
            {
                return(false);
            }

            foreach (var output in op.outputVariable)
            {
                var outputRef = output.value.submodelElement;
                if (!(outputRef is AdminShell.ReferenceElement))
                {
                    return(false);
                }
                var refElement = Program.env[0].AasEnv.FindReferableByReference((outputRef as AdminShell.ReferenceElement).value);
                if (refElement is AdminShell.SubmodelElementCollection)
                {
                    var refSMEC = refElement as AdminShell.SubmodelElementCollection;
                    List <AdminShell.SubmodelElement> list = new List <AdminShell.SubmodelElement>();
                    foreach (var sme in refSMEC.value)
                    {
                        list.Add(sme.submodelElement);
                    }
                    foreach (var sme2 in list)
                    {
                        refSMEC.Remove(sme2);
                        treeChanged = true;
                    }
                }
            }

            return(true);
        }
Example #4
0
        public static bool operation_sendFrame(AdminShell.Operation op, i40LanguageAutomaton auto)
        {
            // inputVariable property protocol: memory, connect
            // inputVariable reference frame proposal: collection
            // inputVariable reference submodel
            // outputVariable reference property sendFrameJSON

            if (auto.name == debugAutomaton)
            {
                int i = 0; // set breakpoint here to debug specific automaton
            }

            if (op.inputVariable.Count != 3 && op.outputVariable.Count != 1)
            {
                return(false);
            }

            AdminShell.Property protocol = null;
            AdminShell.SubmodelElementCollection refFrame = null;
            AdminShell.Submodel refSubmodel   = null;
            AdminShell.Property sendFrameJSON = null;

            foreach (var input in op.inputVariable)
            {
                var inputRef = input.value.submodelElement;
                if (inputRef is AdminShell.Property)
                {
                    protocol = (inputRef as AdminShell.Property);
                    continue;
                }
                if (!(inputRef is AdminShell.ReferenceElement))
                {
                    return(false);
                }
                var refElement = Program.env[0].AasEnv.FindReferableByReference((inputRef as AdminShell.ReferenceElement).value);
                if (refElement is AdminShell.SubmodelElementCollection)
                {
                    refFrame = refElement as AdminShell.SubmodelElementCollection;
                }
                if (refElement is AdminShell.Submodel)
                {
                    refSubmodel = refElement as AdminShell.Submodel;
                }
            }

            foreach (var output in op.outputVariable)
            {
                var outputRef = output.value.submodelElement;
                if (!(outputRef is AdminShell.ReferenceElement))
                {
                    return(false);
                }
                var refElement = Program.env[0].AasEnv.FindReferableByReference((outputRef as AdminShell.ReferenceElement).value);
                if (refElement is AdminShell.Property)
                {
                    sendFrameJSON = refElement as AdminShell.Property;
                }
            }

            if (protocol.value != "memory" && protocol.value != "connect")
            {
                return(false);
            }

            int    frameCount = refFrame.value.Count;
            string frame      = "{ \"frame\": { ";

            foreach (var smew in refFrame.value)
            {
                var sme = smew.submodelElement;
                if (sme.idShort == "_insert_submodel_into_frame")
                {
                    frame += "\"" + "submodel" + "\" : ";
                    var smJson = JsonConvert.SerializeObject(refSubmodel, Newtonsoft.Json.Formatting.Indented);
                    frame += smJson;
                }
                else
                {
                    frame += "\"" + sme.idShort + "\" : ";
                    if (sme is AdminShell.Property)
                    {
                        frame += "\"" + (sme as AdminShell.Property).value + "\"";
                    }
                    else
                    {
                        frame += "\"\"";
                    }
                }
                if (frameCount-- != 1)
                {
                    frame += ",";
                }
            }
            frame += " } }";
            sendFrameJSON.value = frame;

            Console.WriteLine(frame);

            if (auto.name == "automatonServiceRequester")
            {
                switch (protocol.value)
                {
                case "memory":
                    receivedFrameJSONProvider.Add(frame);
                    break;

                case "connect":
                    sendFrameJSONRequester.Add(frame);
                    break;
                }
            }
            if (auto.name == "automatonServiceProvider")
            {
                switch (protocol.value)
                {
                case "memory":
                    receivedFrameJSONRequester.Add(frame);
                    break;

                case "connect":
                    sendFrameJSONProvider.Add(frame);
                    break;
                }
            }

            return(true);
        }
Example #5
0
        public static bool operation_receiveProposals(AdminShell.Operation op, i40LanguageAutomaton auto)
        {
            // inputVariable property protocol: memory, connect
            // inputVariable reference frame proposal: collection
            // inputVariable reference submodel
            // outputVariable reference collected proposals: collection
            // outputVariable reference collected not understood proposals: collection
            // outputVariable reference collected refused proposals: collection
            // outputVariable reference property receivedFrameJSON

            if (auto.name == debugAutomaton)
            {
                int i = 0; // set breakpoint here to debug specific automaton
            }

            if (op.inputVariable.Count != 3 && op.outputVariable.Count != 4)
            {
                return(false);
            }

            AdminShell.Submodel refSubmodel       = null;
            AdminShell.Property protocol          = null;
            AdminShell.Property receivedFrameJSON = null;

            foreach (var input in op.inputVariable)
            {
                var inputRef = input.value.submodelElement;
                if (inputRef is AdminShell.Property)
                {
                    protocol = (inputRef as AdminShell.Property);
                    continue;
                }
                if (!(inputRef is AdminShell.ReferenceElement))
                {
                    return(false);
                }
                var refElement = Program.env[0].AasEnv.FindReferableByReference((inputRef as AdminShell.ReferenceElement).value);
                if (refElement is AdminShell.Submodel)
                {
                    refSubmodel = refElement as AdminShell.Submodel;
                }
            }

            foreach (var output in op.outputVariable)
            {
                var outputRef = output.value.submodelElement;
                if (!(outputRef is AdminShell.ReferenceElement))
                {
                    return(false);
                }
                var refElement = Program.env[0].AasEnv.FindReferableByReference((outputRef as AdminShell.ReferenceElement).value);
                if (refElement is AdminShell.Property)
                {
                    receivedFrameJSON = refElement as AdminShell.Property;
                }
            }

            var out1 = op.outputVariable.First();
            var r2   = out1.value.submodelElement;

            if (!(r2 is AdminShell.ReferenceElement))
            {
                return(false);
            }
            var ref2 = Program.env[0].AasEnv.FindReferableByReference((r2 as AdminShell.ReferenceElement).value);

            if (!(ref2 is AdminShell.SubmodelElementCollection))
            {
                return(false);
            }
            var smc2 = ref2 as AdminShell.SubmodelElementCollection;

            if (protocol.value != "memory" && protocol.value != "connect")
            {
                return(false);
            }

            while ((auto.name == "automatonServiceRequester" && receivedFrameJSONRequester.Count != 0) ||
                   (auto.name == "automatonServiceProvider" && receivedFrameJSONProvider.Count != 0))
            {
                string receivedFrame = "";
                if (auto.name == "automatonServiceRequester")
                {
                    // receivedFrame = sendFrameJSONProvider;
                    // sendFrameJSONProvider = "";
                    if (receivedFrameJSONRequester.Count != 0)
                    {
                        receivedFrame = receivedFrameJSONRequester[0];
                        receivedFrameJSONRequester.RemoveAt(0);
                    }
                }

                if (auto.name == "automatonServiceProvider")
                {
                    // receivedFrame = sendFrameJSONRequester;
                    // sendFrameJSONRequester = "";
                    if (receivedFrameJSONProvider.Count != 0)
                    {
                        receivedFrame = receivedFrameJSONProvider[0];
                        receivedFrameJSONProvider.RemoveAt(0);
                    }
                }

                receivedFrameJSON.value = receivedFrame;

                AdminShell.Submodel submodel = null;
                if (receivedFrame != "")
                {
                    try
                    {
                        if (auto.name == debugAutomaton)
                        {
                            int i = 0; // set breakpoint here to debug specific automaton
                        }

                        JObject parsed = JObject.Parse(receivedFrame);
                        foreach (JProperty jp1 in (JToken)parsed)
                        {
                            if (jp1.Name == "frame")
                            {
                                foreach (JProperty jp2 in jp1.Value)
                                {
                                    if (jp2.Name == "submodel")
                                    {
                                        string text = jp2.Value.ToString();
                                        submodel = JsonConvert.DeserializeObject <AdminShell.Submodel>(text,
                                                                                                       new AdminShellConverters.JsonAasxConverter("modelType", "name"));
                                    }
                                }
                            }
                        }
                    }
                    catch
                    {
                    }
                }

                if (submodel != null)
                {
                    AdminShell.SubmodelElementCollection smcSubmodel = new AdminShell.SubmodelElementCollection();
                    smcSubmodel.idShort = submodel.idShort;
                    foreach (var sme in submodel.submodelElements)
                    {
                        smcSubmodel.Add(sme.submodelElement);
                    }
                    smc2.Add(smcSubmodel);
                }
            }

            return(true);
        }
Example #6
0
        public static bool operation_wait(AdminShell.Operation op, i40LanguageAutomaton auto)
        {
            // inputVariable reference waitingTime: property value
            // outputVariable reference endTime: property value

            if (op.inputVariable.Count != 1 && op.outputVariable.Count != 1)
            {
                return(false);
            }

            var in1 = op.inputVariable.First();
            var r1  = in1.value.submodelElement;

            if (!(r1 is AdminShell.ReferenceElement))
            {
                return(false);
            }
            var ref1 = Program.env[0].AasEnv.FindReferableByReference((r1 as AdminShell.ReferenceElement).value);

            if (!(ref1 is AdminShell.Property))
            {
                return(false);
            }
            var p1          = ref1 as AdminShell.Property;
            int waitingTime = Convert.ToInt32(p1.value);

            var out1 = op.outputVariable.First();
            var r2   = out1.value.submodelElement;

            if (!(r2 is AdminShell.ReferenceElement))
            {
                return(false);
            }
            var ref2 = Program.env[0].AasEnv.FindReferableByReference((r2 as AdminShell.ReferenceElement).value);

            if (!(ref2 is AdminShell.Property))
            {
                return(false);
            }
            var p2 = ref2 as AdminShell.Property;

            DateTime localTime = DateTime.Now;

            if (p2.value == "") // start
            {
                var endTime = localTime.AddSeconds(waitingTime);
                p2.value = endTime.ToString();
                Console.WriteLine("endTime = " + p2.value);
            }
            else // test if time has elapsed
            {
                Console.WriteLine("localTime = " + localTime);
                var endTime = DateTime.Parse(p2.value);
                if (DateTime.Compare(localTime, endTime) > 0)
                {
                    p2.value = "";
                    return(true);
                }
            }

            return(false);
        }
Example #7
0
 public static bool operation_clearMessages(AdminShell.Operation op, i40LanguageAutomaton auto)
 {
     auto.getMessages.Clear();
     return(true);
 }
Example #8
0
        static public void initialize()
        {
            int aascount = Program.env.Length;

            for (int envi = 0; envi < aascount; envi++)
            {
                if (Program.env[envi] != null)
                {
                    foreach (var sm in Program.env[envi].AasEnv.Submodels)
                    {
                        if (sm != null && sm.idShort != null)
                        {
                            bool withI40Language = false;
                            int  count           = sm.qualifiers.Count;
                            if (count != 0)
                            {
                                int j = 0;

                                while (j < count) // Scan qualifiers
                                {
                                    var p = sm.qualifiers[j] as AdminShell.Qualifier;

                                    if (p.type == "i40language")
                                    {
                                        withI40Language = true;
                                    }
                                    j++;
                                }
                            }
                            if (withI40Language)
                            {
                                var auto = new i40LanguageAutomaton();
                                automatons.Add(auto);
                                auto.name = sm.idShort;
                                if (auto.name == "automatonServiceRequester")
                                {
                                    isRequester = true;
                                }
                                if (auto.name == "automatonServiceProvider")
                                {
                                    isProvider = true;
                                }

                                foreach (var smw1 in sm.submodelElements)
                                {
                                    var sme1 = smw1.submodelElement;

                                    if (sme1 is AdminShell.SubmodelElementCollection && sme1.idShort == "automatonControl")
                                    {
                                        var smc1 = sme1 as AdminShell.SubmodelElementCollection;
                                        auto.automatonControl = smc1;

                                        foreach (var smw2 in smc1.value)
                                        {
                                            var sme2 = smw2.submodelElement;
                                            if (sme2 is AdminShell.Property && sme2.idShort == "setSteppingTime")
                                            {
                                                string value = (sme2 as AdminShell.Property).value;
                                                Regex  regex = new Regex(@"^\d+$");
                                                if (regex.IsMatch(value))
                                                {
                                                    auto.setSteppingTime = value;
                                                }
                                                else
                                                {
                                                    (sme2 as AdminShell.Property).value = auto.setSteppingTime;
                                                }
                                            }
                                        }
                                    }

                                    if (sme1 is AdminShell.SubmodelElementCollection && sme1.idShort == "frames")
                                    {
                                        var smc1 = sme1 as AdminShell.SubmodelElementCollection;
                                        auto.frameNames   = new string[smc1.value.Count];
                                        auto.frameContent = new Dictionary <string, string> [smc1.value.Count];

                                        int i = 0;
                                        foreach (var smw2 in smc1.value)
                                        {
                                            var sme2 = smw2.submodelElement;
                                            if (sme2 is AdminShell.SubmodelElementCollection)
                                            {
                                                var smc2 = sme2 as AdminShell.SubmodelElementCollection;
                                                auto.frameNames[i]   = smc2.idShort;
                                                auto.frameContent[i] = new Dictionary <string, string>();

                                                foreach (var smw3 in smc2.value)
                                                {
                                                    var sme3 = smw3.submodelElement;
                                                    if (sme3 is AdminShell.Property)
                                                    {
                                                        auto.frameContent[i].Add(sme3.idShort, (sme3 as AdminShell.Property).value);
                                                    }
                                                }
                                            }
                                            i++;
                                        }
                                    }

                                    if (sme1 is AdminShell.SubmodelElementCollection && sme1.idShort == "constants")
                                    {
                                        var smc1 = sme1 as AdminShell.SubmodelElementCollection;
                                        auto.constants = new Dictionary <string, string>();

                                        foreach (var smw2 in smc1.value)
                                        {
                                            var sme2 = smw2.submodelElement;
                                            if (sme2 is AdminShell.Property)
                                            {
                                                auto.constants.Add(sme2.idShort, (sme2 as AdminShell.Property).value);
                                            }
                                        }
                                    }

                                    if (sme1 is AdminShell.SubmodelElementCollection && sme1.idShort == "variables")
                                    {
                                        var smc1 = sme1 as AdminShell.SubmodelElementCollection;
                                        auto.variablesStrings     = new Dictionary <string, string>();
                                        auto.variablesCollections = new Dictionary <string, AdminShell.SubmodelElementCollection>();

                                        foreach (var smw2 in smc1.value)
                                        {
                                            var sme2 = smw2.submodelElement;
                                            if (sme2 is AdminShell.Property)
                                            {
                                                auto.variablesStrings.Add(sme2.idShort, (sme2 as AdminShell.Property).value);
                                            }
                                            if (sme2 is AdminShell.SubmodelElementCollection)
                                            {
                                                auto.variablesCollections.Add(sme2.idShort, (sme2 as AdminShell.SubmodelElementCollection));
                                            }
                                        }
                                    }

                                    if (sme1 is AdminShell.SubmodelElementCollection && sme1.idShort == "states")
                                    {
                                        var smc1 = sme1 as AdminShell.SubmodelElementCollection;

                                        foreach (var smw2 in smc1.value)
                                        {
                                            var sme2 = smw2.submodelElement;
                                            if (sme2 is AdminShell.Property || (sme2 is AdminShell.SubmodelElementCollection && sme2.idShort != "initialStates"))
                                            {
                                                auto.states.Add(sme2.idShort);
                                            }
                                            if (sme2 is AdminShell.SubmodelElementCollection && sme2.idShort == "initialStates")
                                            {
                                                var smc2 = sme2 as AdminShell.SubmodelElementCollection;

                                                foreach (var smw3 in smc2.value)
                                                {
                                                    var sme3 = smw3.submodelElement;
                                                    if (sme3 is AdminShell.Property || (sme3 is AdminShell.SubmodelElementCollection && sme3.idShort != "initialStates"))
                                                    {
                                                        auto.initialStates.Add(sme3.idShort);
                                                        auto.states.Add(sme3.idShort);
                                                    }
                                                }
                                            }
                                        }
                                    }

                                    if (sme1 is AdminShell.SubmodelElementCollection && sme1.idShort == "transitions")
                                    {
                                        var smc1 = sme1 as AdminShell.SubmodelElementCollection;
                                        auto.transitions = new List <AdminShell.SubmodelElementCollection>();

                                        foreach (var smw2 in smc1.value)
                                        {
                                            var sme2 = smw2.submodelElement;
                                            if (sme2 is AdminShell.SubmodelElementCollection)
                                            {
                                                auto.transitions.Add(sme2 as AdminShell.SubmodelElementCollection);
                                            }
                                        }
                                    }
                                }

                                auto.actualStates = auto.initialStates;
                                auto.getStatus    = "run";
                                auto.maxTick      = Convert.ToInt32(auto.setSteppingTime);
                            }
                        }
                    }
                }
            }

            if (automatons.Count != 0)
            {
                threadDelegate    = new ThreadStart(nextTick);
                i40LanguageThread = new Thread(threadDelegate);
                i40LanguageThread.Start();
            }
        }