/**
  * Return all instances of an entity that match the given criteria. Use whereClause to specify
  * condition, using reqular SQL syntax for WHERE clause.
  *
  * <p>For example selecting all JOHNs born in 2001 from USERS table may look like:
  *
  * <pre>
  * users.find(Users.class, "NAME='?' and YEAR=?", new String[] {"John", "2001"});
  * </pre>
  *
  * @param <T> Any ActiveRecordBase class.
  * @param type The class of the entities to return.
  * @param whereClause The condition to match (Don't include "where").
  * @param whereArgs The arguments to replace "?" with.
  * @return A generic list of all matching entities.
  * @throws IllegalArgumentException
  * @throws IllegalAccessException
  * @throws InstantiationException
  */
 public <T extends ActiveRecordBase> List<T> find(
     Class<T> type, String whereClause, String[] whereArgs) throws ActiveRecordException {
   if (m_Database == null) throw new ActiveRecordException("Set database first");
   T entity = null;
   try {
     entity = type.newInstance();
   } catch (IllegalAccessException e1) {
     throw new ActiveRecordException(e1.getLocalizedMessage());
   } catch (InstantiationException e1) {
     throw new ActiveRecordException(e1.getLocalizedMessage());
   }
   List<T> toRet = new ArrayList<T>();
   Cursor c = m_Database.query(entity.getTableName(), null, whereClause, whereArgs);
   try {
     while (c.moveToNext()) {
       entity = s_EntitiesMap.get(type, c.getLong(c.getColumnIndex("_id")));
       if (entity == null) {
         entity = type.newInstance();
         entity.m_Database = m_Database;
         entity.m_NeedsInsert = false;
         entity.inflate(c);
       }
       toRet.add(entity);
     }
   } catch (IllegalAccessException e) {
     throw new ActiveRecordException(e.getLocalizedMessage());
   } catch (InstantiationException e) {
     throw new ActiveRecordException(e.getLocalizedMessage());
   } finally {
     c.close();
   }
   return toRet;
 }
 /**
  * This method creates the constraint instance.
  *
  * @param constraintStructure the constraint's structure (bConstraint clss in blender 2.49).
  * @param ownerOMA the old memory address of the constraint's owner
  * @param influenceIpo the ipo curve of the influence factor
  * @param blenderContext the blender context
  * @throws BlenderFileException this exception is thrown when the blender file is somehow
  *     corrupted
  */
 protected Constraint createConstraint(
     Structure constraintStructure, Long ownerOMA, Ipo influenceIpo, BlenderContext blenderContext)
     throws BlenderFileException {
   String constraintClassName = this.getConstraintClassName(constraintStructure, blenderContext);
   Class<? extends Constraint> constraintClass = constraintClasses.get(constraintClassName);
   if (constraintClass != null) {
     try {
       return (Constraint)
           constraintClass.getDeclaredConstructors()[0].newInstance(
               constraintStructure, ownerOMA, influenceIpo, blenderContext);
     } catch (IllegalArgumentException e) {
       throw new BlenderFileException(e.getLocalizedMessage(), e);
     } catch (SecurityException e) {
       throw new BlenderFileException(e.getLocalizedMessage(), e);
     } catch (InstantiationException e) {
       throw new BlenderFileException(e.getLocalizedMessage(), e);
     } catch (IllegalAccessException e) {
       throw new BlenderFileException(e.getLocalizedMessage(), e);
     } catch (InvocationTargetException e) {
       throw new BlenderFileException(e.getLocalizedMessage(), e);
     }
   } else {
     throw new BlenderFileException("Unknown constraint type: " + constraintClassName);
   }
 }
  /**
   * Return the instance of an entity with a matching id.
   *
   * @param <T> Any ActiveRecordBase class.
   * @param type The class of the entity to return.
   * @param id The desired ID.
   * @return The matching entity if reocrd found in DB, null otherwise
   * @throws ActiveRecordException
   */
  public <T extends ActiveRecordBase> T findByID(Class<T> type, long id)
      throws ActiveRecordException {
    if (m_Database == null) throw new ActiveRecordException("Set database first");
    T entity = s_EntitiesMap.get(type, id);
    if (entity != null) return entity;

    try {
      entity = type.newInstance();
    } catch (IllegalAccessException e) {
      throw new ActiveRecordException(e.getLocalizedMessage());
    } catch (InstantiationException e) {
      throw new ActiveRecordException(e.getLocalizedMessage());
    }

    Cursor c =
        m_Database.query(entity.getTableName(), null, "_id = ?", new String[] {String.valueOf(id)});
    try {
      if (!c.moveToNext()) {
        return null;
      } else {
        entity.inflate(c);
        entity.m_NeedsInsert = false;
        entity.m_Database = m_Database;
      }
    } finally {
      c.close();
    }
    return entity;
  }
  /**
   * Returns a copy of this neural net
   *
   * @return INeuralNet Copy of this neural net
   */
  public INeuralNet copy() {

    // Resulting neural net
    AbstractNeuralNet result = null;

    try {
      result = this.getClass().newInstance();

      // Copy the input layer
      result.inputLayer = this.inputLayer.copy();

      // Copy hidden layers
      ILayer<? extends INeuron> lastCopiedLayer = result.inputLayer;
      for (LinkedLayer hl : this.hiddenLayers) {
        // Copy hidden layer
        LinkedLayer hlr = hl.copy(lastCopiedLayer);
        result.addHlayer(hlr);

        // Update last copied layer
        lastCopiedLayer = hlr;
      }

      // Copy the output layer
      result.outputLayer = this.outputLayer.copy(lastCopiedLayer);
    } catch (InstantiationException e) {
      System.out.println("Instantiation Error " + e.getLocalizedMessage());
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      System.out.println("Illegal Access Error " + e.getLocalizedMessage());
      e.printStackTrace();
    }
    return result;
  }
 /**
  * Delete selected entities from the database.
  *
  * @param <T> Any AREntity class.
  * @param type The class of the entities to delete.
  * @param whereClause The condition to match (Don't include "where").
  * @param whereArgs The arguments to replace "?" with.
  * @return The number of rows affected.
  * @throws IllegalAccessException
  * @throws InstantiationException
  */
 public <T extends ActiveRecordBase> int delete(
     Class<T> type, String whereClause, String[] whereArgs) throws ActiveRecordException {
   if (m_Database == null) throw new ActiveRecordException("Set database first");
   T entity;
   try {
     entity = type.newInstance();
   } catch (IllegalAccessException e) {
     throw new ActiveRecordException(e.getLocalizedMessage());
   } catch (InstantiationException e) {
     throw new ActiveRecordException(e.getLocalizedMessage());
   }
   return m_Database.delete(entity.getTableName(), whereClause, whereArgs);
 }
 public boolean createApplet() {
   try {
     applet = launcherFrame.launcher.launcher.createApplet();
     return true;
   } catch (final ClassNotFoundException e) {
     MCLogger.info(e.getLocalizedMessage());
   } catch (final InstantiationException e) {
     MCLogger.info(e.getLocalizedMessage());
   } catch (final IllegalAccessException e) {
     MCLogger.info(e.getLocalizedMessage());
   }
   return false;
 }
  /**
   * Use this method in eclipse environments. Must be called to initialize the registration of
   * adapters.
   *
   * @param urlLocator The URL location converter needed in eclipse environments.
   */
  public static void initRegistration(IUrlLocator urlLocator) {
    Class[] adapterFactories =
        findClassesOfType(urlLocator, ADAPTER_PACKAGE_NAME, IAdapterFactory.class);

    // Register all found factories
    for (int i = 0; i < adapterFactories.length; i++) {
      try {
        IAdapterFactory factory = (IAdapterFactory) adapterFactories[i].newInstance();
        getInstance().registerFactory(factory);
      } catch (IllegalAccessException e) {
        log.error(e.getLocalizedMessage(), e);
      } catch (InstantiationException e) {
        log.error(e.getLocalizedMessage(), e);
      }
    }
  }
  public String create() {
    FacesContext context = FacesContext.getCurrentInstance();

    try {
      this.setCurrentEntity(this.entityClass.newInstance());
      this.setSelection(this.getCurrentEntity());
      updateTable(this.getCurrentEntity());
    } catch (InstantiationException e) {
      context.addMessage("Cannot create entity", new FacesMessage(e.getLocalizedMessage()));
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      context.addMessage(
          "Access denied for entity creation",
          new FacesMessage(e.getLocalizedMessage() + this.entityClass.getName()));
      e.printStackTrace();
    }
    return "";
  }
  private <T extends RealFieldElement<T>> RungeKuttaStepInterpolator convertInterpolator(
      final RungeKuttaFieldStepInterpolator<T> fieldInterpolator,
      final FirstOrderFieldDifferentialEquations<T> eqn) {

    RungeKuttaStepInterpolator regularInterpolator = null;
    try {

      String interpolatorName = fieldInterpolator.getClass().getName();
      String integratorName = interpolatorName.replaceAll("Field", "");
      @SuppressWarnings("unchecked")
      Class<RungeKuttaStepInterpolator> clz =
          (Class<RungeKuttaStepInterpolator>) Class.forName(integratorName);
      regularInterpolator = clz.newInstance();

      double[][] yDotArray = null;
      java.lang.reflect.Field fYD = RungeKuttaFieldStepInterpolator.class.getDeclaredField("yDotK");
      fYD.setAccessible(true);
      @SuppressWarnings("unchecked")
      T[][] fieldYDotk = (T[][]) fYD.get(fieldInterpolator);
      yDotArray = new double[fieldYDotk.length][];
      for (int i = 0; i < yDotArray.length; ++i) {
        yDotArray[i] = new double[fieldYDotk[i].length];
        for (int j = 0; j < yDotArray[i].length; ++j) {
          yDotArray[i][j] = fieldYDotk[i][j].getReal();
        }
      }
      double[] y = new double[yDotArray[0].length];

      EquationsMapper primaryMapper = null;
      EquationsMapper[] secondaryMappers = null;
      java.lang.reflect.Field fMapper =
          AbstractFieldStepInterpolator.class.getDeclaredField("mapper");
      fMapper.setAccessible(true);
      @SuppressWarnings("unchecked")
      FieldEquationsMapper<T> mapper = (FieldEquationsMapper<T>) fMapper.get(fieldInterpolator);
      java.lang.reflect.Field fStart = FieldEquationsMapper.class.getDeclaredField("start");
      fStart.setAccessible(true);
      int[] start = (int[]) fStart.get(mapper);
      primaryMapper = new EquationsMapper(start[0], start[1]);
      secondaryMappers = new EquationsMapper[mapper.getNumberOfEquations() - 1];
      for (int i = 0; i < secondaryMappers.length; ++i) {
        secondaryMappers[i] = new EquationsMapper(start[i + 1], start[i + 2]);
      }

      AbstractIntegrator dummyIntegrator =
          new AbstractIntegrator("dummy") {
            @Override
            public void integrate(ExpandableStatefulODE equations, double t) {
              Assert.fail("this method should not be called");
            }

            @Override
            public void computeDerivatives(final double t, final double[] y, final double[] yDot) {
              T fieldT = fieldInterpolator.getCurrentState().getTime().getField().getZero().add(t);
              T[] fieldY =
                  MathArrays.buildArray(
                      fieldInterpolator.getCurrentState().getTime().getField(), y.length);
              for (int i = 0; i < y.length; ++i) {
                fieldY[i] =
                    fieldInterpolator.getCurrentState().getTime().getField().getZero().add(y[i]);
              }
              T[] fieldYDot = eqn.computeDerivatives(fieldT, fieldY);
              for (int i = 0; i < yDot.length; ++i) {
                yDot[i] = fieldYDot[i].getReal();
              }
            }
          };
      regularInterpolator.reinitialize(
          dummyIntegrator,
          y,
          yDotArray,
          fieldInterpolator.isForward(),
          primaryMapper,
          secondaryMappers);

      T[] fieldPreviousY = fieldInterpolator.getPreviousState().getState();
      for (int i = 0; i < y.length; ++i) {
        y[i] = fieldPreviousY[i].getReal();
      }
      regularInterpolator.storeTime(fieldInterpolator.getPreviousState().getTime().getReal());

      regularInterpolator.shift();

      T[] fieldCurrentY = fieldInterpolator.getCurrentState().getState();
      for (int i = 0; i < y.length; ++i) {
        y[i] = fieldCurrentY[i].getReal();
      }
      regularInterpolator.storeTime(fieldInterpolator.getCurrentState().getTime().getReal());

    } catch (ClassNotFoundException cnfe) {
      Assert.fail(cnfe.getLocalizedMessage());
    } catch (InstantiationException ie) {
      Assert.fail(ie.getLocalizedMessage());
    } catch (IllegalAccessException iae) {
      Assert.fail(iae.getLocalizedMessage());
    } catch (NoSuchFieldException nsfe) {
      Assert.fail(nsfe.getLocalizedMessage());
    } catch (IllegalArgumentException iae) {
      Assert.fail(iae.getLocalizedMessage());
    }

    return regularInterpolator;
  }
