superclause.cpp

00001 /*
00002  * All of the documentation and software included in the
00003  * Alchemy Software is copyrighted by Stanley Kok, Parag
00004  * Singla, Matthew Richardson, Pedro Domingos, Marc
00005  * Sumner, Hoifung Poon, and Daniel Lowd.
00006  * 
00007  * Copyright [2004-08] Stanley Kok, Parag Singla, Matthew
00008  * Richardson, Pedro Domingos, Marc Sumner, Hoifung
00009  * Poon, and Daniel Lowd. All rights reserved.
00010  * 
00011  * Contact: Pedro Domingos, University of Washington
00012  * (pedrod@cs.washington.edu).
00013  * 
00014  * Redistribution and use in source and binary forms, with
00015  * or without modification, are permitted provided that
00016  * the following conditions are met:
00017  * 
00018  * 1. Redistributions of source code must retain the above
00019  * copyright notice, this list of conditions and the
00020  * following disclaimer.
00021  * 
00022  * 2. Redistributions in binary form must reproduce the
00023  * above copyright notice, this list of conditions and the
00024  * following disclaimer in the documentation and/or other
00025  * materials provided with the distribution.
00026  * 
00027  * 3. All advertising materials mentioning features or use
00028  * of this software must display the following
00029  * acknowledgment: "This product includes software
00030  * developed by Stanley Kok, Parag Singla, Matthew
00031  * Richardson, Pedro Domingos, Marc Sumner, Hoifung
00032  * Poon, and Daniel Lowd in the Department of Computer Science and
00033  * Engineering at the University of Washington".
00034  * 
00035  * 4. Your publications acknowledge the use or
00036  * contribution made by the Software to your research
00037  * using the following citation(s): 
00038  * Stanley Kok, Parag Singla, Matthew Richardson and
00039  * Pedro Domingos (2005). "The Alchemy System for
00040  * Statistical Relational AI", Technical Report,
00041  * Department of Computer Science and Engineering,
00042  * University of Washington, Seattle, WA.
00043  * http://www.cs.washington.edu/ai/alchemy.
00044  * 
00045  * 5. Neither the name of the University of Washington nor
00046  * the names of its contributors may be used to endorse or
00047  * promote products derived from this software without
00048  * specific prior written permission.
00049  * 
00050  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF WASHINGTON
00051  * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
00052  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00053  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00054  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY
00055  * OF WASHINGTON OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
00056  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00057  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00058  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00059  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00060  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00061  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00062  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
00063  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00064  * 
00065  */
00066 #include "util.h"
00067 #include "mrf.h"
00068 #include "superclause.h"
00069 #include "superpred.h"
00070 
00071 int SuperClause::superClauseIndex__ = 0;
00072 
00073   //create the super clauses given the current set of super preds
00074 void createSuperClauses(Array<Array<SuperClause*>*>* const & superClausesArr,
00075                         Domain * const & domain)
00076 {
00077     //reset the index of superClauses (i.e. we will assign new ids starting
00078     //from 0)
00079   SuperClause::resetIndex();
00080 
00081   IntArrayToSuperClause * superPredIdsToSuperClause;
00082   IntArrayToSuperClause::iterator iaToScItr;
00083   Array<int> *pconstants;
00084   Array<int> *superPredIds;
00085   Clause* clause;
00086   SuperClause *superClause, *newSuperClause;
00087   Predicate *pred;
00088   Array<SuperClause *> *superClauses;
00089   Array<SuperClause *> *newSuperClauses;
00090   int superClauseCnt;
00091 
00092     //iterate over all the super clauses
00093   for (int arrIndex = 0; arrIndex < superClausesArr->size(); arrIndex++)
00094   {
00095     superClauses = (*superClausesArr)[arrIndex];
00096     superClauseCnt = superClauses->size();
00097     newSuperClauses = new Array<SuperClause*>();
00098     superPredIdsToSuperClause = new IntArrayToSuperClause();
00099 
00100       //iterate over all the super clauses
00101     for (int scindex = 0; scindex < superClauseCnt; scindex++)
00102     {
00103       superPredIdsToSuperClause->clear();
00104       superClause = (*superClauses)[scindex];
00105       clause = superClause->getClause();
00106       int predCnt = clause->getNumPredicates();
00107 
00108         //for each tuple, first extract the predicate tuple and
00109         //then corresponding superpred id
00110       int tupleCnt = superClause->getNumTuples();
00111       for (int tindex = 0; tindex < tupleCnt; tindex++)
00112       {
00113         superPredIds = new Array<int>();
00114         for (int pindex = 0; pindex < predCnt; pindex++)
00115         {
00116           pred = clause->getPredicate(pindex);
00117           pconstants = superClause->getPredicateConstants(tindex, pred);
00118           int predId = domain->getPredicateId(pred->getName());
00119           superPredIds->append(SuperPred::getSuperPredId(pconstants, predId));
00120           delete pconstants;
00121         }
00122         iaToScItr = superPredIdsToSuperClause->find(superPredIds);
00123         if (iaToScItr == superPredIdsToSuperClause->end())
00124         {
00125           newSuperClause = superClause->createSuperClauseFromTemplate();
00126           (*superPredIdsToSuperClause)[superPredIds] = newSuperClause;
00127           newSuperClauses->append(newSuperClause);
00128         }
00129         else
00130         {
00131           delete superPredIds;
00132           newSuperClause = iaToScItr->second;
00133         }
00134         double tcnt = superClause->getTupleCount(tindex);
00135         Array<int> * constants = superClause->getConstantTuple(tindex);
00136         newSuperClause->addConstantTuple(constants);
00137         newSuperClause->incrementTupleCount(constants, tcnt);
00138       }
00139 
00140         //trick to dereference/delete the keys of the map
00141         //- first extract all the keys, then clear the map,
00142         //then delete the keys
00143       Array<Array<int>*> keysArr;
00144       keysArr.clear();
00145       for (iaToScItr = superPredIdsToSuperClause->begin();
00146            iaToScItr != superPredIdsToSuperClause->end();
00147            iaToScItr++)
00148       {
00149         superPredIds = iaToScItr->first;
00150         keysArr.append(superPredIds);
00151       }
00152       superPredIdsToSuperClause->clear();
00153       for (int i = 0; i < keysArr.size(); i++)
00154       {
00155         delete keysArr[i];
00156       }
00157     }
00158 
00159       //clean up
00160     delete superPredIdsToSuperClause;
00161     for (int i = 0; i < superClauses->size(); i++)
00162     {
00163       delete (*superClauses)[i];
00164     }
00165     superClauses->clear();
00166 
00167       //store the newly create superclauses into the array of superclauses
00168     for (int i = 0; i < newSuperClauses->size(); i++)
00169     {
00170       superClauses->append((*newSuperClauses)[i]);
00171     }
00172     delete newSuperClauses;
00173   }
00174 }
00175 
00176 

Generated on Sun Jun 7 11:55:13 2009 for Alchemy by  doxygen 1.5.1