Exemplo n.º 1
0
        public void GetOrderedEffectsByCapabilityTest()
        {
            CapabilityValue target = new CapabilityValue(); // TODO: Initialize to an appropriate value
            CapabilityValue.Effect ef = new CapabilityValue.Effect("Capability1", 1000.0, 10, 1);
            target.effects.Add(ef);
            ef = new CapabilityValue.Effect("Capability2", 2000.0, 20, 1);
            target.effects.Add(ef);
            ef = new CapabilityValue.Effect("Capability1", 2000.0, 20, 1);
            target.effects.Add(ef);
            ef = new CapabilityValue.Effect("Capability1", 500.0, 5, 0.5);
            target.effects.Add(ef);
            ef = new CapabilityValue.Effect("Capability2", 555.0, 6, 0.6);
            target.effects.Add(ef);
            ef = new CapabilityValue.Effect("Capability1", 5000.0, 50, 1);
            target.effects.Add(ef);

            //this shows that the function returns the correctly ordered list for Capability1
            List<CapabilityValue.Effect> actual;
            actual = target.GetOrderedEffectsByCapability("Capability1");
            Assert.AreEqual(actual.Count, 4);
            Assert.AreEqual(actual[0].range, 500.0);
            Assert.AreEqual(actual[1].range, 1000.0);
            Assert.AreEqual(actual[0].intensity, 5);
            Assert.AreEqual(actual[1].intensity, 10);

            //this shows that the function returns the correctly ordered list for Capability2
            actual = target.GetOrderedEffectsByCapability("Capability2");
            Assert.AreEqual(actual.Count, 2);
            Assert.AreEqual(actual[0].range, 555.0);
            Assert.AreEqual(actual[1].range, 2000.0);
            Assert.AreEqual(actual[0].intensity, 6);
            Assert.AreEqual(actual[1].intensity, 20);

//this shows that removal from the returned list doesn't affect the structure of the internal list.
            actual.RemoveAt(1); 

            actual = target.effects;
            Assert.AreEqual(actual.Count, 6);
            Assert.AreEqual(actual[0].range, 1000.0);
            Assert.AreEqual(actual[1].range, 2000.0);
            Assert.AreEqual(actual[0].intensity, 10);
            Assert.AreEqual(actual[1].intensity, 20);
        }
Exemplo n.º 2
0
        /*
        static public void SkipToReset(ref List<SimulationEvent> eventList)
        {
            bool resetFound = false;

            foreach (SimulationEvent e in eventList)
            {
                if (e.eventType == "ResetSimulation")
                {
                    resetFound = true;
                }
            }

            if (resetFound)
            {
                while (eventList.Count > 0 && eventList[0].eventType != "ResetSimulation")
                {
                    eventList.Remove(eventList[0]);
                }
            }
        }
         */
        static public CapabilityValue.Effect FindCapabilityEffect(CapabilityValue cap, VulnerabilityValue vuln)
        {
            List<string> vulnNames = new List<string>();

            foreach (VulnerabilityValue.Transition t in vuln.transitions)
            {
                foreach (VulnerabilityValue.TransitionCondition tc in t.conditions)
                {
                    if (!vulnNames.Contains(tc.capability))
                    {
                        vulnNames.Add(tc.capability);
                    }
                }
            }

            foreach (CapabilityValue.Effect e in cap.effects)
            {
                if (vulnNames.Contains(e.name))
                {
                    return e;
                }
            }
            return null;
        }