Exemple #10
0
  /**
   * Setup parameter accordnig to ZkParameter annotation.
   *
   * @param <T> type of field.
   * @param annot the annotation with parameter definition
   * @param field the field to set
   * @param clazz class of the field and parameter
   */
  protected static <T> void setupZkParameter(
      ZkParameter annot,
      String paramName,
      Class<T> clazz,
      Field field,
      Method method,
      Object instance) {
    T result = null;

    Map[] paramMaps =
        new Map[] {
          Executions.getCurrent().getArg(),
          Executions.getCurrent().getAttributes(),
          Executions.getCurrent().getParameterMap()
        };

    Map paramMap = paramMaps[0];
    for (Map map : paramMaps) {
      if (map.containsKey(paramName)) {
        paramMap = map;
        break;
      }
    }

    try {
      if (annot.required()) result = ZKHelper.getRequiredParameter(paramMap, paramName, clazz);
      else if (!paramMap.containsKey(paramName)) return; // optional parameter doesn't do anything
      else result = ZKHelper.getOptionalParameter(paramMap, paramName, clazz, null);
    } catch (IllegalArgumentException ex) {
      throw new IllegalArgumentException(
          "@ZkParameter(name='"
              + paramName
              + "', "
              + "required="
              + (annot.required() ? "true" : "false")
              + ", "
              + "createIfNull="
              + (annot.createIfNull() ? "true" : "false")
              + "): "
              + ex.getLocalizedMessage());
    }

    if (result == null && annot.createIfNull())
      try {
        result = (T) clazz.newInstance();
      } catch (InstantiationException ex) {
        throw new InstantiationError(
            "@ZkParameter(name='"
                + paramName
                + "', "
                + "required="
                + (annot.required() ? "true" : "false")
                + ", "
                + "createIfNull="
                + (annot.createIfNull() ? "true" : "false")
                + ") - Parameter is null, unable to create new object with error: "
                + ex.getLocalizedMessage());
      } catch (IllegalAccessException ex) {
        throw new InstantiationError(
            "@ZkParameter(name='"
                + paramName
                + "', "
                + "required="
                + (annot.required() ? "true" : "false")
                + ", "
                + "createIfNull="
                + (annot.createIfNull() ? "true" : "false")
                + ") - Parameter is null, unable to create new object, no public default constructor: "
                + ex.getLocalizedMessage());
      }

    if (field != null)
      try {
        field.setAccessible(true);
        field.set(instance, result);
        field.setAccessible(false);
      } catch (IllegalArgumentException ex) {
        throw new IllegalArgumentException(
            "@ZkParameter(name='"
                + paramName
                + "', "
                + "required="
                + (annot.required() ? "true" : "false")
                + ", "
                + "createIfNull="
                + (annot.createIfNull() ? "true" : "false")
                + ") - Unable to set new value of field to '"
                + result
                + "': "
                + ex.getLocalizedMessage(),
            ex);
      } catch (IllegalAccessException ex) {
        throw SystemException.Aide.wrap(ex);
      }
    if (method != null)
      try {
        method.setAccessible(true);
        method.invoke(instance, new Object[] {result});
        method.setAccessible(false);
      } catch (IllegalAccessException ex) {
        throw SystemException.Aide.wrap(ex);
      } catch (IllegalArgumentException ex) {
        throw new IllegalArgumentException(
            "@ZkParameter(name='"
                + paramName
                + "', "
                + "required="
                + (annot.required() ? "true" : "false")
                + ", "
                + "createIfNull="
                + (annot.createIfNull() ? "true" : "false")
                + ") - Unable to set new value of method to '"
                + result
                + "': "
                + ex.getClass()
                + " - "
                + ex.getLocalizedMessage(),
            ex);
      } catch (InvocationTargetException ex) {
        throw new IllegalArgumentException(
            "@ZkParameter(name='"
                + paramName
                + "', "
                + "required="
                + (annot.required() ? "true" : "false")
                + ", "
                + "createIfNull="
                + (annot.createIfNull() ? "true" : "false")
                + ") - Unable to set new value of method to '"
                + result
                + "', error in method invocation: "
                + ex.getClass()
                + " - "
                + (ex.getTargetException() == null
                    ? ex.getLocalizedMessage()
                    : ex.getTargetException().getLocalizedMessage()),
            ex);
      }
  }