static bool TryUpdateRequirementType <T>(TraitRequirement requirement, int dataId,
                                                 QueryResult result, MARSTraitDataProvider <T> provider)
        {
            if (requirement.Required)
            {
                if (provider.dictionary.TryGetValue(requirement.TraitName, out var traitValues))
                {
                    if (traitValues.TryGetValue(dataId, out var value))
                    {
                        result.SetTrait(requirement.TraitName, value);
                        return(true);
                    }

                    return(false);
                }

                return(false);
            }
            else
            {
                if (provider.dictionary.TryGetValue(requirement.TraitName, out var traitValues))
                {
                    if (traitValues.TryGetValue(dataId, out var value))
                    {
                        result.SetTrait(requirement.TraitName, value);
                    }
                }

                return(true);
            }
        }
        static bool FindTraitCollections <T>(MARSTraitDataProvider <T> traitProvider,
                                             IRelation <T>[] conditions,
                                             List <RelationTraitCache.ChildTraits <T> > traitCollections)
        {
            traitCollections.Clear();
            foreach (var condition in conditions)
            {
                if (!traitProvider.TryGetAllTraitValues(condition.child1TraitName, out var traits))
                {
                    return(false);
                }

                if (!traitProvider.TryGetAllTraitValues(condition.child2TraitName, out var traits2))
                {
                    return(false);
                }

                traitCollections.Add(new RelationTraitCache.ChildTraits <T>()
                {
                    One = traits,
                    Two = traits2
                });
            }

            return(true);
        }
        static bool ConditionsPass <T>(MARSTraitDataProvider <T> traitProvider, ICondition <T>[] conditions, int dataId)
        {
            foreach (var condition in conditions)
            {
                var passed = traitProvider.TryGetTraitValue(dataId, condition.traitName, out var value) &&
                             condition.PassesCondition(ref value);

                if (!passed)
                {
                    return(false);
                }
            }

            return(true);
        }
        void RegisterTraitTypeProvider <T>(MARSTraitDataProvider <T> traitProvider)
        {
            var tType = typeof(T);

            if (TypeToFilterAction.ContainsKey(tType))
            {
                const string message = "There is already a registered trait data provider for type {0}, " +
                                       "not adding the new one";

                Debug.LogWarningFormat(message, tType.FullName);
            }

            // this mapping will allow us to later filter match sets based on the presence or absence of traits,
            // without having to directly refer to types
            TypeToFilterAction.Add(tType, traitProvider.FilterByTraitPresence);
        }
        public bool RateConditionMatches <T>(ICondition <T>[] conditions, MARSTraitDataProvider <T> traitProvider,
                                             List <Dictionary <int, float> > preAllocatedRatingStorage)
        {
            if (conditions == null)
            {
                return(true);
            }

            for (var i = 0; i < conditions.Length; i++)
            {
                var condition = conditions[i];
                Dictionary <int, T> traits;

                if (!traitProvider.TryGetAllTraitValues(condition.traitName, out traits))
                {
                    return(false);              // trait not found
                }
                var ratings = preAllocatedRatingStorage[i];
                ratings.Clear();
                foreach (var kvp in traits)
                {
                    var value  = kvp.Value;
                    var rating = condition.RateDataMatch(ref value);

                    // exclude all failing pieces of data from the results list
                    if (rating <= 0f)
                    {
                        continue;
                    }

                    var id = kvp.Key;
                    ratings.Add(id, rating);
                }

                if (ratings.Count == 0)        // trait found, but no matches
                {
                    return(false);
                }
            }

            return(true);
        }
        internal static void GetAllTraitsForId <T>(int dataId, MARSTraitDataProvider <T> traitCollection, Dictionary <string, T> results)
        {
            foreach (var trait in traitCollection.dictionary)
            {
                var traitDictionary = trait.Value;
                var traitName       = trait.Key;

                if (traitDictionary.TryGetValue(dataId, out var value))
                {
                    if (results.ContainsKey(traitName))
                    {
                        results[traitName] = value;
                    }
                    else
                    {
                        results.Add(traitName, value);
                    }
                }
            }
        }
Exemplo n.º 7
0
        void InitializeTraitProviders(Action <int> setDirtyIfNeeded)
        {
            SemanticTagTraitProvider = new MARSTraitDataProvider <System.Boolean>(setDirtyIfNeeded, this);
            RegisterTraitTypeProvider(SemanticTagTraitProvider);

            IntTraitProvider = new MARSTraitDataProvider <System.Int32>(setDirtyIfNeeded, this);
            RegisterTraitTypeProvider(IntTraitProvider);

            FloatTraitProvider = new MARSTraitDataProvider <System.Single>(setDirtyIfNeeded, this);
            RegisterTraitTypeProvider(FloatTraitProvider);

            StringTraitProvider = new MARSTraitDataProvider <System.String>(setDirtyIfNeeded, this);
            RegisterTraitTypeProvider(StringTraitProvider);

            PoseTraitProvider = new MARSTraitDataProvider <UnityEngine.Pose>(setDirtyIfNeeded, this);
            RegisterTraitTypeProvider(PoseTraitProvider);

            Vector2TraitProvider = new MARSTraitDataProvider <UnityEngine.Vector2>(setDirtyIfNeeded, this);
            RegisterTraitTypeProvider(Vector2TraitProvider);
        }
