优先考虑的CDI替代品没有按预期工作?可能的原因是什么?

时间:2017-08-16 07:31:11

标签: java-ee cdi

当我尝试import java.util.Iterator; import java.util.Scanner; import java.util.Stack; public class SolutionStack { Stack st = new Stack(); public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for(int i=0; i<n; i++){ int ch = sc.nextInt(); stackMethod(ch); } } private static void stackMethod(int ch) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); st.size(); System.out.println(st.size()); switch(ch){ case 1: int num = sc.nextInt(); st.push(num); break; case 2: st.pop(); break; case 3 : Iterator<Integer> iterator = st.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } } } } CDIalternatives进行实验时,它正在以相反的方式工作,选择优先级最低的bean而不是最高优先级的bean。优先权的替代方案如何运作?

priority

beans.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" bean-discovery-mode="all"> <alternatives> <class>org.bala.java.jee.cdi.alternatives.priority.entity.RegularGreeting</class> <class>org.bala.java.jee.cdi.alternatives.priority.entity.FancyGreeting</class> <class>org.bala.java.jee.cdi.alternatives.priority.entity.PriorityGreeting</class> </alternatives> </beans>

Greeting.java

package org.bala.java.jee.cdi.alternatives.priority.entity; public interface Greeting { public String greet(final String greet); }

FancyGreeting.java

package org.bala.java.jee.cdi.alternatives.priority.entity; import javax.annotation.Priority; import javax.enterprise.inject.Alternative; import javax.interceptor.Interceptor; @Alternative @Priority(Interceptor.Priority.APPLICATION + 1) public class FancyGreeting implements Greeting { @Override public String greet(String greet) { return "schöner Tag, " + greet; } }

PriorityGreeting.java

package org.bala.java.jee.cdi.alternatives.priority.entity; import javax.annotation.Priority; import javax.enterprise.inject.Alternative; import javax.interceptor.Interceptor; @Alternative @Priority(Interceptor.Priority.APPLICATION + 2) public class PriorityGreeting implements Greeting { @Override public String greet(String greet) { return "der Vorrang, Hello, " + greet; } }

RegularGreeting.java

package org.bala.java.jee.cdi.alternatives.priority.entity; import javax.annotation.Priority; import javax.enterprise.inject.Alternative; import javax.interceptor.Interceptor; @Alternative @Priority(Interceptor.Priority.APPLICATION) public class RegularGreeting implements Greeting { @Override public String greet(String greet) { return "hello " + greet; } }

AlternativesPriorityController.java

package org.bala.java.jee.cdi.alternatives.priority.controller; import javax.enterprise.context.ApplicationScoped; import javax.inject.Inject; import org.bala.java.jee.cdi.alternatives.priority.entity.Greeting; @ApplicationScoped public class AlternativesPriorityController { @Inject Greeting greet; public String getGreet(String string) { return greet.greet(string); } }

AlternativesPriorityResource.java

输出:

package org.bala.java.jee.cdi.alternatives.priority.boundary;

import javax.inject.Inject;
import javax.json.Json;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;

    import javax.ws.rs.core.Response;

    import org.bala.java.jee.cdi.alternatives.priority.controller.AlternativesPriorityController;

    @Path("/alternatives")
    public class AlternativesPriorityResource {

        @Inject
        AlternativesPriorityController controller;

        @Path("/priority")
        @GET
        public Response getGreet() {
            return Response.ok(Json.createObjectBuilder().add("Greet", controller.getGreet("Princess !")).build(), MediaType.APPLICATION_JSON).build();
        }
    }

但是所需的输出是,

{
  "Greet": "hello Princess !"
}

感谢。

根据@BalusC

的建议更新 { "Greet": "der Vorrang, Hello, Princess !" }

beans.xml

beans.xml

1 个答案:

答案 0 :(得分:2)

这是因为您在beans.xml中注册了RegularGreeting,其中@Alternative排名最高。 XML配置始终覆盖注释配置。换句话说,<alternatives>完全被忽略。

