Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ core/.externalToolBuilders/Maven_Ant_Builder.launch
core/maven-eclipse.xml
service/.classpath
web/.classpath
storage/lib
storage/.classpath
storage/.externalToolBuilders/Maven_Ant_Builder.launch
storage/.gitignore
storage/maven-eclipse.xml
storage/target
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.sdap.mudrod</groupId>
<groupId>org.apache.sdap</groupId>
<artifactId>mudrod-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../</relativePath>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@
import java.util.*;

/**
* Recommend metadata using combination all two methods, including content-based
* similarity and session-level similarity
* Recommend metadata using combination of content-based
* similarity and session-level similarity.
*/
public class HybridRecommendation extends DiscoveryStepAbstract {
/**
*
*/

private static final long serialVersionUID = 1L;
// recommended metadata list
protected transient List<LinkedTerm> termList = new ArrayList<>();
Expand All @@ -50,17 +48,17 @@ public class HybridRecommendation extends DiscoveryStepAbstract {
private static final String WEIGHT = "weight";

/**
* recommended data class Date: Sep 12, 2016 2:25:28 AM
* recommended data class
*/
class LinkedTerm {
public String term = null;
public double weight = 0;
public String model = null;
private String term;
private double weight = 0;
private String model;

public LinkedTerm(String str, double w, String m) {
term = str;
weight = w;
model = m;
this.term = str;
this.weight = w;
this.model = m;
}
}

Expand All @@ -79,7 +77,7 @@ public Object execute(Object o) {
}

/**
* Get recommended data for a giving dataset
* Get recommended data for a given dataset
*
* @param input: a giving dataset
* @param num: the number of recommended dataset
Expand All @@ -104,15 +102,15 @@ public JsonObject getRecomDataInJson(String input, int num) {
JsonElement sessionSimJson = mapToJson(sortedSessionSimMap, num);
resultJson.add("sessionSim", sessionSimJson);

Map<String, Double> hybirdSimMap = new HashMap<String, Double>();
Map<String, Double> hybirdSimMap = new HashMap<>();

for (String name : sortedAbstractSimMap.keySet()) {
hybirdSimMap.put(name, sortedAbstractSimMap.get(name) /** 0.4 */);
hybirdSimMap.put(name, sortedAbstractSimMap.get(name));
}

