Пример #1
0
        private ShapeNode CreateShapeNode(ShapeSymbol ss, object obj, out List <object> relGoals)
        {
            var gn = new ShapeNode(ss);

            relGoals = new List <object>();
            var lst = obj as List <Tuple <object, object> >;

            if (lst == null)
            {
                return(gn);
            }

            foreach (var tuple in lst)
            {
                if (tuple.Item1.Equals(ss))
                {
                    BuildRelation(gn, tuple.Item2);
                    relGoals.Add(tuple.Item2);
                }

                if (tuple.Item2.Equals(ss))
                {
                    BuildRelation(tuple.Item1, gn);
                    relGoals.Add(tuple.Item1);
                }
            }

            _nodes.Add(gn);
            return(gn);
        }
        private static bool Reify(ShapeSymbol currShape, ShapeSymbol shape1, ShapeSymbol shape2)
        {
            var line = currShape as LineSymbol;

            if (line != null)
            {
                var pt1 = shape1 as PointSymbol;
                var pt2 = shape2 as PointSymbol;
                Debug.Assert(pt1 != null);
                Debug.Assert(pt2 != null);
                return(line.Reify(pt1, pt2));
            }

            var lineSeg = currShape as LineSegmentSymbol;

            if (lineSeg != null)
            {
                var pt1 = shape1 as PointSymbol;
                var pt2 = shape2 as PointSymbol;
                Debug.Assert(pt1 != null);
                Debug.Assert(pt2 != null);
                return(lineSeg.Reify(pt1, pt2));
            }

            throw new Exception("TODO");
        }
Пример #3
0
        public static Expr Generate(ShapeSymbol ss)
        {
            var ps = ss as PointSymbol;

            if (ps != null)
            {
                return(ps.ToExpr());
            }

            var ls = ss as LineSymbol;

            if (ls != null)
            {
                return(ls.ToExpr());
            }

            var lineSeg = ss as LineSegmentSymbol;

            if (lineSeg != null)
            {
                return(lineSeg.ToExpr());
            }

            var circle = ss as CircleSymbol;

            if (circle != null)
            {
                return(circle.ToExpr());
            }

            return(null);
        }
Пример #4
0
        public bool RelationExist(ShapeSymbol shape)
        {
            object relations;

            ConstraintSatisfy(shape, out relations);
            var lst = relations as List <Tuple <object, object> >;

            Debug.Assert(lst != null);
            return(lst.Count != 0);
        }
 public static string Plot(ShapeSymbol ss)
 {
     if (ss.Shape.Concrete)
     {
         return(string.Format("Plot shape {0} onto the geometrical side", ss));
     }
     else
     {
         return(string.Format("Write shape {0} onto the algebraic side", ss));
     }
 }
Пример #6
0
        /// <summary>
        /// ShapeSymbol Input Pattern Match
        /// </summary>
        /// <param name="expr"></param>
        /// <param name="ss"></param>
        /// <param name="output"></param>
        /// <returns></returns>
        private bool EvalExprPatterns(Expr expr, ShapeSymbol ss, out object output, bool userInput = false)
        {
            if (!userInput)
            {
                RelationGraph.AddNode(ss);
            }

            //expr = ExprG.Generate(ss);
            output = new AGShapeExpr(expr, ss);
            return(true);
        }
        public static bool Reify(ShapeSymbol currShape, object depend1, object depend2)
        {
            var ss1 = depend1 as ShapeSymbol;
            var ss2 = depend2 as ShapeSymbol;

            if (ss1 != null && ss2 != null)
            {
                return(Reify(currShape, ss1, ss2));
            }
            //TODO
            return(false);
        }
Пример #8
0
        //Geometry Side Input Variation
        private object Load(ShapeSymbol rTemp, bool tutorMode = false)
        {
            Expr   expr = ExprG.Generate(rTemp);
            object output;

            EvalExprPatterns(expr, rTemp, null, out output, tutorMode);
            var iKnowledge = output as IKnowledge;

            if (iKnowledge != null && !tutorMode)
            {
                _cache.Add(new KeyValuePair <object, object>(rTemp, iKnowledge));
            }
            return(output);
        }
