Exemplo n.º 1
0
/**************************************************************************************************
 *                      IO
 * ***********************************************************************************************/
Formula IO::readFromFile(const std::string fileName){
    Formula result;
    uint numberVars;
    std::fstream data;
    data.open(fileName, std::ios::in);
    if(!data.is_open()){
        std::cout << "Could not open: " << fileName;
        exit(1);
    }
    while(!data.eof()){
        std::string line;
        getline(data, line);
        if(line.empty()) continue;
        if(line[0] == 'c' || line[0] == 'C') continue;
// Header line
        if(line[0] == 'p'){
            line = line.substr(line.find(" ")+1);
            line = line.substr(line.find(" ")+1);
            numberVars = std::stoi(line.substr(0, line.find(" ")));

            result.setNumberOfVariables(numberVars);
            continue;
        }

        result.addClauses(Clauses(line));


    }

    return result;
}
Exemplo n.º 2
0
void MainWindow::impIClicked()
{

    QList<QGraphicsItem *> selected_list = scene->selectedItems();
    Node* selected = (Node*)(selected_list.at(0));

    selected->setRule("impI");
    selected->update();

    Formula f = selected->getFormula();
    Formula op1 = ((Imp*)f.get())->getOperand1();
    Formula op2 = ((Imp*)f.get())->getOperand2();


    std::ostringstream stream;
    op2->printFormula(stream);
    qreal rect_width =  stream.str().length()*PARAMETER;
    qreal rect_height = 20;

    int rect_x = selected->getx() ;
    int rect_y = selected->gety() - 20 - depth/2;

    QVector<Formula> assumptions = selected->getAssumptions();
    assumptions.push_back(op1);
    Node* item = new Node( op2, rect_width, rect_height, rect_x, rect_y, selected_list.at(0), assumptions);
    scene->addNode(item);

}
Exemplo n.º 3
0
 bool equalTo( const Formula & f) const
 {
   return f->getType() == this->getType() &&
     _op1->equalTo(((BinaryConjective *)f.get())->getOperand1())
     &&
     _op2->equalTo(((BinaryConjective *)f.get())->getOperand2());
 }
Exemplo n.º 4
0
/**
 * ((succ(_Y,_Z) & s(_X,_Z)) | theta__(_X,_Y)) -> s(_X,_Y)
 * @return 
 */
Formula HengZhang::createSuccAndSOrTheta__ImplS(const Formula& _originalFml) {
    //1 (succ(_Y,_Z)
    _term* term_y_z = Utils::combineTerms(m_vTermsY, m_vTermsZ);
    _formula* succ_y_z = Utils::compositeToAtom(m_nSymbolSucc, term_y_z);
    //2 s(_X,_Z)
    _term* term_x_z = Utils::combineTerms(m_vTermsX, m_vTermsZ);
    _formula* s_x_z = Utils::compositeToAtom(m_nSymbolS, term_x_z);
    //3 theta__(_X,_Y)
    Formula copyOriginalFml = _originalFml;
    map<int, string> mapPredicates = Vocabulary::instance().getAllIntensionPredicates();
    copyOriginalFml.doubleNegationPredicates(mapPredicates);
    _formula* theta__ = Utils::copyFormula(copyOriginalFml.getFormula());
    //4 s(_X,_Y)
    _term* term_x_y = Utils::combineTerms(m_vTermsX, m_vTermsY);
    _formula* s_x_y   = Utils::compositeToAtom(m_nSymbolS, term_x_y);

    //create structure
    _formula* ll = Utils::compositeByConnective(CONJ, succ_y_z, s_x_z);
    _formula* l = Utils::compositeByConnective(DISJ, ll, theta__);
    _formula* r = s_x_y;
    _formula* f = Utils::compositeByConnective(IMPL, l, r);
    
    Formula fml = Formula(f, false);
    return fml;
}
Exemplo n.º 5
0
Formula Formula::differentiateWithRespectTo(std::string varName) {
	using AST::Expression::Expression;
	using AST::Expression::Text;
    using AST::Visitor::GetDerivativeVisitor;
    using AST::Visitor::StringifyVisitor;
    using AST::Visitor::SimplifyVisitor;

    if (formulaExpr == NULL) {
    	throw FormulaException("invalid formula");
    }

	std::unique_ptr<GetDerivativeVisitor> getDerivativeVisitorPtr(new GetDerivativeVisitor());
	std::unique_ptr<SimplifyVisitor> simplifyVisitorPtr(new SimplifyVisitor());
	std::unique_ptr<StringifyVisitor> stringifyVisitorPtr(new StringifyVisitor());

	getDerivativeVisitorPtr.get()->setDerivativeVariableName(varName);

    std::unique_ptr<Expression> derivativeExprPtr((Expression *)formulaExpr->accept(getDerivativeVisitorPtr.get()));
	std::unique_ptr<Expression> simplifiedDerivativeExprPtr((Expression *)derivativeExprPtr.get()->accept(simplifyVisitorPtr.get()));
    std::unique_ptr<Text> stringifiedDerivativeExprPtr((Text *)simplifiedDerivativeExprPtr.get()->accept(stringifyVisitorPtr.get()));

	Formula derivative;
    derivative.setFormulaString(stringifiedDerivativeExprPtr.get()->getContent());
    derivative.setFormulaExpr(simplifiedDerivativeExprPtr.release());

	return derivative;
}
Exemplo n.º 6
0
/**
 * 章衡量词消去公式五 2   succ(_Y,_Z) -> ((theta(_X,_Z) | t(_X,_Z)) -> t(_X,_Y)) 
 * @param originalFml 一阶语句
 * @return 
 */
