Exemple #1
0
 /** Gets a copy of this variable. */
 Term copy(AbstractMap vMap, AbstractMap substMap) {
   Var v;
   Object temp = vMap.get(this);
   if (temp == null) {
     v =
         new Var(
             null,
             Var.PROGRESSIVE,
             vMap.size(),
             timestamp); // name,Var.PROGRESSIVE,vMap.size(),timestamp);
     vMap.put(this, v);
   } else {
     v = (Var) temp;
   }
   Term t = getTerm();
   if (t instanceof Var) {
     Object tt = substMap.get(t);
     if (tt == null) {
       substMap.put(t, v);
       v.link = null;
     } else {
       v.link = (tt != v) ? (Var) tt : null;
     }
   }
   if (t instanceof Struct) {
     v.link = t.copy(vMap, substMap);
   }
   if (t instanceof Number) v.link = t;
   return v;
 }
 public VarDTO_Light getVarLight(long id) {
   Var v = em.find(Var.class, id);
   if (v != null) {
     v.setEM(em);
     return v.toDTO_Light();
   } else return null;
 }
 @Override
 public VarDTO_Detailed getVarDetailed(long id, UserAccountDTO dto, UserHistoryDTO hist_dto) {
   Var v = em.find(Var.class, id);
   if (v != null) {
     v.setEM(em);
     if (dto != null && dto.getId() != 0 && dto.getCurrent_research() == 0) {
       dto.setCurrent_research(v.getResearch_id());
     }
     return v.toDTO_Detailed(dto, hist_dto, em);
   } else return null;
 }
Exemple #4
0
  /**
   * Binds the specified value to a variable.
   *
   * @param name variable name
   * @param args argument array
   * @param value value to be bound
   * @throws QueryException query exception
   */
  private void bind(final QNm name, final Expr[] args, final Value value) throws QueryException {
    // skip nulled values
    if (value == null) return;

    for (int i = 0; i < function.args.length; i++) {
      final Var var = function.args[i];
      if (!var.name.eq(name)) continue;
      // casts and binds the value
      args[i] = var.checkType(value, context, null);
      break;
    }
  }
 @Override
 public ArrayList<VarDTO_Research> getVarsResearchNames(ArrayList<Long> keys) {
   ArrayList<VarDTO_Research> map = new ArrayList<VarDTO_Research>();
   for (Long k : keys) {
     Var v = em.find(Var.class, k);
     if (v != null) {
       v.setEM(em);
       VarDTO_Detailed dto = v.toDTO_Detailed(null, null, em);
       map.add(
           new VarDTO_Research(
               dto.getId(), dto.getResearch_id().intValue(), dto.getResearch_name()));
     }
   }
   return map;
 }
Exemple #6
0
 @Override
 public String toString() {
   final TokenBuilder tb = new TokenBuilder().add(var.toString()).add(' ').add(ASSIGN);
   tb.add(' ').add(expr.toString());
   if (coll != null) tb.add(' ').add(COLLATION).add(" \"").add(coll.uri()).add('"');
   return tb.toString();
 }
