Java Code Examples for com.jstarcraft.core.common.reflection.ReflectionUtility#getInstance()

The following examples show how to use com.jstarcraft.core.common.reflection.ReflectionUtility#getInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LockableAspect.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
private Object execute(Class<? extends LockableStrategy> clazz, ProceedingJoinPoint point, Signature signature) throws Throwable {
    Method method = ((MethodSignature) signature).getMethod();
    // 获取锁策略
    LockableStrategy strategy = strategies.get(method);
    if (strategy == null) {
        synchronized (method) {
            if (strategy == null) {
                strategy = ReflectionUtility.getInstance(clazz, method);
                strategies.put(method, strategy);
            }
        }
    }
    Object[] arguments = point.getArgs();
    try (Lockable lock = strategy.getLock(arguments)) {
        lock.open();
        return point.proceed(arguments);
    }
}
 
Example 2
Source File: MovieModelConfigurer.java    From jstarcraft-example with Apache License 2.0 5 votes vote down vote up
private Model getModel(Class<? extends Model> clazz, DataSpace dataSpace, DataModule dataModule) throws Exception {
    Model model = ReflectionUtility.getInstance(clazz);
    EnvironmentContext context = EnvironmentFactory.getContext();
    Future<?> task = context.doTask(() -> {
        model.prepare(configuration, dataModule, dataSpace);
        model.practice();
    });
    task.get();
    return model;
}
 
Example 3
Source File: PRankDModel.java    From jstarcraft-rns with Apache License 2.0 5 votes vote down vote up
/**
 * initialization
 *
 * @throws ModelException if error occurs
 */
@Override
public void prepare(Configurator configuration, DataModule model, DataSpace space) {
    super.prepare(configuration, model, space);
    similarityFilter = configuration.getFloat("recommender.sim.filter", 4F);
    float denominator = 0F;
    itemWeights = DenseVector.valueOf(itemSize);
    for (int itemIndex = 0; itemIndex < itemSize; itemIndex++) {
        float numerator = scoreMatrix.getColumnScope(itemIndex);
        denominator = denominator < numerator ? numerator : denominator;
        itemWeights.setValue(itemIndex, numerator);
    }
    // compute item relative importance
    for (int itemIndex = 0; itemIndex < itemSize; itemIndex++) {
        itemWeights.setValue(itemIndex, itemWeights.getValue(itemIndex) / denominator);
    }

    // compute item correlations by cosine similarity
    // TODO 修改为配置枚举
    try {
        Class<MathCorrelation> correlationClass = (Class<MathCorrelation>) Class.forName(configuration.getString("recommender.correlation.class"));
        MathCorrelation correlation = ReflectionUtility.getInstance(correlationClass);
        itemCorrelations = new SymmetryMatrix(scoreMatrix.getColumnSize());
        correlation.calculateCoefficients(scoreMatrix, true, itemCorrelations::setValue);
    } catch (Exception exception) {
        throw new RuntimeException(exception);
    }
}
 
Example 4
Source File: SoRegModel.java    From jstarcraft-rns with Apache License 2.0 5 votes vote down vote up
@Override
public void prepare(Configurator configuration, DataModule model, DataSpace space) {
    super.prepare(configuration, model, space);
    userFactors = DenseMatrix.valueOf(userSize, factorSize);
    userFactors.iterateElement(MathCalculator.SERIAL, (scalar) -> {
        scalar.setValue(RandomUtility.randomFloat(1F));
    });
    itemFactors = DenseMatrix.valueOf(itemSize, factorSize);
    itemFactors.iterateElement(MathCalculator.SERIAL, (scalar) -> {
        scalar.setValue(RandomUtility.randomFloat(1F));
    });

    // TODO 修改为配置枚举
    try {
        Class<MathCorrelation> correlationClass = (Class<MathCorrelation>) Class.forName(configuration.getString("recommender.correlation.class"));
        MathCorrelation correlation = ReflectionUtility.getInstance(correlationClass);
        socialCorrelations = new SymmetryMatrix(socialMatrix.getRowSize());
        correlation.calculateCoefficients(socialMatrix, false, socialCorrelations::setValue);
    } catch (Exception exception) {
        throw new RuntimeException(exception);
    }

    for (MatrixScalar term : socialCorrelations) {
        float similarity = term.getValue();
        if (similarity == 0F) {
            continue;
        }
        similarity = (1F + similarity) / 2F;
        term.setValue(similarity);
    }
}
 
Example 5
Source File: AbstractTask.java    From jstarcraft-rns with Apache License 2.0 5 votes vote down vote up
protected AbstractTask(Class<? extends Model> clazz, Configurator configurator) {
    this.configurator = configurator;
    Long seed = configurator.getLong("recommender.random.seed");
    if (seed != null) {
        RandomUtility.setSeed(seed);
    }
    this.model = (Model) ReflectionUtility.getInstance(clazz);
}
 
Example 6
Source File: QualityProbability.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
public QualityProbability(Class<? extends RandomGenerator> randomClazz, int randomSeed, Class<? extends AbstractIntegerDistribution> distributionClazz, Object... distributionParameters) {
    this.randomSeed = randomSeed;
    this.random = ReflectionUtility.getInstance(randomClazz, randomSeed);
    this.distributionParameters = distributionParameters;
    distributionParameters = ArrayUtility.insert(0, distributionParameters, random);
    this.distribution = ReflectionUtility.getInstance(distributionClazz, distributionParameters);
}
 