Formula HengZhang::createFormula_5_2(const Formula& _originalFml) {
    //1 _succ(_Y,_Z)
    _term* term_y_z = Utils::combineTerms(m_vTermsY, m_vTermsZ);
    _formula* succ_y_z  = Utils::compositeToAtom(m_nSymbolSucc, term_y_z);

    //2 T(_X,_Y)
    _term* term_x_y = Utils::combineTerms(m_vTermsX, m_vTermsY);
    _formula* t_x_y  = Utils::compositeToAtom(m_nSymbolT, term_x_y);

    //3 theta(_X,_Z)
    Formula copyOriginalFml = _originalFml;
    copyOriginalFml.replaceTerms(m_vTermsY, m_vTermsZ);
    _formula* theta_x_z = Utils::copyFormula(copyOriginalFml.getFormula());

    //4 T(_X,_Z)
    _term* term_x_z = Utils::combineTerms(m_vTermsX, m_vTermsZ);
    _formula* t_x_z = Utils::compositeToAtom(m_nSymbolT, term_x_z);

    //create structure
    _formula* left = succ_y_z;
    _formula* rl = Utils::compositeByConnective(DISJ, theta_x_z, t_x_z);
    _formula* right = Utils::compositeByConnective(IMPL, rl, t_x_y);
    _formula* F = Utils::compositeByConnective(IMPL, left, right);
    
    Formula fml = Formula(F, false);
    return fml;
}
Exemplo n.º 7
0
void copyConsTest()
{
	char str[100];
	while (gets(str)) {
		Parser p(str);

		Formula *f = p.parse();
		f->print();
		puts("");
		
		Predicate *pred = f->getPredicate();
		Term *t = pred->getTermList()->getList()->at(0);

		puts("here");
		Term w = *t;
		w.print();
		puts("here");
		w.setName("chan");
		w.print();
		puts("");

		f->print();
		puts("");
	}
}
Exemplo n.º 8
0
void formulaTest()
{
	TermList *list = new TermList();
	list->addTerm(new Term(Term::VAR, "x"));
	list->addTerm(new Term(Term::VAR, NULL));
	list->addTerm(new Term(Term::CONS, "A"));
	Formula *f = new Formula(new Predicate("P", list));
	Formula *g = new Formula(f);
	Formula *q = new Formula(f, g, '&');
	
	Formula *r = new Formula(new Term(Term::VAR, NULL), q, Formula::UNIV);
	r->print();

	Term *x = new Term(Term::VAR, "xy");
	Term *y = new Term(Term::VAR, "x");

	TermList *list2 = new TermList();
	list2->addTerm(x);
	list2->addTerm(y);
	Term *f2 = new Term("f10", list2);
	Term *g2 = new Term("f10", list2);

	f2->print();
	puts("");
	g2->print();
	puts("");
	if (!(*f2 != *g2)) {
		printf("yes! equal!");
	}

}
Exemplo n.º 9
0
bool Prover::isClosed(queue<Formula*> &q, int tabCount)
{
	vector<Formula*> aList;
	vector<Formula*> ntAList;
	int sz = q.size();

	for (int i = 0; i < sz; ++i) {
		Formula *f = q.front();
		q.pop();
		q.push(f);
		if (f->isAtomic()) {
			aList.push_back(f);
		}
		else if (f->isNegated() && f->nextFormula->isAtomic()) {
			ntAList.push_back(f->nextFormula);
		}
	}

	for (int i = 0; i < aList.size(); i++) {
		Formula *f1 = aList.at(i);
		for (int j = 0; j < ntAList.size(); j++) {
			Formula *f2 = ntAList.at(j);
			if (!strcmp(f1->getPredicate()->getName(), f2->getPredicate()->getName())) {
				//if (isUnifiable(f1->getPredicate()->getTermList(), f2->getPredicate()->getTermList())) {
				if (isUnifiable(f1->getPredicate(), f2->getPredicate(), tabCount)) {
					return true;
				}
			}
		}
	}
	return false;
}
Exemplo n.º 10
0
//
// Function: MULTIPLE.OPERATIONS
//
Value func_multiple_operations(valVector args, ValueCalc *, FuncExtra *e)
{
    if (args.count() != 3 && args.count() != 5)
        return Value::errorVALUE(); // invalid number of parameters

    for (int i = 0; i < args.count(); i++) {
        if (e->ranges[i].col1 == -1 || e->ranges[i].row1 == -1)
            return Value::errorVALUE();
    }

    CellStorage *s = e->sheet->cellStorage();

    // get formula to evaluate
    int formulaCol = e->ranges[0].col1;
    int formulaRow = e->ranges[0].row1;
    Formula formula = s->formula(formulaCol, formulaRow);
    if (!formula.isValid())
        return Value::errorVALUE();

    CellIndirection cellIndirections;
    cellIndirections.insert(Cell(e->sheet, e->ranges[1].col1, e->ranges[1].row1), Cell(e->sheet, e->ranges[2].col1, e->ranges[2].row1));
    if (args.count() > 3) {
        cellIndirections.insert(Cell(e->sheet, e->ranges[3].col1, e->ranges[3].row1), Cell(e->sheet, e->ranges[4].col1, e->ranges[4].row1));
    }

    return formula.eval(cellIndirections);
}
Exemplo n.º 11
0
Formula* Formula::getDelta(Term *newFunc)
{
	Formula *f;
	Term *v;
	if (this->type == EXIST) {
		f = this->nextFormula;
		v = this->qVar;
	}
	else {
		f = new Formula(this->nextFormula->getNext());
		v = this->nextFormula->getQVar();
	}

	if (newFunc == NULL) {

		TermList *list = new TermList();
		f->getFreeVarList(list);

		newFunc = new Term(NULL, list);

	}

	f->replace(v, newFunc);
	return f;


}
Exemplo n.º 12
0
 int formulaDepth() const{
     if(_op1->formulaDepth() > _op2->formulaDepth()){
         return _op1->formulaDepth() + 1;
     }
     else{
         return _op2->formulaDepth() + 1;
     }
 }
