mirror of
https://github.com/balkian/SOJA.git
synced 2025-10-23 03:38:18 +00:00
Initial commit
This commit is contained in:
87
src/java/es/upm/dit/gsi/jason/utils/CollectionUtils.java
Normal file
87
src/java/es/upm/dit/gsi/jason/utils/CollectionUtils.java
Normal file
@@ -0,0 +1,87 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package es.upm.dit.gsi.jason.utils;
|
||||
|
||||
import jason.asSyntax.Literal;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
|
||||
/**
|
||||
*
|
||||
* Project: Web40SOJason
|
||||
* Package: es.upm.dit.gsi.jason.utils
|
||||
* Class: CollectionUtils
|
||||
*
|
||||
* @author Miguel Coronado (miguelcb@dit.upm.es)
|
||||
* @version Mar 9, 2012
|
||||
*
|
||||
*/
|
||||
public abstract class CollectionUtils {
|
||||
|
||||
/**
|
||||
* This wraps a Literal in a collection
|
||||
* @param literal The literal
|
||||
* @return Collection containing the literal given
|
||||
*/
|
||||
public static Collection<Literal> wrapList(Literal literal) {
|
||||
Collection<Literal> res = new LinkedList<Literal>();
|
||||
res.add(literal);
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* This wraps a Literal in a collection
|
||||
* @param literal The string that represents a literal
|
||||
* @return Collection containing the literal given
|
||||
*/
|
||||
public static Collection<Literal> wrapList(String literal) {
|
||||
Collection<Literal> res = new LinkedList<Literal>();
|
||||
res.add(Literal.parseLiteral(literal));
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* This
|
||||
* @param collection
|
||||
* @return
|
||||
*/
|
||||
public static String[] toStringArray (Collection<? extends Object> collection){
|
||||
String[] strArray = new String[collection.size()];
|
||||
|
||||
int index = 0;
|
||||
for(Object obj : collection){
|
||||
if(obj == null) {
|
||||
strArray[index] = "null";
|
||||
}
|
||||
else {
|
||||
strArray[index] = obj.toString();
|
||||
}
|
||||
index++;
|
||||
}
|
||||
|
||||
return strArray;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param items
|
||||
* @return
|
||||
*/
|
||||
public static String[] toStringArray (Object[] items){
|
||||
String[] strArray = new String[items.length];
|
||||
|
||||
for(int index = 0; index < items.length; index++){
|
||||
Object obj = items[index];
|
||||
if(obj == null){
|
||||
strArray[index] = "null";
|
||||
}
|
||||
else{
|
||||
strArray[index] = items[index].toString();
|
||||
}
|
||||
}
|
||||
|
||||
return strArray;
|
||||
}
|
||||
}
|
83
src/java/es/upm/dit/gsi/jason/utils/NotationUtils.java
Normal file
83
src/java/es/upm/dit/gsi/jason/utils/NotationUtils.java
Normal file
@@ -0,0 +1,83 @@
|
||||
package es.upm.dit.gsi.jason.utils;
|
||||
|
||||
|
||||
/**
|
||||
* This Utils class is used to validate string according to the Jason atom
|
||||
* notation criteria, and transform an invalid notation into a valid one
|
||||
* and vice versa.
|
||||
*
|
||||
* This is useful in some context where the agents need to interact with an
|
||||
* uncontrollable environment such as the Web.
|
||||
*
|
||||
* In Jason notation, white-spaces are not allowed, neither, words that starts
|
||||
* with capital letter.
|
||||
*
|
||||
* @author gsi.dit.upm.es
|
||||
*
|
||||
*/
|
||||
public class NotationUtils {
|
||||
|
||||
/**
|
||||
* @param toCheck
|
||||
* @return
|
||||
*/
|
||||
public static boolean isValidAtom (String toCheck) {
|
||||
String lowerCase = toCheck.toLowerCase();
|
||||
return !toCheck.contains(" ") && !toCheck.contains(",") && toCheck.equals(lowerCase);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param toCheck
|
||||
* @return
|
||||
*/
|
||||
public static boolean isCompactable (String toCheck) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param str
|
||||
* @return
|
||||
*/
|
||||
public static String compact(String str) {
|
||||
|
||||
if (isValidAtom (str)) {return str;}
|
||||
if (!isCompactable(str)) {return null;}
|
||||
|
||||
str = str.replace("_", "___");
|
||||
str = str.replace(" ", "_");
|
||||
str = str.replace("ñ", "n");
|
||||
return str.toLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param str
|
||||
* @return
|
||||
*/
|
||||
public static String uncompact(String str) {
|
||||
str = str.replace("___", "#");
|
||||
str = str.replace("_", " ");
|
||||
str = str.replace("#", " ");
|
||||
return str;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>This removes the quotation mark from the string given. If that
|
||||
* string has no quotation marks it returned trimmed.</p>
|
||||
*
|
||||
* <p>The quotation marks are only removed from the beginning and the
|
||||
* end of the string, so any quotation mark inserted in the middle of
|
||||
* the string will be kept.</p>
|
||||
*
|
||||
* @return the string without the quotation marks
|
||||
*/
|
||||
public static String removeQuotation (String str) {
|
||||
String message = str.trim();
|
||||
if(message.startsWith("\"")) message = message.substring(1);
|
||||
if(message.endsWith("\"")) message = message.substring(0, message.length()-1);
|
||||
return message;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user