Example 7
Source File: QualityProbability.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
public void afterLoad() {
    try {
        random = (RandomGenerator) ReflectionUtility.getInstance(Class.forName(randomClass), randomSeed);
        Object[] parameters = ArrayUtility.insert(0, distributionParameters, random);
        distribution = (AbstractIntegerDistribution) ReflectionUtility.getInstance(Class.forName(distributionClass), parameters);
    } catch (Exception exception) {
    }
}
 
Example 8
Source File: QuantityProbability.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
public QuantityProbability(Class<? extends RandomGenerator> randomClazz, int randomSeed, Class<? extends AbstractRealDistribution> distributionClazz, Object... distributionParameters) {
    this.randomSeed = randomSeed;
    this.random = ReflectionUtility.getInstance(randomClazz, randomSeed);
    this.distributionParameters = distributionParameters;
    distributionParameters = ArrayUtility.insert(0, distributionParameters, random);
    this.distribution = ReflectionUtility.getInstance(distributionClazz, distributionParameters);
}
 
Example 9
Source File: QuantityProbability.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
public void afterLoad() {
    try {
        random = (RandomGenerator) ReflectionUtility.getInstance(Class.forName(randomClass), randomSeed);
        Object[] parameters = ArrayUtility.insert(0, distributionParameters, random);
        distribution = (AbstractRealDistribution) ReflectionUtility.getInstance(Class.forName(distributionClass), parameters);
    } catch (Exception exception) {
    }
}
 
Example 10
Source File: ItemKNNModel.java    From jstarcraft-rns with Apache License 2.0 4 votes vote down vote up
@Override
public void prepare(Configurator configuration, DataModule model, DataSpace space) {
    super.prepare(configuration, model, space);
    neighborSize = configuration.getInteger("recommender.neighbors.knn.number", 50);
    // TODO 设置容量
    itemNeighbors = new MathVector[itemSize];
    Neighborhood<Integer2FloatKeyValue>[] knns = new Neighborhood[itemSize];
    for (int itemIndex = 0; itemIndex < itemSize; itemIndex++) {
        knns[itemIndex] = new Neighborhood<>(neighborSize, comparator);
    }
    // TODO 修改为配置枚举
    try {
        Class<MathCorrelation> correlationClass = (Class<MathCorrelation>) Class.forName(configuration.getString("recommender.correlation.class"));
        MathCorrelation correlation = ReflectionUtility.getInstance(correlationClass);
        correlation.calculateCoefficients(scoreMatrix, true, (leftIndex, rightIndex, coefficient) -> {
            if (leftIndex == rightIndex) {
                return;
            }
            // 忽略相似度为0的物品
            if (coefficient == 0F) {
                return;
            }
            knns[leftIndex].updateNeighbor(new Integer2FloatKeyValue(rightIndex, coefficient));
            knns[rightIndex].updateNeighbor(new Integer2FloatKeyValue(leftIndex, coefficient));
        });
    } catch (Exception exception) {
        throw new RuntimeException(exception);
    }
    for (int itemIndex = 0; itemIndex < itemSize; itemIndex++) {
        itemNeighbors[itemIndex] = getNeighborVector(knns[itemIndex].getNeighbors());
    }

    itemMeans = DenseVector.valueOf(itemSize);

    userVectors = new SparseVector[userSize];
    for (int userIndex = 0; userIndex < userSize; userIndex++) {
        userVectors[userIndex] = scoreMatrix.getRowVector(userIndex);
    }

    itemVectors = new SparseVector[itemSize];
    for (int itemIndex = 0; itemIndex < itemSize; itemIndex++) {
        itemVectors[itemIndex] = scoreMatrix.getColumnVector(itemIndex);
    }
}
 
Example 11
Source File: UserKNNModel.java    From jstarcraft-rns with Apache License 2.0 4 votes vote down vote up
@Override
public void prepare(Configurator configuration, DataModule model, DataSpace space) {
    super.prepare(configuration, model, space);
    neighborSize = configuration.getInteger("recommender.neighbors.knn.number");
    // TODO 设置容量
    userNeighbors = new MathVector[userSize];
    Neighborhood<Integer2FloatKeyValue>[] knns = new Neighborhood[userSize];
    for (int userIndex = 0; userIndex < userSize; userIndex++) {
        knns[userIndex] = new Neighborhood<>(neighborSize, comparator);
    }
    // TODO 修改为配置枚举
    try {
        Class<MathCorrelation> correlationClass = (Class<MathCorrelation>) Class.forName(configuration.getString("recommender.correlation.class"));
        MathCorrelation correlation = ReflectionUtility.getInstance(correlationClass);
        correlation.calculateCoefficients(scoreMatrix, false, (leftIndex, rightIndex, coefficient) -> {
            if (leftIndex == rightIndex) {
                return;
            }
            // 忽略相似度为0的物品
            if (coefficient == 0F) {
                return;
            }
            knns[leftIndex].updateNeighbor(new Integer2FloatKeyValue(rightIndex, coefficient));
            knns[rightIndex].updateNeighbor(new Integer2FloatKeyValue(leftIndex, coefficient));
        });
    } catch (Exception exception) {
        throw new RuntimeException(exception);
    }
    for (int userIndex = 0; userIndex < userSize; userIndex++) {
        userNeighbors[userIndex] = getNeighborVector(knns[userIndex].getNeighbors());
    }

    userMeans = DenseVector.valueOf(userSize);

    userVectors = new SparseVector[userSize];
    for (int userIndex = 0; userIndex < userSize; userIndex++) {
        userVectors[userIndex] = scoreMatrix.getRowVector(userIndex);
    }

    itemVectors = new SparseVector[itemSize];
    for (int itemIndex = 0; itemIndex < itemSize; itemIndex++) {
        itemVectors[itemIndex] = scoreMatrix.getColumnVector(itemIndex);
    }
}