コード例 #1
0
        public void TestGetExpressionParameters()
        {
            // Create a content filtered topic without expression parameters
            string query     = "Id <= 1";
            string topicName = "FilteredTopic";
            ContentFilteredTopic filteredTopic = _participant.CreateContentFilteredTopic(topicName, _topic, query);

            Assert.IsNotNull(filteredTopic);

            // Test with null parameter
            ReturnCode result = filteredTopic.GetExpressionParameters(null);

            Assert.AreEqual(ReturnCode.BadParameter, result);

            // Check empty expression parameters
            List <string> parameters = new List <string> {
                "1"
            };

            result = filteredTopic.GetExpressionParameters(parameters);
            Assert.AreEqual(ReturnCode.Ok, result);
            Assert.IsNotNull(parameters);
            Assert.AreEqual(0, parameters.Count);

            // Create a content filtered topic with one expression parameter
            query     = "Id <= %0";
            topicName = "FilteredTopic1";
            string parameter1 = "10";
            ContentFilteredTopic filteredTopic1 = _participant.CreateContentFilteredTopic(topicName, _topic, query, parameter1);

            Assert.IsNotNull(filteredTopic1);

            parameters = new List <string> {
                "1"
            };
            result = filteredTopic1.GetExpressionParameters(parameters);
            Assert.AreEqual(ReturnCode.Ok, result);
            Assert.IsNotNull(parameters);
            Assert.AreEqual(1, parameters.Count);
            Assert.AreEqual(parameter1, parameters[0]);

            // Create a content filtered topic with two expression parameter
            query      = "Id <= %0 AND Id <= %1";
            topicName  = "FilteredTopic2";
            parameter1 = "0";
            string parameter2 = "10";
            ContentFilteredTopic filteredTopic2 = _participant.CreateContentFilteredTopic(topicName, _topic, query, parameter1, parameter2);

            Assert.IsNotNull(filteredTopic2);

            parameters = new List <string> {
                "1"
            };
            result = filteredTopic2.GetExpressionParameters(parameters);
            Assert.AreEqual(ReturnCode.Ok, result);
            Assert.IsNotNull(parameters);
            Assert.AreEqual(2, parameters.Count);
            Assert.AreEqual(parameter1, parameters[0]);
            Assert.AreEqual(parameter2, parameters[1]);
        }
コード例 #2
0
        public void TestProperties()
        {
            // Create a content filtered topic and check the properties
            TestStructTypeSupport support = new TestStructTypeSupport();
            string typeName  = support.GetTypeName();
            string query     = "Id <= 1";
            string topicName = "FilteredTopic";
            ContentFilteredTopic filteredTopic = _participant.CreateContentFilteredTopic(topicName, _topic, query);

            Assert.IsNotNull(filteredTopic);
            Assert.AreEqual(topicName, filteredTopic.Name);
            Assert.AreEqual(typeName, filteredTopic.TypeName);
            Assert.AreEqual(query, filteredTopic.FilterExpression);
            Assert.AreEqual(_participant, filteredTopic.Participant);
            Assert.AreEqual(_topic, filteredTopic.RelatedTopic);
        }
コード例 #3
0
        public void TestSetExpressionParameters()
        {
            // Create a content filtered topic with two expression parameter
            string query      = "Id <= %0 AND Id <= %1";
            string topicName  = "FilteredTopic";
            string parameter1 = "0";
            string parameter2 = "10";

            // Test with incorrect number of parameters
            ContentFilteredTopic filteredTopic = _participant.CreateContentFilteredTopic(topicName, _topic, query, parameter1);

            Assert.IsNull(filteredTopic);

            // Test with correct number of parameters
            filteredTopic = _participant.CreateContentFilteredTopic(topicName, _topic, query, parameter1, parameter2);
            Assert.IsNotNull(filteredTopic);

            // Test with null parameter
            ReturnCode result = filteredTopic.SetExpressionParameters(null);

            Assert.AreEqual(ReturnCode.BadParameter, result);

            // Test with incorrect number of expression parameters
            result = filteredTopic.SetExpressionParameters(parameter1);
            Assert.AreEqual(ReturnCode.Error, result);

            result = filteredTopic.SetExpressionParameters();
            Assert.AreEqual(ReturnCode.Error, result);

            // Test with correct number of expression parameters
            result = filteredTopic.SetExpressionParameters(parameter2, parameter1);
            Assert.AreEqual(ReturnCode.Ok, result);

            List <string> parameters = new List <string> {
                "1"
            };

            result = filteredTopic.GetExpressionParameters(parameters);
            Assert.AreEqual(ReturnCode.Ok, result);
            Assert.IsNotNull(parameters);
            Assert.AreEqual(2, parameters.Count);
            Assert.AreEqual(parameter2, parameters[0]);
            Assert.AreEqual(parameter1, parameters[1]);
        }