Exemplo n.º 8
0
        static void DrawTraitCollection <T>(string label, MARSTraitDataProvider <T> provider)
        {
            if (provider == null || provider.dictionary.Count < 1)
            {
                return;
            }

            EditorGUILayout.Separator();
            EditorGUILayout.LabelField(label, EditorStyles.largeLabel);

            foreach (var traitsKvp in provider.dictionary)
            {
                k_StringBuilder.Length = 0;
                EditorGUILayout.LabelField("Trait name: " + traitsKvp.Key, EditorStyles.boldLabel);

                foreach (var kvp in traitsKvp.Value)
                {
                    k_StringBuilder.AppendFormat("ID: {0}, Value: {1}\n", kvp.Key, kvp.Value);
                }

                EditorGUILayout.HelpBox(k_StringBuilder.ToString(), MessageType.None);
            }
        }
        static void FillRelationChildResults <T>(MARSTraitDataProvider <T> traitProvider,
                                                 Dictionary <IMRObject, QueryResult> childResults, Dictionary <IMRObject, int> dataAssignments,
                                                 IRelation <T>[] typeRelations)
        {
            foreach (var relation in typeRelations)
            {
                var child1 = relation.child1;
                if (childResults.TryGetValue(child1, out var child1Result))
                {
                    var trait1Name = relation.child1TraitName;
                    traitProvider.TryGetTraitValue(dataAssignments[child1], trait1Name, out var value1);
                    child1Result.SetTrait(trait1Name, value1);
                }

                var child2 = relation.child2;
                if (childResults.TryGetValue(child2, out var child2Result))
                {
                    var trait2Name = relation.child2TraitName;
                    traitProvider.TryGetTraitValue(dataAssignments[child2], trait2Name, out var value2);
                    child2Result.SetTrait(trait2Name, value2);
                }
            }
        }
        static void FillRequirementType <T>(TraitRequirement requirement, int dataId,
                                            QueryResult result, MARSTraitDataProvider <T> provider)
        {
            if (requirement.Required)
            {
                // if we get an error with any of the dictionary access here, that means something
                // went wrong in the trait filtering stage, which should ensure that this
                // trait, with this type & data ID, is present in the database.
                var value = provider.dictionary[requirement.TraitName][dataId];
                result.SetTrait(requirement.TraitName, value);
            }
            else
            {
                if (!provider.dictionary.TryGetValue(requirement.TraitName, out var traitValues))
                {
                    return;
                }

                if (traitValues.TryGetValue(dataId, out var value))
                {
                    result.SetTrait(requirement.TraitName, value);
                }
            }
        }
Exemplo n.º 11
0
 internal void GetTraitProvider(out MARSTraitDataProvider <UnityEngine.Pose> provider)
 {
     provider = PoseTraitProvider;
 }
Exemplo n.º 12
0
 internal void GetTraitProvider(out MARSTraitDataProvider <System.String> provider)
 {
     provider = StringTraitProvider;
 }
Exemplo n.º 13
0
 internal void GetTraitProvider(out MARSTraitDataProvider <System.Single> provider)
 {
     provider = FloatTraitProvider;
 }
Exemplo n.º 14
0
 internal void GetTraitProvider(out MARSTraitDataProvider <System.Int32> provider)
 {
     provider = IntTraitProvider;
 }
Exemplo n.º 15
0
 internal void GetTraitProvider(out MARSTraitDataProvider <System.Boolean> provider)
 {
     provider = SemanticTagTraitProvider;
 }
Exemplo n.º 16
0
 internal void GetTraitProvider(out MARSTraitDataProvider <UnityEngine.Vector2> provider)
 {
     provider = Vector2TraitProvider;
 }
        // this function is used by generated code
        static bool CheckIfDataStillMatches <T>(Dictionary <IMRObject, int> dataAssignments,
                                                IRelation <T>[] relations, MARSTraitDataProvider <T> traitProvider,
                                                Dictionary <IMRObject, SetChildArgs> children, HashSet <IMRObject> nonRequiredChildrenLost)
        {
            foreach (var relation in relations)
            {
                var child1 = relation.child1;
                var child2 = relation.child2;

                // If a child is not in dataAssignments then it is a non-required child that was lost in a previous update,
                // and there's no need to check this relation.
                if (!dataAssignments.TryGetValue(child1, out var dataID1) || !dataAssignments.TryGetValue(child2, out var dataID2))
                {
                    continue;
                }

                var child1Required = children[child1].required;
                var child2Required = children[child2].required;
                var trait1Name     = relation.child1TraitName;
                var trait2Name     = relation.child2TraitName;
                var trait1Exists   = traitProvider.TryGetTraitValue(dataID1, trait1Name, out var trait1Value);
                var trait2Exists   = traitProvider.TryGetTraitValue(dataID2, trait2Name, out var trait2Value);

                if (!trait1Exists)
                {
                    if (child1Required)
                    {
                        return(false);
                    }

                    nonRequiredChildrenLost.Add(child1);
                }

                if (!trait2Exists)
                {
                    if (child2Required)
                    {
                        return(false);
                    }

                    nonRequiredChildrenLost.Add(child2);
                }

                if (trait1Exists && trait2Exists && !relation.PassesRelation(ref trait1Value, ref trait2Value))
                {
                    if (child1Required && child2Required)
                    {
                        return(false);
                    }

                    if (!child1Required)
                    {
                        nonRequiredChildrenLost.Add(child1);
                    }

                    if (!child2Required)
                    {
                        nonRequiredChildrenLost.Add(child2);
                    }
                }
            }
            return(true);
        }
 // ReSharper disable once UnusedMember.Global
 internal void GetTraitProvider <T>(out MARSTraitDataProvider <T> providerObject)
 {
     providerObject = default;
 }