Exemplo n.º 13
0
void EspressoData::add(EspressoCover &f, Formula::State state)
{
    if (origFormula) {
        Formula *formula = new Formula(*origFormula, f.cover);
        formula->setState(state);
        steps.push_back(formula);
    }
}
Exemplo n.º 14
0
// Function: ISFORMULA
Value func_isformula(valVector /*args*/, ValueCalc */*calc*/, FuncExtra *e)
{
    const Calligra::Sheets::Region &region = e->regions[0];
    QPoint p = region.firstRange().topLeft();
    CellStorage *s = region.firstSheet()->cellStorage();
    Formula formula = s->formula(p.x(), p.y());
    return Value(formula.isValid());
}
Exemplo n.º 15
0
bool AntimonyEvent::SetUseValuesFromTriggerTime(const Formula& form)
{
  if (!form.IsBoolean()) {
    g_registry.SetError("Unable to use '" + form.ToDelimitedStringWithEllipses(".") + "': only 'true' or 'false' may be used to set the value of 'fromTrigger' on an event.");
    return true;
  }
  m_useValuesFromTriggerTime = form.GetBoolean();
  return false;
}
Exemplo n.º 16
0
bool AntimonyEvent::SetInitialValue(const Formula& form)
{
  if (!form.IsBoolean()) {
    g_registry.SetError("Unable to use '" + form.ToDelimitedStringWithEllipses(".") + "': only 'true' or 'false' may be used to set the value of 't0' (the initial value) of an event.");
    return true;
  }
  m_initialValue = form.GetBoolean();
  return false;
}
Exemplo n.º 17
0
bool AntimonyEvent::SetPersistent(const Formula& form)
{
  if (!form.IsBoolean()) {
    g_registry.SetError("Unable to use '" + form.ToDelimitedStringWithEllipses(".") + "': only 'true' or 'false' may be used to set the value of 'persistent' on an event.");
    return true;
  }
  m_persistent = form.GetBoolean();
  return false;
}
Exemplo n.º 18
0
void CreatorDialog::accept()
{
    Formula *f = m_gm->getNewFormula();
    if (f) {
        f->setRepre(m_repre);
        f->setName(m_name);
    }

    QDialog::accept();
}
Exemplo n.º 19
0
bool Formula::operator==(Formula& other) {
	bool sameBB = true;

	sameBB = sameBB && this->getBoundingBox().getTopCoord() == other.getBoundingBox().getTopCoord();
	sameBB = sameBB && this->getBoundingBox().getBottomCoord() == other.getBoundingBox().getBottomCoord();
	sameBB = sameBB && this->getBoundingBox().getRightCoord() == other.getBoundingBox().getRightCoord();
	sameBB = sameBB && this->getBoundingBox().getLeftCoord() == other.getBoundingBox().getLeftCoord();

	return sameBB;
}
Exemplo n.º 20
0
Formula* Formula::getAlpha2()
{
	if (this->isCompound()) {
		return this->formula2;
	}
	else {
		Formula *next = this->nextFormula;
		return new Formula(next->getRight());
	}
}
Exemplo n.º 21
0
void parseTest()
{
	char str[100];
	while (gets(str)) {
		Parser p(str);

		Formula *f = p.parse();
		f->print();
		formulaFeatureTest(f);
	}
}
Exemplo n.º 22
0
bool Formula::isBeta()
{
	if (this->isCompound() && (this->cntv == '|' || this->cntv=='>')) {
		return true;
	}
	else if (this->isNegated()) {
		Formula *next = this->getNext();
		if (next->isCompound() && next->getCntv()=='&') {
			return true;
		}
		else return false;
	}
	else return false;
}
Exemplo n.º 23
0
Formula* Formula::getBeta2()
{
	if (this->isCompound()) {
		return this->formula2;
	}
	else if (this->nextFormula->getCntv() == '&') {
		Formula *next = this->nextFormula;
		return new Formula(next->getRight());
	}
	else {
		Formula *next = this->nextFormula;
		return next->getRight();
	}
}
Exemplo n.º 24
0
Formula* Formula::getAlpha1()
{
	if (this->isCompound()) {
		return this->formula1;
	}
	else if (this->nextFormula->getCntv() == '|') {
		Formula *next = this->nextFormula;
		return new Formula(next->getLeft());
	}
	else {
		Formula *next = this->nextFormula;
		return next->getLeft();
	}
}
Exemplo n.º 25
0
/**
 * 章衡量词消去公式三    t(_X,_MIN) | theta(_X,_MIN)
 * @param originalFml 一阶语句
 * @return 
 */