コード例 #4
0
        /// <summary>
        /// Runs the subscriber example.
        /// </summary>
        public static void RunSubscriber(
            int domainId    = 0,
            int sampleCount = int.MaxValue)
        {
            // A DomainParticipant allows an application to begin communicating in
            // a DDS domain. Typically there is one DomainParticipant per application.
            // DomainParticipant QoS is configured in USER_QOS_PROFILES.xml
            //
            // A participant needs to be Disposed to release middleware resources.
            // The 'using' keyword indicates that it will be Disposed when this
            // scope ends.
            using DomainParticipant participant = DomainParticipantFactory.Instance.CreateParticipant(domainId);

            // A Topic has a name and a datatype.
            Topic <StockPrice> topic = participant.CreateTopic <StockPrice>("Example market_data");

            // A Subscriber allows an application to create one or more DataReaders
            // Subscriber QoS is configured in USER_QOS_PROFILES.xml
            Subscriber subscriber = participant.CreateSubscriber();

            // Create a ContentFiltered Topic and specify the STRINGMATCH filter name
            // to use a built-in filter for matching multiple strings.
            // More information can be found in the example
            var symbolFilter = new Filter(
                expression: "Symbol MATCH 'A'",
                parameters: Array.Empty <string>(),
                name: Filter.StringMatchFilterName);

            ContentFilteredTopic <StockPrice> filteredTopic =
                participant.CreateContentFilteredTopic(
                    name: "Filtered Example market_data",
                    relatedTopic: topic,
                    filter: symbolFilter);

            // Create a reader for the content-filtered topic, and set a
            // handler for the DataAvailable event that simply prints the data.
            DataReader <StockPrice> reader = subscriber.CreateDataReader(
                filteredTopic,
                preEnableAction: reader =>
            {
                reader.DataAvailable += _ => PrintData(reader);
            });

            int t = 0;

            while (receivedSamples < sampleCount)
            {
                if (t == 3)
                {
                    // On t = 3 we add the symbol 'D' to the filter parameter
                    // to match 'A' and 'D'.
                    filteredTopic.AppendToExpressionParameter(0, "D");
                    Console.WriteLine("Changed filter to Symbol MATCH 'A,D'");
                }
                else if (t == 6)
                {
                    // On t = 6 we remove the symbol 'A' to the filter parameter
                    // to match only 'D'.
                    filteredTopic.RemoveFromExpressionParameter(0, "A");
                    Console.WriteLine("Changed filter to Symbol MATCH 'D'");
                }

                Thread.Sleep(4000);
                t++;
            }
        }
