/* * Annotated scaffolding * */ public static void FromPointsToLineSegment(LineSegmentSymbol lss) { var ls = lss.Shape as LineSegment; Debug.Assert(ls != null); var pt1Symbol = new PointSymbol(ls.Pt1); var pt2Symbol = new PointSymbol(ls.Pt2); string strategy; TraceStep ts00 = new TraceStep(null, pt1Symbol, null, PlottingRule.PlottingStrategy, PlottingRule.Plot(pt1Symbol)); TraceStep ts01 = new TraceStep(null, pt2Symbol, null, PlottingRule.PlottingStrategy, PlottingRule.Plot(pt2Symbol)); TraceStep ts02 = null; if (ls.Pt1.Concrete && ls.Pt2.Concrete) { ts02 = new TraceStep(null, lss, null, PlottingRule.PlottingStrategy, PlottingRule.Plot(lss)); } strategy = "Plot a Line Segment passing through two points."; lss._innerLoop.Add(ts00); lss._innerLoop.Add(ts01); if (ts02 != null) { lss._innerLoop.Add(ts02); } lss.GenerateATrace(strategy); /* string stepMetaRule = "Consider draw a line segment passing through the two points."; string stepAppliedRule = String.Format("Draw the line segment passing through two points {0} and {1}", pt1Symbol, pt2Symbol); var traceStep = new TraceStep(null, lss, stepMetaRule, stepAppliedRule); lss._innerLoop.Add(traceStep);*/ }
public void Test_OneStrategy_MultiTraceStep_Author() { var expr1 = new Term(Expression.Add, new List <object> { 1, 1, 1 }); var expr2 = new Term(Expression.Add, new List <object> { 2, 1 }); var ts1 = new TraceStep(expr1, expr2, null, "meta-rule todo", "rule todo"); var ts1Expr = new TraceStepExpr(ts1); var ts2 = new TraceStep(expr2, 2, null, "meta-rule todo", "rule todo"); var ts2Expr = new TraceStepExpr(ts2); var lst = new List <TraceStepExpr>() { ts1Expr, ts2Expr }; var tuple = new Tuple <object, object>("strategy2", lst); var lstStrategy = new List <Tuple <object, object> >(); lstStrategy.Add(tuple); var graph = new BehaviorGraph(); graph.Insert(lstStrategy); Assert.True(graph.Nodes.Count == 2); var node0 = graph.Nodes[0]; Assert.Null(node0.SubGraph); var node1 = graph.Nodes[1]; Assert.NotNull(node1.SubGraph); Assert.True(node1.SubGraph.Nodes.Count == 3); ///////////////////////////////////////////// // User Input, //2 steps trace var expr3 = new Term(Expression.Add, new List <object> { 1, 2 }); var ts3 = new TraceStep(expr1, expr3, null, "meta-rule todo", "rule todo"); var ts3Expr = new TraceStepExpr(ts3); var ts4 = new TraceStep(expr3, 2, null, "meta-rule todo", "rule todo"); var ts4Expr = new TraceStepExpr(ts4); var lst2 = new List <TraceStepExpr>() { ts3Expr, ts4Expr }; var tuple2 = new Tuple <object, object>("strategy1", lst2); var lstStrategy2 = new List <Tuple <object, object> >(); lstStrategy2.Add(tuple2); //Under the same strategy graph.Update(lstStrategy2); Assert.True(graph.Nodes.Count == 3); Assert.True(node1.SubGraph.Nodes.Count == 3); }
public TraceStepExpr(TraceStep ts) { MetaRule = ts.Rule as string; AppliedRule = ts.AppliedRule as string; Source = ExprG.Generate(ts.Source); Target = ExprG.Generate(ts.Target); //StepExpr = ExprG.Derive(Source, Target); StepExpr = Target; TraceStep = ts; }
public TraceStepExpr(TraceStep ts) { MetaRule = ts.Rule as string; AppliedRule = ts.AppliedRule as string; Source = ExprG.Generate(ts.Source); Target = ExprG.Generate(ts.Target); //StepExpr = ExprG.Derive(Source, Target); StepExpr = Target; TraceStep = ts; }
public static void FromPointPointToLine(PointSymbol ps1, PointSymbol ps2, LineSymbol ls) { var m = new Var("m"); var k = new Var("k"); var x = new Var("x"); var y = new Var("y"); var term = new Term(Expression.Multiply, new List <object>() { m, x }); var term1 = new Term(Expression.Add, new List <object>() { term, k }); var eqPattern = new Equation(y, term1); var term2 = new Term(Expression.Multiply, new List <object>() { m, ps1.SymXCoordinate }); var term22 = new Term(Expression.Add, new List <object>() { term2, k }); var eqPattern1 = new Equation(ps1.SymYCoordinate, term22); var term3 = new Term(Expression.Multiply, new List <object>() { m, ps2.SymXCoordinate }); var term33 = new Term(Expression.Add, new List <object>() { term3, k }); var eqPattern2 = new Equation(ps2.SymYCoordinate, term33); string strategy = "Generate a line by substituting two given points into the line slope-intercept form y=mx+k."; var ts0 = new TraceStep(eqPattern, eqPattern1, SubstitutionRule.SubstituteKC(), SubstitutionRule.SubstitutionStrategy, SubstitutionRule.ApplySubstitute(eqPattern, ps1)); var ts1 = new TraceStep(eqPattern, eqPattern2, SubstitutionRule.SubstituteKC(), SubstitutionRule.SubstitutionStrategy, SubstitutionRule.ApplySubstitute(eqPattern, ps2)); string kc = GeometryScaffold.KC_LineSlopeForm; var ts2 = new TraceStep(null, ls, kc, "calculate m and k through the above two linear equations.", "calculate m and k through linear equation and retrieve y=mx+k line form."); ls._innerLoop.Add(ts0); ls._innerLoop.Add(ts1); ls._innerLoop.Add(ts2); ls.GenerateATrace(strategy); }
/// <summary> /// Returns the current nondeterministic choice as a program trace step. /// </summary> /// <param name="uniqueId">Unique nondet id</param> /// <param name="choice">Choice</param> /// <param name="enabledMachines">Enabled machines</param> /// <returns>TraceStep</returns> private TraceStep GetNondeterministicChoiceTraceStep(string uniqueId, bool choice, HashSet <Machine> enabledMachines) { var fingerprint = PSharpRuntime.CaptureProgramState(); var traceStep = TraceStep.CreateNondeterministicChoice(fingerprint, uniqueId, choice, enabledMachines, PSharpRuntime.LivenessChecker.GetMonitorStatus()); Output.Debug(DebugType.Liveness, "<LivenessDebug> Captured program state '{0}' at " + "nondeterministic choice '{1}-{2}'.", fingerprint.GetHashCode(), uniqueId, choice); return(traceStep); }
public void Test_OneStrategy_MultiTraceStep_UserInput() { var expr1 = new Term(Expression.Add, new List <object> { 1, 1, 1 }); var eq1 = new Equation(expr1, 20); var expr2 = new Term(Expression.Add, new List <object> { 2, 1 }); var ts1 = new TraceStep(eq1, expr2, null, "meta-rule todo", "rule todo"); var ts1Expr = new TraceStepExpr(ts1); var ts2 = new TraceStep(expr2, 2, null, "meta-rule todo", "rule todo"); var ts2Expr = new TraceStepExpr(ts2); var lst = new List <TraceStepExpr>() { ts1Expr, ts2Expr }; var tuple = new Tuple <object, object>("strategy1", lst); var lstStrategy = new List <Tuple <object, object> >(); lstStrategy.Add(tuple); var graph = new BehaviorGraph(); graph.Insert(lstStrategy); Assert.True(graph.Nodes.Count == 2); var node0 = graph.Nodes[0]; Assert.Null(node0.SubGraph); var node1 = graph.Nodes[1]; Assert.NotNull(node1.SubGraph); Assert.True(node1.SubGraph.Nodes.Count == 3); ///////////////////////////////////////////// // User Input var userExpr1 = new Term(Expression.Add, new List <object> { 1, 1, 1 }); var userEq1 = new Equation(userExpr1, 20); var node = graph.SearchInnerLoopNode(userEq1); Assert.NotNull(node); var userExpr2 = new Term(Expression.Add, new List <object> { 1, 3 }); node = graph.SearchInnerLoopNode(userExpr2); Assert.Null(node); }
/// <summary> /// Returns the current scheduling choice as a program trace step. /// </summary> /// <param name="scheduledMachine">Scheduled machine</param> /// <param name="enabledMachines">Enabled machines</param> /// <returns>TraceStep</returns> private TraceStep GetSchedulingChoiceTraceStep(Machine scheduledMachine, HashSet <Machine> enabledMachines) { var fingerprint = PSharpRuntime.CaptureProgramState(); var traceStep = TraceStep.CreateSchedulingChoice(fingerprint, scheduledMachine, enabledMachines, PSharpRuntime.LivenessChecker.GetMonitorStatus()); Output.Debug(DebugType.Liveness, "<LivenessDebug> Captured program state '{0}' at " + "scheduling choice.", fingerprint.GetHashCode()); return(traceStep); }
/// <summary> /// Creates a scheduling choice trace step. /// </summary> /// <param name="index">Index</param> /// <param name="scheduledMachine">Scheduled machine</param> /// <returns>TraceStep</returns> internal static TraceStep CreateSchedulingChoice(int index, AbstractMachine scheduledMachine) { var traceStep = new TraceStep(); traceStep.Index = index; traceStep.Type = TraceStepType.SchedulingChoice; traceStep.ScheduledMachine = scheduledMachine; traceStep.Previous = null; traceStep.Next = null; return traceStep; }
/// <summary> /// Creates a scheduling choice trace step. /// </summary> /// <param name="index">Index</param> /// <param name="scheduledMachine">Scheduled machine</param> /// <returns>TraceStep</returns> internal static TraceStep CreateSchedulingChoice(int index, BaseMachine scheduledMachine) { var traceStep = new TraceStep(); traceStep.Index = index; traceStep.Type = TraceStepType.SchedulingChoice; traceStep.ScheduledMachine = scheduledMachine; traceStep.Previous = null; traceStep.Next = null; return(traceStep); }
/// <summary> /// Creates a nondeterministic choice trace step. /// </summary> /// <param name="index">Index</param> /// <param name="choice">Choice</param> /// <returns>TraceStep</returns> internal static TraceStep CreateNondeterministicChoice(int index, bool choice) { var traceStep = new TraceStep(); traceStep.Index = index; traceStep.Type = TraceStepType.NondeterministicChoice; traceStep.Choice = choice; traceStep.Previous = null; traceStep.Next = null; return(traceStep); }
public void Test_OneStrategy_OneTraceStep() { //strategy1 : 1+1->2 var expr1 = new Term(Expression.Add, new List<object>{1,1}); var ts = new TraceStep(expr1, 2, "null", "meta-rule todo", "rule todo"); var tsExpr = new TraceStepExpr(ts); var lst = new List<TraceStepExpr>() { tsExpr}; var tuple = new Tuple<object, object>("strategy1", lst); var lstStrategy = new List<Tuple<object, object>>(); lstStrategy.Add(tuple); var graph = new BehaviorGraph(); graph.Insert(lstStrategy); Assert.True(graph.Nodes.Count == 2); var node0 = graph.Nodes[0]; Assert.Null(node0.SubGraph); var node1 = graph.Nodes[1]; Assert.True(node0.OutEdges.Count == 1); var edgeInfo = node0.OutEdges[0].Property as OuterLoopEdgeProperty; Assert.NotNull(edgeInfo); Assert.True(edgeInfo.Strategy.Equals("strategy1")); Assert.NotNull(node1.SubGraph); Assert.True(node1.InEdges.Count == 1); Assert.True(node1.OutEdges.Count == 0); Assert.True(node1.SubGraph.Nodes.Count == 2); ///////////////////////////////////////////////////// //Search Test var initNode = graph.RetrieveInitInnerNode(); Assert.NotNull(initNode); int count = graph.PathFinding(initNode); Assert.True(count == 1); var nextObj = graph.SearchNextInnerLoopNode(initNode); var tuple2 = nextObj as Tuple<object,object>; Assert.NotNull(tuple2); var nextNode = tuple2.Item2 as BehaviorGraphNode; Assert.NotNull(nextNode); count = graph.PathFinding(nextNode); Assert.True(count == 0); var prevObj = graph.SearchPrevInnerLoopNode(nextNode) as BehaviorGraphNode; Assert.NotNull(prevObj); count = graph.PathFinding(prevObj); Assert.True(count == 1); }
/// <summary> /// Creates a fair nondeterministic choice trace step. /// </summary> /// <param name="index">Index</param> /// <param name="uniqueId">Unique id</param> /// <param name="choice">Choice</param> /// <returns>TraceStep</returns> internal static TraceStep CreateFairNondeterministicChoice(int index, string uniqueId, bool choice) { var traceStep = new TraceStep(); traceStep.Index = index; traceStep.Type = TraceStepType.FairNondeterministicChoice; traceStep.NondetId = uniqueId; traceStep.Choice = choice; traceStep.Previous = null; traceStep.Next = null; return(traceStep); }
/// <summary> /// Determines whether the specified System.Object is equal /// to the current System.Object. /// </summary> /// <param name="obj">Object</param> /// <returns>Boolean value</returns> public override bool Equals(object obj) { if (obj == null) { return(false); } TraceStep mid = obj as TraceStep; if (mid == null) { return(false); } return(this.Index == mid.Index); }
public static void FromLineSlopeIntercetpToLineGeneralForm(LineSymbol ls) { var line = ls.Shape as Line; Debug.Assert(line != null); Debug.Assert(ls.OutputType == LineType.GeneralForm); string step1metaRule = "Given the line slope-intercept from y=mx+b, move y term to the right side of equation."; string step1AppliedRule = String.Format("Move y to the right side of equation"); string kc = GeometryScaffold.KC_LinePatternsTransform; var ts = new TraceStep(ls.SlopeInterceptForm, ls.GeneralForm, kc, step1metaRule, step1AppliedRule); string strategy = strategy_si_general; ls._innerLoop.Add(ts); ls.GenerateATrace(strategy); }
//forward solving public static void FromLineToSlope(LineSymbol ls, EqGoal goal) { //one strategy, one step. var line = ls.Shape as Line; Debug.Assert(line != null); var lst = new List <TraceStep>(); string step1metaRule = "Given the line slope intercept form y=mx+b, the slope is m."; string step1AppliedRule = String.Format("Given line slope-intercept form {0}, the slope is {1}.", ls.ToString(), ls.SymSlope); string kc = GeometryScaffold.KC_LineSlopeForm; var ts = new TraceStep(ls, goal, kc, step1metaRule, step1AppliedRule); lst.Add(ts); var strategy = strategy_si_slope; var tuple = new Tuple <object, object>(strategy, lst); goal.Traces.Add(tuple); }
/// <summary> /// if x = y, then y = x /// </summary> /// <param name="currentEq"></param> /// <param name="rootEq"></param> /// <returns></returns> public static Equation ApplySymmetric(this Equation currentEq, Equation rootEq) { Equation localEq = currentEq; object lhs = currentEq.Lhs; object rhs = currentEq.Rhs; if (SatisfySymmetricCondition(lhs, rhs)) { var cloneEq = currentEq.Clone(); object tempObj = cloneEq.Lhs; cloneEq.Lhs = cloneEq.Rhs; cloneEq.Rhs = tempObj; string rule = EquationsRule.Rule(EquationsRule.EquationRuleType.Symmetric); string appliedRule = EquationsRule.Rule( EquationsRule.EquationRuleType.Symmetric, localEq, null); string KC = EquationsRule.RuleConcept(EquationsRule.EquationRuleType.Symmetric); var ts = new TraceStep(localEq, cloneEq, KC, rule, appliedRule); rootEq._innerLoop.Add(ts); localEq = cloneEq; } return localEq; }
public void GenerateYCacheSymbol(object obj, EqGoal goal) { var point = Shape as Point; Debug.Assert(point != null); CachedGoals.Add(new KeyValuePair<object, EqGoal>(PointAcronym.Y, goal)); if (CachedSymbols.Count == 0) { var gPoint = new Point(point.Label, point.XCoordinate, obj); var gPointSymbol = new PointSymbol(gPoint); gPointSymbol.CachedGoals.Add(new KeyValuePair<object, EqGoal>(PointAcronym.Y, goal)); CachedSymbols.Add(gPointSymbol); gPointSymbol.Traces.AddRange(goal.Traces); //transform goal trace for (int i = goal.Traces.Count - 1; i >= 0; i--) { gPoint.Traces.Insert(0, goal.Traces[i]); } //Substitution trace var rule = SubstitutionRule.ApplySubstitute(); var appliedRule = SubstitutionRule.ApplySubstitute(this, goal); var ts = new TraceStep(this, gPointSymbol, SubstitutionRule.SubstituteKC(), rule, appliedRule); gPointSymbol._innerLoop.Add(ts); string strategy = "Reify a Point's y-coordinate by substituing a given fact."; gPointSymbol.GenerateATrace(strategy); //gPoint.Traces.Insert(0, ts); } else { foreach (ShapeSymbol ss in CachedSymbols.ToList()) { var pt = ss.Shape as Point; if (pt != null) { var yResult = LogicSharp.Reify(pt.YCoordinate, goal.ToDict()); if (!pt.YCoordinate.Equals(yResult)) { var gPt = new Point(pt.Label, pt.XCoordinate, pt.YCoordinate); var gPointSymbol = new PointSymbol(gPt); //substitute pt.YCoordinate = yResult; ss.CachedGoals.Add(new KeyValuePair<object, EqGoal>(PointAcronym.Y, goal)); gPointSymbol.Traces.AddRange(goal.Traces); //transform goal trace for (int i = goal.Traces.Count - 1; i >= 0; i--) { pt.Traces.Insert(0, goal.Traces[i]); } string rule = SubstitutionRule.ApplySubstitute(); string appliedRule = SubstitutionRule.ApplySubstitute(this, goal); var ts = new TraceStep(this, gPointSymbol, SubstitutionRule.SubstituteKC(), rule, appliedRule); gPointSymbol._innerLoop.Add(ts); string strategy = "Reify a Point's y-coordinate by substituing a given fact."; gPointSymbol.GenerateATrace(strategy); //pt.Traces.Insert(0, ts); } else { //generate var gPoint = new Point(pt.Label, pt.XCoordinate, obj); var gPointSymbol = new PointSymbol(gPoint); gPointSymbol.Traces.AddRange(goal.Traces); gPointSymbol.CachedGoals.Add(new KeyValuePair<object, EqGoal>(PointAcronym.Y, goal)); foreach (KeyValuePair<object, EqGoal> pair in ss.CachedGoals) { if (pair.Key.Equals(PointAcronym.X)) { gPointSymbol.CachedGoals.Add(new KeyValuePair<object, EqGoal>(PointAcronym.X, pair.Value)); } } CachedSymbols.Add(gPointSymbol); //substitute //Add traces from pt to gPoint for (int i = pt.Traces.Count - 1; i >= 0; i--) { gPoint.Traces.Insert(0, pt.Traces[i]); } //transform goal trace for (int i = goal.Traces.Count - 1; i >= 0; i--) { gPoint.Traces.Insert(0, goal.Traces[i]); } var rule = SubstitutionRule.ApplySubstitute(); var appliedRule = SubstitutionRule.ApplySubstitute(this, goal); var ts = new TraceStep(this, gPointSymbol, SubstitutionRule.SubstituteKC(), rule, appliedRule); gPointSymbol._innerLoop.Add(ts); string strategy = "Reify a Point's y-coordinate by substituing a given fact."; gPointSymbol.GenerateATrace(strategy); //gPoint.Traces.Insert(0, ts); } } } } }
public bool Reify(EqGoal goal) { var lhsTerm = Lhs as Term; var rhsTerm = Rhs as Term; var lhsVar = Lhs as Var; var rhsVar = Rhs as Var; string strategy = "Reify equation's internal variable by substituing a given fact."; if (lhsVar != null) { var lhsNum = LogicSharp.Reify(lhsVar, goal.ToDict()); if (lhsNum != null && !lhsNum.Equals(lhsVar)) { var cloneEq = Clone(); cloneEq.Lhs = lhsNum; string rule = SubstitutionRule.ApplySubstitute(); string appliedRule = SubstitutionRule.ApplySubstitute(this, cloneEq); var ts = new TraceStep(this, cloneEq, SubstitutionRule.SubstituteKC(), rule, appliedRule); cloneEq._innerLoop.Add(ts); cloneEq.GenerateATrace(strategy); CachedEntities.Add(cloneEq); return true; } } if (rhsVar != null) { var rhsNum = LogicSharp.Reify(rhsVar, goal.ToDict()); if (rhsNum != null && !rhsNum.Equals(lhsVar)) { var cloneEq = Clone(); cloneEq.Rhs = rhsNum; string rule = SubstitutionRule.ApplySubstitute(); string appliedRule = SubstitutionRule.ApplySubstitute(this, goal); var ts = new TraceStep(this, cloneEq, SubstitutionRule.SubstituteKC(), rule, appliedRule); cloneEq._innerLoop.Add(ts); cloneEq.GenerateATrace(strategy); CachedEntities.Add(cloneEq); return true; } } if(lhsTerm != null) { var term1 = lhsTerm.Reify(goal) as Term; if (lhsTerm.Equals(term1) || term1 == null) { return false; } var obj = term1.Eval(); var cloneEq = Clone(); cloneEq.Lhs = obj; string rule = SubstitutionRule.ApplySubstitute(); string appliedRule = SubstitutionRule.ApplySubstitute(this, goal); var ts = new TraceStep(this, cloneEq, SubstitutionRule.SubstituteKC(), rule, appliedRule); cloneEq._innerLoop.Add(ts); cloneEq.GenerateATrace(strategy); CachedEntities.Add(cloneEq); return true; } if (rhsTerm != null) { object obj = rhsTerm.Reify(goal); if (rhsTerm.Equals(obj)) { return false; } var cloneEq = Clone(); cloneEq.Rhs = obj; string rule = SubstitutionRule.ApplySubstitute(); string appliedRule = SubstitutionRule.ApplySubstitute(this, goal); var ts = new TraceStep(this, cloneEq, SubstitutionRule.SubstituteKC(), rule, appliedRule); cloneEq._innerLoop.Add(ts); cloneEq.GenerateATrace(strategy); CachedEntities.Add(cloneEq); return true; } return false; }
/* * given m=2, k=3, y=3x+2 */ public static void FromSlopeInterceptToLineSlopeIntercept(EqGoal slopeGoal, EqGoal interceptGoal, LineSymbol ls) { //1. Substitute slope and intercept properties into the line slope-intercept form y=mx+b. //////////////////////////////////////////////////////// var ts0 = new TraceStep(null, slopeGoal, GeometryScaffold.KC_LineSlopeForm, PlottingRule.PlottingStrategy, PlottingRule.Plot(slopeGoal)); var ts1 = new TraceStep(null, interceptGoal, GeometryScaffold.KC_LineInterceptForm, PlottingRule.PlottingStrategy, PlottingRule.Plot(interceptGoal)); ls._innerLoop.Add(ts0); var abstractLs = new Line(ls.Shape.Label, slopeGoal.Lhs, interceptGoal.Lhs); var abstractLss = new LineSymbol(abstractLs); var internalLs = new Line(ls.Shape.Label, ls.SymSlope, interceptGoal.Lhs); var internalLss = new LineSymbol(internalLs); var traceStep1 = new TraceStep(abstractLss, internalLss, SubstitutionRule.SubstituteKC(), SubstitutionRule.SubstitutionStrategy, SubstitutionRule.ApplySubstitute(abstractLss, slopeGoal)); ls._innerLoop.Add(traceStep1); ls._innerLoop.Add(ts1); /* var rule = "Substitute given property to line slope-intercept form."; var appliedRule1 = string.Format("Substitute slope={0} into y=mx+b", ls.SymSlope); var appliedRule2= string.Format("Substitute intercept={0} into y=mx+b", ls.SymIntercept);*/ var traceStep2 = new TraceStep(internalLss, ls, SubstitutionRule.SubstituteKC(), SubstitutionRule.SubstitutionStrategy, SubstitutionRule.ApplySubstitute(internalLss, interceptGoal)); ls._innerLoop.Add(traceStep2); string strategy = "Substitute slope and intercept properties into the line slope-intercept form y = mx + b."; ls.GenerateATrace(strategy); }
public static void FromPointSlopeToLine(PointSymbol ps, EqGoal goal, LineSymbol ls) { string strategy = "Substitute point and slope property into line form."; // 1. Substitute slope property into the line slope-intercept form. // 2. Calculate the line intercept by substituting the point into line pattern. ////////////////////////////////////////////////////////////////////// string strategy1 = "Substitute slope property into the line slope-intercept form."; var m = new Var("m"); var k = new Var("k"); var x = new Var("x"); var y = new Var("y"); var term = new Term(Expression.Multiply, new List<object>() {m, x}); var term1 = new Term(Expression.Add, new List<object>() {term, k}); var eqPattern = new Equation(y, term1); var term2 = new Term(Expression.Multiply, new List<object>() {goal.Rhs,x}); var term3 = new Term(Expression.Add, new List<object>() {term2, k}); var eqInternal1 = new Equation(y, term3); var appliedRule1 = SubstitutionRule.ApplySubstitute(eqPattern, goal); var ts0 = new TraceStep(eqPattern, eqInternal1, SubstitutionRule.SubstituteKC(), strategy1, appliedRule1); ls._innerLoop.Add(ts0); ////////////////////////////////////////////////////////////////////// var point = ps.Shape as Point; Debug.Assert(point != null); string strategy2 = "Calculate the line intercept by substituting the point into line pattern."; var term4 = new Term(Expression.Multiply, new List<object>() { goal.Rhs, point.XCoordinate}); var term5 = new Term(Expression.Add, new List<object>() {term4, k}); var eqinternal2 = new Equation(point.YCoordinate, term5); object obj; bool result = eqinternal2.IsEqGoal(out obj); var eqGoal = obj as EqGoal; Debug.Assert(eqGoal != null); var gTuple = eqGoal.Traces[0]; var gLst = gTuple.Item2 as List<TraceStep>; Debug.Assert(gLst != null); ls._innerLoop.AddRange(gLst); var appliedRule2 = SubstitutionRule.ApplySubstitute(eqInternal1, ps); var ts = new TraceStep(eqInternal1, ls, SubstitutionRule.SubstituteKC(), strategy2, appliedRule2); ls._innerLoop.Add(ts); ls.GenerateATrace(strategy); }
/* * Automatic scaffolding * * Distance function (x-x0)^2+(y-y0)^2 = d^2 * * Forward chaining to derive d. * Backward chaining to derive other four parameters. */ private static Tuple<object, object> DistanceSubstitution(LineSegmentSymbol lss) { var ls = lss.Shape as LineSegment; var lst = new List<TraceStep>(); string step1metaRule = "The Distance Function between two points it: d^2=(x0-x1)^2+(y0-y1)^2"; string step1AppliedRule = String.Format( "Substitute two points into the distance function d^2=({0}-{1})^2+({2}-{3})^2", ls.Pt1.XCoordinate.ToString(), ls.Pt2.XCoordinate.ToString(), ls.Pt1.YCoordinate.ToString(), ls.Pt2.YCoordinate.ToString()); var pt1X = new Var("x0"); var pt1Y = new Var("y0"); var pt2X = new Var("x1"); var pt2Y = new Var("y1"); var term1_1 = new Term(Expression.Subtract, new List<object>() { pt1X, pt2X }); var term11_1 = new Term(Expression.Power, new List<object>() { term1_1, 2.0 }); var term2_1 = new Term(Expression.Subtract, new List<object>() { pt1Y, pt2Y }); var term22_1 = new Term(Expression.Power, new List<object>() { term2_1, 2.0 }); var rhs_1 = new Term(Expression.Add, new List<object>() { term11_1, term22_1 }); var variable = new Var('d'); var lhs = new Term(Expression.Power, new List<object>() { variable, 2.0 }); var term1 = new Term(Expression.Subtract, new List<object>() { ls.Pt1.XCoordinate, ls.Pt2.XCoordinate }); var term11 = new Term(Expression.Power, new List<object>() { term1, 2.0 }); var term2 = new Term(Expression.Subtract, new List<object>() { ls.Pt1.YCoordinate, ls.Pt2.YCoordinate }); var term22 = new Term(Expression.Power, new List<object>() { term2, 2.0 }); var rhs = new Term(Expression.Add, new List<object>() { term11, term22 }); var eq = new Equation(lhs, rhs); var old_eq = new Equation(lhs, rhs_1); var trace = new TraceStep(old_eq, eq, GeometryScaffold.KC_Distance, step1metaRule, step1AppliedRule); lst.Add(trace); string strategy = "Substitute two points coordinates into the distance function."; var newTuple = new Tuple<object, object>(strategy, lst); return newTuple; }
public static object ApplyTransitive2(this Equation currentEq, Equation rootEq, bool withEqRule, bool lineCheck = false) { Equation localEq = currentEq; object lhs = currentEq.Lhs; object rhs = currentEq.Rhs; if (!withEqRule) return localEq; //Divide Transform object newLhs, newRhs; if (SatisfyTransitiveCondition5(lhs, rhs, out newLhs, out newRhs)) { var cloneEq = currentEq.Clone(); var newEq = new Equation(newLhs, newRhs); string rule = EquationsRule.Rule(EquationsRule.EquationRuleType.Transitive); string appliedRule = EquationsRule.Rule( EquationsRule.EquationRuleType.Transitive, localEq, newEq); string KC = EquationsRule.RuleConcept(EquationsRule.EquationRuleType.Transitive); var traceStep = new TraceStep(localEq, newEq, KC, rule, appliedRule); rootEq._innerLoop.Add(traceStep); localEq = newEq; return localEq; } return localEq; }
/// <summary> /// Creates a nondeterministic choice trace step. /// </summary> /// <param name="index">Index</param> /// <param name="choice">Choice</param> /// <returns>TraceStep</returns> internal static TraceStep CreateNondeterministicChoice(int index, bool choice) { var traceStep = new TraceStep(); traceStep.Index = index; traceStep.Type = TraceStepType.NondeterministicChoice; traceStep.Choice = choice; traceStep.Previous = null; traceStep.Next = null; return traceStep; }
public static void LineSlopeIntercepToGraph(LineSymbol ls) { string strategy = strategy_graphing; //Plotting shapes //1. plotting Y-Intercept //2. plotting X-Intercept //3. plotting the line ////////////////////////////////////////////////////////////// // Step 1: var pt = new Point(0, ls.SymIntercept); var ptSym = new PointSymbol(pt); var ts0 = new TraceStep(null, ptSym, null, PlottingRule.PlottingStrategy, PlottingRule.Plot(ptSym)); ls._innerLoop.Add(ts0); ////////////////////////////////////////////////////////////// // Step 2: var line = ls.Shape as Line; Debug.Assert(line != null); Equation eq; if (line.B == null) { } else { var x = new Var("x"); //step 2.1 var term = new Term(Expression.Multiply, new List<object>() { line.Slope, x }); var term1 = new Term(Expression.Add, new List<object>() { term, line.Intercept }); eq = new Equation(term1, 0); object obj; EqGoal gGoal = null; bool result = eq.IsEqGoal(out obj); if (result) { gGoal = obj as EqGoal; } if (gGoal != null) { double dX; LogicSharp.IsDouble(gGoal.Rhs, out dX); var pt1 = new Point(dX, 0); var ptSym1 = new PointSymbol(pt1); var ts1 = new TraceStep(null, ptSym1, null, PlottingRule.PlottingStrategy, PlottingRule.Plot(ptSym1)); ls._innerLoop.Add(ts1); } } ///////////////////////////////////////////////////////////////// const string step1MetaRule = "Given the line slope-intercept form y=mx+b, plot the line by passing points (0,b) and (-b/m,0)."; string step1AppliedRule = String.Format("Plotting the line passing through (0,{0}) and ({1},0) ", ls.SymIntercept, ls.SymC); //var ts = new TraceStep(null, ls.SlopeInterceptForm, step1MetaRule, step1AppliedRule); string kc = GeometryScaffold.KC_LineGraphing; var ts = new TraceStep(null, ls, kc, step1MetaRule, step1AppliedRule); ls._innerLoop.Add(ts); ////////////////////////////////////////////////////////////////// ls.GenerateATrace(strategy); }
public void CacheC(object obj, EqGoal goal) { CachedGoals.Add(new KeyValuePair<object, EqGoal>(LineAcronym.C, goal)); if (CachedSymbols.Count == 0) { var line = Shape as Line; Debug.Assert(line != null); #region generate new object var gLine = new Line(line.Label, line.A, line.B, obj); var gLineSymbol = new LineSymbol(gLine); gLineSymbol.CachedGoals.Add(new KeyValuePair<object, EqGoal>(LineAcronym.C, goal)); CachedSymbols.Add(gLineSymbol); //Transform goal trace for (int i = goal.Traces.Count - 1; i >= 0; i--) { gLine.Traces.Insert(0, goal.Traces[i]); } //Substitution trace string rule = SubstitutionRule.ApplySubstitute(); string appliedRule = SubstitutionRule.ApplySubstitute(this, goal); string kc = SubstitutionRule.SubstituteKC(); var ts = new TraceStep(this, gLineSymbol, kc, rule, appliedRule); gLine._innerLoop.Add(ts); gLine.GenerateATrace(SubstitutionRule.SubstitutionStrategy); //gLine.Traces.Insert(0, ts); #endregion } else { #region Iterate existing point object foreach (ShapeSymbol ss in CachedSymbols.ToList()) { var line = ss.Shape as Line; if (line != null) { var cResult = LogicSharp.Reify(line.C, goal.ToDict()); if (!line.C.Equals(cResult)) { var gline = new Line(line.Label, line.A, line.B, line.C); var gLineSymbol = new LineSymbol(gline); //substitute line.C = cResult; ss.CachedGoals.Add(new KeyValuePair<object, EqGoal>(LineAcronym.C, goal)); //transform goal trace for (int i = goal.Traces.Count - 1; i >= 0; i--) { line.Traces.Insert(0, goal.Traces[i]); } string rule = SubstitutionRule.ApplySubstitute(); string appliedRule = SubstitutionRule.ApplySubstitute(gLineSymbol, goal); string kc = SubstitutionRule.SubstituteKC(); var ts = new TraceStep(gLineSymbol, ss, kc, rule, appliedRule); //line.Traces.Insert(0, ts); line._innerLoop.Add(ts); line.GenerateATrace(SubstitutionRule.SubstitutionStrategy); } else { //generate var gLine = new Line(line.Label, line.A, line.B, obj); var gLineSymbol = new LineSymbol(gLine); gLineSymbol.CachedGoals.Add(new KeyValuePair<object, EqGoal>(LineAcronym.C, goal)); foreach (KeyValuePair<object, EqGoal> pair in ss.CachedGoals) { if (pair.Key.Equals(LineAcronym.A)) { gLineSymbol.CachedGoals.Add(new KeyValuePair<object, EqGoal>(LineAcronym.A, pair.Value)); } else if (pair.Key.Equals(LineAcronym.B)) { gLineSymbol.CachedGoals.Add(new KeyValuePair<object, EqGoal>(LineAcronym.B, pair.Value)); } } CachedSymbols.Add(gLineSymbol); //substitute //Add traces from line to gLine for (int i = line.Traces.Count - 1; i >= 0; i--) { gLine.Traces.Insert(0, line.Traces[i]); } //transform goal trace for (int i = goal.Traces.Count - 1; i >= 0; i--) { gLine.Traces.Insert(0, goal.Traces[i]); } string rule = SubstitutionRule.ApplySubstitute(); string appliedRule = SubstitutionRule.ApplySubstitute(gLineSymbol, goal); string kc = SubstitutionRule.SubstituteKC(); var ts = new TraceStep(ss, gLineSymbol, kc, rule, appliedRule); gLine._innerLoop.Add(ts); gLine.GenerateATrace(SubstitutionRule.SubstitutionStrategy); //gLine.Traces.Insert(0, ts); } } } #endregion } }
/* * Automatic scaffolding * * Distance function (x-x0)^2+(y-y0)^2 = d^2 * * Forward chaining to derive d. * Backward chaining to derive other four parameters. */ private static Tuple <object, object> DistanceSubstitution(LineSegmentSymbol lss) { var ls = lss.Shape as LineSegment; var lst = new List <TraceStep>(); string step1metaRule = "The Distance Function between two points it: d^2=(x0-x1)^2+(y0-y1)^2"; string step1AppliedRule = String.Format( "Substitute two points into the distance function d^2=({0}-{1})^2+({2}-{3})^2", ls.Pt1.XCoordinate.ToString(), ls.Pt2.XCoordinate.ToString(), ls.Pt1.YCoordinate.ToString(), ls.Pt2.YCoordinate.ToString()); var pt1X = new Var("x0"); var pt1Y = new Var("y0"); var pt2X = new Var("x1"); var pt2Y = new Var("y1"); var term1_1 = new Term(Expression.Subtract, new List <object>() { pt1X, pt2X }); var term11_1 = new Term(Expression.Power, new List <object>() { term1_1, 2.0 }); var term2_1 = new Term(Expression.Subtract, new List <object>() { pt1Y, pt2Y }); var term22_1 = new Term(Expression.Power, new List <object>() { term2_1, 2.0 }); var rhs_1 = new Term(Expression.Add, new List <object>() { term11_1, term22_1 }); var variable = new Var('d'); var lhs = new Term(Expression.Power, new List <object>() { variable, 2.0 }); var term1 = new Term(Expression.Subtract, new List <object>() { ls.Pt1.XCoordinate, ls.Pt2.XCoordinate }); var term11 = new Term(Expression.Power, new List <object>() { term1, 2.0 }); var term2 = new Term(Expression.Subtract, new List <object>() { ls.Pt1.YCoordinate, ls.Pt2.YCoordinate }); var term22 = new Term(Expression.Power, new List <object>() { term2, 2.0 }); var rhs = new Term(Expression.Add, new List <object>() { term11, term22 }); var eq = new Equation(lhs, rhs); var old_eq = new Equation(lhs, rhs_1); var trace = new TraceStep(old_eq, eq, GeometryScaffold.KC_Distance, step1metaRule, step1AppliedRule); lst.Add(trace); string strategy = "Substitute two points coordinates into the distance function."; var newTuple = new Tuple <object, object>(strategy, lst); return(newTuple); }
public void GenerateYCacheSymbol(object obj, EqGoal goal) { var point = Shape as Point; Debug.Assert(point != null); CachedGoals.Add(new KeyValuePair <object, EqGoal>(PointAcronym.Y, goal)); if (CachedSymbols.Count == 0) { var gPoint = new Point(point.Label, point.XCoordinate, obj); var gPointSymbol = new PointSymbol(gPoint); gPointSymbol.CachedGoals.Add(new KeyValuePair <object, EqGoal>(PointAcronym.Y, goal)); CachedSymbols.Add(gPointSymbol); gPointSymbol.Traces.AddRange(goal.Traces); //transform goal trace for (int i = goal.Traces.Count - 1; i >= 0; i--) { gPoint.Traces.Insert(0, goal.Traces[i]); } //Substitution trace var rule = SubstitutionRule.ApplySubstitute(); var appliedRule = SubstitutionRule.ApplySubstitute(this, goal); var ts = new TraceStep(this, gPointSymbol, SubstitutionRule.SubstituteKC(), rule, appliedRule); gPointSymbol._innerLoop.Add(ts); string strategy = "Reify a Point's y-coordinate by substituing a given fact."; gPointSymbol.GenerateATrace(strategy); //gPoint.Traces.Insert(0, ts); } else { foreach (ShapeSymbol ss in CachedSymbols.ToList()) { var pt = ss.Shape as Point; if (pt != null) { var yResult = LogicSharp.Reify(pt.YCoordinate, goal.ToDict()); if (!pt.YCoordinate.Equals(yResult)) { var gPt = new Point(pt.Label, pt.XCoordinate, pt.YCoordinate); var gPointSymbol = new PointSymbol(gPt); //substitute pt.YCoordinate = yResult; ss.CachedGoals.Add(new KeyValuePair <object, EqGoal>(PointAcronym.Y, goal)); gPointSymbol.Traces.AddRange(goal.Traces); //transform goal trace for (int i = goal.Traces.Count - 1; i >= 0; i--) { pt.Traces.Insert(0, goal.Traces[i]); } string rule = SubstitutionRule.ApplySubstitute(); string appliedRule = SubstitutionRule.ApplySubstitute(this, goal); var ts = new TraceStep(this, gPointSymbol, SubstitutionRule.SubstituteKC(), rule, appliedRule); gPointSymbol._innerLoop.Add(ts); string strategy = "Reify a Point's y-coordinate by substituing a given fact."; gPointSymbol.GenerateATrace(strategy); //pt.Traces.Insert(0, ts); } else { //generate var gPoint = new Point(pt.Label, pt.XCoordinate, obj); var gPointSymbol = new PointSymbol(gPoint); gPointSymbol.Traces.AddRange(goal.Traces); gPointSymbol.CachedGoals.Add(new KeyValuePair <object, EqGoal>(PointAcronym.Y, goal)); foreach (KeyValuePair <object, EqGoal> pair in ss.CachedGoals) { if (pair.Key.Equals(PointAcronym.X)) { gPointSymbol.CachedGoals.Add(new KeyValuePair <object, EqGoal>(PointAcronym.X, pair.Value)); } } CachedSymbols.Add(gPointSymbol); //substitute //Add traces from pt to gPoint for (int i = pt.Traces.Count - 1; i >= 0; i--) { gPoint.Traces.Insert(0, pt.Traces[i]); } //transform goal trace for (int i = goal.Traces.Count - 1; i >= 0; i--) { gPoint.Traces.Insert(0, goal.Traces[i]); } var rule = SubstitutionRule.ApplySubstitute(); var appliedRule = SubstitutionRule.ApplySubstitute(this, goal); var ts = new TraceStep(this, gPointSymbol, SubstitutionRule.SubstituteKC(), rule, appliedRule); gPointSymbol._innerLoop.Add(ts); string strategy = "Reify a Point's y-coordinate by substituing a given fact."; gPointSymbol.GenerateATrace(strategy); //gPoint.Traces.Insert(0, ts); } } } } }
public void CacheC(object obj, EqGoal goal) { CachedGoals.Add(new KeyValuePair <object, EqGoal>(LineAcronym.C, goal)); if (CachedSymbols.Count == 0) { var line = Shape as Line; Debug.Assert(line != null); #region generate new object var gLine = new Line(line.Label, line.A, line.B, obj); var gLineSymbol = new LineSymbol(gLine); gLineSymbol.CachedGoals.Add(new KeyValuePair <object, EqGoal>(LineAcronym.C, goal)); CachedSymbols.Add(gLineSymbol); //Transform goal trace for (int i = goal.Traces.Count - 1; i >= 0; i--) { gLine.Traces.Insert(0, goal.Traces[i]); } //Substitution trace string rule = SubstitutionRule.ApplySubstitute(); string appliedRule = SubstitutionRule.ApplySubstitute(this, goal); string kc = SubstitutionRule.SubstituteKC(); var ts = new TraceStep(this, gLineSymbol, kc, rule, appliedRule); gLine._innerLoop.Add(ts); gLine.GenerateATrace(SubstitutionRule.SubstitutionStrategy); //gLine.Traces.Insert(0, ts); #endregion } else { #region Iterate existing point object foreach (ShapeSymbol ss in CachedSymbols.ToList()) { var line = ss.Shape as Line; if (line != null) { var cResult = LogicSharp.Reify(line.C, goal.ToDict()); if (!line.C.Equals(cResult)) { var gline = new Line(line.Label, line.A, line.B, line.C); var gLineSymbol = new LineSymbol(gline); //substitute line.C = cResult; ss.CachedGoals.Add(new KeyValuePair <object, EqGoal>(LineAcronym.C, goal)); //transform goal trace for (int i = goal.Traces.Count - 1; i >= 0; i--) { line.Traces.Insert(0, goal.Traces[i]); } string rule = SubstitutionRule.ApplySubstitute(); string appliedRule = SubstitutionRule.ApplySubstitute(gLineSymbol, goal); string kc = SubstitutionRule.SubstituteKC(); var ts = new TraceStep(gLineSymbol, ss, kc, rule, appliedRule); //line.Traces.Insert(0, ts); line._innerLoop.Add(ts); line.GenerateATrace(SubstitutionRule.SubstitutionStrategy); } else { //generate var gLine = new Line(line.Label, line.A, line.B, obj); var gLineSymbol = new LineSymbol(gLine); gLineSymbol.CachedGoals.Add(new KeyValuePair <object, EqGoal>(LineAcronym.C, goal)); foreach (KeyValuePair <object, EqGoal> pair in ss.CachedGoals) { if (pair.Key.Equals(LineAcronym.A)) { gLineSymbol.CachedGoals.Add(new KeyValuePair <object, EqGoal>(LineAcronym.A, pair.Value)); } else if (pair.Key.Equals(LineAcronym.B)) { gLineSymbol.CachedGoals.Add(new KeyValuePair <object, EqGoal>(LineAcronym.B, pair.Value)); } } CachedSymbols.Add(gLineSymbol); //substitute //Add traces from line to gLine for (int i = line.Traces.Count - 1; i >= 0; i--) { gLine.Traces.Insert(0, line.Traces[i]); } //transform goal trace for (int i = goal.Traces.Count - 1; i >= 0; i--) { gLine.Traces.Insert(0, goal.Traces[i]); } string rule = SubstitutionRule.ApplySubstitute(); string appliedRule = SubstitutionRule.ApplySubstitute(gLineSymbol, goal); string kc = SubstitutionRule.SubstituteKC(); var ts = new TraceStep(ss, gLineSymbol, kc, rule, appliedRule); gLine._innerLoop.Add(ts); gLine.GenerateATrace(SubstitutionRule.SubstitutionStrategy); //gLine.Traces.Insert(0, ts); } } } #endregion } }
public static void FromLineSlopeIntercetpToLineGeneralForm(LineSymbol ls) { var line = ls.Shape as Line; Debug.Assert(line != null); Debug.Assert(ls.OutputType == LineType.GeneralForm); string step1metaRule = "Given the line slope-intercept from y=mx+b, move y term to the right side of equation."; string step1AppliedRule = String.Format("Move y to the right side of equation"); string kc = GeometryScaffold.KC_LinePatternsTransform; var ts = new TraceStep(ls.SlopeInterceptForm, ls.GeneralForm, kc, step1metaRule, step1AppliedRule); string strategy = strategy_si_general; ls._innerLoop.Add(ts); ls.GenerateATrace(strategy); }
//forward solving public static void FromLineToIntercept(LineSymbol ls, EqGoal goal) { var line = ls.Shape as Line; Debug.Assert(line != null); var lst = new List<TraceStep>(); string step1metaRule = "Given the line slope intercept form y=mx+K, the y-intercept is K."; string step1AppliedRule = String.Format("Given line slope-intercept form {0}, the slope is {1}.", ls.ToString(), ls.SymIntercept); string kc = GeometryScaffold.KC_LineInterceptForm; var ts = new TraceStep(ls, goal, kc, step1metaRule, step1AppliedRule); lst.Add(ts); var strategy = strategy_si_intercept; var tuple = new Tuple<object, object>(strategy, lst); goal.Traces.Add(tuple); }
/// <summary> /// Trace from Line General Form to Slope-Intercept Form /// ax+by+c=0 => y=mx+b /// </summary> /// <param name="target"></param> /// <returns></returns> public static void FromLineGeneralFormToSlopeIntercept(LineSymbol ls) { string strategy = "Given a general form line ax+by+c=0, the slope-intercept form of it is y = -(a/b)x-c/b."; var line = ls.Shape as Line; Debug.Assert(line != null); var newLS = new LineSymbol(line); newLS.OutputType = LineType.SlopeIntercept; var x = new Var("x"); var y = new Var("y"); var xTerm = new Term(Expression.Multiply, new List <object>() { line.A, x }); var yTerm = new Term(Expression.Multiply, new List <object>() { line.B, y }); var oldEqLeft = new Term(Expression.Add, new List <object>() { xTerm, yTerm, line.C }); var oldEq = new Equation(oldEqLeft, 0); var invertXTerm = new Term(Expression.Multiply, new List <object>() { -1, xTerm }); var intertZ = new Term(Expression.Multiply, new List <object>() { -1, line.C }); var internalRight = new Term(Expression.Add, new List <object>() { invertXTerm, intertZ }); var internalEq = new Equation(yTerm, internalRight); var finalXTerm = new Term(Expression.Multiply, new List <object>() { line.Slope, x }); var finalRight = new Term(Expression.Add, new List <object>() { finalXTerm, line.Intercept }); var lastEq = new Equation(yTerm, finalRight); string step1metaRule = "Given the line general form ax+by+c=0, move x term and Constant term to the right side of equation."; string step1AppliedRule = String.Format("Move {0}x and {1} to right side of equation.", ls.SymA, ls.SymC); string kc = EquationsRule.RuleConcept(EquationsRule.EquationRuleType.Transitive); var ts0 = new TraceStep(null, internalEq, kc, step1metaRule, step1AppliedRule); ls._innerLoop.Add(ts0); string appliedRule = string.Format("divide coefficient b in both side of equation."); kc = EquationsRule.RuleConcept(EquationsRule.EquationRuleType.Inverse); var ts1 = new TraceStep(null, lastEq, kc, AlgebraRule.AlgebraicStrategy, appliedRule); ls._innerLoop.Add(ts1); ls.GenerateATrace(strategy); }
public void Test_OneStrategy_MultiTraceStep() { var expr1 = new Term(Expression.Add, new List<object> { 1, 1 , 1}); var expr2 = new Term(Expression.Add, new List<object> {2, 1} ); var ts1 = new TraceStep(expr1, expr2, "null", "meta-rule todo", "rule todo"); var ts1Expr = new TraceStepExpr(ts1); var ts2 = new TraceStep(expr2, 2, "null", "meta-rule todo", "rule todo"); var ts2Expr = new TraceStepExpr(ts2); var lst = new List<TraceStepExpr>() { ts1Expr,ts2Expr}; var tuple = new Tuple<object, object>("strategy1", lst); var lstStrategy = new List<Tuple<object, object>>(); lstStrategy.Add(tuple); var graph = new BehaviorGraph(); graph.Insert(lstStrategy); Assert.True(graph.Nodes.Count == 2); var node0 = graph.Nodes[0]; Assert.Null(node0.SubGraph); var node1 = graph.Nodes[1]; Assert.NotNull(node1.SubGraph); Assert.True(node1.SubGraph.Nodes.Count == 3); Assert.True(node1.OutEdges.Count == 0); ///////////////////////////////////////////////////// //Search Test var initNode = graph.RetrieveInitInnerNode(); Assert.NotNull(initNode); int count = graph.PathFinding(initNode); Assert.True(count == 2); var nextObj = graph.SearchNextInnerLoopNode(initNode); var tuple2 = nextObj as Tuple<object, object>; Assert.NotNull(tuple2); var nextNode = tuple2.Item2 as BehaviorGraphNode; Assert.NotNull(nextNode); count = graph.PathFinding(nextNode); Assert.True(count == 1); var nextObj2 = graph.SearchNextInnerLoopNode(nextNode); var tuple3 = nextObj2 as Tuple<object, object>; Assert.NotNull(tuple3); var nextNode2 = tuple3.Item2 as BehaviorGraphNode; Assert.NotNull(nextNode2); count = graph.PathFinding(nextNode2); Assert.True(count == 0); }
/// <summary> /// Trace from Line General Form to Slope-Intercept Form /// ax+by+c=0 => y=mx+b /// </summary> /// <param name="target"></param> /// <returns></returns> public static void FromLineGeneralFormToSlopeIntercept(LineSymbol ls) { string strategy = "Given a general form line ax+by+c=0, the slope-intercept form of it is y = -(a/b)x-c/b."; var line = ls.Shape as Line; Debug.Assert(line != null); var newLS = new LineSymbol(line); newLS.OutputType = LineType.SlopeIntercept; var x = new Var("x"); var y = new Var("y"); var xTerm = new Term(Expression.Multiply, new List<object>() { line.A, x }); var yTerm = new Term(Expression.Multiply, new List<object>() { line.B, y }); var oldEqLeft = new Term(Expression.Add, new List<object>() { xTerm, yTerm, line.C }); var oldEq = new Equation(oldEqLeft, 0); var invertXTerm = new Term(Expression.Multiply, new List<object>() { -1, xTerm }); var intertZ = new Term(Expression.Multiply, new List<object>() { -1, line.C }); var internalRight = new Term(Expression.Add, new List<object>() { invertXTerm, intertZ }); var internalEq = new Equation(yTerm, internalRight); var finalXTerm = new Term(Expression.Multiply, new List<object>() { line.Slope, x }); var finalRight = new Term(Expression.Add, new List<object>() { finalXTerm, line.Intercept }); var lastEq = new Equation(yTerm, finalRight); string step1metaRule = "Given the line general form ax+by+c=0, move x term and Constant term to the right side of equation."; string step1AppliedRule = String.Format("Move {0}x and {1} to right side of equation.", ls.SymA, ls.SymC); string kc = EquationsRule.RuleConcept(EquationsRule.EquationRuleType.Transitive); var ts0 = new TraceStep(null, internalEq, kc, step1metaRule, step1AppliedRule); ls._innerLoop.Add(ts0); string appliedRule = string.Format("divide coefficient b in both side of equation."); kc = EquationsRule.RuleConcept(EquationsRule.EquationRuleType.Inverse); var ts1 = new TraceStep(null, lastEq, kc, AlgebraRule.AlgebraicStrategy, appliedRule); ls._innerLoop.Add(ts1); ls.GenerateATrace(strategy); }
public static void FromPointSlopeToLine(PointSymbol ps, EqGoal goal, LineSymbol ls) { string strategy = "Substitute point and slope property into line form."; // 1. Substitute slope property into the line slope-intercept form. // 2. Calculate the line intercept by substituting the point into line pattern. ////////////////////////////////////////////////////////////////////// string strategy1 = "Substitute slope property into the line slope-intercept form."; var m = new Var("m"); var k = new Var("k"); var x = new Var("x"); var y = new Var("y"); var term = new Term(Expression.Multiply, new List <object>() { m, x }); var term1 = new Term(Expression.Add, new List <object>() { term, k }); var eqPattern = new Equation(y, term1); var term2 = new Term(Expression.Multiply, new List <object>() { goal.Rhs, x }); var term3 = new Term(Expression.Add, new List <object>() { term2, k }); var eqInternal1 = new Equation(y, term3); var appliedRule1 = SubstitutionRule.ApplySubstitute(eqPattern, goal); var ts0 = new TraceStep(eqPattern, eqInternal1, SubstitutionRule.SubstituteKC(), strategy1, appliedRule1); ls._innerLoop.Add(ts0); ////////////////////////////////////////////////////////////////////// var point = ps.Shape as Point; Debug.Assert(point != null); string strategy2 = "Calculate the line intercept by substituting the point into line pattern."; var term4 = new Term(Expression.Multiply, new List <object>() { goal.Rhs, point.XCoordinate }); var term5 = new Term(Expression.Add, new List <object>() { term4, k }); var eqinternal2 = new Equation(point.YCoordinate, term5); object obj; bool result = eqinternal2.IsEqGoal(out obj); var eqGoal = obj as EqGoal; Debug.Assert(eqGoal != null); var gTuple = eqGoal.Traces[0]; var gLst = gTuple.Item2 as List <TraceStep>; Debug.Assert(gLst != null); ls._innerLoop.AddRange(gLst); var appliedRule2 = SubstitutionRule.ApplySubstitute(eqInternal1, ps); var ts = new TraceStep(eqInternal1, ls, SubstitutionRule.SubstituteKC(), strategy2, appliedRule2); ls._innerLoop.Add(ts); ls.GenerateATrace(strategy); }
public void Test_MultiStrategy_1() { //strategy1: 1->2, 2->3 var ts1 = new TraceStep(1, 2, "null", "TODO1", "TODO2"); var ts1Expr = new TraceStepExpr(ts1); var ts2 = new TraceStep(2, 3, "null", "TODO2", "TODO3"); var ts2Expr = new TraceStepExpr(ts2); var lst1 = new List<TraceStepExpr>() { ts1Expr, ts2Expr }; var tuple1 = new Tuple<object, object>("strategy1", lst1); //strategy2: 2->3, 3->5, 5->7 var ts3 = new TraceStep(2, 3, null, "TODO", "TODO1"); var ts3Expr = new TraceStepExpr(ts3); var ts4 = new TraceStep(3, 5, null, "TODO2", "TODO5"); var ts4Expr = new TraceStepExpr(ts4); var ts5 = new TraceStep(5, 7, null,"Test1", "test23"); var ts5Expr = new TraceStepExpr(ts5); var lst2 = new List<TraceStepExpr>() {ts3Expr, ts4Expr, ts5Expr}; var tuple2 = new Tuple<object, object>("strategy2", lst2); var lstStrategy = new List<Tuple<object, object>>(); lstStrategy.Add(tuple1); lstStrategy.Add(tuple2); var graph = new BehaviorGraph(); graph.Insert(lstStrategy); Assert.True(graph.Nodes.Count == 3); var node1 = graph.Nodes[1]; Assert.True(node1.OutEdges.Count == 1); var node2 = graph.Nodes[2]; Assert.True(node2.InEdges.Count == 1); Assert.True(node2.OutEdges.Count == 0); Assert.NotNull(node1.SubGraph); Assert.True(node1.SubGraph.Nodes.Count == 3); Assert.NotNull(node2.SubGraph); Assert.True(node2.SubGraph.Nodes.Count == 4); ///////////////////////////////////////////////////// //Search Test var initNode = graph.RetrieveInitInnerNode(); Assert.NotNull(initNode); int count = graph.PathFinding(initNode); Assert.True(count == 5); var nextObj = graph.SearchNextInnerLoopNode(initNode); var tuple5 = nextObj as Tuple<object, object>; Assert.NotNull(tuple5); var nextNode = tuple5.Item2 as BehaviorGraphNode; Assert.NotNull(nextNode); count = graph.PathFinding(nextNode); Assert.True(count == 4); nextObj = graph.SearchNextInnerLoopNode(nextNode); tuple5 = nextObj as Tuple<object, object>; Assert.NotNull(tuple5); nextNode = tuple5.Item2 as BehaviorGraphNode; Assert.NotNull(nextNode); count = graph.PathFinding(nextNode); Assert.True(count == 3); var index = graph.SearchOuterLoopNodeIndex(nextNode); Assert.True(index == 1); nextObj = graph.SearchNextInnerLoopNode(nextNode); tuple5 = nextObj as Tuple<object, object>; Assert.NotNull(tuple5); nextNode = tuple5.Item2 as BehaviorGraphNode; Assert.NotNull(nextNode); count = graph.PathFinding(nextNode); Assert.True(count == 2); index = graph.SearchOuterLoopNodeIndex(nextNode); Assert.True(index == 2); nextObj = graph.SearchNextInnerLoopNode(nextNode); tuple5 = nextObj as Tuple<object, object>; Assert.NotNull(tuple5); nextNode = tuple5.Item2 as BehaviorGraphNode; Assert.NotNull(nextNode); count = graph.PathFinding(nextNode); Assert.True(count == 1); index = graph.SearchOuterLoopNodeIndex(nextNode); Assert.True(index == 2); nextObj = graph.SearchNextInnerLoopNode(nextNode); tuple5 = nextObj as Tuple<object, object>; Assert.NotNull(tuple5); nextNode = tuple5.Item2 as BehaviorGraphNode; Assert.NotNull(nextNode); count = graph.PathFinding(nextNode); Assert.True(count == 0); index = graph.SearchOuterLoopNodeIndex(nextNode); Assert.True(index == 2); var prevNode = graph.SearchPrevInnerLoopNode(nextNode) as BehaviorGraphNode; Assert.NotNull(prevNode); count = graph.PathFinding(prevNode); Assert.True(count == 1); var strateties = graph.SearchAllOuterEdgeInfos(); Assert.True(strateties.Count == 2); }
/// <summary> /// Creates a fair nondeterministic choice trace step. /// </summary> /// <param name="index">Index</param> /// <param name="uniqueId">Unique id</param> /// <param name="choice">Choice</param> /// <returns>TraceStep</returns> internal static TraceStep CreateFairNondeterministicChoice(int index, string uniqueId, bool choice) { var traceStep = new TraceStep(); traceStep.Index = index; traceStep.Type = TraceStepType.FairNondeterministicChoice; traceStep.NondetId = uniqueId; traceStep.Choice = choice; traceStep.Previous = null; traceStep.Next = null; return traceStep; }
public void Test_OneStrategy_OneTraceStep_Author() { //1+1->2 var expr1 = new Term(Expression.Add, new List <object> { 1, 1 }); var ts = new TraceStep(expr1, 2, "null", "meta-rule todo", "rule todo"); var tsExpr = new TraceStepExpr(ts); var lst = new List <TraceStepExpr>() { tsExpr }; var tuple = new Tuple <object, object>("strategy1", lst); var lstStrategy = new List <Tuple <object, object> >(); lstStrategy.Add(tuple); var graph = new BehaviorGraph(); graph.Insert(lstStrategy); Assert.True(graph.Nodes.Count == 2); var node0 = graph.Nodes[0]; Assert.Null(node0.SubGraph); var node1 = graph.Nodes[1]; Assert.NotNull(node1.SubGraph); Assert.True(node1.SubGraph.Nodes.Count == 2); ///////////////////////////////////////////// //4-1->2 var expr2 = new Term(Expression.Add, new List <object> { 4, 1 }); // User Input, //wrong step var ts1 = new TraceStep(expr2, 2); var ts1Expr = new TraceStepExpr(ts1); var tsLst = new List <TraceStepExpr>() { ts1Expr }; var tuple2 = new Tuple <object, object>("strategy1", tsLst); var lst44 = new List <Tuple <object, object> >() { tuple2 }; bool matchResult = graph.Match(lst44); Assert.True(matchResult); graph.Update(lst44); Assert.True(graph.Nodes.Count == 2); Assert.True(node1.SubGraph.Nodes.Count == 3); ///////////////////////////////////////////// //test search var initNode = graph.RetrieveInitInnerNode(); Assert.NotNull(initNode); var innerState = initNode.State as InnerLoopBehaviorState; Assert.NotNull(innerState); Assert.True(innerState.UserKnowledge.ToString().Equals("1+1")); Assert.True(initNode.InEdges.Count == 0); Assert.True(initNode.OutEdges.Count == 1); int count = graph.PathFinding(initNode); Assert.True(count == 1); var nextObj = graph.SearchNextInnerLoopNode(initNode); var tuple22 = nextObj as Tuple <object, object>; Assert.NotNull(tuple22); var nextNode = tuple22.Item2 as BehaviorGraphNode; Assert.NotNull(nextNode); count = graph.PathFinding(nextNode); Assert.True(count == 0); }
public void Test_OneStrategy_MultiTraceStep_Author() { var expr1 = new Term(Expression.Add, new List<object> { 1, 1, 1 }); var expr2 = new Term(Expression.Add, new List<object> { 2, 1 }); var ts1 = new TraceStep(expr1, expr2, null, "meta-rule todo", "rule todo"); var ts1Expr = new TraceStepExpr(ts1); var ts2 = new TraceStep(expr2, 2, null, "meta-rule todo", "rule todo"); var ts2Expr = new TraceStepExpr(ts2); var lst = new List<TraceStepExpr>() { ts1Expr, ts2Expr }; var tuple = new Tuple<object, object>("strategy2", lst); var lstStrategy = new List<Tuple<object, object>>(); lstStrategy.Add(tuple); var graph = new BehaviorGraph(); graph.Insert(lstStrategy); Assert.True(graph.Nodes.Count == 2); var node0 = graph.Nodes[0]; Assert.Null(node0.SubGraph); var node1 = graph.Nodes[1]; Assert.NotNull(node1.SubGraph); Assert.True(node1.SubGraph.Nodes.Count == 3); ///////////////////////////////////////////// // User Input, //2 steps trace var expr3 = new Term(Expression.Add, new List<object> { 1, 2 }); var ts3 = new TraceStep(expr1, expr3, null, "meta-rule todo", "rule todo"); var ts3Expr = new TraceStepExpr(ts3); var ts4 = new TraceStep(expr3, 2, null, "meta-rule todo", "rule todo"); var ts4Expr = new TraceStepExpr(ts4); var lst2 = new List<TraceStepExpr>() { ts3Expr, ts4Expr }; var tuple2 = new Tuple<object, object>("strategy1", lst2); var lstStrategy2 = new List<Tuple<object, object>>(); lstStrategy2.Add(tuple2); //Under the same strategy graph.Update(lstStrategy2); Assert.True(graph.Nodes.Count == 3); Assert.True(node1.SubGraph.Nodes.Count == 3); }
public void Test_OneStrategy_OneTraceStep() { //strategy1 : 1+1->2 var expr1 = new Term(Expression.Add, new List <object> { 1, 1 }); var ts = new TraceStep(expr1, 2, "null", "meta-rule todo", "rule todo"); var tsExpr = new TraceStepExpr(ts); var lst = new List <TraceStepExpr>() { tsExpr }; var tuple = new Tuple <object, object>("strategy1", lst); var lstStrategy = new List <Tuple <object, object> >(); lstStrategy.Add(tuple); var graph = new BehaviorGraph(); graph.Insert(lstStrategy); Assert.True(graph.Nodes.Count == 2); var node0 = graph.Nodes[0]; Assert.Null(node0.SubGraph); var node1 = graph.Nodes[1]; Assert.True(node0.OutEdges.Count == 1); var edgeInfo = node0.OutEdges[0].Property as OuterLoopEdgeProperty; Assert.NotNull(edgeInfo); Assert.True(edgeInfo.Strategy.Equals("strategy1")); Assert.NotNull(node1.SubGraph); Assert.True(node1.InEdges.Count == 1); Assert.True(node1.OutEdges.Count == 0); Assert.True(node1.SubGraph.Nodes.Count == 2); ///////////////////////////////////////////////////// //Search Test var initNode = graph.RetrieveInitInnerNode(); Assert.NotNull(initNode); int count = graph.PathFinding(initNode); Assert.True(count == 1); var nextObj = graph.SearchNextInnerLoopNode(initNode); var tuple2 = nextObj as Tuple <object, object>; Assert.NotNull(tuple2); var nextNode = tuple2.Item2 as BehaviorGraphNode; Assert.NotNull(nextNode); count = graph.PathFinding(nextNode); Assert.True(count == 0); var prevObj = graph.SearchPrevInnerLoopNode(nextNode) as BehaviorGraphNode; Assert.NotNull(prevObj); count = graph.PathFinding(prevObj); Assert.True(count == 1); }
/// <summary> /// if x = y and y = z, then x = z /// if x = y, then x + a = y + a /// if x^2 = y^2, then x = y /// if x = y, then ax = ay /// ax = ay -> x=y /// </summary> /// <param name="goal"></param> /// <param name="gGoal"></param> /// <returns></returns> public static object ApplyTransitive(this Equation currentEq, Equation rootEq, bool withEqRule, bool lineCheck = false) { Equation localEq = currentEq; object lhs = currentEq.Lhs; object rhs = currentEq.Rhs; if (!withEqRule) return localEq; //Power Inverse if (SatisfyTransitiveCondition2(lhs, rhs)) { #region Condition2 var cloneEq = currentEq.Clone(); var cloneEq2 = currentEq.Clone(); var lhsTerm = cloneEq.Lhs as Term; Debug.Assert(lhsTerm != null); var cloneLst = lhsTerm.Args as List<object>; Debug.Assert(cloneLst != null); cloneEq.Lhs = cloneLst[0]; cloneEq.Rhs = new Term(Expression.Power, new List<object>() { cloneEq.Rhs, 0.5 }); var lhsTerm2 = cloneEq2.Lhs as Term; Debug.Assert(lhsTerm2 != null); var cloneLst2 = lhsTerm2.Args as List<object>; Debug.Assert(cloneLst2 != null); cloneEq2.Lhs = cloneLst2[0]; var internal1 = new Term(Expression.Power, new List<object>() { cloneEq2.Rhs, 0.5 }); cloneEq2.Rhs = new Term(Expression.Multiply, new List<object>() { -1, internal1 }); string rule = EquationsRule.Rule(EquationsRule.EquationRuleType.Transitive); string appliedRule = EquationsRule.Rule( EquationsRule.EquationRuleType.Transitive, localEq, null); string KC = EquationsRule.RuleConcept(EquationsRule.EquationRuleType.Transitive); var ts = new TraceStep(localEq, cloneEq, KC, rule, appliedRule); rootEq._innerLoop.Add(ts); //localEq = cloneEq; var lst = new List<Equation>(); lst.Add(cloneEq); lst.Add(cloneEq2); return lst; #endregion } if (!lineCheck) { //Add Inverse if (SatifyTransitiveCondition0(lhs, rhs)) { #region condition0 var cloneEq = currentEq.Clone(); var rhsTerm = new Term(Expression.Add, new List<object>() { cloneEq.Rhs }); var lhsTerm = cloneEq.Lhs as Term; Debug.Assert(lhsTerm != null); var lst = lhsTerm.Args as List<object>; Debug.Assert(lst != null); for (int i = 0; i < lst.Count; i++) { var temp = lst[i]; bool isNumber = LogicSharp.IsNumeric(temp); if (isNumber) { var inverseRhs = new Term(Expression.Multiply, new List<object>() { -1, temp }); lst.Remove(temp); var rhsArgLst = rhsTerm.Args as List<object>; Debug.Assert(rhsArgLst != null); rhsArgLst.Add(inverseRhs); break; } var term = temp as Term; if (term != null && !term.ContainsVar()) { var inverseRhs = new Term(Expression.Multiply, new List<object>() { -1, temp }); lst.Remove(i); var rhsArgLst = rhsTerm.Args as List<object>; Debug.Assert(rhsArgLst != null); rhsArgLst.Add(inverseRhs); break; } } cloneEq.Rhs = rhsTerm; if (lst.Count == 1) { cloneEq.Lhs = lst[0]; } string rule = EquationsRule.Rule(EquationsRule.EquationRuleType.Transitive); string appliedRule = EquationsRule.Rule( EquationsRule.EquationRuleType.Transitive, localEq, null); string KC = EquationsRule.RuleConcept(EquationsRule.EquationRuleType.Transitive); var traceStep = new TraceStep(localEq, cloneEq, KC, rule, appliedRule); rootEq._innerLoop.Add(traceStep); localEq = cloneEq; return localEq; #endregion } } else { if (SatisfyTransitiveCondition1(lhs, rhs)) { #region Condition1 var cloneEq = currentEq.Clone(); var inverseRhs = new Term(Expression.Multiply, new List<object>() { -1, rhs }); var lhsTerm = cloneEq.Lhs as Term; if (lhsTerm != null) { var cloneLst = lhsTerm.Args as List<object>; Debug.Assert(cloneLst != null); if (lhsTerm.Op.Method.Name.Equals("Add")) { cloneLst.Add(inverseRhs); } else { cloneEq.Lhs = new Term(Expression.Add, new List<object>() { lhs, inverseRhs }); } } else { cloneEq.Lhs = new Term(Expression.Add, new List<object>() { lhs, inverseRhs }); } cloneEq.Rhs = 0; string rule = EquationsRule.Rule(EquationsRule.EquationRuleType.Transitive); string appliedRule = EquationsRule.Rule( EquationsRule.EquationRuleType.Transitive, localEq, null); string KC = EquationsRule.RuleConcept(EquationsRule.EquationRuleType.Transitive); var traceStep = new TraceStep(localEq, cloneEq, KC, rule, appliedRule); rootEq._innerLoop.Add(traceStep); localEq = cloneEq; #endregion } } //Mutliply Inverse if (SatisfyTransitiveCondition3(lhs, rhs)) { #region condition3 var cloneEq = currentEq.Clone(); var rhsTerm = new Term(Expression.Multiply, new List<object>() { cloneEq.Rhs }); var lhsTerm = cloneEq.Lhs as Term; Debug.Assert(lhsTerm != null); var lst = lhsTerm.Args as List<object>; Debug.Assert(lst != null); for (int i = 0; i < lst.Count; i++) { var temp = lst[i]; bool isNumber = LogicSharp.IsNumeric(temp); if (isNumber) { var inverseRhs = new Term(Expression.Divide, new List<object>() { 1, temp }); lst.Remove(temp); var rhsArgLst = rhsTerm.Args as List<object>; Debug.Assert(rhsArgLst != null); rhsArgLst.Add(inverseRhs); break; } var term = temp as Term; if (term != null && !term.ContainsVar()) { var inverseRhs = new Term(Expression.Divide, new List<object>() { 1, temp }); lst.Remove(i); var rhsArgLst = rhsTerm.Args as List<object>; Debug.Assert(rhsArgLst != null); rhsArgLst.Add(inverseRhs); break; } } cloneEq.Rhs = rhsTerm; if (lst.Count == 1) { cloneEq.Lhs = lst[0]; } string rule = EquationsRule.Rule(EquationsRule.EquationRuleType.Transitive); string appliedRule = EquationsRule.Rule( EquationsRule.EquationRuleType.Transitive, localEq, null); string KC = EquationsRule.RuleConcept(EquationsRule.EquationRuleType.Transitive); var traceStep = new TraceStep(localEq, cloneEq, KC, rule, appliedRule); rootEq._innerLoop.Add(traceStep); localEq = cloneEq; return localEq; #endregion } //Divide Inverse if (SatisfyTransitiveCondition4(lhs, rhs)) { #region condition4 var cloneEq = currentEq.Clone(); var lhsTerm = cloneEq.Lhs as Term; Debug.Assert(lhsTerm != null); var lst = lhsTerm.Args as List<object>; Debug.Assert(lst != null); Debug.Assert(lst.Count == 2); bool numerator = LogicSharp.IsNumeric(lst[0]); bool deNumerator = LogicSharp.IsNumeric(lst[1]); if (deNumerator) { var rhsTerm = new Term(Expression.Multiply, new List<object>() { lst[1], cloneEq.Rhs }); var newEq = new Equation(lst[0], rhsTerm); string rule = EquationsRule.Rule(EquationsRule.EquationRuleType.Transitive); string appliedRule = EquationsRule.Rule( EquationsRule.EquationRuleType.Transitive, localEq, newEq); string KC = EquationsRule.RuleConcept(EquationsRule.EquationRuleType.Transitive); var traceStep = new TraceStep(localEq, newEq, KC, rule, appliedRule); rootEq._innerLoop.Add(traceStep); localEq = newEq; return localEq; } if (numerator) { var rhsTerm = new Term(Expression.Divide, new List<object>() { lst[0], cloneEq.Rhs }); var newEq = new Equation(lst[1], rhsTerm); string rule = EquationsRule.Rule(EquationsRule.EquationRuleType.Transitive); string appliedRule = EquationsRule.Rule( EquationsRule.EquationRuleType.Transitive, localEq, newEq); string KC = EquationsRule.RuleConcept(EquationsRule.EquationRuleType.Transitive); var traceStep = new TraceStep(localEq, newEq, KC, rule, appliedRule); rootEq._innerLoop.Add(traceStep); localEq = newEq; return localEq; } #endregion } return localEq; }
public void Test_OneStrategy_MultiTraceStep() { var expr1 = new Term(Expression.Add, new List <object> { 1, 1, 1 }); var expr2 = new Term(Expression.Add, new List <object> { 2, 1 }); var ts1 = new TraceStep(expr1, expr2, "null", "meta-rule todo", "rule todo"); var ts1Expr = new TraceStepExpr(ts1); var ts2 = new TraceStep(expr2, 2, "null", "meta-rule todo", "rule todo"); var ts2Expr = new TraceStepExpr(ts2); var lst = new List <TraceStepExpr>() { ts1Expr, ts2Expr }; var tuple = new Tuple <object, object>("strategy1", lst); var lstStrategy = new List <Tuple <object, object> >(); lstStrategy.Add(tuple); var graph = new BehaviorGraph(); graph.Insert(lstStrategy); Assert.True(graph.Nodes.Count == 2); var node0 = graph.Nodes[0]; Assert.Null(node0.SubGraph); var node1 = graph.Nodes[1]; Assert.NotNull(node1.SubGraph); Assert.True(node1.SubGraph.Nodes.Count == 3); Assert.True(node1.OutEdges.Count == 0); ///////////////////////////////////////////////////// //Search Test var initNode = graph.RetrieveInitInnerNode(); Assert.NotNull(initNode); int count = graph.PathFinding(initNode); Assert.True(count == 2); var nextObj = graph.SearchNextInnerLoopNode(initNode); var tuple2 = nextObj as Tuple <object, object>; Assert.NotNull(tuple2); var nextNode = tuple2.Item2 as BehaviorGraphNode; Assert.NotNull(nextNode); count = graph.PathFinding(nextNode); Assert.True(count == 1); var nextObj2 = graph.SearchNextInnerLoopNode(nextNode); var tuple3 = nextObj2 as Tuple <object, object>; Assert.NotNull(tuple3); var nextNode2 = tuple3.Item2 as BehaviorGraphNode; Assert.NotNull(nextNode2); count = graph.PathFinding(nextNode2); Assert.True(count == 0); }
public void Test_OneStrategy_MultiTraceStep_UserInput() { var expr1 = new Term(Expression.Add, new List<object> { 1, 1, 1 }); var eq1 = new Equation(expr1, 20); var expr2 = new Term(Expression.Add, new List<object> { 2, 1 }); var ts1 = new TraceStep(eq1, expr2, null, "meta-rule todo", "rule todo"); var ts1Expr = new TraceStepExpr(ts1); var ts2 = new TraceStep(expr2, 2, null, "meta-rule todo", "rule todo"); var ts2Expr = new TraceStepExpr(ts2); var lst = new List<TraceStepExpr>() { ts1Expr, ts2Expr }; var tuple = new Tuple<object, object>("strategy1", lst); var lstStrategy = new List<Tuple<object, object>>(); lstStrategy.Add(tuple); var graph = new BehaviorGraph(); graph.Insert(lstStrategy); Assert.True(graph.Nodes.Count == 2); var node0 = graph.Nodes[0]; Assert.Null(node0.SubGraph); var node1 = graph.Nodes[1]; Assert.NotNull(node1.SubGraph); Assert.True(node1.SubGraph.Nodes.Count == 3); ///////////////////////////////////////////// // User Input var userExpr1 = new Term(Expression.Add, new List<object> { 1, 1, 1 }); var userEq1 = new Equation(userExpr1, 20); var node = graph.SearchInnerLoopNode(userEq1); Assert.NotNull(node); var userExpr2 = new Term(Expression.Add, new List<object> { 1, 3 }); node = graph.SearchInnerLoopNode(userExpr2); Assert.Null(node); }
public static void FromPointPointToLine(PointSymbol ps1, PointSymbol ps2, LineSymbol ls) { var m = new Var("m"); var k = new Var("k"); var x = new Var("x"); var y = new Var("y"); var term = new Term(Expression.Multiply, new List<object>() { m, x }); var term1 = new Term(Expression.Add, new List<object>() { term, k }); var eqPattern = new Equation(y, term1); var term2 = new Term(Expression.Multiply, new List<object>() {m, ps1.SymXCoordinate}); var term22 = new Term(Expression.Add, new List<object>() {term2, k}); var eqPattern1 = new Equation(ps1.SymYCoordinate, term22); var term3 = new Term(Expression.Multiply, new List<object>() {m, ps2.SymXCoordinate}); var term33 = new Term(Expression.Add, new List<object>() { term3, k }); var eqPattern2 = new Equation(ps2.SymYCoordinate, term33); string strategy = "Generate a line by substituting two given points into the line slope-intercept form y=mx+k."; var ts0 = new TraceStep(eqPattern, eqPattern1, SubstitutionRule.SubstituteKC(),SubstitutionRule.SubstitutionStrategy, SubstitutionRule.ApplySubstitute(eqPattern, ps1)); var ts1 = new TraceStep(eqPattern, eqPattern2, SubstitutionRule.SubstituteKC(),SubstitutionRule.SubstitutionStrategy, SubstitutionRule.ApplySubstitute(eqPattern, ps2)); string kc = GeometryScaffold.KC_LineSlopeForm; var ts2 = new TraceStep(null, ls, kc, "calculate m and k through the above two linear equations.", "calculate m and k through linear equation and retrieve y=mx+k line form."); ls._innerLoop.Add(ts0); ls._innerLoop.Add(ts1); ls._innerLoop.Add(ts2); ls.GenerateATrace(strategy); }
public void Test_MultiStrategy_1() { //strategy1: 1->2, 2->3 var ts1 = new TraceStep(1, 2, "null", "TODO1", "TODO2"); var ts1Expr = new TraceStepExpr(ts1); var ts2 = new TraceStep(2, 3, "null", "TODO2", "TODO3"); var ts2Expr = new TraceStepExpr(ts2); var lst1 = new List <TraceStepExpr>() { ts1Expr, ts2Expr }; var tuple1 = new Tuple <object, object>("strategy1", lst1); //strategy2: 2->3, 3->5, 5->7 var ts3 = new TraceStep(2, 3, null, "TODO", "TODO1"); var ts3Expr = new TraceStepExpr(ts3); var ts4 = new TraceStep(3, 5, null, "TODO2", "TODO5"); var ts4Expr = new TraceStepExpr(ts4); var ts5 = new TraceStep(5, 7, null, "Test1", "test23"); var ts5Expr = new TraceStepExpr(ts5); var lst2 = new List <TraceStepExpr>() { ts3Expr, ts4Expr, ts5Expr }; var tuple2 = new Tuple <object, object>("strategy2", lst2); var lstStrategy = new List <Tuple <object, object> >(); lstStrategy.Add(tuple1); lstStrategy.Add(tuple2); var graph = new BehaviorGraph(); graph.Insert(lstStrategy); Assert.True(graph.Nodes.Count == 3); var node1 = graph.Nodes[1]; Assert.True(node1.OutEdges.Count == 1); var node2 = graph.Nodes[2]; Assert.True(node2.InEdges.Count == 1); Assert.True(node2.OutEdges.Count == 0); Assert.NotNull(node1.SubGraph); Assert.True(node1.SubGraph.Nodes.Count == 3); Assert.NotNull(node2.SubGraph); Assert.True(node2.SubGraph.Nodes.Count == 4); ///////////////////////////////////////////////////// //Search Test var initNode = graph.RetrieveInitInnerNode(); Assert.NotNull(initNode); int count = graph.PathFinding(initNode); Assert.True(count == 5); var nextObj = graph.SearchNextInnerLoopNode(initNode); var tuple5 = nextObj as Tuple <object, object>; Assert.NotNull(tuple5); var nextNode = tuple5.Item2 as BehaviorGraphNode; Assert.NotNull(nextNode); count = graph.PathFinding(nextNode); Assert.True(count == 4); nextObj = graph.SearchNextInnerLoopNode(nextNode); tuple5 = nextObj as Tuple <object, object>; Assert.NotNull(tuple5); nextNode = tuple5.Item2 as BehaviorGraphNode; Assert.NotNull(nextNode); count = graph.PathFinding(nextNode); Assert.True(count == 3); var index = graph.SearchOuterLoopNodeIndex(nextNode); Assert.True(index == 1); nextObj = graph.SearchNextInnerLoopNode(nextNode); tuple5 = nextObj as Tuple <object, object>; Assert.NotNull(tuple5); nextNode = tuple5.Item2 as BehaviorGraphNode; Assert.NotNull(nextNode); count = graph.PathFinding(nextNode); Assert.True(count == 2); index = graph.SearchOuterLoopNodeIndex(nextNode); Assert.True(index == 2); nextObj = graph.SearchNextInnerLoopNode(nextNode); tuple5 = nextObj as Tuple <object, object>; Assert.NotNull(tuple5); nextNode = tuple5.Item2 as BehaviorGraphNode; Assert.NotNull(nextNode); count = graph.PathFinding(nextNode); Assert.True(count == 1); index = graph.SearchOuterLoopNodeIndex(nextNode); Assert.True(index == 2); nextObj = graph.SearchNextInnerLoopNode(nextNode); tuple5 = nextObj as Tuple <object, object>; Assert.NotNull(tuple5); nextNode = tuple5.Item2 as BehaviorGraphNode; Assert.NotNull(nextNode); count = graph.PathFinding(nextNode); Assert.True(count == 0); index = graph.SearchOuterLoopNodeIndex(nextNode); Assert.True(index == 2); var prevNode = graph.SearchPrevInnerLoopNode(nextNode) as BehaviorGraphNode; Assert.NotNull(prevNode); count = graph.PathFinding(prevNode); Assert.True(count == 1); var strateties = graph.SearchAllOuterEdgeInfos(); Assert.True(strateties.Count == 2); }
public void Test_OneStrategy_OneTraceStep_Author() { //1+1->2 var expr1 = new Term(Expression.Add, new List<object> { 1, 1 }); var ts = new TraceStep(expr1, 2, "null", "meta-rule todo", "rule todo"); var tsExpr = new TraceStepExpr(ts); var lst = new List<TraceStepExpr>() { tsExpr }; var tuple = new Tuple<object, object>("strategy1", lst); var lstStrategy = new List<Tuple<object, object>>(); lstStrategy.Add(tuple); var graph = new BehaviorGraph(); graph.Insert(lstStrategy); Assert.True(graph.Nodes.Count == 2); var node0 = graph.Nodes[0]; Assert.Null(node0.SubGraph); var node1 = graph.Nodes[1]; Assert.NotNull(node1.SubGraph); Assert.True(node1.SubGraph.Nodes.Count == 2); ///////////////////////////////////////////// //4-1->2 var expr2 = new Term(Expression.Add, new List<object> { 4, 1 }); // User Input, //wrong step var ts1 = new TraceStep(expr2, 2); var ts1Expr = new TraceStepExpr(ts1); var tsLst = new List<TraceStepExpr>() { ts1Expr }; var tuple2 = new Tuple<object, object>("strategy1", tsLst); var lst44 = new List<Tuple<object, object>>() { tuple2 }; bool matchResult = graph.Match(lst44); Assert.True(matchResult); graph.Update(lst44); Assert.True(graph.Nodes.Count == 2); Assert.True(node1.SubGraph.Nodes.Count == 3); ///////////////////////////////////////////// //test search var initNode = graph.RetrieveInitInnerNode(); Assert.NotNull(initNode); var innerState = initNode.State as InnerLoopBehaviorState; Assert.NotNull(innerState); Assert.True(innerState.UserKnowledge.ToString().Equals("1+1")); Assert.True(initNode.InEdges.Count == 0); Assert.True(initNode.OutEdges.Count == 1); int count = graph.PathFinding(initNode); Assert.True(count == 1); var nextObj = graph.SearchNextInnerLoopNode(initNode); var tuple22 = nextObj as Tuple<object, object>; Assert.NotNull(tuple22); var nextNode = tuple22.Item2 as BehaviorGraphNode; Assert.NotNull(nextNode); count = graph.PathFinding(nextNode); Assert.True(count == 0); }
public static void FromPointsToMidPoint(PointSymbol ps1, PointSymbol ps2, PointSymbol midPoint) { //1. plot the ps1, ps2 and the line //2. generate x coordinate //3. generate y coordinate //4. generate point (x,y) string strategy; var ts00 = new TraceStep(null, ps1, null, PlottingRule.PlottingStrategy, PlottingRule.Plot(ps1)); var ts01 = new TraceStep(null, ps2, null, PlottingRule.PlottingStrategy, PlottingRule.Plot(ps2)); var ls = LineBinaryRelation.Unify(ps1, ps2) as LineSymbol; var ts02 = new TraceStep(null, ls, null, PlottingRule.PlottingStrategy, PlottingRule.Plot(ls)); strategy = "Plot the given two points and the connected line."; midPoint._innerLoop.Add(ts00); midPoint._innerLoop.Add(ts01); midPoint._innerLoop.Add(ts02); midPoint.GenerateATrace(strategy); //////////////////////////////////////////////// var xCoord = new Var("x"); var pt1 = ps1.Shape as Point; Debug.Assert(pt1 != null); var pt2 = ps2.Shape as Point; Debug.Assert(pt2 != null); var term1 = new Term(Expression.Add, new List<object>(){pt1.XCoordinate, pt2.XCoordinate}); var term2 = new Term(Expression.Divide, new List<object>(){term1, 2}); var eq = new Equation(xCoord, term2); ///////////////////////////////////////////////// object obj; bool result = eq.IsEqGoal(out obj); EqGoal goal1 = null; if (result) { goal1 = obj as EqGoal; Debug.Assert(goal1!=null); Debug.Assert(goal1.Traces.Count == 1); var currentTrace = goal1.Traces.ToList()[0]; strategy = "Generate x coordinate of midpoint."; var newTrace = new Tuple<object, object>(strategy, currentTrace.Item2); midPoint.Traces.Add(newTrace); } ////////////////////////////////////////////////// var yCoord = new Var("y"); term1 = new Term(Expression.Add, new List<object>() { pt1.YCoordinate, pt2.YCoordinate }); term2 = new Term(Expression.Divide, new List<object>() { term1, 2 }); var eq1 = new Equation(yCoord, term2); result = eq1.IsEqGoal(out obj); EqGoal goal2 = null; if (result) { goal2 = obj as EqGoal; Debug.Assert(goal2 != null); Debug.Assert(goal2.Traces.Count == 1); var currentTrace1 = goal2.Traces.ToList()[0]; strategy = "Generate y coordinate of midpoint."; var newTrace = new Tuple<object, object>(strategy, currentTrace1.Item2); midPoint.Traces.Add(newTrace); } ////////////////////////////////////////////////////// var ps = new Point(xCoord, yCoord); var psym = new PointSymbol(ps); var mid = midPoint.Shape as Point; Debug.Assert(mid != null); var ps_inter1 = new Point(mid.XCoordinate, yCoord); var psym_inter1 = new PointSymbol(ps_inter1); var metaRule = "Consider substitute generate x coordinate into the point form."; var appliedRule = SubstitutionRule.ApplySubstitute(goal1, psym); var ts1 = new TraceStep(psym, psym_inter1, null, metaRule, appliedRule); metaRule = "Consider substitute generate y coordinate into the point form."; appliedRule = SubstitutionRule.ApplySubstitute(goal2, psym_inter1); var ts2 = new TraceStep(psym_inter1, midPoint, null, metaRule, appliedRule); strategy = "Generate point (x,y) by subsitute generated x and y."; midPoint._innerLoop.Add(ts1); midPoint._innerLoop.Add(ts2); midPoint.GenerateATrace(strategy); }
public static void LineSlopeIntercepToGraph(LineSymbol ls) { string strategy = strategy_graphing; //Plotting shapes //1. plotting Y-Intercept //2. plotting X-Intercept //3. plotting the line ////////////////////////////////////////////////////////////// // Step 1: var pt = new Point(0, ls.SymIntercept); var ptSym = new PointSymbol(pt); var ts0 = new TraceStep(null, ptSym, null, PlottingRule.PlottingStrategy, PlottingRule.Plot(ptSym)); ls._innerLoop.Add(ts0); ////////////////////////////////////////////////////////////// // Step 2: var line = ls.Shape as Line; Debug.Assert(line != null); Equation eq; if (line.B == null) { } else { var x = new Var("x"); //step 2.1 var term = new Term(Expression.Multiply, new List <object>() { line.Slope, x }); var term1 = new Term(Expression.Add, new List <object>() { term, line.Intercept }); eq = new Equation(term1, 0); object obj; EqGoal gGoal = null; bool result = eq.IsEqGoal(out obj); if (result) { gGoal = obj as EqGoal; } if (gGoal != null) { double dX; LogicSharp.IsDouble(gGoal.Rhs, out dX); var pt1 = new Point(dX, 0); var ptSym1 = new PointSymbol(pt1); var ts1 = new TraceStep(null, ptSym1, null, PlottingRule.PlottingStrategy, PlottingRule.Plot(ptSym1)); ls._innerLoop.Add(ts1); } } ///////////////////////////////////////////////////////////////// const string step1MetaRule = "Given the line slope-intercept form y=mx+b, plot the line by passing points (0,b) and (-b/m,0)."; string step1AppliedRule = String.Format("Plotting the line passing through (0,{0}) and ({1},0) ", ls.SymIntercept, ls.SymC); //var ts = new TraceStep(null, ls.SlopeInterceptForm, step1MetaRule, step1AppliedRule); string kc = GeometryScaffold.KC_LineGraphing; var ts = new TraceStep(null, ls, kc, step1MetaRule, step1AppliedRule); ls._innerLoop.Add(ts); ////////////////////////////////////////////////////////////////// ls.GenerateATrace(strategy); }
public static void FromPointsToMidPoint(PointSymbol ps1, PointSymbol ps2, PointSymbol midPoint) { //1. plot the ps1, ps2 and the line //2. generate x coordinate //3. generate y coordinate //4. generate point (x,y) string strategy; var ts00 = new TraceStep(null, ps1, null, PlottingRule.PlottingStrategy, PlottingRule.Plot(ps1)); var ts01 = new TraceStep(null, ps2, null, PlottingRule.PlottingStrategy, PlottingRule.Plot(ps2)); var ls = LineBinaryRelation.Unify(ps1, ps2) as LineSymbol; var ts02 = new TraceStep(null, ls, null, PlottingRule.PlottingStrategy, PlottingRule.Plot(ls)); strategy = "Plot the given two points and the connected line."; midPoint._innerLoop.Add(ts00); midPoint._innerLoop.Add(ts01); midPoint._innerLoop.Add(ts02); midPoint.GenerateATrace(strategy); //////////////////////////////////////////////// var xCoord = new Var("x"); var pt1 = ps1.Shape as Point; Debug.Assert(pt1 != null); var pt2 = ps2.Shape as Point; Debug.Assert(pt2 != null); var term1 = new Term(Expression.Add, new List <object>() { pt1.XCoordinate, pt2.XCoordinate }); var term2 = new Term(Expression.Divide, new List <object>() { term1, 2 }); var eq = new Equation(xCoord, term2); ///////////////////////////////////////////////// object obj; bool result = eq.IsEqGoal(out obj); EqGoal goal1 = null; if (result) { goal1 = obj as EqGoal; Debug.Assert(goal1 != null); Debug.Assert(goal1.Traces.Count == 1); var currentTrace = goal1.Traces.ToList()[0]; strategy = "Generate x coordinate of midpoint."; var newTrace = new Tuple <object, object>(strategy, currentTrace.Item2); midPoint.Traces.Add(newTrace); } ////////////////////////////////////////////////// var yCoord = new Var("y"); term1 = new Term(Expression.Add, new List <object>() { pt1.YCoordinate, pt2.YCoordinate }); term2 = new Term(Expression.Divide, new List <object>() { term1, 2 }); var eq1 = new Equation(yCoord, term2); result = eq1.IsEqGoal(out obj); EqGoal goal2 = null; if (result) { goal2 = obj as EqGoal; Debug.Assert(goal2 != null); Debug.Assert(goal2.Traces.Count == 1); var currentTrace1 = goal2.Traces.ToList()[0]; strategy = "Generate y coordinate of midpoint."; var newTrace = new Tuple <object, object>(strategy, currentTrace1.Item2); midPoint.Traces.Add(newTrace); } ////////////////////////////////////////////////////// var ps = new Point(xCoord, yCoord); var psym = new PointSymbol(ps); var mid = midPoint.Shape as Point; Debug.Assert(mid != null); var ps_inter1 = new Point(mid.XCoordinate, yCoord); var psym_inter1 = new PointSymbol(ps_inter1); var metaRule = "Consider substitute generate x coordinate into the point form."; var appliedRule = SubstitutionRule.ApplySubstitute(goal1, psym); var ts1 = new TraceStep(psym, psym_inter1, null, metaRule, appliedRule); metaRule = "Consider substitute generate y coordinate into the point form."; appliedRule = SubstitutionRule.ApplySubstitute(goal2, psym_inter1); var ts2 = new TraceStep(psym_inter1, midPoint, null, metaRule, appliedRule); strategy = "Generate point (x,y) by subsitute generated x and y."; midPoint._innerLoop.Add(ts1); midPoint._innerLoop.Add(ts2); midPoint.GenerateATrace(strategy); }
/* * Automatic scaffolding * * Distance function (x-x0)^2+(y-y0)^2 = d^2 * * Forward chaining to derive d. * Backward chaining to derive other four parameters. */ private static Tuple <object, object> SlopeSubstitution(Point pt1, Point pt2, double value) { //out strategy string strategy = "Substitute two points and slope value into the slope function."; //1. Substitute two points into the line slope function. //2. Substitute slope property into the line slope function. var lst = new List <TraceStep>(); /////////////////////////////////////////////////////////////////// var variable = new Var('m'); //1. string step1metaRule = "Consider the Line Slope Function: m=(y1-y0)/(x1-x0)"; string step1AppliedRule = String.Format( "Substitute two points value into the slope function {0}=({1}-{2})/({3}-{4})", variable, pt2.YCoordinate.ToString(), pt1.YCoordinate.ToString(), pt2.XCoordinate.ToString(), pt1.XCoordinate.ToString()); /* var pt1X = new Var("x0"); * var pt1Y = new Var("y0"); * var pt2X = new Var("x1"); * var pt2Y = new Var("y1");*/ /* var term1_1 = new Term(Expression.Subtract, new List<object>() { pt1X, pt2X }); * var term2_1 = new Term(Expression.Subtract, new List<object>() { pt1Y, pt2Y }); * var rhs_1 = new Term(Expression.Divide, new List<object>() { term2_1, term1_1 }); * * var old_eq = new Equation(variable, rhs_1);*/ var term1_11 = new Term(Expression.Subtract, new List <object>() { pt2.XCoordinate, pt1.XCoordinate }); var term2_11 = new Term(Expression.Subtract, new List <object>() { pt2.YCoordinate, pt1.YCoordinate }); var rhs_11 = new Term(Expression.Divide, new List <object>() { term1_11, term2_11 }); var eq = new Equation(variable, rhs_11); string kc = GeometryScaffold.KC_LineSlopeForm; var trace1 = new TraceStep(null, eq, kc, step1metaRule, step1AppliedRule); //lst.Add(trace1); lst.Add(trace1); /////////////////////////////////////////////////////////////////////////// //2. var newEq = new Equation(value, rhs_11); string step2metaRule = "Substitute slope property into the Line Slope Function: m=(y1-y0)/(x1-x0)"; string step2AppliedRule = String.Format( "Substitute slope value into the slope function {0}=({1}-{2})/({3}-{4})", value, pt2.YCoordinate.ToString(), pt1.YCoordinate.ToString(), pt2.XCoordinate.ToString(), pt1.XCoordinate.ToString()); kc = SubstitutionRule.SubstituteKC(); var trace2 = new TraceStep(null, newEq, kc, step2metaRule, step2AppliedRule); //lst.Add(trace1); lst.Add(trace2); //////////////////////////////////////////////////////////////////////////// var newTuple = new Tuple <object, object>(strategy, lst); return(newTuple); }
private Equation EvalTermInEquation(Equation rootEq, bool isLhs) { Equation localEq = this; object obj = isLhs ? Lhs : Rhs; var term = obj as Term; if (term == null) return localEq; object evalResult = term.Eval(); if (evalResult.Equals(term)) return localEq; var cloneEq = Clone(); if (isLhs) { cloneEq.Lhs = evalResult; } else { cloneEq.Rhs = evalResult; } Equation currentEq = FindCurrentEq(rootEq); #region Trace Transformation from term to Equation if (term.Traces.Count != 0) { localEq = currentEq; foreach (var trace in term.Traces) { var strategy = trace.Item1 as string; var lst = trace.Item2 as List<TraceStep>; foreach (var ts in lst) { var cloneEq2 = Generate(currentEq, ts.Source, ts.Target, isLhs); var eqTraceStep = new TraceStep(localEq, cloneEq2, ts.KC, ts.Rule, ts.AppliedRule); rootEq._innerLoop.Add(eqTraceStep); localEq = cloneEq2; } //GenerateATrace(strategy); } } if (term._innerLoop.Count != 0) { foreach (var ts in term._innerLoop) { var cloneEq1 = Generate(currentEq, ts.Source, ts.Target, isLhs); var eqTraceStep = new TraceStep(localEq, cloneEq1, ts.KC, ts.Rule, ts.AppliedRule); rootEq._innerLoop.Add(eqTraceStep); localEq = cloneEq1; } } #endregion return cloneEq; }
/* * Automatic scaffolding * * Distance function (x-x0)^2+(y-y0)^2 = d^2 * * Forward chaining to derive d. * Backward chaining to derive other four parameters. */ private static Tuple<object, object> SlopeSubstitution(Point pt1, Point pt2, double value) { //out strategy string strategy = "Substitute two points and slope value into the slope function."; //1. Substitute two points into the line slope function. //2. Substitute slope property into the line slope function. var lst = new List<TraceStep>(); /////////////////////////////////////////////////////////////////// var variable = new Var('m'); //1. string step1metaRule = "Consider the Line Slope Function: m=(y1-y0)/(x1-x0)"; string step1AppliedRule = String.Format( "Substitute two points value into the slope function {0}=({1}-{2})/({3}-{4})", variable, pt2.YCoordinate.ToString(), pt1.YCoordinate.ToString(), pt2.XCoordinate.ToString(), pt1.XCoordinate.ToString()); /* var pt1X = new Var("x0"); var pt1Y = new Var("y0"); var pt2X = new Var("x1"); var pt2Y = new Var("y1");*/ /* var term1_1 = new Term(Expression.Subtract, new List<object>() { pt1X, pt2X }); var term2_1 = new Term(Expression.Subtract, new List<object>() { pt1Y, pt2Y }); var rhs_1 = new Term(Expression.Divide, new List<object>() { term2_1, term1_1 }); var old_eq = new Equation(variable, rhs_1);*/ var term1_11 = new Term(Expression.Subtract, new List<object>() { pt2.XCoordinate, pt1.XCoordinate }); var term2_11 = new Term(Expression.Subtract, new List<object>() { pt2.YCoordinate, pt1.YCoordinate }); var rhs_11 = new Term(Expression.Divide, new List<object>() { term1_11, term2_11 }); var eq = new Equation(variable, rhs_11); string kc = GeometryScaffold.KC_LineSlopeForm; var trace1 = new TraceStep(null, eq, kc, step1metaRule, step1AppliedRule); //lst.Add(trace1); lst.Add(trace1); /////////////////////////////////////////////////////////////////////////// //2. var newEq = new Equation(value, rhs_11); string step2metaRule = "Substitute slope property into the Line Slope Function: m=(y1-y0)/(x1-x0)"; string step2AppliedRule = String.Format( "Substitute slope value into the slope function {0}=({1}-{2})/({3}-{4})", value, pt2.YCoordinate.ToString(), pt1.YCoordinate.ToString(), pt2.XCoordinate.ToString(), pt1.XCoordinate.ToString()); kc = SubstitutionRule.SubstituteKC(); var trace2 = new TraceStep(null, newEq, kc, step2metaRule, step2AppliedRule); //lst.Add(trace1); lst.Add(trace2); //////////////////////////////////////////////////////////////////////////// var newTuple = new Tuple<object, object>(strategy, lst); return newTuple; }