Ejemplo n.º 1
0
  /**
   * Combines all conditions into a single string keeping only the unique conditions. The result
   * need not be consistent.
   *
   * @param existing A string containing conditions.
   * @param additional A BaseX Value containing additional conditions.
   * @return A single string containing conditions without duplicates.
   */
  @Requires(Permission.NONE)
  @Deterministic
  public String combine(Str existing, Value additional) {
    try {
      // create a condition 'container'
      Set<String> result = new HashSet<String>();

      // store the existing conditions
      for (Condition condition : new ConditionGenerator(existing.toJava())) {
        result.add(condition.toString());
      }

      // read all the additional conditions from the sequence.
      for (Item item : additional) {
        // a single item in the sequence might contain multiple conditions
        for (Condition condition : new ConditionGenerator(item.toJava().toString())) {
          result.add(condition.toString());
        }
      }

      // join the resulting set on a space
      return CollectionUtils.join(result, " ");
    } catch (QueryException e) {
      // TODO: submit message to BaseX logging (or declare thrown?)
      System.err.println("error combining descriptor values: " + e.getMessage());
      return "";
    }
  }
 public ANode call(Str str) throws IOException {
   String input = str.toJava();
   SingleParser singleParser =
       new SingleParser(new IOContent(""), MainOptions.get()) {
         @Override
         protected void parse() throws IOException {}
       };
   MemBuilder memBuilder = new MemBuilder(input, singleParser);
   memBuilder.init();
   BaseXTreeBuilder treeBuilder = new BaseXTreeBuilder(memBuilder);
   Parse_XQDocComments parser = new Parse_XQDocComments();
   parser.initialize(input, treeBuilder);
   try {
     execute(parser);
   } catch (ParseException pe) {
     memBuilder = new MemBuilder(input, singleParser);
     memBuilder.init();
     Atts atts = new Atts();
     atts.add(Token.token("b"), Token.token(pe.getBegin() + 1));
     atts.add(Token.token("e"), Token.token(pe.getEnd() + 1));
     if (pe.getOffending() < 0) {
       atts.add(Token.token("s"), Token.token(pe.getState()));
     } else {
       atts.add(Token.token("o"), Token.token(pe.getOffending()));
       atts.add(Token.token("x"), Token.token(pe.getExpected()));
     }
     memBuilder.openElem(Token.token("ERROR"), atts, new Atts());
     memBuilder.text(Token.token(parser.getErrorMessage(pe)));
     memBuilder.closeElem();
   }
   return new DBNode(memBuilder.data());
 }
Ejemplo n.º 3
0
 /**
  * Returns a boolean value that shows if whether relationships between the boundaries, interiors
  * and exteriors of two geometries match the pattern specified in intersection-matrix-pattern.
  *
  * @param node1 xml element containing gml object(s)
  * @param node2 xml element containing gml object(s)
  * @param intersectionMatrix intersection matrix for two geometries
  * @return boolean value
  * @throws QueryException query exception
  */
 @Deterministic
 public Bln relate(final ANode node1, final ANode node2, final Str intersectionMatrix)
     throws QueryException {
   final Geometry geo1 = checkGeo(node1);
   final Geometry geo2 = checkGeo(node2);
   return Bln.get(geo1.relate(geo2, intersectionMatrix.toJava()));
 }
Ejemplo n.º 4
0
  /**
   * Tests whether condition descriptor a is mutually exclusive with condition descriptor b by
   * checking for variables in a having different values in b.
   *
   * @param a The one condition descriptor.
   * @param b The other condition descriptor.
   * @return Whether the two condition descriptors are mutually exclusive.
   */
  @Requires(Permission.NONE)
  @Deterministic
  public boolean mutuallyExclusive(Str a, Str b) {
    ConditionGenerator condA = new ConditionGenerator(a.toJava());
    ConditionGenerator condB = new ConditionGenerator(b.toJava());
    Map<String, Integer> conditions = new HashMap<String, Integer>();

    // put all conditions a into the map ...
    for (Condition condition : condA) {
      conditions.put(condition.name, condition.value);
    }

    // ... check if there is a condition in b that has the same name but a different value
    for (Condition condition : condB) {
      Integer value = conditions.get(condition.name);
      if (value != null && value != condition.value) {
        // immediately return false if this such a condition is found
        return true;
      }
    }

    return false;
  }
Ejemplo n.º 5
0
  /**
   * Checks whether the provided descriptor is consistent. Consistency means no variables occurring
   * twice with different values.
   *
   * @param descriptor The condition descriptor string to be tested.
   * @return Whether the conditions in the provided condition string are consistent.
   */
  @Requires(Permission.NONE)
  @Deterministic
  public boolean consistent(Str descriptor) {
    ConditionGenerator generator = new ConditionGenerator(descriptor.toJava());
    Map<String, Integer> conditions = new HashMap<String, Integer>();

    for (Condition condition : generator) {
      // rely on Map to enforce uniqueness of name, use Integer as it can be null
      Integer value = conditions.put(condition.name, condition.value);
      if (value != null && value != condition.value) {
        // immediately return false if the name was already encountered with a different value
        return false;
      }
    }

    // no inconsistencies found, return true
    return true;
  }
Ejemplo n.º 6
0
  /**
   * Calculates the probability of all conditions by simply multiplying them together. Does *not*
   * check for consistency (which would make the probability 0.0).
   *
   * @param wsdList The node containing the probabilities.
   * @param conditions The conditions to look up.
   * @return The probability of all conditions being true.
   */
  @Requires(Permission.NONE)
  @ContextDependent
  public double probability(ANode wsdList, Str conditions) {
    double probability = 1.0;
    // find probabilities for all conditions, multiply them
    for (Condition condition : new ConditionGenerator(conditions.toJava())) {
      // use Double to allow null when key is not present
      Double value = this.probabilityCache.get(condition);
      if (value == null) {
        // find it in the wsd-list ...
        value = this.findProbability(wsdList, condition.toString());
        // ... and cache it for future use
        this.probabilityCache.put(condition, value);
      }

      // do the probability multiplication
      probability *= value;
    }

    // return the result
    return probability;
  }