Formula HengZhang::createFormula_3(const Formula& _originalFml) {
    //create left sub-formula t(_X,_MIN)
    _term* term_x_min = Utils::combineTerms(m_vTermsX, m_vTermsMIN);
    _formula* t_x_min   = Utils::compositeToAtom(m_nSymbolT, term_x_min);
    //create right sub-formula theta(_X,_MIN)
    Formula copyOriginalFml = _originalFml;
    copyOriginalFml.replaceTerms(m_vTermsY, m_vTermsMIN);
    _formula* theta_x_min = Utils::copyFormula(copyOriginalFml.getFormula());

    //create structure
    _formula* F   = Utils::compositeByConnective(DISJ, t_x_min, theta_x_min);
    
    Formula fml = Formula(F, false);
    return fml;
}
Exemplo n.º 26
0
static QString tokenizeFormula(const QString& formula)
{
  Formula f;
  QString expr = formula;
  expr.prepend( '=' );
  f.setExpression( expr );
  Tokens tokens = f.tokens();

  QString resultCodes;
  if( tokens.valid() )
    for( int i = 0; i < tokens.count(); i++ )
      resultCodes.append( encodeTokenType( tokens[i] ) );

  return resultCodes;
}
Exemplo n.º 27
0
/**
 * 把量词保存到对应的vector
 * @param originalFml 一阶语句
 * @return 
 */
