Beispiel #1
0
        private static SetOfPrimitives loadPrimitives(List<TouchPoint2> simplegestures, IEnumerable<IPrimitiveConditionData> primitives)
        {
            List<IPrimitiveConditionData> returnList;
            SetOfPrimitives output = new SetOfPrimitives();

            foreach (IPrimitiveConditionData primitive in primitives)
            {
                returnList = null;
                IPrimitiveConditionValidator ruleValidator = GestureLanguageProcessor.GetPrimitiveConditionValidator(primitive);
                returnList = ruleValidator.GenerateRules(simplegestures);
                if (returnList != null)
                {
                    int index = 1;
                    foreach (IPrimitiveConditionData resultData in returnList)
                    {
                        if (resultData != null)
                        {
                            PrimitiveData primitiveData = new PrimitiveData();
                            if (primitive.isComplex())
                            {
                                primitiveData.ID = getIDsFromGDL(resultData.ToGDL());
                            }
                            else
                            {
                                primitiveData.IDadd(index++);
                            }
                            primitiveData.Name = getPrimitiveName(primitive);
                            string strValue = getStrValueFromGDL(resultData.ToGDL());
                            double value;
                            bool isDouble = double.TryParse(strValue, out value);
                            if (isDouble)
                                primitiveData.Value = value;
                            else
                            {
                                int idx = strValue.IndexOf(",");
                                if (idx >= 0)
                                    primitiveData.ListValue = new List<string>(strValue.Split(','));
                                else
                                    primitiveData.ListValue.Add(strValue);
                            }
                            output.Add(primitiveData);

                        }
                        else
                            index++;
                    }
                }
            }
            return output;
        }
Beispiel #2
0
        private static void checkProportionforPrimitive(PrimitiveData reference)
        {
            /*
             * On the same sample, compare to see the relation between the basic primitives, if there is any proportion, if find any, create a new complex primitive that will
             * reference the basic similar ones
             */
            if (reference.Value == 0)
            {
                return;

            }

            /*  IEnumerable<PrimitiveData> similarInSameStep = from set in samples
                                                             from primitive in set
                                                             where primitive.Name == data.Name
                                                             && primitive.ID.Count == 1
                                                             && data.IDcompare(primitive.ID)
                                                             select primitive;
              */

            PrimitiveData originalData = reference;
            SetOfPrimitives gesture;
            List<SetOfPrimitives> proportions = new List<SetOfPrimitives>();
            PrimitiveData newPrimitive = null;
            List<double> values = new List<double>();

            values.Add(reference.Value);

            int i = 0;
            gesture = samples.ElementAt(i);

            while (i < samples.Count)
            {
                PrimitiveData compare;
                for (int j = 0; j < gesture.Count; j++)
                {
                    compare = gesture[j];

                    if ((reference.Name == compare.Name) && (reference.IDcompare(compare.ID)))
                    {
                        values.Add(compare.Value);
                    }

                    if (reference.Equals(compare))
                    {
                        continue;
                    }

                    if ((reference.Name == compare.Name) && (compare.ID.Count == 1) && (reference.ID[0] < compare.ID[0]))
                    {
                        newPrimitive = new PrimitiveData();
                        newPrimitive.Name = reference.Name;
                        newPrimitive.IDadd(reference.ID[0]);
                        newPrimitive.IDadd(compare.ID[0]);
                        newPrimitive.Value = compare.Value / reference.Value;

                        while (proportions.Count <= i)
                        {
                            proportions.Add(new SetOfPrimitives());
                        }

                        proportions[i].Add(newPrimitive);
                    }
                }
                i++;
                if (i < samples.Count)
                {
                    gesture = samples.ElementAt(i);
                    reference = getPrimitiveWithSameName(reference, gesture);
                    if (reference == null)
                        return;

                    values.Add(reference.Value);

                }
            }
            // After the loops, the proportions should have the same amount of items as samples
            // and each setofprimitive of proportion should have the same amount of primitives
            // If not, then nothing will be add to solution
            // if it is, the same comparison should be done in proportions

            if (proportions.Count < samples.Count)
            {
                // No proportion was found, try to see if the primitive is a constant among the samples
                checkConstantValuePrimitives(originalData);
                return;
            }
            List<double> alphas;
            int setCount = proportions[0].Count;
            for (i = 0; i < proportions[0].Count; i++)
            {
                alphas = new List<double>();

                foreach (SetOfPrimitives set in proportions)
                {
                    if (setCount != set.Count)
                        return;

                    alphas.Add(set[i].Value);
                }

                double value = Math.Round(values.Average(), 2);
                double maxDifference = alphas.Max() - alphas.Min();
                double tolerance = Math.Round(value * matchingAccuracy, 2);

                if (maxDifference > tolerance)
                {
                    newPrimitive = new PrimitiveData();
                    newPrimitive.ID = originalData.ID;
                    newPrimitive.Name = originalData.Name;
                    newPrimitive.ListValue.Add((value - tolerance) + ".." + (value + tolerance));
                    solution.checkAndAdd(newPrimitive);
                }

                else if ((maxDifference != 0))
                {
                    string variable = VariableName.getAvailableLetter();
                    // Adds the x primitive
                    newPrimitive = new PrimitiveData();
                    newPrimitive.IDadd(proportions[0][i].ID[0]);
                    newPrimitive.Name = proportions[0][i].Name;
                    newPrimitive.Value = 0;
                    newPrimitive.ListValue.Add(variable);
                    solution.checkAndAdd(newPrimitive);

                    // adds the alpha*x primitive
                    newPrimitive = new PrimitiveData();
                    newPrimitive.IDadd(proportions[0][i].ID[1]);
                    newPrimitive.Name = proportions[0][i].Name;
                    newPrimitive.Value = 0;
                    double alpha = Math.Round(alphas.Average(), 2);
                    //newPrimitive.strValue = alpha + "x";
                    tolerance = Math.Round(alpha * matchingAccuracy, 2);
                    newPrimitive.ListValue.Add((alpha - tolerance) + variable + ".." + (alpha + tolerance) + variable);
                    solution.checkAndAdd(newPrimitive);

                }
                else
                {
                    newPrimitive = new PrimitiveData();
                    newPrimitive.IDadd(proportions[0][i].ID[0]);
                    newPrimitive.Name = proportions[0][i].Name;
                    newPrimitive.Value = proportions[0][i].Value;
                    solution.checkAndAdd(newPrimitive);

                    newPrimitive = new PrimitiveData();
                    newPrimitive.IDadd(proportions[0][i].ID[1]);
                    newPrimitive.Name = proportions[0][i].Name;
                    newPrimitive.Value = proportions[0][i].Value;
                    solution.checkAndAdd(newPrimitive);
                }

            }
        }