Exemplo n.º 3
0
 /// <summary>
 /// Sends an event to the server which updates a single object's Capability object.
 /// This is a complicated data structure.  It contains a series of lists, whose values are related.
 /// These lists contain a series of Capabilities, Ranges, Intensities, and Probabilities.  The index of each of these
 /// collections should correlate such that Capabilities[1], Ranges[1], Intensities[1] and Probabilities[1] are all
 /// for a single capability.  As these are TIGHTLY correlated, you cannot skip inserting any values into this collection.
 /// </summary>
 /// <param name="objectID">The object's ID</param>
 /// <param name="capabilities">The value list index relates to the other List collections passed in as parameters.</param>
 /// <param name="ranges">The value list index relates to the other List collections passed in as parameters.</param>
 /// <param name="intensities">The value list index relates to the other List collections passed in as parameters.</param>
 /// <param name="probabilities">The value list index relates to the other List collections passed in as parameters.</param>
 public void UpdateObjectCapabilities(String objectID, List<String> capabilities, List<Double> ranges, List<int> intensities, List<Double> probabilities)
 {
     CapabilityValue dv = new CapabilityValue();
     //construct list of CapName/Range/Intensity/Probability
     if ((capabilities.Count == ranges.Count) && (ranges.Count == intensities.Count) && (intensities.Count == probabilities.Count))
     {
         for (int x = 0; x < capabilities.Count; x++)
         {
             CapabilityValue.Effect ef = new CapabilityValue.Effect(capabilities[x], ranges[x], intensities[x], probabilities[x]);
             dv.effects.Add(ef);
         }
         SendObjectAttributeUpdateEvent(objectID, "Capability", dv);
     }
     else
     {
         Console.WriteLine("In UpdateObjectCapabilities: Lists of Capabilities, Ranges, Intensities and Probabilities have different counts, no event sent");
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Takes an xml string, and returns a DataValue object.
        /// Returns null if the xml doesn't represent a DataValue.
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        public static DataValue XMLDeserialize(string xml)
        {
            Match m = typeregex.Match(xml);
            if (m.Success)
            {
                Group g = m.Groups[1];

                string dataType = g.ToString();
                switch (dataType)
                {
                    case "StringType":
                        StringValue sv = new StringValue();
                        sv.FromXML(xml);
                        return sv;
                    case "DoubleType":
                        DoubleValue dv = new DoubleValue();
                        dv.FromXML(xml);
                        return dv;
                    case "IntegerType":
                        IntegerValue iv = new IntegerValue();
                        iv.FromXML(xml);
                        return iv;
                    case "BooleanType":
                        BooleanValue bv = new BooleanValue();
                        bv.FromXML(xml);
                        return bv;
                    case "LocationType":
                        LocationValue lv = new LocationValue();
                        lv.FromXML(xml);
                        return lv;
                    case "VelocityType":
                        VelocityValue vv = new VelocityValue();
                        vv.FromXML(xml);
                        return vv;
                    case "AttributeCollectionType":
                        AttributeCollectionValue av = new AttributeCollectionValue();
                        av.FromXML(xml);
                        return av;
                    case "CustomAttributesType":
                        CustomAttributesValue cav = new CustomAttributesValue();
                        cav.FromXML(xml);
                        return cav;
                    case "StringListType":
                        StringListValue slv = new StringListValue();
                        slv.FromXML(xml);
                        return slv;
                    case "PolygonType":
                        PolygonValue polyv = new PolygonValue();
                        polyv.FromXML(xml);
                        return polyv;
                    case "StateTableType":
                        StateTableValue stv = new StateTableValue();
                        stv.FromXML(xml);
                        return stv;
                    case "CapabilityType":
                        CapabilityValue cv = new CapabilityValue();
                        cv.FromXML(xml);
                        return cv;
                    case "VulnerabilityType":
                        VulnerabilityValue vv2 = new VulnerabilityValue();
                        vv2.FromXML(xml);
                        return vv2;
                    case "ConeType":
                        ConeValue cv2 = new ConeValue();
                        cv2.FromXML(xml);
                        return cv2;
                    case "SensorType":
                        SensorValue sv2 = new SensorValue();
                        sv2.FromXML(xml);
                        return sv2;
                    case "SensorArrayType":
                        SensorArrayValue sav = new SensorArrayValue();
                        sav.FromXML(xml);
                        return sav;
                    case "EmitterType":
                        EmitterValue ev = new EmitterValue();
                        ev.FromXML(xml);
                        return ev;
                    case "RangeRingDisplayType":
                        RangeRingDisplayValue rrdv = new RangeRingDisplayValue();
                        rrdv.FromXML(xml);
                        return rrdv;
                    case "AttackCollectionType":
                        AttackCollectionValue attCV = new AttackCollectionValue();
                        attCV.FromXML(xml);
                        return attCV;
                    case "WrapperType":
                        WrapperValue wrapper = new WrapperValue();
                        wrapper.FromXML(xml);
                        return wrapper;
                    case "ClassificationDisplayRulesType":
                        ClassificationDisplayRulesValue cdrv = new ClassificationDisplayRulesValue();
                        cdrv.FromXML(xml);
                        return cdrv;
                    default:
                        return null;
                }
            }
            else
            {
                return null;
            }

        }
Exemplo n.º 5
0
        private void ChangeObjectCapability(CapabilityValue newCapabilitySet, string objectID)
        {
            SimulationObjectProxy obj = objectProxies[objectID];
            if (obj == null)
                return;

            CapabilityValue previousCapabilityValue = obj["Capability"].GetDataValue() as CapabilityValue;
            List<string> previousCapabilityNames = previousCapabilityValue.GetCapabilityNames();
            List<string> newCapabilityNames = newCapabilitySet.GetCapabilityNames();
            List<string> missingCapabilities = new List<string>();
            foreach (string capName in previousCapabilityNames)
            {
                if (!newCapabilityNames.Contains(capName))
                {
                    if (!missingCapabilities.Contains(capName))
                    {
                        missingCapabilities.Add(capName);
                    }
                }
            }
            foreach (string capName in missingCapabilities)
            {
                CancelCapabilityAttack(capName, objectID);
            }

            obj["Capability"].SetDataValue(newCapabilitySet);
            missingCapabilities.Clear();
            newCapabilityNames.Clear();
            previousCapabilityNames.Clear();
        }
Exemplo n.º 6
0
        public void TestAttackProcessorSimAttackObjectLogic()
        { 
            //SETUP
            CapabilityValue target = new CapabilityValue(); 
            CapabilityValue.Effect ef = new CapabilityValue.Effect("Capability1", 1000.0, 10, 1);
            target.effects.Add(ef);
            ef = new CapabilityValue.Effect("Capability2", 2000.0, 20, 1);
            target.effects.Add(ef);
            ef = new CapabilityValue.Effect("Capability1", 2000.0, 20, 1);
            target.effects.Add(ef);
            ef = new CapabilityValue.Effect("Capability1", 500.0, 5, 0.5);
            target.effects.Add(ef);
            ef = new CapabilityValue.Effect("Capability2", 555.0, 6, 0.6);
            target.effects.Add(ef);
            ef = new CapabilityValue.Effect("Capability1", 5000.0, 50, 1);
            target.effects.Add(ef);

            double[] ranges = { 200.0, 800.0, 1500.0, 5000.0 };
            int[] expectedIntensities = {  5, 10, 20, 50 };

            //TEST
            for (int x = 0; x < ranges.Length; x++)//double distance in ranges)
            {
                List<CapabilityValue.Effect> capabilities = target.GetOrderedEffectsByCapability("Capability1");
                bool testComplete = false;
                foreach (CapabilityValue.Effect eff in capabilities)
                {
                    if (eff.name == "Capability1" && ranges[x] <= eff.range)
                    {
                        //int r = random.Next(0, 100);
                        //if (r <= ((int)(eff.probability * 100)))
                        //{
                        //appliedIntensity = (int)(eff.intensity * newAttack.percentageApplied / 100);
                        //newAttack.appliedIntensity = appliedIntensity;
                        //targetVul.ApplyEffect(eff.name, appliedIntensity, distance, ref random);

                        //}
                        //break outside of the if because if the probability failed, you dont want a second chance with a different range.
                        Assert.AreEqual(eff.intensity, expectedIntensities[x]);
                        testComplete = true;
                        break; //break because we only want to apply the first range found that satisfied the distance restraint
                    }
                }
                Assert.IsTrue(testComplete, System.String.Format("Incorrect value when testing for range {0}", ranges[x]));
            }
            List<CapabilityValue.Effect> capability = target.GetOrderedEffectsByCapability("Capability1");
            bool testCompleted = false;
            foreach (CapabilityValue.Effect eff in capability)
            {
                if (eff.name == "Capability1" && 6000 <= eff.range)
                {
                    //int r = random.Next(0, 100);
                    //if (r <= ((int)(eff.probability * 100)))
                    //{
                    //appliedIntensity = (int)(eff.intensity * newAttack.percentageApplied / 100);
                    //newAttack.appliedIntensity = appliedIntensity;
                    //targetVul.ApplyEffect(eff.name, appliedIntensity, distance, ref random);

                    //}
                    //break outside of the if because if the probability failed, you dont want a second chance with a different range.
                    
                    testCompleted = true;
                    break; //break because we only want to apply the first range found that satisfied the distance restraint
                }
            }
            Assert.IsFalse(testCompleted, System.String.Format("Incorrect value when testing for range {0}", 6000));
        }
Exemplo n.º 7
0
        /// <summary>
        /// This method takes in a Create_EventType (ScenCon defined object), and retrieves
        /// the data from the object, packages up the data into a SimulationEvent (SimCore defined
        /// object), and then sends the event to the network server.  This method was pulled out of
        /// the main block of code to simplify sending the event and code readability.
        /// </summary>
        /// <param name="incoming">The Create_EventType object whose data is packaged
        /// into an outgoing SimulationEvent.</param>
        private static void SendCreateEvent(Create_EventType incoming)
        {
            SimulationEvent e = SimulationEventFactory.BuildEvent(ref simModelInfo, "NewObject");
            Dictionary<string, DataValue> myAtt = new Dictionary<string, DataValue>();
            Dictionary<string, object> DictionaryOfStates;
            DataValue dv;
            ObjectInfo myObject;
            string objectType = incoming.Genus.ToString();
            myObject = simModelInfo.objectModel.objects[objectType];
            dv = new StateTableValue();

            try
            {
                e["Time"] = DataValueFactory.BuildInteger(incoming.Time);//ConvertInteger(incoming.Timer);
                e["ObjectType"] = DataValueFactory.BuildString(objectType); //ConvertString(objectType);
                e["ID"] = DataValueFactory.BuildString(incoming.UnitID);//ConvertString(incoming.UnitID);

                /*********** Define the State Table ***********************/
                DictionaryOfStates = incoming.Parameters.GetDictionary();
            }
            catch
            {
                throw new Exception("Missing Required Data for Create Event");
            }
            Dictionary<string, DataValue> SimCoreStateTable = new Dictionary<string, DataValue>();

            foreach (KeyValuePair<string, object> kvp in DictionaryOfStates)
            {
                DataValue capabilities,
                          vulnerabilities,
                          currentSimCoreState;

                capabilities = new CapabilityValue();
                vulnerabilities = new VulnerabilityValue();
                currentSimCoreState = new AttributeCollectionValue();
                ExtendedStateBody currentScenConState = new ExtendedStateBody();

                currentScenConState = (ExtendedStateBody)incoming.Parameters[kvp.Key];

                capabilities = DefineCapabilityValue(currentScenConState.Capabilities);
                ((AttributeCollectionValue)currentSimCoreState).attributes.Add("Capability", capabilities);

                vulnerabilities = DefineVulnerabilityValue(currentScenConState.Vulnerabilities, currentScenConState.Combinations);
                ((AttributeCollectionValue)currentSimCoreState).attributes.Add("Vulnerability", vulnerabilities);

                DataValue sensorArray = DataValueFactory.BuildValue("SensorArrayType");
                DataValue sensor;

                Dictionary<string, SensorType> sensors = new Dictionary<string, SensorType>();

                Dictionary<string, List<ConeValue>> ranges;
                sensors = currentScenConState.Sensors;

                foreach (string sensorKind in sensors.Keys)
                {
                    ranges = new Dictionary<string, List<ConeValue>>();
                    sensor = DataValueFactory.BuildValue("SensorType");
                    SensorType typeOfSensor = sensors[sensorKind];
                    string attributeSensed = typeOfSensor.Attribute;
                    List<Cone> cones = typeOfSensor.Cones;
                    double maxRange = 0.0;

                    ((SensorValue)sensor).attIsEngram[attributeSensed] = sensors[sensorKind].IsEngram;
                    List<ConeValue> simCoreCones = new List<ConeValue>();

                    foreach (Cone aCone in sensors[sensorKind].Cones)
                    {
                        LocationValue direction = new LocationValue();
                        direction.X = aCone.Direction.X;
                        direction.Y = aCone.Direction.Y;
                        direction.Z = aCone.Direction.Z;
                        direction.exists = true;
                        ConeValue cv = new ConeValue();
                        cv.direction = direction;
                        cv.extent = aCone.Extent;
                        if (aCone.Extent > maxRange)
                            maxRange = aCone.Extent;
                        cv.level = aCone.Level;
                        cv.spread = aCone.Spread;
                        simCoreCones.Add(cv);
                    }

                    ranges.Add(attributeSensed, simCoreCones);
                    ((SensorValue)sensor).ranges = ranges;
                    ((SensorValue)sensor).sensorName = sensorKind;
                    if (((SensorValue)sensor).maxRange < maxRange)
                    {
                        ((SensorValue)sensor).maxRange = maxRange;
                    }
                    ((SensorArrayValue)sensorArray).sensors.Add((SensorValue)sensor);
                }


                ((AttributeCollectionValue)currentSimCoreState).attributes.Add("Sensors", sensorArray);

                //Emitters
                DataValue emitter = DataValueFactory.BuildValue("EmitterType");

                Dictionary<string, EmitterType> scenConEmitters = currentScenConState.Emitters;
                EmitterType emission;
                foreach (string s in scenConEmitters.Keys)
                {
                    emission = scenConEmitters[s].DeepCopy();
                    string attributeName = s;
                    ((EmitterValue)emitter).attIsEngram[s] = emission.IsEngram;
                    // string level;
                    //        double variance;
                    Dictionary<string, double> levels = new Dictionary<string, double>();
                    foreach (string level in emission.Levels.Keys)
                    {
                        //           levels.Add(level, (double)(emission[level]));
                        double dublet = Double.Parse((emission.Levels[level].ToString()));
                        levels.Add(level, dublet);
                    }
                    ((EmitterValue)emitter).emitters.Add(attributeName, levels);
                }

                ((AttributeCollectionValue)currentSimCoreState).attributes.Add("Emitters", emitter);

                foreach (AttributeInfo attr in myObject.attributes.Values)
                {
                    string attrType = attr.dataType;
                    string simCoreName = attr.name;
                    string scenConKey = convertSimCoreToScenCon(simCoreName);

                    if (currentScenConState.Parameters.ContainsKey(scenConKey))
                    {
                        switch (attrType)
                        {
                            case "StringType":
                                ((AttributeCollectionValue)currentSimCoreState).attributes.Add(simCoreName, DataValueFactory.BuildString(Convert.ToString(currentScenConState.Parameters[scenConKey])));
                                break;/*ConvertString(Convert.ToString(currentScenConState.Parameters[scenConKey])*/

                            case "IntegerType":
                                ((AttributeCollectionValue)currentSimCoreState).attributes.Add(simCoreName, DataValueFactory.BuildInteger(Convert.ToInt32(currentScenConState.Parameters[scenConKey])));
                                break;/*ConvertInteger(Convert.ToInt32(currentScenConState.Parameters[scenConKey])*/

                            case "DoubleType":
                                ((AttributeCollectionValue)currentSimCoreState).attributes.Add(simCoreName, DataValueFactory.BuildDouble(Convert.ToDouble(currentScenConState.Parameters[scenConKey])));
                                break;/*ConvertDouble(Convert.ToDouble(currentScenConState.Parameters[scenConKey])*/

                            case "LocationType":
                                LocationType lt = currentScenConState.Parameters[scenConKey] as LocationType;
                                ((AttributeCollectionValue)currentSimCoreState).attributes.Add(simCoreName, DataValueFactory.BuildLocation(lt.X, lt.Y, lt.Z, false));
                                break;/*ConvertLocation((LocationType)currentScenConState.Parameters[scenConKey])*/

                            case "VelocityType":
                                VelocityType vt = currentScenConState.Parameters[scenConKey] as VelocityType;
                                ((AttributeCollectionValue)currentSimCoreState).attributes.Add(simCoreName, DataValueFactory.BuildVelocity(vt.VX, vt.VY, vt.VZ));
                                break;/*ConvertVelocity((VelocityType)currentScenConState.Parameters[scenConKey])*/
                            case "BooleanType":
                                if (currentScenConState.Parameters.ContainsKey(attr.name))
                                {
                                    string booleanVal = currentScenConState.Parameters[attr.name].ToString();
                                    bool value = false;
                                    if (booleanVal == "true" || booleanVal == "True" || booleanVal == "TRUE")
                                    {
                                        value = true;
                                    }
                                    ((AttributeCollectionValue)currentSimCoreState).attributes.Add(simCoreName, DataValueFactory.BuildBoolean(value));
                                }/*ConvertBoolean(value)*/
                                break;
                            case "StringListType":
                                ((AttributeCollectionValue)currentSimCoreState).attributes.Add(simCoreName, DataValueFactory.BuildStringList((List<String>)currentScenConState.Parameters[scenConKey]));
                                break;
                            case "ClassificationDisplayRulesType":
                                ((AttributeCollectionValue)currentSimCoreState).attributes.Add(simCoreName, (DataValue)currentScenConState.Parameters[scenConKey]);
                                break;
                            default:
                                break;
                        }
                    }
                }

                SimCoreStateTable.Add(kvp.Key, currentSimCoreState);
            }
            dv = new StateTableValue();
            ((StateTableValue)dv).states = SimCoreStateTable;
            e["StateTable"] = dv;

            ((AttributeCollectionValue)e["Attributes"]).attributes.Add("OwnerID", DataValueFactory.BuildString(incoming.Owner));/*ConvertString(    .Owner)*/
            ((AttributeCollectionValue)e["Attributes"]).attributes.Add("ClassName", DataValueFactory.BuildString(incoming.UnitBase));/*ConvertString(incoming.UnitBase)*/


            server.PutEvent(e);
        }