Пример #1
0
int processInput(Domain* domain, MLN* mln, GroundPredicateHashArray& queries)
{
	  string inMLNFile, wkMLNFile, evidenceFile;
	  StringHashArray queryPredNames;
	  StringHashArray owPredNames;
	  StringHashArray cwPredNames;
	  Array<string> constFilesArr;
	  Array<string> evidenceFilesArr;

	  
	  //the second .mln file to the last one in ainMLNFiles _may_ be used 
	  //to hold constants, so they are held in constFilesArr. They will be
	  //included into the first .mln file.

		//extract .mln, .db file names
	  extractFileNames(ainMLNFiles, constFilesArr);
	  assert(constFilesArr.size() >= 1);
	  inMLNFile.append(constFilesArr[0]);
	  constFilesArr.removeItem(0);
	  extractFileNames(aevidenceFiles, evidenceFilesArr);
	  
	  if (aqueryPredsStr) queryPredsStr.append(aqueryPredsStr);
	  if (aqueryFile) queryFile.append(aqueryFile);

	  if (queryPredsStr.length() == 0 && queryFile.length() == 0 &&
			  (aisLiftedGibbs || aisLiftedGibbsWithCluster))
	  { cout << "No query predicates specified" << endl; return -1; }


		//extract names of all query predicates
	  if (queryPredsStr.length() > 0 || queryFile.length() > 0)
	  {
		if (!extractPredNames(queryPredsStr, &queryFile, queryPredNames)) return -1;
	  }

		//extract names of open-world evidence predicates
	  if (aOpenWorldPredsStr)
	  {
		if (!extractPredNames(string(aOpenWorldPredsStr), NULL, owPredNames)) 
		  return -1;
		assert(owPredNames.size() > 0);
	  }

		//extract names of closed-world non-evidence predicates
	  if (aClosedWorldPredsStr)
	  {
		if (!extractPredNames(string(aClosedWorldPredsStr), NULL, cwPredNames)) 
		  return -1;
		assert(cwPredNames.size() > 0);
		if (!checkQueryPredsNotInClosedWorldPreds(queryPredNames, cwPredNames))
		  return -1;
	  }

	  //////////////////// read in clauses & evidence predicates //////////////////

	  cout << "Reading formulas and evidence predicates..." << endl;

		// Copy inMLNFile to workingMLNFile & app '#include "evid.db"'
	  string::size_type bslash = inMLNFile.rfind("/");
	  string tmp = (bslash == string::npos) ? 
				   inMLNFile:inMLNFile.substr(bslash+1,inMLNFile.length()-bslash-1);
	  char buf[100];
	  sprintf(buf, "%s%d%s", tmp.c_str(), getpid(), ZZ_TMP_FILE_POSTFIX);
	  wkMLNFile = buf;
	  copyFileAndAppendDbFile(inMLNFile, wkMLNFile,
							  evidenceFilesArr, constFilesArr, "", false);

		// Parse wkMLNFile, and create the domain, MLN, database
	  bool addUnitClauses = false;
	  bool mustHaveWtOrFullStop = true;
	  bool warnAboutDupGndPreds = true;
	  bool flipWtsOfFlippedClause = true;
	  bool allPredsExceptQueriesAreCW = false;
	  //bool allPredsExceptQueriesAreCW = owPredNames.empty();
	  Domain* forCheckingPlusTypes = NULL;

		// Parse as if lazy inference is set to true to set evidence atoms in DB
		// If lazy is not used, this is removed from DB
	  cout<<wkMLNFile.c_str()<<endl;
	  if (!runYYParser(mln, domain, wkMLNFile.c_str(), allPredsExceptQueriesAreCW, 
					   &owPredNames, &cwPredNames, &queryPredNames, addUnitClauses, 
					   warnAboutDupGndPreds, 0, mustHaveWtOrFullStop, 
					   forCheckingPlusTypes, true, flipWtsOfFlippedClause))
	  {
		unlink(wkMLNFile.c_str());
		//return -1;
		exit(-1);
	  }

	  unlink(wkMLNFile.c_str());
	  const FormulaAndClausesArray* fca = mln->getFormulaAndClausesArray();
	  for (int i = 0; i < fca->size(); i++)
	  {
		IndexClauseHashArray* indexClauses = (*fca)[i]->indexClauses;
		for (int j = 0; j < indexClauses->size(); j++)
		{
		  int idx = (*indexClauses)[j]->index;
		  Clause* c = (*indexClauses)[j]->clause;
		  cout << "idx " << idx << ": ";
		  c->printWithWtAndStrVar(cout, domain);
		  cout << endl;
		}
	  }
		//////////////////////////// run inference /////////////////////////////////

		///////////////////////// read & create query predicates ///////////////////
	  Array<int> allPredGndingsAreQueries;
	  Array<Array<Predicate* >* >* queryFormulas =  new Array<Array<Predicate*> *>;
		if (queryFile.length() > 0)
		{
		  cout << "Reading query predicates that are specified in query file..."
			   << endl;
		  bool ok = createQueryFilePreds(queryFile, domain, domain->getDB(),
										 &queries, &knownQueries, queryFormulas);
		  if (!ok) { cout<<"Failed to create query predicates."<<endl; exit(-1); }
		}

		allPredGndingsAreQueries.growToSize(domain->getNumPredicates(), false);
		if (queryPredsStr.length() > 0)
		{
		  // unePreds = unknown non-evidence predicates
		  // nePreds  = known non-evidence predicates
		  GroundPredicateHashArray unePreds;
		  GroundPredicateHashArray knePreds;
		  bool ok = createComLineQueryPreds(queryPredsStr, domain, 
									  domain->getDB(), &unePreds, &knePreds,
									  &allPredGndingsAreQueries, queryFormulas);
		  if (!ok) { cout<<"Failed to create query predicates."<<endl; exit(-1); }
		  //evidence groundings
		  queries = unePreds;
		}
		return 0;
}
Пример #2
0
void printResults(const string& queryFile, const string& queryPredsStr,
                  Domain *domain, ostream& out,
                  GroundPredicateHashArray* const &queries,
                  Inference* const &inference, VariableState* const &state)
{
    // Lazy version: Have to generate the queries from the file or query string.
    // This involves calling createQueryFilePreds / createComLineQueryPreds
    if (aLazy)
    {
        const GroundPredicateHashArray* gndPredHashArray = NULL;
        Array<double>* gndPredProbs = NULL;
        // Inference algorithms with probs: have to retrieve this info from state.
        // These are the ground preds which have been brought into memory. All
        // others have always been false throughout sampling.
        if (!(amapPos || amapAll))
        {
            gndPredHashArray = state->getGndPredHashArrayPtr();
            gndPredProbs = new Array<double>;
            gndPredProbs->growToSize(gndPredHashArray->size());
            for (int i = 0; i < gndPredProbs->size(); i++)
                (*gndPredProbs)[i] =
                    inference->getProbability((*gndPredHashArray)[i]);
        }

        if (queryFile.length() > 0)
        {
            cout << "Writing query predicates that are specified in query file..."
                 << endl;
            bool ok = createQueryFilePreds(queryFile, domain, domain->getDB(), NULL,
                                           NULL, true, out, amapPos,
                                           gndPredHashArray, gndPredProbs, NULL);
            if (!ok) {
                cout <<"Failed to create query predicates."<< endl;
                exit(-1);
            }
        }

        Array<int> allPredGndingsAreQueries;
        allPredGndingsAreQueries.growToSize(domain->getNumPredicates(), false);
        if (queryPredsStr.length() > 0)
        {
            cout << "Writing query predicates that are specified on command line..."
                 << endl;
            bool ok = createComLineQueryPreds(queryPredsStr, domain, domain->getDB(),
                                              NULL, NULL, &allPredGndingsAreQueries,
                                              true, out, amapPos, gndPredHashArray,
                                              gndPredProbs, NULL);
            if (!ok) {
                cout <<"Failed to create query predicates."<< endl;
                exit(-1);
            }
        }

        if (!(amapPos || amapAll))
            delete gndPredProbs;
    }
    // Eager version: Queries have already been generated and we can get the
    // information directly from the state
    else
    {
        if (amapPos)
            inference->printTruePreds(out);
        else
        {
            inference->printQFProbs(out, domain);
            if (abpInfer || aefbpInfer)
            {
                inference->printProbabilities(out);
            }
            else
            {
                for (int i = 0; i < queries->size(); i++)
                {
                    // Prob is smoothed in inference->getProbability
                    double prob = inference->getProbability((*queries)[i]);
                    (*queries)[i]->print(out, domain);
                    out << " " << prob << endl;
                }
            }
        }
    }
}