下面列出了org.hibernate.type.BlobType#com.jstarcraft.core.utility.StringUtility 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public String getBeginContent() {
if (AwkOperator.IN.equals(operator)) {
StringBuilder buffer = new StringBuilder();
int length = Array.getLength(value);
for (int index = 0; index < length; index++) {
Object element = Array.get(value, index);
buffer.append(property);
buffer.append("[");
buffer.append(index);
buffer.append("]=");
buffer.append(element);
buffer.append(";");
}
return buffer.toString();
} else {
return StringUtility.EMPTY;
}
}
@Test
public void testSegmenter() throws Exception {
Tokenizer segmenter = getSegmenter();
String text = "中华人民共和国(People's Republic of China),简称'中国'";
segmenter.setReader(new StringReader(text));
segmenter.reset();
while (segmenter.incrementToken()) {
// 词元
CharTermAttribute term = segmenter.getAttribute(CharTermAttribute.class);
// 偏移量
OffsetAttribute offset = segmenter.getAttribute(OffsetAttribute.class);
// 距离
PositionIncrementAttribute position = segmenter.getAttribute(PositionIncrementAttribute.class);
// 词性
TypeAttribute type = segmenter.getAttribute(TypeAttribute.class);
LOGGER.debug(StringUtility.format("segmenter:term is {}, begin is {}, end is {}", term, offset.startOffset(), offset.endOffset()));
Assert.assertEquals(term.toString().toLowerCase(), text.substring(offset.startOffset(), offset.endOffset()).toLowerCase());
}
}
private int[] getRange(String field, int from, int to) {
int[] range = new int[2];
if (field.contains(StringUtility.ASTERISK)) {
// 处理星符
range[0] = from;
range[1] = to - 1;
} else {
// 处理连接符
if (!field.contains(StringUtility.DASH)) {
range[0] = range[1] = field.startsWith("L") ? -Integer.valueOf(field.substring(1)) : Integer.valueOf(field);
} else {
String[] split = field.split(StringUtility.DASH);
if (split.length > 2) {
throw new IllegalArgumentException("Range has more than two fields: '" + field + "' in expression \"" + this.expression + "\"");
}
range[0] = split[0].startsWith("L") ? -Integer.valueOf(split[0].substring(1)) : Integer.valueOf(split[0]);
range[1] = split[1].startsWith("L") ? -Integer.valueOf(split[1].substring(1)) : Integer.valueOf(split[1]);
}
}
return range;
}
@Override
protected void readoutParameters() {
float value;
float sumAlpha = alpha.getSum(false);
for (int userIndex = 0; userIndex < userSize; userIndex++) {
for (int topicIndex = 0; topicIndex < factorSize; topicIndex++) {
value = (userTopicTimes.getValue(userIndex, topicIndex) + alpha.getValue(topicIndex)) / (userTokenNumbers.getValue(userIndex) + sumAlpha);
userTopicSums.shiftValue(userIndex, topicIndex, value);
}
}
for (int topicIndex = 0; topicIndex < factorSize; topicIndex++) {
float betaTopicValue = beta.getRowVector(topicIndex).getSum(false);
for (int previousItemIndex = 0; previousItemIndex < itemSize + 1; previousItemIndex++) {
for (int nextItemIndex = 0; nextItemIndex < itemSize; nextItemIndex++) {
value = (topicItemBigramTimes[topicIndex][previousItemIndex][nextItemIndex] + beta.getValue(topicIndex, previousItemIndex)) / (topicItemProbabilities.getValue(topicIndex, previousItemIndex) + betaTopicValue);
topicItemBigramSums[topicIndex][previousItemIndex][nextItemIndex] += value;
}
}
}
if (logger.isInfoEnabled()) {
String message = StringUtility.format("sumAlpha is {}", sumAlpha);
logger.info(message);
}
numberOfStatistics++;
}
public TermExpression(String expression) {
super(expression);
this.seconds = new BitSet(60);
this.minutes = new BitSet(60);
this.hours = new BitSet(24);
this.terms = new BitSet(24);
this.years = new BitSet(200);
String[] fields = expression.split(StringUtility.SPACE);
if (fields.length != 4 && fields.length != 5) {
throw new IllegalArgumentException();
} else {
this.setBits(this.seconds, fields[0], 0, 60, 0);
this.setBits(this.minutes, fields[1], 0, 60, 0);
this.setBits(this.hours, fields[2], 0, 24, 0);
this.setBits(this.terms, this.replaceOrdinals(fields[3]), 0, 24, 0);
if (fields.length == 5) {
this.setBits(this.years, fields[4], 1900, 2100, 1900);
} else {
this.setBits(this.years, StringUtility.ASTERISK, 1900, 2100, 1900);
}
}
}
/**
* user(离散:1:稠密)-profile(连续:n:稀疏)
*
* <pre>
* 可以当作user(离散:1:稠密)-user(离散:1:稠密)-degree(连续:1:稠密)
* </pre>
*
* >
*/
@Test
public void mockUserProfile() throws Exception {
File file = new File("data/mock/user-profile");
FileUtils.deleteQuietly(file);
file.getParentFile().mkdirs();
file.createNewFile();
StringBuilder buffer = new StringBuilder();
try (FileWriter writer = new FileWriter(file); BufferedWriter out = new BufferedWriter(writer);) {
for (int leftIndex = 0; leftIndex < userSize; leftIndex++) {
buffer.setLength(0);
for (int rightIndex = 0; rightIndex < profileSize; rightIndex++) {
if (RandomUtility.randomFloat(1F) < ratio) {
float degree = RandomUtility.randomFloat(profileScope);
buffer.append(degree);
}
buffer.append(" ");
}
String profile = buffer.substring(0, buffer.length() - 1);
out.write(StringUtility.format("{} {}", leftIndex, profile));
out.newLine();
}
}
}
/**
* item(离散:1:稠密)-profile(连续:n:稀疏)
*
* <pre>
* 可以当作item(离散:1:稠密)-item(离散:1:稠密)-degree(连续:1:稠密)
* </pre>
*/
@Test
public void mockItemProfile() throws Exception {
File file = new File("data/mock/item-profile");
FileUtils.deleteQuietly(file);
file.getParentFile().mkdirs();
file.createNewFile();
StringBuilder buffer = new StringBuilder();
try (FileWriter writer = new FileWriter(file); BufferedWriter out = new BufferedWriter(writer);) {
for (int leftIndex = 0; leftIndex < itemSize; leftIndex++) {
buffer.setLength(0);
for (int rightIndex = 0; rightIndex < profileSize; rightIndex++) {
if (RandomUtility.randomFloat(1F) < ratio) {
float degree = RandomUtility.randomFloat(profileScope);
buffer.append(degree);
}
buffer.append(" ");
}
String profile = buffer.substring(0, buffer.length() - 1);
out.write(StringUtility.format("{} {}", leftIndex, profile));
out.newLine();
}
}
}
@Override
public Object readValueFrom(ProtocolReader context, Type type, ClassDefinition definition) throws IOException {
InputStream in = context.getInputStream();
byte information = (byte) in.read();
byte mark = getMark(information);
if (mark == NULL_MARK) {
return null;
}
if (mark == FALSE_MARK) {
if (type == AtomicBoolean.class) {
return new AtomicBoolean(false);
}
return false;
} else if (mark == TRUE_MARK) {
if (type == AtomicBoolean.class) {
return new AtomicBoolean(true);
}
return true;
}
String message = StringUtility.format("类型码[{}]没有对应标记码[{}]", type, mark);
throw new CodecConvertionException(message);
}
@Test
public void testCodec() throws Exception {
EnvironmentContext context = EnvironmentFactory.getContext();
Future<?> task = context.doTask(() -> {
// 维度设置为100,可以测试编解码的效率.
int dimension = 100;
MathMatrix oldMatrix = getRandomMatrix(dimension);
for (ModemCodec codec : ModemCodec.values()) {
long encodeInstant = System.currentTimeMillis();
byte[] data = codec.encodeModel(oldMatrix);
String encodeMessage = StringUtility.format("编码{}数据的时间:{}毫秒", codec, System.currentTimeMillis() - encodeInstant);
logger.info(encodeMessage);
long decodeInstant = System.currentTimeMillis();
MathMatrix newMatrix = (MathMatrix) codec.decodeModel(data);
String decodeMessage = StringUtility.format("解码{}数据的时间:{}毫秒", codec, System.currentTimeMillis() - decodeInstant);
logger.info(decodeMessage);
Assert.assertThat(newMatrix, CoreMatchers.equalTo(oldMatrix));
}
});
task.get();
}
public WeightLayer(int numberOfInputs, int numberOfOutputs, MathCache factory, Map<String, ParameterConfigurator> configurators, ActivationFunction function) {
super(numberOfInputs, numberOfOutputs, configurators, function);
if (!this.configurators.containsKey(WEIGHT_KEY)) {
String message = StringUtility.format("参数{}配置缺失.", WEIGHT_KEY);
throw new IllegalArgumentException(message);
}
MathMatrix weightParameter = factory.makeMatrix(numberOfInputs, numberOfOutputs);
configurators.get(WEIGHT_KEY).getFactory().setValues(weightParameter);
this.parameters.put(WEIGHT_KEY, weightParameter);
MathMatrix weightGradient = factory.makeMatrix(numberOfInputs, numberOfOutputs);
this.gradients.put(WEIGHT_KEY, weightGradient);
if (this.configurators.containsKey(BIAS_KEY)) {
MathMatrix biasParameter = factory.makeMatrix(1, numberOfOutputs);
configurators.get(BIAS_KEY).getFactory().setValues(biasParameter);
this.parameters.put(BIAS_KEY, biasParameter);
MathMatrix biasGradient = factory.makeMatrix(1, numberOfOutputs);
this.gradients.put(BIAS_KEY, biasGradient);
}
}
@Override
public <T> T doWith(Class<T> clazz, Object... arguments) {
try {
LuaHolder holder = threadHolder.get();
for (int index = 0, size = classes.length; index < size; index++) {
holder.attributes.put(StringUtility.format("argument{}", index), arguments[index]);
}
holder.attributes.putAll(holder.scope.getAttributes());
CompiledScript script = holder.script;
T object = (T) script.eval();
holder.scope.deleteAttributes();
return object;
} catch (ScriptException exception) {
throw new ScriptExpressionException(exception);
}
}
/**
* DES加密
*
* @param data
* @param key
* @return
*/
public static byte[] encryptDes(byte[] data, byte[] key) {
try {
// DES算法要求有一个可信任的随机数源
SecureRandom random = new SecureRandom();
DESKeySpec desKey = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey secretKey = keyFactory.generateSecret(desKey);
Cipher cipher = Cipher.getInstance(DES);
cipher.init(Cipher.ENCRYPT_MODE, secretKey, random);
return cipher.doFinal(data);
} catch (Exception exception) {
String message = StringUtility.format("DES加密数据异常:[{}]", Arrays.toString(data));
throw new RuntimeException(message, exception);
}
}
@Override
public void registerMonitor(Set<Class> types, EventMonitor monitor) {
try {
for (Class type : types) {
EventManager manager = managers.get(type);
if (manager == null) {
manager = new EventManager();
managers.put(type, manager);
address2Classes.put(type.getName(), type);
// TODO 需要防止路径冲突
CountDownLatch latch = new CountDownLatch(1);
session.subscribe(name + StringUtility.DOT + type.getName(), MqttQoS.AT_MOST_ONCE.value(), (subscribe) -> {
latch.countDown();
});
latch.await();
}
manager.attachMonitor(monitor);
}
} catch (Exception exception) {
throw new RuntimeException(exception);
}
}
private void testPerformance(ContentCodec contentCodec, Type type, Object instance) {
byte[] data = contentCodec.encode(type, instance);
String message = StringUtility.format("格式化{}大小:{},{}", type.getTypeName(), data.length, Arrays.toString(data));
logger.debug(message);
Instant now = null;
int times = 100000;
now = Instant.now();
for (int index = 0; index < times; index++) {
contentCodec.encode(type, instance);
}
logger.debug(StringUtility.format("编码{}次一共消耗{}毫秒.", times, System.currentTimeMillis() - now.toEpochMilli()));
now = Instant.now();
for (int index = 0; index < times; index++) {
contentCodec.decode(type, data);
}
logger.debug(StringUtility.format("解码{}次一共消耗{}毫秒.", times, System.currentTimeMillis() - now.toEpochMilli()));
}
public MyBatisAccessor(Collection<Class<?>> classes, SqlSessionTemplate template) {
this.template = template;
Configuration configuration = template.getConfiguration();
for (Class clazz : classes) {
if (!configuration.hasMapper(clazz)) {
configuration.addMapper(clazz);
}
MyBatisMetadata metadata = new MyBatisMetadata(clazz);
metadatas.put(metadata.getOrmClass(), metadata);
String maximumIdSql = StringUtility.format(MAXIMUM_ID, metadata.getColumnName(metadata.getPrimaryName()));
maximumIdSqls.put(metadata.getOrmClass(), maximumIdSql);
String minimumIdSql = StringUtility.format(MINIMUM_ID, metadata.getColumnName(metadata.getPrimaryName()));
minimumIdSqls.put(metadata.getOrmClass(), minimumIdSql);
}
}
public Neo4jAccessor(SessionFactory factory) {
this.template = factory.openSession();
MetaData metaData = factory.metaData();
for (ClassInfo information : metaData.persistentEntities()) {
Class<?> ormClass = information.getUnderlyingClass();
NodeEntity node = ormClass.getAnnotation(NodeEntity.class);
RelationshipEntity relation = ormClass.getAnnotation(RelationshipEntity.class);
if (node == null && relation == null) {
continue;
}
Neo4jMetadata metadata = new Neo4jMetadata(ormClass);
metadatas.put(ormClass, metadata);
String ormName = metadata.getOrmName();
String idName = metadata.getPrimaryName();
String deletecCql = StringUtility.format(DELETE_CQL, ormName, idName);
deleteCqls.put(ormClass, deletecCql);
String maximumIdCql = StringUtility.format(MAXIMUM_ID, ormName, idName, idName, idName);
maximumIdCqls.put(ormClass, maximumIdCql);
String minimumIdCql = StringUtility.format(MINIMUM_ID, ormName, idName, idName, idName);
minimumIdCqls.put(ormClass, minimumIdCql);
}
}
@Override
public synchronized CommunicationSession<Channel> open(InetSocketAddress address, long wait) {
if (sessionManager.getSession(address) != null) {
throw new CommunicationException();
}
try {
ChannelFuture future = connector.connect(address);
future.sync();
Channel channel = future.channel();
channels.put(address, channel);
return sessionManager.attachSession(address, channel);
} catch (Throwable throwable) {
String message = StringUtility.format("客户端异常");
LOGGER.error(message, throwable);
throw new CommunicationException();
}
}
@Override
public void triggerEvent(Object event) {
Class type = event.getClass();
byte[] bytes = codec.encode(type, event);
switch (mode) {
case QUEUE: {
// TODO 需要防止路径冲突
bus.send(name + StringUtility.DOT + type.getName(), bytes);
break;
}
case TOPIC: {
// TODO 需要防止路径冲突
bus.publish(name + StringUtility.DOT + type.getName(), bytes);
break;
}
}
}
@Override
public PersistenceElement deleteInstance(Comparable cacheId) {
PersistenceElement element = new PersistenceElement(PersistenceOperation.DELETE, cacheId, null);
Exception exception = null;
Lock writeLock = lock.writeLock();
try {
writeLock.lock();
accessor.deleteInstance(cacheClass, element.getCacheId());
deletedCount.incrementAndGet();
} catch (Exception throwable) {
String message = StringUtility.format("立即策略[{}]处理元素[{}]时异常", new Object[] { name, element });
LOGGER.error(message, throwable);
exception = throwable;
exceptionCount.incrementAndGet();
} finally {
writeLock.unlock();
}
if (monitor != null) {
monitor.notifyOperate(element.getOperation(), element.getCacheId(), element.getCacheObject(), exception);
}
return element;
}
@Bean("movieItems")
List<MovieItem> getItems(DataSpace movieDataSpace) throws Exception {
File movieItemFile = new File("data/ml-100k/u.item");
List<MovieItem> items = new LinkedList<>();
QualityAttribute<Integer> itemAttribute = movieDataSpace.getQualityAttribute("item");
try (InputStream stream = new FileInputStream(movieItemFile); InputStreamReader reader = new InputStreamReader(stream, StringUtility.CHARSET); BufferedReader buffer = new BufferedReader(reader)) {
try (CSVParser parser = new CSVParser(buffer, CSVFormat.newFormat('|'))) {
Iterator<CSVRecord> iterator = parser.iterator();
while (iterator.hasNext()) {
CSVRecord datas = iterator.next();
// 物品标识
int id = Integer.parseInt(datas.get(0));
// 物品索引
int index = itemAttribute.convertData(id);
// 物品标题
String title = datas.get(1);
// 物品日期
LocalDate date = StringUtility.isEmpty(datas.get(2)) ? LocalDate.of(1970, 1, 1) : LocalDate.parse(datas.get(2), formatter);
MovieItem item = new MovieItem(index, title, date);
items.add(item);
}
}
}
items = new ArrayList<>(items);
return items;
}
@Test
public void testRsa() {
KeyValue<byte[], byte[]> rasKeys = SecurityUtility.getRsa(512);
byte[] data = content.getBytes(StringUtility.CHARSET);
data = SecurityUtility.encryptRsa(data, rasKeys.getValue());
data = SecurityUtility.decryptRsa(data, rasKeys.getKey());
Assert.assertThat(new String(data, StringUtility.CHARSET), CoreMatchers.equalTo(content));
}
/**
* 启动调度器
*
* @param objects
*/
public void start(Collection<Object> objects, int threadSize, int contextWait) {
if (threadSize <= 0) {
throw new CommunicationConfigurationException();
}
if (contextWait <= 0) {
throw new CommunicationConfigurationException();
}
if (!state.compareAndSet(null, CommunicationState.STARTED)) {
throw new CommunicationStateException();
}
for (Object object : objects) {
for (Map<Byte, CommandDefinition> definitions : definitions.values()) {
for (CommandDefinition definition : definitions.values()) {
if (definition.getSide().equals(side) && definition.getClazz().isInstance(object)) {
if (objects.contains(definition)) {
Object newObject = object;
Object oldObject = this.objects.get(definition);
String string = StringUtility.format("新指令对象[{}]与旧指令对象[{}]冲突", newObject, oldObject);
throw new CommunicationDefinitionException(string);
}
this.objects.put(definition, object);
}
}
}
}
for (int index = 0; index < threadSize; index++) {
Thread dispatchThread = dispatchFactory.newThread(dispatchTask);
dispatchThread.setDaemon(true);
dispatchThread.start();
}
}
@Override
public void registerMonitor(Set<Class> types, EventMonitor monitor) {
try {
for (Class type : types) {
EventManager manager = managers.get(type);
if (manager == null) {
manager = new EventManager();
managers.put(type, manager);
Map<String, String> metadatas = new HashMap<>();
// TODO 需要防止路径冲突
String address = name + StringUtility.DOT + type.getName();
metadatas.put("destination", address);
switch (mode) {
case QUEUE: {
// Artemis特定的协议
metadatas.put("destination-type", "ANYCAST");
break;
}
case TOPIC: {
// Artemis特定的协议
metadatas.put("destination-type", "MULTICAST");
break;
}
}
EventHandler handler = new EventHandler(type, manager);
session.subscribe(address, metadatas, handler);
}
manager.attachMonitor(monitor);
}
} catch (Exception exception) {
throw new RuntimeException(exception);
}
}
@Test
public void testQueue() throws Exception {
int size = 5;
DefaultMQPushConsumer[] consumers = new DefaultMQPushConsumer[size];
for (int index = 0; index < size; index++) {
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("queue");
consumer.setInstanceName("queueConsumer" + index);
consumer.setNamesrvAddr("localhost:9876");
consumer.setMessageModel(MessageModel.CLUSTERING);
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
consumer.setConsumeMessageBatchMaxSize(1000);
consumer.subscribe("queue-message", "*");
consumer.registerMessageListener(listener);
consumer.start();
consumers[index] = consumer;
}
DefaultMQProducer producer = new DefaultMQProducer("queue");
producer.setNamesrvAddr("localhost:9876");
producer.start();
for (int index = 0; index < 10; index++) {
Message message = new Message("queue-message", "tag", ("RocketMQ 集群模式 " + index).getBytes(StringUtility.CHARSET));
producer.send(message);
}
semaphore.acquire(10);
producer.shutdown();
for (int index = 0; index < size; index++) {
consumers[index].shutdown();
}
}
public CommandDispatcher(ModuleSide side, Collection<CommandDefinition> definitions, SessionReceiver<T> receiver, SessionSender<T> sender, Map<String, CommandStrategy> strategies, long wait) {
this.side = side;
Collection<Type> types = new HashSet<>();
for (CommandDefinition definition : definitions) {
String module = Arrays.toString(definition.getModule());
Map<Byte, CommandDefinition> commands = this.definitions.get(module);
if (commands == null) {
commands = new HashMap<>();
this.definitions.put(module, commands);
}
byte command = definition.getCommand();
if (commands.containsKey(command)) {
CommandDefinition newDefinition = definition;
CommandDefinition oldDefinition = commands.get(command);
String string = StringUtility.format("新指令定义[{}]与旧指令定义[{}]冲突", newDefinition, oldDefinition);
throw new CommunicationDefinitionException(string);
}
commands.put(command, definition);
types.add(definition.getInputDefinition().getContentType());
types.add(definition.getInputDefinition().getInputType());
types.add(definition.getOutputDefinition().getContentType());
types.add(definition.getOutputDefinition().getOutputType());
}
this.receiver = receiver;
this.sender = sender;
this.strategies = strategies;
CodecDefinition codecDefinition = CodecDefinition.instanceOf(types);
Map<Byte, ContentCodec> codecs = new HashMap<>();
codecs.put(MessageFormat.CSV.getMark(), new CsvContentCodec(codecDefinition));
codecs.put(MessageFormat.JSON.getMark(), new JsonContentCodec(codecDefinition));
codecs.put(MessageFormat.KRYO.getMark(), new KryoContentCodec(codecDefinition));
codecs.put(MessageFormat.PROTOCOL_BUFFER_X.getMark(), new ProtocolContentCodec(codecDefinition));
this.codecs = codecs;
this.wait = wait;
Thread expireThread = new Thread(expireTask);
expireThread.setDaemon(true);
expireThread.start();
}
@Override
public Iterable<CoreNlpToken> tokenize(CharSequence text) {
Iterable<CoreLabel> iterator;
if (StringUtility.isBlank(text)) {
// 空格无需分词
iterator = Collections.EMPTY_LIST;
} else {
Annotation annotation = new Annotation(text.toString());
annotator.annotate(annotation);
iterator = annotation.get(CoreAnnotations.TokensAnnotation.class);
}
CoreNlpToken iterable = new CoreNlpToken(iterator.iterator());
return iterable;
}
/**
* 转换指定类
*
* @param clazz
* @return
* @throws Exception
*/
private Class<?> transformClass(final Class<?> clazz) throws Exception {
CtClass proxyClass = proxyClass(clazz);
proxyCacheFields(clazz, proxyClass);
proxyConstructor(clazz, proxyClass);
ReflectionUtility.doWithMethods(clazz, (method) -> {
CacheChange cacheChange = method.getAnnotation(CacheChange.class);
try {
proxyMethod(clazz, proxyClass, method, cacheChange);
} catch (Exception exception) {
String message = StringUtility.format("缓存类型[{}]转换异常", clazz.getName());
throw new CacheException(message, exception);
}
}, (method) -> {
Class<?>[] classes = DEFAULT_METHODS.get(method.getName());
if (classes != null && Arrays.equals(classes, method.getParameterTypes())) {
return false;
}
if (Modifier.isFinal(method.getModifiers()) || Modifier.isStatic(method.getModifiers()) || Modifier.isPrivate(method.getModifiers())) {
return false;
}
if (method.isSynthetic() && method.getName().equals(METHOD_GET_ID)) {
return false;
}
return true;
});
return proxyClass.toClass();
}
public CDAELayer(int numberOfUsers, int numberOfInputs, int numberOfOutputs, MathCache factory, Map<String, ParameterConfigurator> configurators, ActivationFunction function) {
super(numberOfInputs, numberOfOutputs, factory, configurators, function);
this.numberOfUsers = numberOfUsers;
if (!this.configurators.containsKey(USER_KEY)) {
String message = StringUtility.format("参数{}配置缺失.", USER_KEY);
throw new IllegalArgumentException(message);
}
MathMatrix userParameter = factory.makeMatrix(numberOfUsers, numberOfOutputs);
configurators.get(USER_KEY).getFactory().setValues(userParameter);
this.parameters.put(USER_KEY, userParameter);
MathMatrix userGradient = factory.makeMatrix(numberOfUsers, numberOfOutputs);
this.gradients.put(USER_KEY, userGradient);
}
@Override
public Object readValueFrom(CsvReader context, Type type) throws Exception {
// TODO 处理null
Iterator<String> in = context.getInputStream();
String check = in.next();
if (StringUtility.isEmpty(check)) {
return null;
}
int length = Integer.valueOf(check);
Class<?> componentClass = null;
Type componentType = null;
if (type instanceof GenericArrayType) {
GenericArrayType genericArrayType = GenericArrayType.class.cast(type);
componentType = genericArrayType.getGenericComponentType();
componentClass = TypeUtility.getRawType(componentType, null);
} else {
Class<?> clazz = TypeUtility.getRawType(type, null);
componentType = clazz.getComponentType();
componentClass = clazz.getComponentType();
}
Object array = Array.newInstance(componentClass, length);
Specification specification = Specification.getSpecification(componentClass);
CsvConverter converter = context.getCsvConverter(specification);
for (int index = 0; index < length; index++) {
Object element = converter.readValueFrom(context, componentType);
Array.set(array, index, element);
}
return array;
}
@Override
public void registerMonitor(Set<Class> types, EventMonitor monitor) {
try {
for (Class type : types) {
EventManager manager = managers.get(type);
if (manager == null) {
manager = new EventManager();
managers.put(type, manager);
// TODO 需要防止路径冲突
String address = name + StringUtility.DOT + type.getName();
address = address.replace(StringUtility.DOT, StringUtility.DASH);
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(address);
consumer.setInstanceName(name);
consumer.setNamesrvAddr(connections);
consumer.setConsumeMessageBatchMaxSize(1000);
consumer.subscribe(address, "*");
switch (mode) {
case QUEUE: {
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
consumer.setMessageModel(MessageModel.CLUSTERING);
break;
}
case TOPIC: {
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET);
consumer.setMessageModel(MessageModel.BROADCASTING);
break;
}
}
EventHandler handler = new EventHandler(type, manager);
consumer.registerMessageListener(handler);
consumer.start();
consumers.put(type, consumer);
}
manager.attachMonitor(monitor);
}
} catch (Exception exception) {
throw new RuntimeException(exception);
}
}