EMMA Coverage Report (generated Sat Aug 20 11:00:51 CDT 2011)
[all classes][felix.operator]

COVERAGE SUMMARY FOR SOURCE FILE [LROperator.java]

nameclass, %method, %block, %line, %
LROperator.java100% (1/1)70%  (7/10)84%  (1133/1354)81%  (193.6/239)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class LROperator100% (1/1)70%  (7/10)84%  (1133/1354)81%  (193.6/239)
explain (): String 0%   (0/1)0%   (0/2)0%   (0/1)
getAndAddToDomain (Object, HashMap): Integer 0%   (0/1)0%   (0/26)0%   (0/5)
learn (): void 0%   (0/1)0%   (0/1)0%   (0/1)
run (): void 100% (1/1)74%  (464/630)72%  (72.6/101)
inferAndDumpToTmpFile (String, double [], ArrayList, BufferedWriter): void 100% (1/1)92%  (210/229)85%  (39/46)
logAdd (double, double): double 100% (1/1)94%  (32/34)90%  (9/10)
prepareDMO (HashSet): void 100% (1/1)98%  (306/311)96%  (44/46)
LROperator (FelixQuery, HashSet, FelixCommandOptions): void 100% (1/1)100% (51/51)100% (15/15)
array2str (String []): String 100% (1/1)100% (31/31)100% (4/4)
prepare (): void 100% (1/1)100% (39/39)100% (10/10)