只需从beans.xml中删除整个@Alternative部分,然后@Priority就可以与package com.example; import org.apache.commons.math3.linear.MatrixUtils; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.linear.RealVector; public class MyNeuralNet { float learningRate = .7f; int iterations = 10000; int hiddenUnits = 3; double[][] input = new double[4][2];//asdf double[][] knownOp = new double[4][1];//asdf double[][] errorOutputLayer = new double[4][1]; double[][] inputHiddenArray = new double[2][3]; double[][] hiddenOutputArray = new double[3][1]; RealMatrix inputHidden; RealMatrix hiddenOutput; RealMatrix outputSum; RealMatrix outPutResult; RealMatrix hiddenSum; RealMatrix hidderResult; public static void main(String[] args){ System.out.println("Hello! NeuralNet"); MyNeuralNet mind = new MyNeuralNet(); init(mind); for(int i = 0; i < mind.iterations; i++) { //run forward RealMatrix afterFwd = mind.forward(); //calculate the difference between expected and obtained loggResult("afterFwd:: "+afterFwd.toString()); RealMatrix m = MatrixUtils.createRealMatrix(mind.knownOp); RealMatrix n = afterFwd;//MatrixUtils.createRealMatrix(hiddenWeightsOne); // Now subtract m by n RealMatrix errorOutputLayer = m.subtract(n); RealVector errorOutputLayerVector = errorOutputLayer.getColumnVector(0); loggCost("errorOutputLayer: " + errorOutputLayer.toString()); logg(errorOutputLayer.getRowDimension() + " * " + errorOutputLayer.getColumnDimension()); //back propagate mind.backPropagate(errorOutputLayer); } } private static void init(MyNeuralNet mind) { mind.input[0] = new double[]{0,0}; mind.input[1] = new double[]{0,1}; mind.input[2] = new double[]{1,0}; mind.input[3] = new double[]{1,1}; mind.knownOp[0] = new double[]{0}; mind.knownOp[1] = new double[]{0}; mind.knownOp[2] = new double[]{0}; mind.knownOp[3] = new double[]{1}; mind.inputHiddenArray[0] = new double[]{.8f,.4f,.3f}; mind.inputHiddenArray[1] = new double[]{.2f,.9f,.5f}; mind.inputHidden = MatrixUtils.createRealMatrix(mind.inputHiddenArray); mind.hiddenOutputArray[0] = new double[]{.3f}; mind.hiddenOutputArray[1] = new double[]{.5f}; mind.hiddenOutputArray[2] = new double[]{.9f}; mind.hiddenOutput = MatrixUtils.createRealMatrix(mind.hiddenOutputArray); } private RealMatrix forward() { RealMatrix m = MatrixUtils.createRealMatrix(input); // Now multiply m by n hiddenSum = m.multiply(inputHidden); logg("hiddenSum: "+hiddenSum); logg(hiddenSum.getRowDimension() + " * " + hiddenSum.getColumnDimension()); hidderResult = activate(hiddenSum);//MatrixUtils.createRealMatrix(activatedArray); logg("hidderResult: "+ hidderResult); logg(hidderResult.getRowDimension() + " * " + hidderResult.getColumnDimension()); // Now multiply m by n outputSum = hidderResult.multiply(hiddenOutput); logg("outputSum: "+ outputSum); logg(outputSum.getRowDimension() + " * " + outputSum.getColumnDimension()); outPutResult = activate(outputSum);//MatrixUtils.createRealMatrix(activatedArrayOut); logg("outPutResult:: "+outPutResult.toString()); logg(outPutResult.getRowDimension() + " * " + outPutResult.getColumnDimension()); return outPutResult; } private void backPropagate(RealMatrix errorOutputLayerVector) { RealMatrix sigmaPrimeMatrix = activatePrime(outputSum); RealVector sigmaPrimeVector = sigmaPrimeMatrix.getColumnVector(0); com.example.MatrixUtils.getElementWiseProduct(sigmaPrimeMatrix,errorOutputLayerVector); double deltaOutputLayer = sigmaPrimeVector.dotProduct(errorOutputLayerVector.getColumnVector(0)); RealMatrix deltaOutputLayerMatrix = com.example.MatrixUtils.getElementWiseProduct(sigmaPrimeMatrix,errorOutputLayerVector); logg("deltaOutputLayer1: " + deltaOutputLayerMatrix); logg(deltaOutputLayerMatrix.getRowDimension() + " * " + deltaOutputLayerMatrix.getColumnDimension()); RealMatrix hiddenOutputChanges = hidderResult.transpose().multiply(deltaOutputLayerMatrix).scalarMultiply((learningRate)); logg("hiddenOutputChanges: " + hiddenOutputChanges); logg(hiddenOutputChanges.getRowDimension() + " * " + hiddenOutputChanges.getColumnDimension()); RealMatrix sigmaPrime2Matrix = activatePrime(hiddenSum); RealMatrix p2 = deltaOutputLayerMatrix.multiply(hiddenOutput.transpose()); RealMatrix deltaHiddenLayer = com.example.MatrixUtils.getElementWiseProduct(sigmaPrime2Matrix,p2); logg("deltaHiddenLayer: " + deltaHiddenLayer); logg(deltaHiddenLayer.getRowDimension() + " * " + deltaHiddenLayer.getColumnDimension()); RealMatrix inputMatrix = MatrixUtils.createRealMatrix(input); RealMatrix inputHiddenChanges = inputMatrix.transpose().multiply(deltaHiddenLayer).scalarMultiply(learningRate); logg("inputHiddenChanges: " + inputHiddenChanges.toString()); logg(inputHiddenChanges.getRowDimension() + " * " + inputHiddenChanges.getColumnDimension()); } private RealMatrix activatePrime(RealMatrix hiddenSum) { double activatedPrimeArray[][] = new double[hiddenSum.getRowDimension()][hiddenSum.getColumnDimension()]; for (int i = 0; i < hiddenSum.getRowDimension(); i++){ for (int j = 0; j < hiddenSum.getColumnDimension(); j++){ activatedPrimeArray[i][j] = sigmoidPrime(hiddenSum.getEntry(i,j)); } } RealMatrix sigmaPrimeMatrix = MatrixUtils.createRealMatrix(activatedPrimeArray); return sigmaPrimeMatrix; } private RealMatrix activate(RealMatrix hiddenSum) { double activatedPrimeArray[][] = new double[hiddenSum.getRowDimension()][hiddenSum.getColumnDimension()]; for (int i = 0; i < hiddenSum.getRowDimension(); i++){ for (int j = 0; j < hiddenSum.getColumnDimension(); j++){ activatedPrimeArray[i][j] = sigmoid(hiddenSum.getEntry(i,j)); } } RealMatrix sigmaPrimeMatrix = MatrixUtils.createRealMatrix(activatedPrimeArray); return sigmaPrimeMatrix; } private static void loggCost(String str){ System.out.println("COST:::::::::::::::::::::::" +str); // 2 } private static void loggWeights(String str){ System.out.println("Weights:::::::::::::::::::::::" +str); } private static void loggResult(String s) { System.out.println(s); } public static double sigmoid(double x) { return (1/( 1 + Math.pow(Math.E,(-1*x)))); } public static double sigmoidPrime(double x) { return sigmoid(x) * (1 - sigmoid(x)); } private static void logg(String str){ System.out.println(str); } } 一起完成工作。