示例#1
0
  // Plays a card.
  public static void handleRequestPlay(Message m) {

    boolean heartsBroken = false;
    if (m.info.equals("true")) heartsBroken = true;

    Deck legal = Rules.extractLegalCards(hand, trick, heartsBroken);

    Card c = randomCards(legal, 1).cardAt(0);
    hand.remove(c);

    Message m2 = new Message(Message.PLAY, null, c);
    server.sendMessage(m2);
  }
示例#2
0
  /**
   * according to .gwt.xml files generates a LinkedMap which has interfaces as keys array of
   * generators as values keys are sorted according order of <generate-with> elements in .gwt.xml
   * files
   *
   * @param context
   * @throws UnableToCompleteException
   */
  private void fillUpGeneratorChainMap(TreeLogger logger, GeneratorContext context)
      throws UnableToCompleteException {

    GeneratorChain.customGenerators = new LinkedList<AbstractGenerator>();
    GeneratorChain.replacers = new LinkedList<AbstractGenerator>();
    GeneratorChain.thirdPartyGenerators = new LinkedHashMap<Generator, AbstractGenerator>();
    ModuleDef moduleDef =
        ((CompilerContext) getPrivateField(context, "compilerContext")).getModule();
    Rules rules = moduleDef.getRules();
    Iterator<Rule> rulesIter = rules.iterator();
    while (rulesIter.hasNext()) {
      Rule rul = rulesIter.next();
      Generator gen = null;

      // =================replace with
      if (rul instanceof RuleReplaceWith) {
        String replaceClass = (String) getPrivateField(rul, "replacementTypeName");
        gen = new ReplaceByGenerator(replaceClass);
        // gen = null;

        // =================generate with
      } else if (rul instanceof RuleGenerateWith) {
        Class<? extends Generator> generatorClass =
            (Class<? extends Generator>) getPrivateField(rul, "generatorClass");
        Constructor<?> constructor;
        try {
          constructor = generatorClass.getDeclaredConstructor();
        } catch (Exception e) {
          logger.log(
              Type.ERROR, "Unable to obtain default constructor of generator " + generatorClass);
          throw new UnableToCompleteException();
        }
        constructor.setAccessible(true);
        try {
          gen = (Generator) constructor.newInstance();
        } catch (Exception e) {
          logger.log(Type.ERROR, "Unable to create instance of generator " + generatorClass);
          throw new UnableToCompleteException();
        }
      }

      if (gen != null) {
        if (gen instanceof AbstractGenerator) {
          GenPredicGroup newGroup = null;
          AbstractGenerator myGen = (AbstractGenerator) gen;
          if (GeneratorChain.customGenerators.contains(gen)
              || GeneratorChain.replacers.contains(gen)) {
            newGroup = addPredicsToExisting(rul, myGen.getConditions());
            myGen.setConditions(newGroup);
          } else {
            newGroup = getGroupConditions(rul.getRootCondition().getConditions(), null);
            myGen.setConditions(newGroup);
            if (gen instanceof ReplaceByGenerator) {
              GeneratorChain.replacers.addFirst(myGen);
            } else {
              GeneratorChain.customGenerators.addFirst(myGen);
            }
          }

        } else {
          if (GeneratorChain.thirdPartyGenerators.containsKey(gen)) {
            AbstractGenerator myGen = GeneratorChain.thirdPartyGenerators.get(gen);
            GenPredicGroup newGroup = addPredicsToExisting(rul, myGen.getConditions());
            myGen.setConditions(newGroup);
            GeneratorChain.thirdPartyGenerators.put(gen, myGen);
          } else {
            AbstractGenerator myGen =
                new AbstractGenerator() {

                  @Override
                  public String doGenerate(
                      TreeLogger logger, GeneratorContext context, String typeName)
                      throws UnableToCompleteException {
                    return null;
                  }
                };
            myGen.setConditions(getGroupConditions(rul.getRootCondition().getConditions(), null));
            GeneratorChain.thirdPartyGenerators.put(gen, myGen);
          }
        }
      }
    }
  }
 private void combiningNP(Map<String, Rules> rl, Map<String, RuleSet> ruleset, int i) {
   RuleSet rs;
   String rkey;
   int order1, order2;
   Rules rule;
   List<RuleData> ls;
   String keys[] = {
     "NNNP", "VBNP", "JJNP", "NNCP", "VBCP", "JJCP",
   }; // noun, adj, vb in noun phrase
   for (String s : rl.keySet()) {
     rule = rl.get(s);
     rule.initMap();
     // process NP chunk
     // cases to considered: PRO-TG (PRO)*
     // TG prep1 PRO1 (prep2 PRO2)*
     for (String subkey : keys) {
       ls = rule.getEvalRules(subkey); // all NP pattern of current trg
       if (ls == null) {
         continue;
       }
       rkey = s + i + subkey; // trg + type + pos(first two letters) + chunk type
       // has NP patterns
       rs = new RuleSet();
       order1 = 0;
       order2 = 0;
       for (RuleData dt : ls) {
         if (dt.in_chunk) { // in chunk case
           rs.in_chunk = true;
           rs.inchunk_count += dt.count; // count number of inchunk event
           if (!dt.has_cause) { // all event type without theme2/cause
             if (dt.event1) {
               rs.ecount += dt.count;
             } else {
               rs.pcount += dt.count;
             }
           } else if (i == 5 && dt.has_cause) { // theme2 PRO-TG PRO
             rs.t2count += dt.count;
             rs.pcount += dt.count;
             rs.dist2 = Math.max(rs.dist2, dt.dist2);
           } else if (i > 5 && dt.theme_pos && dt.has_cause) { // has cause
             rs.pcause += dt.count; // assume only pro is cause : PRO - TG Theme (Pro/Evt)
             if (dt.event1) {
               rs.ecount += dt.count;
             } else {
               rs.pcount += dt.count;
             }
             rs.dist1 = Math.max(rs.dist1, dt.dist1);
           }
         } else if (dt.theme_pos) { // for all POS : TG - prep - PRO
           if ((dt.POS.startsWith("NN") && !dt.prep1.isEmpty())
               || !dt.POS.startsWith("NN")) { // (NN && prep) or (VB/JJ)
             if (!dt.has_cause) { // all event type, no theme2 / cause
               if (i <= 5) {
                 rs.pcount += dt.count;
               } else {
                 if (dt.event1) {
                   rs.ecount += dt.count;
                 } else {
                   rs.pcount += dt.count;
                 }
               }
               rs.dist1 = Math.max(rs.dist1, dt.dist1);
             } else if (dt.cause_pos
                 && !dt.prep2.isEmpty()
                 && dt.POS.startsWith("NN")) { // TG-prep1-PRO1-prep2-PRO2 ; only NNx
               if (i == 5) {
                 rs.t2count += dt.count;
                 rs.pcount += dt.count;
               } else {
                 if (dt.event1) {
                   rs.ecount += dt.count;
                 } else {
                   rs.pcount += dt.count;
                 }
                 if (dt.event2) {
                   rs.ecause += dt.count;
                 } else {
                   rs.pcause += dt.count;
                 }
               }
               rs.dist1 = Math.max(rs.dist1, dt.dist1);
               rs.dist2 = Math.max(rs.dist2, dt.dist2);
             }
           }
         } else if (i == 5
             && !dt.theme_pos
             && ((dt.has_cause && dt.cause_pos)
                 || !dt.POS.startsWith("NN"))) { // Binding: PRO1 - TG - PRO2
           rs.in_front += dt.count;
           if (!dt.prep2.isEmpty()) {
             rs.prep_2.add(dt.prep2);
           }
           if (!dt.prep1.isEmpty()) {
             rs.prep_1.add(dt.prep1);
           }
           rs.dist1 = Math.max(rs.dist1, dt.dist1);
           rs.dist2 = Math.max(rs.dist2, dt.dist2);
           rs.pcount += dt.count;
           rs.dist2 = Math.max(rs.dist2, dt.dist2);
         }
       }
       rs.detected = getCountDetectedTG(s, subkey);
       if (order2 > order1) {
         rs.order = false;
       }
       if (rs.getFreq() >= 2) {
         ruleset.put(rkey, rs);
       }
     }
   }
 }
 private boolean eventToVP(
     VerbChunk vc,
     Word tg,
     Word pr,
     Word pr2,
     List<Word> prep,
     Map<String, Rules> rules[],
     int ev_type,
     boolean evt1,
     boolean evt2,
     boolean add) {
   boolean prep1_pos, prep2_pos = false, prep_order = false, in_chunk = false;
   String prep1 = "", prep2 = "", ctype, pos_type;
   int count1 = 0, count2 = 0; // count2 -> for theme2/cause
   boolean has_theme2 = false;
   String childTrg = "", parentTrg = "";
   if (vc.subject.belongTO(tg, pr, pr2)) {
     eventToNP(vc.subject, tg, pr, pr2, prep, rules, ev_type, evt1, evt2, add);
   } else if (vc.object.belongTO(tg, pr, pr2)) {
     eventToNP(vc.object, tg, pr, pr2, prep, rules, ev_type, evt1, evt2, add);
   } else if (vc.verb.contains(tg)) { // verb contains trigger
     vppt++;
     if (curr_event.getLevel(0) >= 2) {
       vppt2++;
     }
     ctype = "VP";
     pos_type = tg.pos_tag;
     prep1_pos = tg.pos < pr.pos ? true : false;
     if (pr2 == null) {
       if (vc.subject.containsKey(pr)) {
         count1 = vc.subject.getChunkPos(pr.pos);
         // or relative clause
         prep1 = getPrepFront(tg, pr, vc.subject);
       } else if (vc.object.containsKey(pr)) {
         count1 = vc.object.getChunkPos(pr.pos);
         prep1 = getPrep(tg, pr, vc.object);
       }
       if (evt1) {
         childTrg = pr.word;
       }
     } else { // Pr2!=null
       // for both binding and regulatory events
       has_theme2 = true;
       prep2_pos = tg.pos < pr2.pos ? true : false;
       if (prep1_pos && prep2_pos) { // both are behind trig
         count1 = vc.object.getChunkPos(pr.pos);
         count2 = vc.object.getChunkPos(pr2.pos);
         if (pr.pos < pr2.pos) {
           prep1 = getPrep(tg, pr, vc.object);
           prep2 = getPrep2(pr, pr2, vc.object);
         } else {
           prep2 = getPrep(tg, pr2, vc.object);
           prep1 = getPrep2(pr2, pr, vc.object);
         }
       } else if (prep1_pos && !prep2_pos) { // cause - tg - theme
         prep1 = getPrep(tg, pr, vc.object);
         prep2 = getPrepFront(tg, pr2, vc.subject);
         count1 = vc.object.getChunkPos(pr.pos);
         count2 = vc.subject.getChunkPos(pr2.pos);
       } else if (!prep1_pos && prep2_pos) { // theme - tg - cause
         prep1 = getPrepFront(tg, pr, vc.subject);
         prep2 = getPrep(tg, pr2, vc.object);
         count2 = vc.object.getChunkPos(pr2.pos);
         count1 = vc.subject.getChunkPos(pr.pos);
       } else if (!prep1_pos && !prep2_pos) { // both are in front of tg
         // reg event: few cases, can skip
         prep1 = getPrep2(pr, pr2, vc.subject);
         prep2 = getPrepFront(tg, pr2, vc.subject);
         count1 = vc.subject.getChunkPos(pr.pos);
         count2 = vc.subject.getChunkPos(pr2.pos);
         if (ev_type > 5) {
           return false; // skip this case
         }
       }
       if (ev_type
           > 5) { // for binding events: the order of proteins always are PR1-PR2 if they are in
                  // the same side
         if (evt1) {
           childTrg = pr.word;
         }
         if (evt2) {
           parentTrg = pr2.word;
         }
       }
     }
     Rules rl = rules[ev_type].get(tg.word);
     if (rl == null) {
       rl = new Rules(ev_type, tg.word);
       rules[ev_type].put(tg.word, rl);
     }
     if (ev_type < 5) {
       rl.addPattern(
           vc.verb_type, pos_type, ctype, prep1_pos, prep1, in_chunk, count1, childTrg, add);
     } else if (ev_type == 5) {
       rl.addPattern(
           vc.verb_type,
           pos_type,
           ctype,
           prep1_pos,
           prep2_pos,
           prep_order,
           prep1,
           prep2,
           has_theme2,
           in_chunk,
           count1,
           count2,
           childTrg,
           add);
     } else {
       rl.addPattern(
           vc.verb_type,
           pos_type,
           ctype,
           prep1_pos,
           prep2_pos,
           prep_order,
           prep1,
           prep2,
           has_theme2,
           in_chunk,
           count1,
           count2,
           evt1,
           evt2,
           childTrg,
           parentTrg,
           add);
     }
   } else { // merge subject and subject
     BasicChunk new_ch = new BasicChunk();
     new_ch.addChunk(vc.subject);
     new_ch.addChunk(vc.verb);
     new_ch.addChunk(vc.object);
     new_ch.is_merged = true;
     return eventToNP(new_ch, tg, pr, pr2, prep, rules, ev_type, evt1, evt2, add);
   }
   return true;
 }
 private boolean eventToNP(
     BasicChunk bs,
     Word tg,
     Word pr,
     Word pr2,
     List<Word> prep,
     Map<String, Rules> rules[],
     int ev_type,
     boolean evt1,
     boolean evt2,
     boolean add) {
   boolean prep1_pos, prep2_pos = false, prep_order = false, in_chunk = false;
   String prep1 = "", prep2 = "", ctype, pos_type;
   int count1, count2 = 0; // for theme2/cause
   int verb_type;
   boolean has_theme2 = false;
   String themeTrg = "", causeTrg = "";
   bs.mergeNP();
   if (bs.is_merged) {
     ctype = "CP";
   } else {
     ctype = "NP";
   }
   if (evt1) { // theme
     themeTrg = pr.word;
   }
   if (evt2) { // cause
     causeTrg = pr2.word;
   }
   pos_type = tg.pos_tag;
   prep1_pos = pr.pos > tg.pos ? true : false; // prep 1 position
   nppt++; // NP pattern
   if (pr2 == null) { // only theme
     if (bs.isSameChunk(tg, pr) && !evt1) {
       Counter ct = sameChunk.get(tg.word);
       if (ct == null) {
         ct = new Counter(1);
         sameChunk.put(tg.word, ct);
       } else {
         ct.inc();
       }
     }
     count1 = bs.countChunks(tg, pr);
     if (prep1_pos) { // TG - PRO
       prep1 = getPrep(tg, pr, bs);
       if (ev_type <= 5) {
         Word sub_tg = findTrg(tg, pr.pos, bs);
         if (sub_tg != null) { // found sub-trg
           String key = tg.word + tg.pos_tag;
           Map<String, Counter> ct = subTG.get(key);
           if (ct == null) {
             ct = new HashMap<String, Counter>();
             subTG.put(key, ct);
           }
           Counter c = ct.get(sub_tg.word + sub_tg.pos_tag);
           if (c == null) {
             c = new Counter(1);
             ct.put(sub_tg.word + sub_tg.pos_tag, c);
           } else {
             c.inc();
           }
         }
       }
     } else { // PRO - TG
       if (!evt1 && bs.inChunk(tg, pr)) {
         in_chunk = true;
         count1 = 0;
       } else if (evt1 && tg.pos == pr.pos) {
         in_chunk = true;
         count1 = 0;
       }
     }
     if (in_chunk && !evt1) {
       Chunk tgc = bs.getChunk(tg.pos);
       if (tgc.is_inChunk(tg, tokens) && tgc.trigs.size() == 2) {
         // System.out.println("---->"+ tgc.getText()+"\n"+curr_pmid+" "+curr_sen_id);
         // bs.printChunk();
         // System.out.println("");
       }
     }
   } else {
     has_theme2 = true; // has cause/theme2
     prep2_pos = pr2.pos > tg.pos ? true : false;
     count1 = bs.countChunks(tg, pr);
     count2 = bs.countChunks(tg, pr2);
     if (prep1_pos && prep2_pos) { // both are behind trig
       if (pr.pos > pr2.pos) { // reg only: TG - cause - theme
         prep1 = getPrep2(pr2, pr, bs); // theme
         prep2 = getPrep(tg, pr2, bs); // cause
         prep_order = true; // need to swich order
       } else { // binding  & reg : TG- theme - cause/theme2
         prep2 = getPrep2(pr, pr2, bs); // cause
         prep1 = getPrep(tg, pr, bs); // theme
       }
     } else if (prep1_pos && !prep2_pos) { // cause - tg - theme -> reg only
       prep1 = getPrep(tg, pr, bs);
       prep2 = getPrepFront(tg, pr2, bs);
     } else if (!prep1_pos && prep2_pos) { // theme - tg - cause | theme1 - tg - theme2
       prep1 = getPrepFront(tg, pr, bs);
       prep2 = getPrep(tg, pr2, bs);
     } else { // both are in front of tg
       // binding & reg
       if (pr.pos > pr2.pos) { // cause/theme2 - theme -TG: should skip this
         prep1 = getPrepFront(tg, pr, bs);
         prep2 = getPrep2(pr2, pr, bs);
         prep_order = true;
       } else { // Reg only : theme - cause/theme2 - TG
         prep1 = getPrep2(pr, pr2, bs);
         prep2 = getPrepFront(tg, pr2, bs);
       }
     }
     if (ev_type == 5) {
       in_chunk = bs.inChunk(tg, pr);
       // count1 = 0;
     } else if (ev_type > 5 && !evt2 && !prep2_pos) { // cause must be pro
       in_chunk = bs.inChunk(tg, pr2);
       // count2 = 0;
     }
   } // pr2 condition
   verb_type = 0;
   Rules rl = rules[ev_type].get(tg.word);
   if (rl == null) {
     rl = new Rules(ev_type, tg.word);
     rules[ev_type].put(tg.word, rl);
   }
   if (ev_type < 5) {
     rl.addPattern(verb_type, pos_type, ctype, prep1_pos, prep1, in_chunk, count1, themeTrg, add);
   } else if (ev_type == 5) {
     rl.addPattern(
         verb_type,
         pos_type,
         ctype,
         prep1_pos,
         prep2_pos,
         prep_order,
         prep1,
         prep2,
         has_theme2,
         in_chunk,
         count1,
         count2,
         themeTrg,
         add);
   } else { // regulatory events
     rl.addPattern(
         verb_type,
         pos_type,
         ctype,
         prep1_pos,
         prep2_pos,
         prep_order,
         prep1,
         prep2,
         has_theme2,
         in_chunk,
         count1,
         count2,
         evt1,
         evt2,
         themeTrg,
         causeTrg,
         add);
   }
   return true;
 }
 private void combiningVP(Map<String, Rules> rl, Map<String, RuleSet> ruleset, int i) {
   RuleSet rs;
   String rkey;
   int order1;
   Rules rule;
   List<RuleData> ls;
   String keys[] = {"VBVP", "JJVP"}; // noun, adj, vb in noun phrase
   for (String s : rl.keySet()) {
     rule = rl.get(s);
     rule.initMap();
     // ** VP JJ
     for (String subkey : keys) {
       ls = rule.getEvalRules(subkey); // all NP pattern of current trg
       if (ls == null) {
         continue;
       }
       rkey = s + i + subkey;
       rs = new RuleSet();
       order1 = 0;
       for (RuleData dt : ls) {
         if (dt.count < 2 && i < 5) {
           // continue;
         }
         if (!dt.has_cause) {
           if (i <= 5) {
             rs.pcount += dt.count;
           } else {
             if (dt.event1) {
               rs.ecount += dt.count;
             } else {
               rs.pcount += dt.count;
             }
           }
         } else { // has theme2/cause
           if (i == 5) {
             rs.t2count += dt.count;
             rs.pcount += dt.count;
           } else {
             if (dt.event1) {
               rs.ecount += dt.count;
             } else {
               rs.pcount += dt.count;
             }
             if (dt.event2) {
               rs.ecause += dt.count;
             } else {
               rs.pcause += dt.count;
             }
           }
         }
         if (dt.verb_type == 1 && dt.POS.equals("VBN") && dt.theme_pos) { //
           order1 += dt.count;
         } else if (dt.verb_type == 1 && dt.POS.equals("VBN") && !dt.theme_pos) {
           order1 -= dt.count;
         }
         rs.dist1 = Math.max(rs.dist1, dt.dist1);
         rs.dist2 = Math.max(rs.dist2, dt.dist2);
       }
       rs.detected = getCountDetectedTG(s, subkey);
       if (order1 > 0) {
         rs.order = false;
       }
       if (rs.getFreq() >= 2) {
         ruleset.put(rkey, rs);
       }
     }
   }
 }