Exemple #7
0
 @Override
 public void plan(final FElem plan) {
   final FElem e = planElem();
   var.plan(e);
   expr.plan(e);
   plan.add(e);
 }
 @Override
 public ArrayList<VarDTO_Light> getResearchVarsSummaries(long research_id) {
   // Variant 1
   //        ArrayList<VarDTO_Light> list = new ArrayList<VarDTO_Light>();
   //        SocioResearch dsResearch,detached;
   //	try {
   //		 dsResearch = em.find(SocioResearch.class, research_id);
   //	     ArrayList<Long> var_ids = dsResearch.getVar_ids();
   //	     for(Long var_id:var_ids)
   //	     {
   //	    	 Var var = em.find(Var.class,var_id);
   //	    	 list.add(var.toDTO_Light());
   //	     }
   //	} finally
   //        {
   //        }
   // Variant 2
   //        TypedQuery<Var> q = em.createQuery("SELECT x FROM Var x WHERE x.research_id = :id
   // ORDER BY x.id", Var.class);
   //        q.setParameter("id", research_id);
   //        List<Var> l = q.getResultList();
   //        for(Var v:l)
   //        {
   //            list.add(v.toDTO_Light());
   //        }
   // Variant 3
   return Var.getResearchVarsLight(em, research_id);
   // throw new UnsupportedOperationException("Not supported yet.");
 }
 @Override
 public ArrayList<VarDTO_Light> getVarDTOs(ArrayList<Long> ids) {
   //       ArrayList<VarDTO_Light> arr = new ArrayList<VarDTO_Light>();
   //	if (ids != null)
   //		for(Long key:ids)
   //		{
   //			VarDTO_Light dto = getVarLight(key);
   //			arr.add(dto);
   //		}
   return Var.getResearchVarsLightDTOs(em, ids);
 }
 private String getVariableAlias(Var var) {
   return parent.getVariables().get(var.getName()).getAlias();
 }
  @Override
  public void meet(Var node) throws RuntimeException {
    // distinguish between the case where the variable is plain and the variable is bound
    SQLVariable sv = parent.getVariables().get(node.getName());

    if (sv == null) {
      builder.append("NULL");
    } else if (sv.getBindings().size() > 0) {
      // in case the variable is actually an alias for an expression, we evaluate that expression
      // instead, effectively replacing the
      // variable occurrence with its value
      sv.getBindings().get(0).visit(this);
    } else {
      String var = sv.getAlias();

      if (sv.getProjectionType() != ValueType.NODE && sv.getProjectionType() != ValueType.NONE) {
        // in case the variable represents a constructed or bound value instead of a node, we need
        // to
        // use the SQL expression as value; SQL should take care of proper casting...
        // TODO: explicit casting needed?
        builder.append(sv.getExpressions().get(0));
      } else {
        // in case the variable represents an entry from the NODES table (i.e. has been bound to a
        // node
        // in the database, we take the NODES alias and resolve to the correct column according to
        // the
        // operator type
        switch (optypes.peek()) {
          case STRING:
            Preconditions.checkState(var != null, "no alias available for variable");
            builder.append(var).append(".svalue");
            break;
          case INT:
            Preconditions.checkState(var != null, "no alias available for variable");
            builder.append(var).append(".ivalue");
            break;
          case DECIMAL:
          case DOUBLE:
            Preconditions.checkState(var != null, "no alias available for variable");
            builder.append(var).append(".dvalue");
            break;
          case BOOL:
            Preconditions.checkState(var != null, "no alias available for variable");
            builder.append(var).append(".bvalue");
            break;
          case DATE:
            Preconditions.checkState(var != null, "no alias available for variable");
            builder.append(var).append(".tvalue");
            break;
          case TZDATE:
            Preconditions.checkState(var != null, "no alias available for variable");
            builder.append(parent.getDialect().getDateTimeTZ(var));
            break;
          case URI:
            Preconditions.checkState(var != null, "no alias available for variable");
            builder.append(var).append(".svalue");
            break;
          case TERM:
          case NODE:
            if (sv.getExpressions().size() > 0) {
              // this allows us to avoid joins with the nodes table for simple expressions that only
              // need the ID
              builder.append(sv.getExpressions().get(0));
            } else {
              Preconditions.checkState(var != null, "no alias available for variable");
              builder.append(var).append(".id");
            }
            break;
        }
      }
    }
  }
  /**
   * Linear forward search algorithm
   *
   * @param p
   * @param algorithm
   * @param targetVars
   * @param _computeAll
   * @return
   */
  private boolean linearForwardSearch(
      PlanningContext context, EvaluationAlgorithm algorithm, boolean _computeAll) {

    /*
     * while iterating through hashset, items cant be removed from/added to
     * that set. Theyre collected into these sets and added/removedall
     * together after iteration is finished
     */
    Set<Var> newVars = new LinkedHashSet<Var>();
    Set<Var> relOutputs = new LinkedHashSet<Var>();
    Set<Var> removableVars = new LinkedHashSet<Var>();

    boolean changed = true;

    if (isLinearLoggingOn())
      logger.debug(
          "------Starting linear planning with (sub)goals: "
              + context.getRemainingGoals()
              + "--------");

    if (isLinearLoggingOn()) logger.debug("Algorithm " + algorithm);

    int counter = 1;

    while ((!_computeAll && changed && !context.getRemainingGoals().isEmpty())
        || (changed && _computeAll)) {

      if (isLinearLoggingOn()) logger.debug("----Iteration " + counter + " ----");

      counter++;
      changed = false;

      // iterate through all knownvars
      if (isLinearLoggingOn()) logger.debug("Known:" + context.getKnownVars());

      for (Var var : context.getKnownVars()) {

        if (isLinearLoggingOn()) logger.debug("Current Known: " + var);

        // Check the relations of all components
        for (Rel rel : var.getRels()) {
          if (isLinearLoggingOn()) logger.debug("And its rel: " + rel);
          if (context.isAvailableRel(rel)) {
            context.removeUnknownInput(rel, var);

            if (isLinearLoggingOn()) logger.debug("problem contains it " + rel);

            removableVars.add(var);

            if (context.isRelReadyToUse(rel) && rel.getType() != RelType.TYPE_METHOD_WITH_SUBTASK) {

              if (isLinearLoggingOn()) logger.debug("rel is ready to be used " + rel);

              boolean relIsNeeded = false;

              if (isLinearLoggingOn()) logger.debug("its outputs " + rel.getOutputs());

              for (Var relVar : rel.getOutputs()) {

                if (!context.getFoundVars().contains(relVar)) {
                  relIsNeeded = true;
                }
              }

              if (rel.getOutputs().isEmpty()) {
                relIsNeeded = true;
              }
              if (isLinearLoggingOn()) logger.debug("relIsNeeded " + relIsNeeded);

              if (relIsNeeded) {

                if (isLinearLoggingOn()) logger.debug("needed rel:  " + rel);

                if (!rel.getOutputs().isEmpty()) {
                  relOutputs.clear();
                  unfoldVarsToSet(rel.getOutputs(), relOutputs);
                  newVars.addAll(relOutputs);
                  context.getFoundVars().addAll(relOutputs);
                }
                algorithm.addRel(rel);
                if (isLinearLoggingOn()) logger.debug("algorithm " + algorithm);
              }

              context.removeRel(rel);
              changed = true;
            }
          }
        }
      }

      // remove targets if they have already been found
      for (Iterator<Var> targetIter = context.getRemainingGoals().iterator();
          targetIter.hasNext(); ) {
        Var targetVar = targetIter.next();
        if (context.getFoundVars().contains(targetVar)) {
          targetIter.remove();
        }
      }

      if (isLinearLoggingOn()) logger.debug("foundvars " + context.getFoundVars());

      context.getKnownVars().addAll(newVars);
      context.getKnownVars().removeAll(removableVars);
      newVars.clear();
    }
    if (isLinearLoggingOn()) logger.debug("algorithm " + algorithm);

    if (!_computeAll) {
      Optimizer.optimize(context, algorithm);

      if (isLinearLoggingOn()) logger.debug("optimized algorithm " + algorithm);
    }

    if (isLinearLoggingOn()) logger.debug("\n---!!!Finished linear planning!!!---\n");

    return context.getRemainingGoals().isEmpty()
        || context.getFoundVars().containsAll(context.getAllGoals());
  }
 @Override
 public ArrayList<VarDTO_Light> getResearchVarsWeightCandidates(long research_id) {
   return Var.getResearchVarsWeightCandidates(em, research_id);
 }
 @Override
 public VarDTO getVar(long id, UserAccountDTO dto, UserHistoryDTO hist_dto) {
   Var v = em.find(Var.class, id);
   v.setEM(em);
   return v.toDTO(dto, hist_dto, em);
 }
 @Override
 public ArrayList<Double> get2DDistribution(
     long var_id1, long var_id2, UserAccountDTO dto, UserHistoryDTO hist_dto) {
   return Var.calc2DDistribution(var_id1, var_id2, dto, hist_dto, em);
 }