Пример #9
0
        //Geometry Side Input Variation
        private object UnLoad(ShapeSymbol rTemp)
        {
            List <KeyValuePair <object, object> > fact
                = _cache.Where(x => x.Key.Equals(rTemp)).ToList();

            if (fact.Count != 0)
            {
                Debug.Assert(fact.Count == 1);
                bool result = UnEvalExprPatterns(fact[0].Value);
                _cache.Remove(fact[0]);
                return(result);
            }
            return(false);
        }
        private bool DispatchUnreify(ShapeSymbol ss, EqGoal goal)
        {
            var ps = ss as PointSymbol;
            var ls = ss as LineSymbol;

            if (ps != null)
            {
                return(ps.UnReify(goal));
            }
            if (ls != null)
            {
                return(ls.UnReify(goal));
            }
            return(false);
        }
 public ShapeNode RetrieveShapeNode(ShapeSymbol ss)
 {
     foreach (var gn in Nodes)
     {
         var sn = gn as ShapeNode;
         if (sn == null)
         {
             continue;
         }
         if (sn.ShapeSymbol.Equals(ss))
         {
             return(sn);
         }
     }
     return(null);
 }
Пример #12
0
        private bool DeleteShapeNode(ShapeSymbol shape)
        {
            var shapeNode = SearchNode(shape) as ShapeNode;

            if (shapeNode == null)
            {
                return(false);
            }
            _nodes.Remove(shapeNode);
            UpdateUpperQueryNode(shapeNode);
            UnReify(shapeNode);
            DeleteSynNode(shapeNode);
            UnBuildRelation(shapeNode);
            _preCache.Remove(shape);
            return(true);
        }
Пример #13
0
        public bool ConstraintSatisfy(ShapeSymbol ss, out object obj)
        {
            var graphNodes = _nodes;

            var lst = new List <Tuple <object, object> >();

            #region Unary Relation Checking

            for (var i = 0; i < graphNodes.Count; i++)
            {
                var goalNode = graphNodes[i] as GoalNode;
                if (goalNode != null)
                {
                    var goal = goalNode.Goal as EqGoal;
                    if (goal != null)
                    {
                        if (ss.UnifyExplicitProperty(goal))
                        {
                            lst.Add(new Tuple <object, object>(goalNode, ss));
                        }
                    }
                }
                var shapeNode = graphNodes[i] as ShapeNode;
                if (shapeNode != null)
                {
                    var tempSs = shapeNode.ShapeSymbol;
                    if (tempSs != null)
                    {
                        if (ss.UnifyShape(tempSs))
                        {
                            lst.Add(new Tuple <object, object>(ss, tempSs));
                        }
                        if (tempSs.UnifyShape(ss))
                        {
                            lst.Add(new Tuple <object, object>(tempSs, ss));
                        }
                    }
                }
            }

            #endregion

            obj = lst;
            return(lst.Count != 0);
        }
Пример #14
0
 private void InternalValidate(Expr expr, ShapeSymbol ss, out object output)
 {
     output = false;
     foreach (var gn in RelationGraph.Nodes)
     {
         var sn = gn as ShapeNode;
         if (sn == null)
         {
             continue;
         }
         bool result = sn.ShapeSymbol.ApproximateMatch(ss);
         if (result)
         {
             output = true;
             return;
         }
     }
     output = RelationGraph.RelationExist(ss);
 }
Пример #15
0
        private bool ShapeVerify(IKnowledge obj, ShapeSymbol shape, out string msg, out object output)
        {
            msg    = AGTutorMessage.VerifyWrong;
            output = null;

            List <Tuple <object, object> > trace = null;

            var agShapeExpr = new AGShapeExpr(obj.Expr, shape);

            agShapeExpr.IsSelected = true;
            agShapeExpr.GenerateSolvingTrace();
            trace = agShapeExpr.AutoTrace;

            if (trace == null || trace.Count == 0)
            {
                return(false);
            }

            /*   var lastTuple = trace[trace.Count - 1] as Tuple<object, object>;
             * var lastLst = lastTuple.Item2 as List<object>;
             * Debug.Assert(lastLst != null);
             * Debug.Assert(lastLst.Count != 0);
             * var lastTs = lastLst[lastLst.Count - 1] as TraceStepExpr;*/
            bool matchResult = UserGraph.Match(trace); //match and update

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

            //insert nodes
            UserGraph.Insert(trace);
            CurrentStateNode = UserGraph.SearchInnerLoopNode(obj); //update _currentStateNode;
            //var nextTuple1 = UserGraph.SearchNextInnerLoopNode(CurrentStateNode);

            /*  if (nextTuple1 == null) // query-end
             * {
             *    msg = AGTutorMessage.SolvedProblem;
             *    return true;
             * }*/
            msg = AGTutorMessage.VerifyCorrect;
            return(true);
        }
Пример #16
0
        private ShapeNode AddShapeNode(ShapeSymbol shape)
        {
            //1. build relation using geometry constraint-solving (synthesize new node) (Top-Down)
            object relations;

            ConstraintSatisfy(shape, out relations);
            List <object> goalRels;
            ShapeNode     shapeNode = CreateShapeNode(shape, relations, out goalRels);

            //Search bottom up goalNodes, start reify
            foreach (object obj in goalRels)
            {
                var goalNode = obj as GoalNode;
                if (goalNode == null)
                {
                    continue;
                }
                Reify(goalNode);
            }
            return(shapeNode);
        }
