public override bool Equals(object obj) { PredicateFormula fOther = null; if (obj is CompoundFormula) { Formula fSimplify = ((CompoundFormula)obj).Simplify(); if (fSimplify is PredicateFormula) { fOther = (PredicateFormula)fSimplify; } else { return(false);//might not be accurate - could be not } } else if (obj is PredicateFormula) { fOther = (PredicateFormula)obj; } else { return(false); } return(Predicate.Equals(fOther.Predicate)); }
public override Formula GenerateGiven(string sTag, List <string> lAlwaysKnown) { if (lAlwaysKnown.Contains(Predicate.Name)) { return(this); } PredicateFormula pfGiven = new PredicateFormula(Predicate.GenerateKnowGiven(sTag)); return(pfGiven); }
public bool ConsistentWith(Formula f) { if (f is CompoundFormula) { CompoundFormula cf = (CompoundFormula)f; bool bConsistent = false; foreach (Formula fOperand in cf.Operands) { bConsistent = ConsistentWith(fOperand); if (cf.Operator == "and" && !bConsistent) { return(false); } if (cf.Operator == "or" && bConsistent) { return(true); } if (cf.Operator == "not") { return(!bConsistent); } } if (cf.Operator == "and") { return(true); } if (cf.Operator == "or") { return(false); } } else { PredicateFormula vf = (PredicateFormula)f; return(ConsistentWith(vf.Predicate)); } return(false); }
public bool AddOperand(Predicate p) { PredicateFormula pf = new PredicateFormula(p); return AddOperand(pf); }
public void SimpleAddOperand(Predicate p) { PredicateFormula pf = new PredicateFormula(p); SimpleAddOperand(pf); }
private Formula ReadPredicate(CompoundExpression exp, Dictionary <string, string> dParameterNameToType, bool bParametrized, Domain d) { Predicate p = null; int iExpression = 0; string sName = ""; if (bParametrized) { p = new ParametrizedPredicate(exp.Type); } else { p = new GroundedPredicate(exp.Type); } bool bAllConstants = true; for (iExpression = 0; iExpression < exp.SubExpressions.Count; iExpression++) { sName = exp.SubExpressions[iExpression].ToString(); if (bParametrized) { Argument a = null; if (sName.StartsWith("?")) { a = new Parameter(dParameterNameToType[sName], sName); bAllConstants = false; } else { a = new Constant(d.ConstantNameToType[sName], sName); } ((ParametrizedPredicate)p).AddParameter(a); } else { try { Constant c = new Constant(d.ConstantNameToType[sName], sName); ((GroundedPredicate)p).AddConstant(c); } catch (Exception e) { Console.WriteLine(); } } } if (bParametrized) { if (!MatchParametersToPredicateDeclaration((ParametrizedPredicate)p, d)) { throw new NotImplementedException(); } } if (bParametrized && bAllConstants) { GroundedPredicate gp = new GroundedPredicate(p.Name); foreach (Constant c in ((ParametrizedPredicate)p).Parameters) { gp.AddConstant(c); } p = gp; } PredicateFormula vf = new PredicateFormula(p); return(vf); }
private Formula ReadFormula(CompoundExpression exp, Dictionary <string, string> dParameterNameToType, bool bParamterized, Domain d) { bool bPredicate = true; //Console.WriteLine(exp); if (d != null && d.IsFunctionExpression(exp.Type)) { Predicate p = ReadFunctionExpression(exp, dParameterNameToType, d); return(new PredicateFormula(p)); } else if (IsUniversalQuantifier(exp)) { CompoundExpression eParameter = (CompoundExpression)exp.SubExpressions[0]; CompoundExpression eBody = (CompoundExpression)exp.SubExpressions[1]; string sParameter = eParameter.Type; string sType = eParameter.SubExpressions[1].ToString(); dParameterNameToType[sParameter] = sType; ParametrizedFormula cfQuantified = new ParametrizedFormula(exp.Type); cfQuantified.Parameters[sParameter] = sType; Formula fBody = ReadFormula(eBody, dParameterNameToType, true, d); cfQuantified.AddOperand(fBody); return(cfQuantified); } else if (exp.Type == "probabilistic") { ProbabilisticFormula pf = new ProbabilisticFormula(); int iExpression = 0; for (iExpression = 0; iExpression < exp.SubExpressions.Count; iExpression += 2) { //if (exp.SubExpressions[iExpression] is StringExpression) // throw new InvalidDataException(); string sProb = exp.SubExpressions[iExpression].ToString(); double dProb = 0.0; if (sProb.Contains("/")) { string[] a = sProb.Split('/'); dProb = double.Parse(a[0]) / double.Parse(a[1]); } else { dProb = double.Parse(sProb); } Formula f = ReadFormula((CompoundExpression)exp.SubExpressions[iExpression + 1], dParameterNameToType, bParamterized, d); pf.AddOption(f, dProb); } return(pf); } else { foreach (Expression eSub in exp.SubExpressions) { if (eSub is CompoundExpression) { bPredicate = false; break; } } if (bPredicate) { return(ReadPredicate(exp, dParameterNameToType, bParamterized, d)); } else { CompoundFormula cf = new CompoundFormula(exp.Type); int iExpression = 0; for (iExpression = 0; iExpression < exp.SubExpressions.Count; iExpression++) { if (exp.SubExpressions[iExpression] is StringExpression) { throw new InvalidDataException(); } Formula f = ReadFormula((CompoundExpression)exp.SubExpressions[iExpression], dParameterNameToType, bParamterized, d); cf.SimpleAddOperand(f); } if (cf.Operator == "not" && cf.Operands[0] is PredicateFormula) { PredicateFormula fNegate = new PredicateFormula(((PredicateFormula)cf.Operands[0]).Predicate.Negate()); return(fNegate); } return(cf); } } }
public override Formula Clone() { PredicateFormula f = new PredicateFormula(Predicate); return f; }
public override Formula GenerateGiven(string sTag, List<string> lAlwaysKnown) { if (lAlwaysKnown.Contains(Predicate.Name)) return this; PredicateFormula pfGiven = new PredicateFormula(Predicate.GenerateKnowGiven(sTag)); return pfGiven; }
private Formula ReadPredicate(CompoundExpression exp, Dictionary<string, string> dParameterNameToType, bool bParametrized, Domain d) { Predicate p = null; int iExpression = 0; string sName = ""; if (bParametrized) p = new ParametrizedPredicate(exp.Type); else p = new GroundedPredicate(exp.Type); bool bAllConstants = true; for (iExpression = 0; iExpression < exp.SubExpressions.Count; iExpression++) { sName = exp.SubExpressions[iExpression].ToString(); if (bParametrized) { Argument a = null; if (sName.StartsWith("?")) { a = new Parameter(dParameterNameToType[sName], sName); bAllConstants = false; } else { a = new Constant(d.ConstantNameToType[sName], sName); } ((ParametrizedPredicate)p).AddParameter(a); } else { try { Constant c = new Constant(d.ConstantNameToType[sName], sName); ((GroundedPredicate)p).AddConstant(c); } catch (Exception e) { Console.WriteLine(); } } } if (bParametrized) if (!MatchParametersToPredicateDeclaration((ParametrizedPredicate)p, d)) throw new NotImplementedException(); if (bParametrized && bAllConstants) { GroundedPredicate gp = new GroundedPredicate(p.Name); foreach (Constant c in ((ParametrizedPredicate)p).Parameters) gp.AddConstant(c); p = gp; } PredicateFormula vf = new PredicateFormula(p); return vf; }
private Formula ReadFormula(CompoundExpression exp, Dictionary<string, string> dParameterNameToType, bool bParamterized, Domain d) { bool bPredicate = true; //Console.WriteLine(exp); if (d!= null && d.IsFunctionExpression(exp.Type)) { Predicate p = ReadFunctionExpression(exp, dParameterNameToType, d); return new PredicateFormula(p); } else if (IsUniversalQuantifier(exp)) { CompoundExpression eParameter = (CompoundExpression)exp.SubExpressions[0]; CompoundExpression eBody = (CompoundExpression)exp.SubExpressions[1]; string sParameter = eParameter.Type; string sType = eParameter.SubExpressions[1].ToString(); dParameterNameToType[sParameter] = sType; ParametrizedFormula cfQuantified = new ParametrizedFormula(exp.Type); cfQuantified.Parameters[sParameter] = sType; Formula fBody = ReadFormula(eBody, dParameterNameToType, true, d); cfQuantified.AddOperand(fBody); return cfQuantified; } else if (exp.Type == "probabilistic") { ProbabilisticFormula pf = new ProbabilisticFormula(); int iExpression = 0; for (iExpression = 0; iExpression < exp.SubExpressions.Count; iExpression+=2) { //if (exp.SubExpressions[iExpression] is StringExpression) // throw new InvalidDataException(); string sProb = exp.SubExpressions[iExpression].ToString(); double dProb = 0.0; if (sProb.Contains("/")) { string[] a = sProb.Split('/'); dProb = double.Parse(a[0]) / double.Parse(a[1]); } else { dProb = double.Parse(sProb); } Formula f = ReadFormula((CompoundExpression)exp.SubExpressions[iExpression + 1], dParameterNameToType, bParamterized, d); pf.AddOption(f, dProb); } return pf; } else { foreach (Expression eSub in exp.SubExpressions) { if (eSub is CompoundExpression) { bPredicate = false; break; } } if (bPredicate) return ReadPredicate(exp, dParameterNameToType, bParamterized, d); else { CompoundFormula cf = new CompoundFormula(exp.Type); int iExpression = 0; for (iExpression = 0; iExpression < exp.SubExpressions.Count; iExpression++) { if (exp.SubExpressions[iExpression] is StringExpression) throw new InvalidDataException(); Formula f = ReadFormula((CompoundExpression)exp.SubExpressions[iExpression], dParameterNameToType, bParamterized, d); cf.SimpleAddOperand(f); } if (cf.Operator == "not" && cf.Operands[0] is PredicateFormula) { PredicateFormula fNegate = new PredicateFormula(((PredicateFormula)cf.Operands[0]).Predicate.Negate()); return fNegate; } return cf; } } }
public override Formula Clone() { PredicateFormula f = new PredicateFormula(Predicate); return(f); }