Formula HengZhang::recordQuantifier(const Formula& _originalFml) {

    m_vTermsX.clear();
    m_vTermsY.clear();
    m_vTermsZ.clear();
    m_vTermsMIN.clear();
    m_vTermsMAX.clear();
    
    _formula* fml = _originalFml.getFormula();
    while(fml->formula_type == UNIV) {
        m_vTermsX.push_back(fml->variable_id);
        fml = fml->subformula_l;
    }
    while (fml->formula_type == EXIS) {
        m_vTermsY.push_back(fml->variable_id);
        
        const char* sDomainName = Vocabulary::instance().getVariableDomain(fml->variable_id);
        // MIN
        int id = Vocabulary::instance().generateDomainMIN(sDomainName);
        m_vTermsMIN.push_back(id);
        // MAX
        id = Vocabulary::instance().generateDomainMAX(sDomainName);
        m_vTermsMAX.push_back(id);
        // Z
        id = Vocabulary::instance().generateNewVariable(fml->variable_id);
        m_vTermsZ.push_back(id);

        fml = fml->subformula_l;
    }
    Formula ret = Formula(fml, true);
    return ret;
}
Exemplo n.º 28
0
Value TestTextFunctions::evaluate(const QString& formula, Value& ex)
{
    Formula f;
    QString expr = formula;
    if (expr[0] != '=')
        expr.prepend('=');
    f.setExpression(expr);
    Value result = f.eval();

    if (result.isFloat() && ex.isInteger())
        ex = Value(ex.asFloat());
    if (result.isInteger() && ex.isFloat())
        result = Value(result.asFloat());

    return result;
}
Exemplo n.º 29
0
/**
 * 对每条公式进行章衡公式转换
 * @param _originalFmls Formulas 需要进行转换的公式
 * @return 返回Formulas*,需要手动销毁
 */
Formulas* HengZhang::convert(const Formulas& _originalFmls) {
    Formulas tempFmls = _originalFmls;
    Formulas* pFinalFmls = new Formulas();
    while (! tempFmls.isEmpty()) {
        Formula curFml = tempFmls.popFront();
        curFml.convertToPNF();
        if (curFml.isUniversal()) {
            pFinalFmls->pushBack(curFml);
            continue;
        }
        Formulas hzFmls = transform(curFml);
        tempFmls.joinFront(hzFmls);
    }
    
    return pFinalFmls;
}
Exemplo n.º 30
0
Formula* Formula::getGamma(Term *newV)
{
	Formula *f;
	Term *v;
	if (this->type == UNIV) {
		f = this->nextFormula;
		v = this->qVar;
	}
	else {
		f = new Formula(this->nextFormula->getNext());
		v = this->nextFormula->getQVar();
	}

	f->replace(v, newV);
	return f;
}