Пример #17
0
        public void SplitComponent(string query, ShapeSymbol symbol)
        {
            this.log.Trace("COMP: {0}, {1}", query, this.CurrentScope.Transform);

            var currentVol = currentNode.Value.Volume;
            var components = currentVol.Query(query);

            foreach (var cmp in components)
            {
                // Get the correct position of the component based on the current scope and volume.
                var newPos = this.CurrentScope.Transform.Position + (this.CurrentScope.Transform.Rotation * Vector3.Scale(currentVol.Transform.Scale, cmp.Transform.Position));

                // Get the correct rotation of the component based on the current scope.
                var newRot = this.CurrentScope.Transform.Rotation * cmp.Transform.Rotation;

                // Rotate the volume's scale by the rotation of the component so that we can apply the correct scale to the component.
                // Rotating a scale vector can result in negative values, so we need to make sure that they are all positive.
                //          var s = currentVol.Transform.Scale;
                //          s = cmp.Rotation * s;
                //          s = new Vector3(Mathf.Abs(s.x), Mathf.Abs(s.y), Mathf.Abs(s.z));
                //          var newScale = s;
                //          var newScale = cmp.AxisMap(currentVol.Transform.Scale);
                var newScale = cmp.AxisMap(this.CurrentScope.Transform.Scale);
                newScale = Vector3.Scale(newScale, cmp.Transform.Scale);

                var newTrans = new SimpleTransform(newPos, newRot, newScale);

                this.log.Trace("**** {0}", newTrans);

                var node = this.NewNode(this.currentNode);
                node.Value.Transform = newTrans;
                node.Value.Rule      = this.rules [symbol.Name];
                node.Value.Args      = this.ResolveArgs(symbol.UnresolvedArgs).ToList(); //symbol.ResolvedArgs.ToList();

                this.AddNode(node);
            }
        }
Пример #18
0
 public AGShapeExpr(starPadSDK.MathExpr.Expr expr, ShapeSymbol ss)
     : base(expr)
 {
     _shapeSymbol = ss;
 }
Пример #19
0
        public override bool UnifyShape(ShapeSymbol ss)
        {
            var ps = ss as PointSymbol;

            if (ps == null)
            {
                return(false);
            }
            var pt = ps.Shape as Point;

            Debug.Assert(pt != null);
            var line = Shape as Line;

            Debug.Assert(line != null);

            Term xTerm = null;

            if (line.A != null)
            {
                xTerm = new Term(Expression.Multiply, new List <object>()
                {
                    line.A, pt.XCoordinate
                });
            }
            Term yTerm = null;

            if (line.B != null)
            {
                yTerm = new Term(Expression.Multiply, new List <object>()
                {
                    line.B, pt.YCoordinate
                });
            }

            var lst = new List <object>();

            if (xTerm != null)
            {
                lst.Add(xTerm);
            }
            if (yTerm != null)
            {
                lst.Add(yTerm);
            }
            if (line.C != null)
            {
                lst.Add(line.C);
            }

            var lhs = new Term(Expression.Add, lst);
            var eq  = new Equation(lhs, 0);

            object obj;
            bool?  satisified = eq.Eval(out obj, false);

            if (satisified == null)
            {
                return(false);
            }
            if (satisified.Value)
            {
                return(true);
            }

            return(false);
        }
Пример #20
0
 public ShapeNode(ShapeSymbol shape)
 {
     _shape = shape;
 }