for (String name : sortedVariableSimMap.keySet()) {
if (hybirdSimMap.get(name) != null) {
double sim = hybirdSimMap.get(name) + sortedVariableSimMap.get(name) /** 0.3 */;
double sim = hybirdSimMap.get(name) + sortedVariableSimMap.get(name);
hybirdSimMap.put(name, Double.parseDouble(df.format(sim)));
} else {
double sim = sortedVariableSimMap.get(name);
Expand All @@ -122,7 +120,7 @@ public JsonObject getRecomDataInJson(String input, int num) {

for (String name : sortedSessionSimMap.keySet()) {
if (hybirdSimMap.get(name) != null) {
double sim = hybirdSimMap.get(name) + sortedSessionSimMap.get(name) /** 0.1 */;
double sim = hybirdSimMap.get(name) + sortedSessionSimMap.get(name);
hybirdSimMap.put(name, Double.parseDouble(df.format(sim)));
} else {
double sim = sortedSessionSimMap.get(name);
Expand Down Expand Up @@ -164,13 +162,11 @@ protected JsonElement mapToJson(Map<String, Double> wordweights, int num) {
}

String nodesJson = gson.toJson(nodes);
JsonElement nodesElement = gson.fromJson(nodesJson, JsonElement.class);

return nodesElement;
return gson.fromJson(nodesJson, JsonElement.class);
}

/**
* Get recommend dataset for a giving dataset
* Get recommend dataset for a given dataset
*
* @param type recommend method
* @param input a giving dataset
Expand Down Expand Up @@ -206,8 +202,12 @@ public Map<String, Double> getRelatedData(String type, String input, int num) {
*/
public List<LinkedTerm> getRelatedDataFromES(String type, String input, int num) {

SearchRequestBuilder builder = es.getClient().prepareSearch(props.getProperty(INDEX_NAME)).setTypes(type).setQuery(QueryBuilders.termQuery("concept_A", input)).addSort(WEIGHT, SortOrder.DESC)
.setSize(num);
SearchRequestBuilder builder = es.getClient()
.prepareSearch(props.getProperty(INDEX_NAME))
.setTypes(type)
.setQuery(QueryBuilders.termQuery("concept_A", input))
.addSort(WEIGHT, SortOrder.DESC)
.setSize(num);

SearchResponse usrhis = builder.execute().actionGet();

Expand Down Expand Up @@ -266,7 +266,6 @@ public static void main(String[] args) throws IOException {
ESDriver es = new ESDriver(me.getConfig());
HybridRecommendation test = new HybridRecommendation(props, es, null);

// String input = "NSCAT_LEVEL_1.7_V2";
String input = "AQUARIUS_L3_SSS_SMIA_MONTHLY-CLIMATOLOGY_V4";
JsonObject json = test.getRecomDataInJson(input, 10);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.sdap.mudrod.utils;

public class ClassLoadingUtils {

private ClassLoadingUtils() {
//Utility Class
}

/**
* Loads a class using the class loader.
* 1. The class loader of the current class is being used.
* 2. The thread context class loader is being used.
* If both approaches fail, returns null.
*
* @param className The name of the class to load.
* @return The class or null if no class loader could load the class.
* @throws ClassNotFoundException if and only if no definition for the class with the specified name could be found.
*/
public static Class<?> loadClass(String className) throws ClassNotFoundException {
return ClassLoadingUtils.loadClass(ClassLoadingUtils.class,className);
}

/**
* Loads a class using the class loader.
* 1. The class loader of the context class is being used.
* 2. The thread context class loader is being used.
* If both approaches fail, returns null.
*
* @param contextClass The name of a context class to use.
* @param className The name of the class to load.
* @return The class or null if no class loader could load the class.
* @throws ClassNotFoundException Aif and only if no definition for the class with the specified name could be found.
*/
public static Class<?> loadClass(Class<?> contextClass, String className) throws ClassNotFoundException {
Class<?> clazz = null;
if (contextClass.getClassLoader() != null) {
clazz = loadClass(className, contextClass.getClassLoader());
}
if (clazz == null && Thread.currentThread().getContextClassLoader() != null) {
clazz = loadClass(className, Thread.currentThread().getContextClassLoader());
}
if (clazz == null) {
throw new ClassNotFoundException("Failed to load class" + className);
}
return clazz;
}

/**
* Loads a {@link Class} from the specified {@link ClassLoader} without throwing {@ClassNotFoundException}.
*
* @param className The name of the class to load.
* @param classLoader Class loader instance where given class to be loaded.
* @return The class or null if no class loader could load the class.
*/
private static Class<?> loadClass(String className, ClassLoader classLoader) {
Class<?> clazz = null;
if (classLoader != null && className != null) {
try {
clazz = classLoader.loadClass(className);
} catch (ClassNotFoundException e) {
//Ignore and return null
}
}
return clazz;
}
}
148 changes: 148 additions & 0 deletions core/src/main/java/org/apache/sdap/mudrod/utils/ReflectionUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.sdap.mudrod.utils;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
* Utility methods related to reflection
*/
public class ReflectionUtils {

public static Class<?>[] EMPTY_CLASS_ARRAY = new Class<?>[0];
public static Object[] EMPTY_OBJECT_ARRAY = new Object[0];

/**
* Returns the empty argument constructor of the class.
*
* @param clazz the class reference of given type <code>T</code>.
* @param <T> class type variable.
* @return constructor for the given class type <code>T</code>.
* @throws SecurityException if the caller's class loader is not the same as the
* class loader of above class.
* @throws NoSuchMethodException default construct cannot by found for given class type.
*/
public static<T> Constructor<T> getConstructor(Class<T> clazz)
throws SecurityException, NoSuchMethodException {
if(clazz == null) {
throw new IllegalArgumentException("class cannot be null");
}
Constructor<T> cons = clazz.getConstructor(EMPTY_CLASS_ARRAY);
cons.setAccessible(true);
return cons;
}

/**
* Returns whether the class defines an empty argument constructor.
*
* @param clazz class reference of given type <code>T</code>.
* @return boolean indicating constructor for the given class type <code>T</code> exist.
* @throws SecurityException if the caller's class loader is not the same as the
* class loader of above class.
* @throws NoSuchMethodException default construct cannot by found for given class type.
*/
public static boolean hasConstructor(Class<?> clazz)
throws SecurityException, NoSuchMethodException {
if(clazz == null) {
throw new IllegalArgumentException("class cannot be null");
}
Constructor<?>[] consts = clazz.getConstructors();

boolean found = false;
for(Constructor<?> cons : consts) {
if(cons.getParameterTypes().length == 0) {
found = true;
}
}

return found;
}

/**
* Constructs a new instance of the class using the no-arg constructor.
*
* @param clazz the class of the object.
* @param <T> class type variable.
* @return a new instance of the object.
* @throws SecurityException if the caller's class loader is not the same as the
* class loader of above class.
* @throws IllegalArgumentException this will not be thrown since <code>field.get(obj)</code> passing obj is null
* since the field is a static class level variable inside the class.
* @throws IllegalAccessException if the field is inaccessible due to java language access control.
* @throws InstantiationException could not be instantiated from the given constructor.
* @throws NoSuchMethodException default construct cannot by found for given class type.
* @throws InvocationTargetException if the underlying constructor throws an exception.
*/
public static <T> T newInstance(Class<T> clazz)
throws InstantiationException, IllegalAccessException,
SecurityException, NoSuchMethodException, IllegalArgumentException,
InvocationTargetException {

Constructor<T> cons = getConstructor(clazz);

return cons.newInstance(EMPTY_OBJECT_ARRAY);
}

/**
* Constructs a new instance of the class using the no-arg constructor.
*
* @param classStr the class name of the object.
* @return a new instance of the object.
* @throws SecurityException if the caller's class loader is not the same as the
* class loader of above class.
* @throws IllegalArgumentException this will not be thrown since <code>field.get(obj)</code> passing obj is null.
* since the field is a static class level variable inside the class.
* @throws IllegalAccessException if the field is inaccessible due to java language access control.
* @throws ClassNotFoundException class definition cannot be found for the class type.
* @throws InstantiationException could not be instantiated from the given constructor.
* @throws NoSuchMethodException default construct cannot by found for given class type.
* @throws InvocationTargetException if the underlying constructor throws an exception.
*/
public static Object newInstance(String classStr)
throws InstantiationException, IllegalAccessException,
ClassNotFoundException, SecurityException, IllegalArgumentException,
NoSuchMethodException, InvocationTargetException {
if(classStr == null) {
throw new IllegalArgumentException("class cannot be null");
}
Class<?> clazz = ClassLoadingUtils.loadClass(classStr);
return newInstance(clazz);
}

/**
* Returns the value of a named static field.
*
* @param clazz the class of the object.
* @param fieldName field name of the instance which value is required.
* @return a new instance of the object.
* @throws SecurityException if the caller's class loader is not the same as the
* class loader of above class.
* @throws NoSuchFieldException if a field with the specified name is not found.
* @throws IllegalArgumentException this will not be thrown since <code>field.get(obj)</code> passing obj is null
* since the field is a static class level variable inside the class.
* @throws IllegalAccessException if the field is inaccessible due to java language access control.
*/
public static Object getStaticField(Class<?> clazz, String fieldName)
throws IllegalArgumentException, SecurityException,
IllegalAccessException, NoSuchFieldException {

return clazz.getField(fieldName).get(null);
}

}
Loading