コード例 #5
0
        private void RunExample(int domainId, string stationKind)
        {
            if (!Enum.TryParse <StationKind>(stationKind, out var currentStation))
            {
                throw new ArgumentException("Invalid station");
            }

            // A DomainParticipant allows an application to begin communicating in
            // a DDS domain. Typically there is one DomainParticipant per application.
            // Uses TemperingApplication QoS profile to set participant name.
            var qosProvider = new QosProvider("./qos_profiles.xml");

            // By specifying a default library, we can later refer to the
            // profiles without the library name
            qosProvider.DefaultLibrary = "ChocolateFactoryLibrary";

            DomainParticipant participant = DomainParticipantFactory.Instance
                                            .CreateParticipant(
                domainId,
                qosProvider.GetDomainParticipantQos("IngredientApplication"));

            Topic <Temperature> temperatureTopic =
                participant.CreateTopic <Temperature>("ChocolateTemperature");
            Topic <ChocolateLotState> lotStateTopic =
                participant.CreateTopic <ChocolateLotState>("ChocolateLotState");

            ContentFilteredTopic <ChocolateLotState> filteredLotStateTopic =
                participant.CreateContentFilteredTopic(
                    name: "FilteredLot",
                    relatedTopic: lotStateTopic,
                    filter: new Filter(
                        expression: "next_station = %0",
                        parameters: new string[] { $"'{stationKind}'" }));

            Publisher publisher = participant.CreatePublisher();

            // Create DataWriter of Topic "ChocolateLotState"
            // using ChocolateLotStateProfile QoS profile for State Data
            DataWriter <ChocolateLotState> lotStateWriter = publisher.CreateDataWriter(
                lotStateTopic,
                qosProvider.GetDataWriterQos("ChocolateLotStateProfile"));

            Subscriber subscriber = participant.CreateSubscriber();

            // Create DataReader of Topic "ChocolateLotState", filtered by
            // next_station, and using ChocolateLotStateProfile QoS profile for
            // State Data.
            DataReader <ChocolateLotState> lotStateReader = subscriber.CreateDataReader(
                filteredLotStateTopic,
                qosProvider.GetDataReaderQos("ChocolateLotStateProfile"));

            // Monitor the DataAvailable status
            StatusCondition statusCondition = lotStateReader.StatusCondition;

            statusCondition.EnabledStatuses = StatusMask.DataAvailable;
            statusCondition.Triggered      +=
                _ => ProcessLot(currentStation, lotStateReader, lotStateWriter);
            var waitset = new WaitSet();

            waitset.AttachCondition(statusCondition);

            while (!shutdownRequested)
            {
                // Wait for ChocolateLotState
                Console.WriteLine("Waiting for lot");
                waitset.Dispatch(Duration.FromSeconds(10));
            }
        }
        private void RunExample(
            int domainId       = 0,
            uint lotsToProcess = 10)
        {
            // Loads the QoS from the qos_profiles.xml file.
            var qosProvider = new QosProvider("./qos_profiles.xml");

            // A DomainParticipant allows an application to begin communicating in
            // a DDS domain. Typically there is one DomainParticipant per application.
            // Load DomainParticipant QoS profile
            var participantQos = qosProvider.GetDomainParticipantQos(
                "ChocolateFactoryLibrary::MonitoringControlApplication");
            DomainParticipant participant = DomainParticipantFactory.Instance
                                            .CreateParticipant(domainId, participantQos);

            // A Topic has a name and a datatype. Create a Topic with type
            // ChocolateLotState.  Topic name is a constant defined in the IDL file.
            Topic <ChocolateLotState> lotStateTopic =
                participant.CreateTopic <ChocolateLotState>("ChocolateLotState");
            // Add a Topic for Temperature to this application
            Topic <Temperature> temperatureTopic =
                participant.CreateTopic <Temperature>("ChocolateTemperature");
            ContentFilteredTopic <Temperature> filteredTemperatureTopic =
                participant.CreateContentFilteredTopic(
                    name: "FilteredTemperature",
                    relatedTopic: temperatureTopic,
                    filter: new Filter(
                        expression: "degrees > %0 or degrees < %1",
                        parameters: new string[] { "32", "30" }));

            // A Publisher allows an application to create one or more DataWriters
            // Publisher QoS is configured in USER_QOS_PROFILES.xml
            Publisher publisher = participant.CreatePublisher();

            // This DataWriter writes data on Topic "ChocolateLotState"
            var writerQos = qosProvider.GetDataWriterQos(
                "ChocolateFactoryLibrary::ChocolateLotStateProfile");
            DataWriter <ChocolateLotState> lotStateWriter =
                publisher.CreateDataWriter(lotStateTopic, writerQos);

            // A Subscriber allows an application to create one or more DataReaders
            // Subscriber QoS is configured in USER_QOS_PROFILES.xml
            Subscriber subscriber = participant.CreateSubscriber();

            // Create DataReader of Topic "ChocolateLotState".
            // DataReader QoS is configured in USER_QOS_PROFILES.xml
            var readerQos = qosProvider.GetDataReaderQos(
                "ChocolateFactoryLibrary::ChocolateLotStateProfile");
            DataReader <ChocolateLotState> lotStateReader =
                subscriber.CreateDataReader(lotStateTopic, readerQos);

            // Add a DataReader for Temperature to this application
            readerQos = qosProvider.GetDataReaderQos(
                "ChocolateFactoryLibrary::ChocolateTemperatureProfile");
            DataReader <Temperature> temperatureReader =
                subscriber.CreateDataReader(filteredTemperatureTopic, readerQos);

            // Obtain the DataReader's Status Condition
            StatusCondition temperatureStatusCondition = temperatureReader.StatusCondition;

            temperatureStatusCondition.EnabledStatuses = StatusMask.DataAvailable;

            // Associate a handler with the status condition. This will run when the
            // condition is triggered, in the context of the dispatch call (see below)
            temperatureStatusCondition.Triggered +=
                _ => MonitorTemperature(temperatureReader);

            // Do the same with the lotStateReader's StatusCondition
            StatusCondition lotStateStatusCondition = lotStateReader.StatusCondition;

            lotStateStatusCondition.EnabledStatuses = StatusMask.DataAvailable;

            int lotsProcessed = 0;

            lotStateStatusCondition.Triggered +=
                _ => lotsProcessed            += MonitorLotState(lotStateReader);

            // Create a WaitSet and attach the StatusCondition
            var waitset = new WaitSet();

            waitset.AttachCondition(lotStateStatusCondition);

            // Add the new DataReader's StatusCondition to the Waitset
            waitset.AttachCondition(temperatureStatusCondition);

            // Start publishing in a separate thread
            var startLotTask = Task.Run(() => PublishStartLot(lotStateWriter, lotsToProcess));

            while (!shutdownRequested && lotsProcessed < lotsToProcess)
            {
                waitset.Dispatch(Duration.FromSeconds(4));
            }

            startLotTask.Wait();
        }