Пример #21
0
        public void Repeat(string axis, Size size, ShapeSymbol shape)
        {
            this.log.Trace("REPEAT: {0}, {1}", axis, this.CurrentScope.Transform);

            var pos   = this.CurrentScope.Transform.Position;
            var rot   = this.CurrentScope.Transform.Rotation;
            var scale = this.CurrentScope.Transform.Scale;

            Func <float, int>       repetitionsFunc;
            Func <Vector3, Vector3> startPosAction;
            Func <float, Vector3>   deltaAction;
            Func <float, Vector3>   newScaleAction;

            switch (axis.ToUpper())
            {
            case "X":
                repetitionsFunc = (s) => (int)(scale.x / s);
                startPosAction  = (s) => new Vector3(s.x / 2f, 0f, 0f);
                deltaAction     = (s) => new Vector3(s / 2f, 0f, 0f);
                newScaleAction  = (s) => new Vector3(s, scale.y, scale.z);
                break;

            case "Y":
                repetitionsFunc = (s) => (int)(scale.y / s);
                startPosAction  = (s) => new Vector3(0f, s.y / 2f, 0f);
                deltaAction     = (s) => new Vector3(0f, s / 2f, 0f);
                newScaleAction  = (s) => new Vector3(scale.x, s, scale.z);
                break;

            case "Z":
                repetitionsFunc = (s) => (int)(scale.z / s);
                startPosAction  = (s) => new Vector3(0f, 0f, s.z / 2f);
                deltaAction     = (s) => new Vector3(0f, 0f, s / 2f);
                newScaleAction  = (s) => new Vector3(scale.x, scale.y, s);
                break;

            default:
                throw new ArgumentException(string.Format("Unsupported subdivision axis \"{0}\"", axis), "axis");
            }

            var absSize     = size.GetAbsolute(this.CurrentScope.Transform.Scale.x);
            var repetitions = repetitionsFunc(absSize);

            this.log.Trace("repeating {0} {1} times along the {2} axis", shape.Name, repetitions, axis);

            // Start at one end of the selected scope axis.
            var startPos = pos - (rot * startPosAction(scale));

            for (int i = 0; i < repetitions; i++)
            {
                var delta = rot * deltaAction(absSize);

                var newScale = newScaleAction(absSize);
                var newPos   = startPos + delta;

                var node = this.NewNode(this.currentNode);
                node.Value.Rule      = this.rules[shape.Name];
                node.Value.Transform = new SimpleTransform(newPos, rot, newScale);
                node.Value.Args      = this.ResolveArgs(shape.UnresolvedArgs).ToList();
                this.AddNode(node);

                startPos += delta * 2f;
            }
        }
Пример #22
0
        private void EnterSuccessor(ParseTreeNode successorNode)
        {
            foreach (var child in successorNode.ChildNodes)
            {
                if (child.Term.Name == IronyArchitectureGrammar.RuleSymbolName)
                {
                    var symbolName = child.FirstChild.Token.Text;
                    var symbolArgs = child.ChildNodes.Count > 1
                        ? this.GetArgs(child.ChildNodes [1])
                        : new List <Argument> ();

                    var symbol = new ShapeSymbol(symbolName, symbolArgs);

                    var successor = new SymbolShapeSuccessor
                    {
                        Symbol      = symbol,
                        Probability = 1f
                    };

                    this.currentRule.Successors.Last().Successors.Add(successor);
                }
                else if (child.Term.Name == IronyArchitectureGrammar.CommandName)
                {
                    string cmdName;

                    if (child.FirstChild.Term.Name == IronyArchitectureGrammar.SimpleCommandName)
                    {
                        cmdName = child.FirstChild.FirstChild.Token.Text;
                    }
                    else if (child.FirstChild.Term.Name == IronyArchitectureGrammar.ScopeCommandName)
                    {
                        cmdName = child.FirstChild.ChildNodes [1].Token.Text;
                    }
                    else
                    {
                        throw new System.ArgumentException(string.Format("Cannot evalute grammar command node: {0}", child.FirstChild));
                    }

                    var cmd = new ShapeCommand
                    {
                        Name = cmdName,
                    };

                    if (cmdName == "[")
                    {
                        cmd.Name = "Push";
                    }
                    else if (cmdName == "]")
                    {
                        cmd.Name = "Pop";
                    }
                    else
                    {
                        var cmdArgs = child.ChildNodes.Count > 1
                            ? this.GetArgs(child.ChildNodes [1])
                            : new List <Argument> ();
                        cmd.Arguments = cmdArgs.ToArray();

                        var shapes = new List <ShapeSymbol> ();

                        // Check if command block exists
                        if (child.ChildNodes.Count > 2)
                        {
                            var ruleListNode = child.ChildNodes [2];

                            foreach (var shapeNode in ruleListNode.ChildNodes)
                            {
                                var symbolName = shapeNode.FirstChild.Token.Text;
                                var symbolArgs = shapeNode.ChildNodes.Count > 1
                                    ? this.GetArgs(shapeNode.ChildNodes [1])
                                    : new List <Argument> ();

                                shapes.Add(new ShapeSymbol(symbolName, symbolArgs));
                            }
                        }

                        cmd.Shapes = shapes.ToArray();
                    }

                    var cmdSuccessor = new CommandShapeSuccessor
                    {
                        Command = cmd
                    };

                    this.currentRule.Successors.Last().Successors.Add(cmdSuccessor);
                }
            }
        }
Пример #23
0
 public override bool UnifyShape(ShapeSymbol ss)
 {
     return(false);
 }
Пример #24
0
 public override bool UnifyShape(ShapeSymbol ss)
 {
     throw new NotImplementedException();
 }