1package felix.operator;
2 
3import java.io.BufferedReader;
4import java.io.BufferedWriter;
5import java.io.FileInputStream;
6import java.io.FileWriter;
7import java.io.InputStream;
8import java.io.InputStreamReader;
9import java.sql.Array;
10import java.util.ArrayList;
11import java.util.Arrays;
12import java.util.Date;
13import java.util.HashMap;
14import java.util.HashSet;
15 
16import org.apache.commons.collections.Bag;
17import org.apache.commons.collections.bag.HashBag;
18import org.postgresql.PGConnection;
19 
20 
21import tuffy.db.RDB;
22import tuffy.mln.Literal;
23import tuffy.ra.ConjunctiveQuery;
24import tuffy.util.Config;
25import tuffy.util.ExceptionMan;
26import tuffy.util.FileMan;
27import tuffy.util.StringMan;
28import tuffy.util.Timer;
29import tuffy.util.UIMan;
30import felix.dstruct.DataMovementOperator;
31import felix.dstruct.FelixPredicate;
32import felix.dstruct.FelixQuery;
33import felix.dstruct.StatOperator;
34import felix.dstruct.FelixPredicate.FPProperty;
35import felix.parser.FelixCommandOptions;
36import felix.util.FelixConfig;
37import felix.util.FelixStringMan;
38import felix.util.FelixUIMan;
39 
40/**
41 * A LR operator in Felix.
42 * @author Ce Zhang
43 *
44 */
45public class LROperator extends StatOperator{
46        
47        /**
48         * Target predicate of this LR operator.
49         */
50        FelixPredicate lrHead;
51        
52 
53        /**
54         * Mapping from label's constant ID to a new label ID. This new label ID is from 0, which 
55         * is used in the inference of LR (s.t., we can use array to represent
56         * labels).
57         */
58        HashMap<String, Integer> label2ID = new HashMap<String, Integer>();
59        
60        /**
61         * The inverse map of {@link LROperator#label2ID}.
62         */
63        HashMap<Integer, String[]> id2Label = new HashMap<Integer, String[]>();
64        
65        /**
66         * All DataMovementOperators used as LR rules.
67         */
68        ArrayList<DataMovementOperator> lrDMOs = new ArrayList<DataMovementOperator>();
69        
70        /**
71         * The DataMovementOperator which is the union of all LR rules.
72         */
73        DataMovementOperator lrDMO = null;
74        
75        /**
76         * The DataMovementOperator representing the table/view for all unigram features.
77         */
78        DataMovementOperator unigramDMO = null;
79        
80        /**
81         * The DataMovementOperator representing the table/view for all possible labels.
82         */
83        DataMovementOperator labelDomainDMO = null;
84        
85        /**
86         * The DataMovementOperator fetching all unigram features.
87         */
88        DataMovementOperator getAllUnigramFeaturesDMO = null;
89        
90        /**
91         * @deprecated
92         */
93        DataMovementOperator getAllLearningFeatureDMO = null;
94        
95        /**
96         * @deprecated
97         */
98        DataMovementOperator getGroundTruthDMO = null;
99        
100        /**
101         * @deprecated
102         */
103        DataMovementOperator getFeatureTableDMO = null;
104        
105        /**
106         * The constructor of LROperator.
107         * @param _fq Felix query.
108         * @param _goalPredicates target predicates of this coref operator.
109         * @param _opt Command line options of this Felix run.
110         */
111        public LROperator(FelixQuery _fq, HashSet<FelixPredicate> _goalPredicates,
112                        FelixCommandOptions _opt) {
113                super(_fq, _goalPredicates, _opt);
114                this.type = OPType.LR;
115                this.precedence = 4;
116                
117        }
118        
119        /**
120         * Adds object to domain if necessary.
121         * @param obj
122         * @param map
123         * @return
124         */
125        public <E> Integer getAndAddToDomain(E obj, HashMap<String, Integer> map){
126                int rs;
127                if(map.containsKey(obj.toString())){
128                        return map.get(obj.toString());
129                }
130                rs = map.size() + 1;
131                map.put(obj.toString(), rs);
132                return rs;
133        }
134        
135        /**
136         * @deprecated
137         */
138        @Override
139        public void learn() {
140                
141        }
142        
143        
144        boolean prepared = false;
145        
146        /**
147         * Prepares operator for execution.
148         */
149        @Override
150        public void prepare() {
151                
152                lrDMOs.clear();
153                allDMOs.clear();
154                
155                //if(!prepared){
156                        
157                        db = RDB.getRDBbyConfig(Config.db_schema);
158                
159                        lrHead = this.getTargetPredicateIfHasOnlyOne();
160                        
161                        HashSet<ConjunctiveQuery> lrQueries;
162                        
163                        if(options.isDLearningMode){
164                                //lrQueries = 
165                                //        this.translateFelixClasesIntoLearningQueriesForVictor(lrHead, FPProperty.NON_RECUR);
166                                //this.prepareDMO4ForLearning(lrQueries);
167                                
168                        }else{
169                                lrQueries = 
170                                        this.translateFelixClasesIntoFactorGraphEdgeQueries(lrHead, false, this.inputPredicateScope, FPProperty.NON_RECUR);
171                                this.prepareDMO(lrQueries);
172                        }
173                
174                        prepared = true;
175                        
176                        //db.close();
177                        
178                //}
179                
180        }
181        
182        
183        /**
184         * Returns string representation of string array. 
185         * @param _array
186         * @return
187         */
188        String array2str(String[] _array){
189                String ret = "";
190                for(String s : _array){
191                        ret = ret + ":" + s;
192                }
193                return ret;
194        }
195        
196        /**
197         * Executes operator.
198         */
199        @Override
200        public void run() {
201                
202                if(options.isDLearningMode){
203                        this.learn();
204                }else{
205                
206                        this.isMarginal = belongsToBucket.isMarginal();
207                        
208                        UIMan.println(">>> Start Running " + this);
209                        
210                        ArrayList<Integer> nonZeroWeights = new ArrayList<Integer>();
211        
212                        try{
213                                
214                                Timer.start("LR-Operator-" + lrHead.getName());
215                                
216                                BufferedWriter bw = new BufferedWriter(
217                                                new FileWriter(Config.getLoadingDir() + "/_loading_lr_" + lrHead.getName() + this.getId()));
218                                                
219                                // get the domain of Label, and assign them an integer ID (start from 0, s.t., we
220                                // can use array to store them
221                                int labelID = 0;
222                                int nLabelFileds = lrHead.getLabelFieldsArgs().size();
223                                if(this.labelDomainDMO != null){
224                                        this.labelDomainDMO.execute(null, new ArrayList<Integer>());
225                                        while(this.labelDomainDMO.next()){
226                                                String[] currLabel = new String[nLabelFileds];
227                                                
228                                                for(int i=0;i<nLabelFileds;i++){
229                                                        currLabel[i] = this.labelDomainDMO.getNext(i+1).toString();
230                                                }
231                                                
232                                                this.id2Label.put(labelID, currLabel);
233                                                this.label2ID.put(array2str(currLabel), labelID);
234                                                labelID++;
235                                        }
236                                }
237                                
238                                
239                                if(this.id2Label.size() == 0 && this.isBinaryArbLR == false){
240                                        return;
241                                }
242                                
243                                if(this.unigramDMO == null){
244                                        UIMan.warn("There are no LR-rules for predicate " + lrHead.getName());
245                                        return;
246                                }
247                                
248                                int ct = 0;
249                                
250                                String workingSignature = null;
251                                this.getAllUnigramFeaturesDMO.db.disableAutoCommitForNow();
252                                this.getAllUnigramFeaturesDMO.execute(null, new ArrayList<Integer>());
253                                double[] weights;
254                                
255                                if(this.isBinaryArbLR == false){
256                                        weights = new double[this.label2ID.keySet().size()];
257                                }else{
258                                        weights = new double[1];
259                                }
260                                        
261                                String[] currLabel = new String[lrHead.getLabelFieldsArgs().size()];
262                                
263                                while(this.getAllUnigramFeaturesDMO.next()){
264                                                        
265                                        String currSignature = "";
266                                        int lct = 0;
267                                        
268                                        ArrayList<String> toSig = new ArrayList<String>();
269                                        if(this.isBinaryArbLR == false){
270                                                for(int i=0;i<lrHead.arity();i++){
271                                                        if(lrHead.getLabelPositions().contains(i)){
272                                                                currLabel[lct++] = this.getAllUnigramFeaturesDMO.getNext(i+1).toString();
273                                                                toSig.add("%s");
274                                                        }else{
275                                                                toSig.add(this.getAllUnigramFeaturesDMO.getNext(i+1).toString());
276                                                                
277                                                                //if(this.getAllUnigramFeaturesDMO.getNext(i+1).toString().equals("264014")){
278                                                                //        System.out.println();
279                                                                //}
280                                                        }
281                                                }
282                                        }else{
283                                                for(int i=0;i<lrHead.arity();i++){
284                                                        toSig.add(this.getAllUnigramFeaturesDMO.getNext(i+1).toString());
285                                                }
286                                        }
287                                        
288                                        currSignature = StringMan.commaList(toSig);
289                                        Double weight = this.getAllUnigramFeaturesDMO.getNextDouble(lrHead.arity()+1);
290                                        
291                                        if(! currSignature.equals(workingSignature)){
292                                                //dump and init. 
293                                                if(workingSignature != null){
294                                                        if(ct % 10000 == 0){
295                                                                UIMan.verbose(3, "" + (ct++));
296                                                        }
297                                                        this.inferAndDumpToTmpFile(workingSignature, weights, nonZeroWeights, bw);
298                                                        for(int i : nonZeroWeights){
299                                                                weights[i] = 0;
300                                                        }
301                                                        nonZeroWeights.clear();
302                                                }
303                                                workingSignature = currSignature;
304                                        }
305                                        
306                                        if(this.isBinaryArbLR == false){
307                                                /*
308                                                System.out.println("!  " + array2str(currLabel) + "");
309                                                System.out.println("@  " + this.label2ID.get(array2str(currLabel)) + "");
310                                                System.out.println("#  " + weight + "\n\n");
311                                                */
312                                                currLabel = currLabel;
313                                                array2str(currLabel);
314                                                int wlen = this.label2ID.get(array2str(currLabel));
315                                                //System.out.print(wlen + " | ");
316                                                //System.out.print(weights.length + " | ");
317                                                //System.out.println(weight);
318                                                weights[this.label2ID.get(array2str(currLabel))] += weight;
319                                                nonZeroWeights.add(this.label2ID.get(array2str(currLabel)));
320                                        }else{
321                                                weights[0] += weight;
322                                        }
323                                }
324                                if(workingSignature != null){
325                                        this.inferAndDumpToTmpFile(workingSignature, weights, nonZeroWeights, bw);
326                                }
327                                
328                                
329                                this.getAllUnigramFeaturesDMO.db.setAutoCommit(true);
330                                bw.close();
331        
332                                //System.out.println("~\t" + Timer.elapsed("LR-Operator-" + lrHead.getName()));
333                                
334                                //UIMan.print("Start COPY...");
335                                FileInputStream in = new FileInputStream(Config.getLoadingDir() + "/_loading_lr_" + lrHead.getName() + this.getId());
336                                PGConnection con = (PGConnection)db.getConnection();
337        
338                                String sql;
339                                
340                                if(options.useDualDecomposition){
341                                        for(FelixPredicate fp : this.dd_CommonOutput){
342                                                if(!fp.getName().equals(this.lrHead.getName())){
343                                                        ExceptionMan.die("LR 583: There must be something wrong with the parser!");
344                                                        continue;
345                                                }
346                                                
347                                                in = new FileInputStream(Config.getLoadingDir() + 
348                                                                "/_loading_lr_" + lrHead.getName() + this.getId());
349                                                String tableName = this.dd_commonOutputPredicate_2_tableName.get(fp);
350                                                
351                                                sql = "DELETE from " + tableName;
352                                                db.execute(sql);
353                                                
354                                                sql = "COPY " + tableName + "(truth, prior, club, " + StringMan.commaList(lrHead.getArgs())  + " ) FROM STDIN CSV";
355                                                con.getCopyAPI().copyIn(sql, in);
356                                                in.close();
357                                                
358                                        }
359                                        
360                                        if(FelixConfig.isFirstRunOfDD){
361                                                in = new FileInputStream(Config.getLoadingDir() + 
362                                                                "/_loading_lr_" + lrHead.getName() + this.getId());
363                                                
364                                                sql = "COPY " + lrHead.getRelName() + "(truth, prior, club, " + StringMan.commaList(lrHead.getArgs())  + " ) FROM STDIN CSV";
365                                                con.getCopyAPI().copyIn(sql, in);
366                                                in.close();
367                                        }
368                                        lrHead.isCurrentlyView = false;
369                                        
370                                }else{
371                                        sql = "COPY " + lrHead.getRelName() + "(truth, prior, club, " + StringMan.commaList(lrHead.getArgs())  + " ) FROM STDIN CSV";
372                                        con.getCopyAPI().copyIn(sql, in);
373                                        in.close();
374                                        lrHead.isCurrentlyView = false;
375                                }
376        
377                                FelixUIMan.println(0,0,"\n>>> {" + this + "} uses " + Timer.elapsed("LR-Operator-" + lrHead.getName()));
378                                
379                        //        for(FelixPredicate fp : this.outputPredicates){
380                        //                fp.setClosedWorld(true);
381                        //        }
382                                
383                                lrHead.setHasSoftEvidence(true);
384                        
385                        
386                                this.db.close();
387                                
388                                if(!options.useDualDecomposition){
389                                        this.belongsToBucket.runNextOperatorInBucket();
390                                }
391                                
392                                
393                                return;
394                                
395                        }catch(Exception e){
396                                e.printStackTrace();
397                        }
398                }
399                
400        }
401 
402        @Override
403        public String explain() {
404 
405                return null;
406        }
407 
408        /**
409         * Generate Data Movement Operator used by this LR Operator.
410         * @param rules rules defining this operator.
411         */
412        public void prepareDMO(HashSet<ConjunctiveQuery> lrQueries){
413                
414                try {
415                        
416                        // DMO for LR rules
417                        for(ConjunctiveQuery cq : lrQueries){
418                                
419                                DataMovementOperator dmo = new DataMovementOperator(db, this);
420                                dmo.logicQueryPlan.addQuery(cq, cq.head.getPred().getArgs(),
421                                                new ArrayList<String>(Arrays.asList("weight", "prov", "deepprov")) );
422                                
423                                dmo.predictedBB = 0;
424                                dmo.PredictedFF = 1;
425                                dmo.PredictedBF = 0;
426                                
427                                dmo.allowOptimization = false;
428                                
429                                allDMOs.add(dmo);
430                                lrDMOs.add(dmo);
431                        }
432                        
433                        //the DMO for the union of all LR DMOs
434                        if(this.lrDMOs.size() > 0){
435                                this.lrDMO = DataMovementOperator.UnionAll(db, this, 
436                                                this.lrDMOs, StringMan.zeros(this.lrHead.arity() + 3), new ArrayList<Integer>());
437                                allDMOs.add(lrDMO);
438                                
439                //                if(!db.isTableExists(FelixConfig.db_schema, "_prov_metainfo")){
440                //                        db.execute("CREATE TABLE _prov_metainfo (predicate TEXT, property TEXT, value TEXT)");
441                //                }
442                        //        
443                        //        db.execute("INSERT INTO _prov_metainfo (predicate, property, value) VALUES ('" + lrHead.getName() 
444                        //                        + "', 'optype', '" + "lr" + "')");
445                        //        db.execute("INSERT INTO _prov_metainfo (predicate, property, value) VALUES ('" + lrHead.getName() 
446                        //                        + "', 'provtable', '" + lrDMO.getAllFreeViewName()+ "')");
447                        //        db.execute("INSERT INTO _prov_metainfo (predicate, property, value) VALUES ('" + lrHead.getName() 
448                        //                        + "', 'labelfield', '" + StringMan.commaList(lrHead.getLabelFieldsArgs())+ "')");
449                                
450                                                                
451                                //the DMO for all unigram features.
452                                this.unigramDMO = new DataMovementOperator(db, this);
453                                this.unigramDMO.allowOptimization = false;
454                                this.unigramDMO.asView = false;
455                                this.unigramDMO.logicQueryPlan.addQuery(db.getPrepareStatement(
456                                                "SELECT " + StringMan.commaList(lrDMO.selListFromRule) + ", sum(weight) AS sumweight " +
457                                                                " FROM " + lrDMO.getAllFreeViewName() + " GROUP BY " + StringMan.commaList(lrDMO.selListFromRule) + 
458                                                                (lrHead.getKeyFieldsArgs().size() == 0 ? "" :
459                                                                                " ORDER BY " + StringMan.commaList(lrHead.getKeyFieldsArgs())) ),
460                                                                lrDMO.selListFromRule, new ArrayList<String>(Arrays.asList("sumweight")));
461                                allDMOs.add(this.unigramDMO);
462                        }                
463                        
464                        //the DMO for the label domain
465                        if(this.isBinaryArbLR == false){
466                                this.labelDomainDMO = new DataMovementOperator(db, this);
467                                this.labelDomainDMO.allowOptimization = false;
468                                ArrayList<String> fields = lrHead.getLabelFieldsTypeTable();
469                                ArrayList<String> fieldsViewName = new ArrayList<String>();
470                                for(int i=0;i<fields.size();i++){
471                                        fieldsViewName.add("t" + i + "." + "constantid AS l" + i);
472                                        fields.set(i, fields.get(i) + " t" + i);
473                                }
474                                this.labelDomainDMO.logicQueryPlan.addQuery(db.getPrepareStatement(
475                                                "SELECT " + StringMan.commaList(fieldsViewName) +" FROM " + StringMan.commaList(fields)),
476                                                                lrHead.getLabelFieldsArgs(), 
477                                                                new ArrayList<String>());
478                                allDMOs.add(this.labelDomainDMO);
479                        }
480                                
481                        if(this.unigramDMO != null){
482                                this.getAllUnigramFeaturesDMO = DataMovementOperator.Select(db, this, 
483                                                this.unigramDMO, new ArrayList<String>());
484                                this.getAllUnigramFeaturesDMO.isIntermediaDMO = true;
485                                this.getAllUnigramFeaturesDMO.hasKnownFetchingOrder = true;
486                                allDMOs.add(this.getAllUnigramFeaturesDMO);
487                        }
488                
489                } catch (Exception e) {
490                        e.printStackTrace();
491                }
492        }
493        
494        /**
495         * Returns sum of given log numbers.
496         * @param logX
497         * @param logY
498         * @return
499         */
500        //ACKNOWLEDGE: FROM https://facwiki.cs.byu.edu/nlp/index.php/Log_Domain_Computations
501        //COPYRIGHT OF THIS FUNCTION BELONGS TO ITS ORIGINAL AUTHOR
502        public static double logAdd(double logX, double logY) {
503 
504               if (logY > logX) {
505                   double temp = logX;
506                   logX = logY;
507                   logY = temp;
508               }
509 
510               if (logX == Double.NEGATIVE_INFINITY) {
511                   return logX;
512               }
513               
514               double negDiff = logY - logX;
515               if (negDiff < -200) {
516                   return logX;
517               }
518               
519               return logX + java.lang.Math.log(1.0 + java.lang.Math.exp(negDiff)); 
520         }
521        
522        /**
523         * Infer and dump answers to the given buffered writer (with a format that can be COPY
524         * into postgres table directly).
525         * @param signature signature of current predicate instance.
526         * @param weights array of weights. Each entry corresponding to the weight of a label.
527         * @param bw
528         */
529        public void inferAndDumpToTmpFile(String signature, double[] weights, ArrayList<Integer> noneZeroWeights, BufferedWriter bw){
530                
531                try{
532 
533                        double sum = Double.NEGATIVE_INFINITY;
534                        double tmp = Double.NEGATIVE_INFINITY;
535                        double max = Double.NEGATIVE_INFINITY;
536                        int nmax = -1;
537                        
538                        for(int i : noneZeroWeights){                
539//                        for(int i=0;i<weights.length;i++){        
540                                tmp = weights[i];
541                                if(tmp > max){
542                                        max = tmp;
543                                        nmax = i;
544                                }
545                                sum = logAdd(tmp, sum);
546                        }
547                        
548                        sum = logAdd(sum, Math.log(this.label2ID.size() - noneZeroWeights.size()));
549                        
550                        if(this.isBinaryArbLR == true){
551                                sum = logAdd(0,sum);
552                        }
553                        
554                        if(this.isMarginal){
555                                
556                                if(nmax == -1){
557                                        return;
558                                }
559                                
560                                for(int i : noneZeroWeights){        
561//                                for(int i=0;i<weights.length;i++){
562                                
563                                        tmp = weights[i];
564                                        
565                                        double prob = Math.exp(tmp - sum);
566                                        
567                                        if(prob >= Config.soft_evidence_activation_threshold){
568                                        
569                                                ArrayList<String> parts = new ArrayList<String>();
570                                                //parts.add(Integer.toString(lrHead.nextTupleIDAndUpdate()));
571                                                parts.add("TRUE");
572                                                parts.add(Double.toString(prob));
573                                                
574                                                if(options.useDualDecomposition){
575                                                        parts.add(Integer.toString(2));
576                                                }else{
577                                                        parts.add(Integer.toString(2));
578                                                }
579                                                //parts.add("1");//this is for vote
580                                                
581                                                String tmpstr = String.format(signature, (Object[]) id2Label.get(i));
582                                                tmpstr = tmpstr.replaceAll("INTERNAL_MARGINAL", prob+"");
583                                                
584                                                bw.append(FelixStringMan.commaListNoSpace(parts) + "," + tmpstr + "\n");
585                                        }
586                                }
587                                
588                        }else{
589                                
590                                if(nmax == -1){
591                                        return;
592                                }
593                                
594//                                double prob = Math.exp(weights[nmax]- sum);
595        
596                                if(this.isBinaryArbLR == true){
597                                        if(Math.exp(weights[0] - sum) <= 0.5){
598                                                return;
599                                        }
600                                }
601                                
602                                ArrayList<String> parts = new ArrayList<String>();
603                                //parts.add(Integer.toString(lrHead.nextTupleIDAndUpdate()));
604                                parts.add("TRUE");
605                                parts.add("");
606                                
607                                if(options.useDualDecomposition){
608                                        parts.add(Integer.toString(2));
609                                }else{
610                                        parts.add(Integer.toString(2));
611                                }
612                                
613                                
614                                //parts.add("1");//this is for vote
615                                
616                                String tmpstr = String.format(signature, (Object[]) id2Label.get(nmax));
617                                
618                                tmpstr = tmpstr.replaceAll("INTERNAL_MARGINAL", "1");
619                                
620                                bw.append(FelixStringMan.commaListNoSpace(parts) + "," + tmpstr + "\n");
621                                
622                        }
623                        
624                }catch(Exception e){
625                        e.printStackTrace();
626                }
627                
628        }
629 
630 
631 
632 
633        
634}

[all classes][felix.operator]
EMMA 2.0.5312 EclEmma Fix 2 (C) Vladimir Roubtsov