/// <summary> /// construct a line through a point and a goal, /// e.g A(1,2) ^ S = 2=> Conjunctive Norm Form /// </summary> /// <param name="pt1"></param> /// <param name="goal"></param> /// <returns></returns> public static LineSymbol Unify(PointSymbol pt, EqGoal goal) { var variable1 = goal.Lhs as Var; Debug.Assert(variable1 != null); if (LineAcronym.EqualSlopeLabels(variable1.ToString())) { double dValue; bool result = LogicSharp.IsDouble(goal.Rhs, out dValue); if (result) { if (!pt.Shape.Concrete) { return(null); } var line = LineGenerationRule.GenerateLine((Point)pt.Shape, dValue, null); if (pt.Traces.Count != 0) { line.Traces.AddRange(pt.Traces); } if (goal.Traces.Count != 0) { line.Traces.AddRange(goal.Traces); } TraceInstructionalDesign.FromPointSlopeToLine(pt, goal, line); line.OutputType = LineType.SlopeIntercept; return(line); } else { var line = new Line(null); //ghost line var ls = new LineSymbol(line); ls.OutputType = LineType.SlopeIntercept; return(ls); } } return(null); }
private static bool ConstraintCheck(PointSymbol pt1, PointSymbol pt2, object constraint, out object output) { output = null; Debug.Assert(constraint != null); var shapeType = constraint as ShapeType?; if (shapeType != null) { #region ShapeType Constraint Solving if (shapeType == ShapeType.Line) { output = LineBinaryRelation.Unify(pt1, pt2); if (output != null) { return(true); } output = LineGenerationRule.IdentityPoints; return(false); } if (shapeType == ShapeType.LineSegment) { output = LineSegBinaryRelation.Unify(pt1, pt2); if (output != null) { return(true); } output = LineSegmentGenerationRule.IdentityPoints; return(false); } return(false); #endregion } var label = constraint as string; if (label != null) { #region Label Constraint Solving if (PointAcronym.MidPoint.Equals(label)) { //Point output = PointBinaryRelation.Unify(pt1, pt2); var ps = output as PointSymbol; if (ps != null) { return(true); } return(false); } #region Line Inference //Case 2 if (LineAcronym.EqualGeneralFormLabels(label)) { output = LineBinaryRelation.Unify(pt1, pt2); var ls = output as LineSymbol; if (ls != null) { ls.OutputType = LineType.GeneralForm; return(true); } return(false); } if (LineAcronym.EqualSlopeLabels(label)) { output = LineBinaryRelation.Unify(pt1, pt2); if (output == null) { return(false); } var lss = output as LineSymbol; //TraceInstructionalDesign.FromPointsToLine(lss); var output1 = lss.Unify(label); output = output1; return(true); } if (LineAcronym.EqualInterceptLabels(label)) { output = LineBinaryRelation.Unify(pt1, pt2); if (output == null) { return(false); } var lss = output as LineSymbol; //TraceInstructionalDesign.FromPointsToLine(lss); var output1 = lss.Unify(label); output = output1; return(true); } if (LineAcronym.EqualSlopeInterceptFormLabels(label)) { output = LineBinaryRelation.Unify(pt1, pt2); var ls = output as LineSymbol; if (ls != null) { ls.OutputType = LineType.SlopeIntercept; return(true); } return(false); } #endregion #region Line Segment Inference if (LineSegmentAcronym.EqualDistanceLabel(label)) { output = LineSegBinaryRelation.Unify(pt1, pt2); if (output == null) { return(false); } var lss = output as LineSegmentSymbol; var output1 = lss.Unify(label); output = output1; #region Embed Line Segment(obsolete) /* * Debug.Assert(lss != null); * if (lss.CachedSymbols.Count == 0) * { * output = output1; * var lst = new List<object>() * { * output, output1 * }; * * output = lst; * } * else * { * var goalLst = output1 as List<EqGoal>; * Debug.Assert(goalLst != null); * var lst = new List<object>(); * for (int i = 0; i < lss.CachedSymbols.Count; i++) * { * var cachedSS = lss.CachedSymbols.ToList()[i]; * lst.Add(cachedSS); * lst.Add(goalLst[i]); * } * output = lst; * }*/ #endregion return(true); } #endregion #region ambiguious inference //Case 1 char[] charr = label.ToCharArray(); if (charr.Length != 2) { return(false); } string str1 = label.ToCharArray()[0].ToString(CultureInfo.InvariantCulture); string str2 = label.ToCharArray()[1].ToString(CultureInfo.InvariantCulture); string label1 = pt1.Shape.Label; string label2 = pt2.Shape.Label; if (label1 == null || label2 == null) { return(false); } bool condition1 = label1.Equals(str1) && label2.Equals(str2); bool condition2 = label1.Equals(str2) && label2.Equals(str1); if (condition1 || condition2) { var supportTypes = new List <ShapeType> { ShapeType.Line, ShapeType.LineSegment }; output = supportTypes; return(false); } #endregion #endregion } var eqGoal = constraint as EqGoal; if (eqGoal != null) { var goalLabel = eqGoal.Lhs.ToString(); #region Line Segment Inference if (LineSegmentAcronym.EqualDistanceLabel(goalLabel)) { output = LineSegBinaryRelation.Unify(pt1, pt2); if (output == null) { return(false); } var lss = output as LineSegmentSymbol; //TraceInstructionalDesign.FromPointsToLineSegment(lss); output = lss.Unify(eqGoal); if (output == null) { return(false); } return(true); } if (LineAcronym.EqualSlopeLabels(goalLabel)) { output = LineBinaryRelation.Unify(pt1, pt2, eqGoal); if (output == null) { return(false); } return(true); } #endregion } return(false); }
public static object Unify(this LineSymbol ls, object constraint) { var refObj = constraint as string; var eqGoal = constraint as EqGoal; if (refObj != null) { #region forward searching if (LineAcronym.EqualSlopeLabels(refObj)) { return(ls.InferSlope(refObj)); } if (LineAcronym.EqualInterceptLabels(refObj)) { return(ls.InferIntercept(refObj)); } if (LineAcronym.EqualGeneralFormLabels(refObj)) { return(ls.InferGeneralForm(refObj)); } if (LineAcronym.EqualSlopeInterceptFormLabels(refObj)) { return(ls.InferSlopeInterceptForm()); } if (LineAcronym.GraphLine.Equals(refObj)) { return(ls.InferGraph()); } #endregion } if (eqGoal != null) { var rhs = eqGoal.Rhs; double dNum; bool isDouble = LogicSharp.IsDouble(rhs, out dNum); if (!isDouble) { return(null); } var lhs = eqGoal.Lhs.ToString(); if (LineAcronym.EqualSlopeLabels(lhs)) { var obj = ls.InferSlope(dNum); var lstObj = obj as List <object>; Debug.Assert(lstObj != null); var eqGoal1 = lstObj[0] as EqGoal; if (eqGoal1 != null) { var newTraces = new List <Tuple <object, object> >(); newTraces.AddRange(eqGoal.Traces); newTraces.AddRange(eqGoal1.Traces); eqGoal1.Traces = newTraces; } return(obj); } } return(null); }
/// <summary> /// construct a line through two goals /// e.g m=2, k=3 => conjunctive norm form /// </summary> /// <param name="goal1"></param> /// <param name="goal2"></param> /// <returns></returns> public static LineSymbol Unify(EqGoal goal1, EqGoal goal2) { var variable1 = goal1.Lhs as Var; var variable2 = goal2.Lhs as Var; Debug.Assert(variable1 != null); Debug.Assert(variable2 != null); var dict = new Dictionary <string, object>(); string slopeKey = "slope"; string interceptKey = "intercept"; if (LineAcronym.EqualSlopeLabels(variable1.ToString())) //if (variable1.ToString().Equals(LineAcronym.Slope1)) { dict.Add(slopeKey, goal1.Rhs); } if (LineAcronym.EqualInterceptLabels(variable1.ToString())) //if (variable1.ToString().Equals(LineAcronym.Intercept1)) { dict.Add(interceptKey, goal1.Rhs); } if (LineAcronym.EqualSlopeLabels(variable2.ToString())) //if (variable2.ToString().Equals(LineAcronym.Slope1)) { if (dict.ContainsKey(slopeKey)) { return(null); } dict.Add(slopeKey, goal2.Rhs); } if (LineAcronym.EqualInterceptLabels(variable2.ToString())) //if (variable2.ToString().Equals(LineAcronym.Intercept1)) { if (dict.ContainsKey(interceptKey)) { return(null); } dict.Add(interceptKey, goal2.Rhs); } if (dict.Count == 2 && dict[slopeKey] != null && dict[interceptKey] != null) { if (LogicSharp.IsNumeric(dict[slopeKey]) && LogicSharp.IsNumeric(dict[interceptKey])) { double dSlope, dIntercept; LogicSharp.IsDouble(dict[slopeKey], out dSlope); LogicSharp.IsDouble(dict[interceptKey], out dIntercept); var line = LineGenerationRule.GenerateLine(dSlope, dIntercept); var ls = new LineSymbol(line) { OutputType = LineType.SlopeIntercept }; TraceInstructionalDesign.FromSlopeInterceptToLineSlopeIntercept(goal1, goal2, ls); return(ls); } else { //lazy evaluation //Constraint solving on Graph var line = new Line(null); //ghost line var ls = new LineSymbol(line); ls.OutputType = LineType.SlopeIntercept; return(ls); } } return(null); }