Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
weka.core.packageManagement.DefaultPackage Maven / Gradle / Ivy
Go to download
The Waikato Environment for Knowledge Analysis (WEKA), a machine
learning workbench. This version represents the developer version, the
"bleeding edge" of development, you could say. New functionality gets added
to this version.
package weka.core.packageManagement;
import java.io.File;
import java.io.Serializable;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
public class DefaultPackage extends Package implements Serializable {
private static final long serialVersionUID = 3643121886457892125L ;
protected File m_packageHome;
protected transient PackageManager m_packageManager;
@Override
public Object clone () {
DefaultPackage newP = null ;
if (m_packageHome != null ) {
newP = new DefaultPackage(new File(m_packageHome.toString()),
m_packageManager);
} else {
newP = new DefaultPackage(null , m_packageManager);
}
HashMap metaData = new HashMap();
Set keys = m_packageMetaData.keySet();
Iterator i = keys.iterator();
while (i.hasNext()) {
Object key = i.next();
Object value = m_packageMetaData.get(key);
metaData.put(key, value);
}
newP.setPackageMetaData(metaData);
return newP;
}
public void setPackageManager (PackageManager p) {
m_packageManager = p;
}
public DefaultPackage (File packageHome, PackageManager manager,
Map packageMetaData) {
this (packageHome, manager);
setPackageMetaData(packageMetaData);
}
public DefaultPackage (File packageHome, PackageManager manager) {
m_packageHome = packageHome;
m_packageManager = manager;
}
@Override
public URL getPackageURL () throws Exception {
String url = getPackageMetaDataElement("PackageURL" ).toString().trim();
URL packageURL = new URL(url);
return packageURL;
}
@Override
public String getName () {
return getPackageMetaDataElement("PackageName" ).toString();
}
protected static String[] splitNameVersion(String nameAndVersion) {
String[] result = new String[3 ];
nameAndVersion = nameAndVersion.trim();
if (nameAndVersion.indexOf('(' ) < 0 ) {
result[0 ] = nameAndVersion;
} else if (nameAndVersion.indexOf(')' ) >= 0 ) {
boolean ok = true ;
result[0 ] = nameAndVersion.substring(0 , nameAndVersion.indexOf('(' ));
result[0 ] = result[0 ].trim();
String secondInequality = null ;
int delimiterIndex = nameAndVersion.indexOf('|' );
if (delimiterIndex >= 0 ) {
secondInequality = nameAndVersion.substring(delimiterIndex + 1 ,
nameAndVersion.length());
secondInequality = secondInequality.trim();
String[] result2 = new String[5 ];
result2[0 ] = result[0 ];
result = result2;
} else {
delimiterIndex = nameAndVersion.length();
}
nameAndVersion = nameAndVersion.substring(
nameAndVersion.indexOf('(' ) + 1 , delimiterIndex);
nameAndVersion = nameAndVersion.trim();
int pos = 1 ;
if (nameAndVersion.charAt(0 ) == '=' ) {
result[1 ] = "=" ;
} else if (nameAndVersion.charAt(1 ) == '=' ) {
pos++;
if (nameAndVersion.charAt(0 ) == '<' ) {
result[1 ] = "<=" ;
} else {
result[1 ] = ">=" ;
}
} else if (nameAndVersion.charAt(0 ) == '<' ) {
result[1 ] = "<" ;
} else if (nameAndVersion.charAt(0 ) == '>' ) {
result[1 ] = ">" ;
} else {
ok = false ;
}
if (ok) {
if (secondInequality != null ) {
delimiterIndex = nameAndVersion.length();
} else {
delimiterIndex = nameAndVersion.indexOf(')' );
}
nameAndVersion = nameAndVersion.substring(pos, delimiterIndex);
result[2 ] = nameAndVersion.trim();
}
if (secondInequality != null ) {
ok = true ;
pos = 1 ;
if (secondInequality.charAt(0 ) == '=' ) {
result[3 ] = "=" ;
} else if (secondInequality.charAt(1 ) == '=' ) {
pos++;
if (secondInequality.charAt(0 ) == '<' ) {
result[3 ] = "<=" ;
} else {
result[3 ] = ">=" ;
}
} else if (secondInequality.charAt(0 ) == '<' ) {
result[3 ] = "<" ;
} else if (secondInequality.charAt(0 ) == '>' ) {
result[3 ] = ">" ;
} else {
ok = false ;
}
if (ok) {
secondInequality = secondInequality.substring(pos,
secondInequality.indexOf(')' ));
result[4 ] = secondInequality.trim();
}
}
}
return result;
}
@Override
public List getDependencies () throws Exception {
List dependencies = new ArrayList();
String dependenciesS = getPackageMetaDataElement("Depends" ).toString();
if (dependenciesS != null ) {
StringTokenizer tok = new StringTokenizer(dependenciesS, "," );
while (tok.hasMoreTokens()) {
String nextT = tok.nextToken().trim();
String[] split = splitNameVersion(nextT);
Package toAdd = null ;
if (!(split[0 ].equalsIgnoreCase(m_packageManager.getBaseSystemName()))) {
toAdd = m_packageManager.getRepositoryPackageInfo(split[0 ], split[2 ]);
if (split.length == 3 ) {
VersionPackageConstraint versionConstraint = new VersionPackageConstraint(
toAdd);
if (split[2 ] == null ) {
versionConstraint
.setVersionConstraint(VersionPackageConstraint.VersionComparison.LESSTHANOREQUAL);
} else {
versionConstraint.setVersionConstraint(split[1 ]);
}
Dependency dep = new Dependency(this , versionConstraint);
dependencies.add(dep);
} else {
VersionRangePackageConstraint versionConstraint = new VersionRangePackageConstraint(
toAdd);
VersionPackageConstraint.VersionComparison comp1 = VersionPackageConstraint
.getVersionComparison(split[1 ]);
VersionPackageConstraint.VersionComparison comp2 = VersionPackageConstraint
.getVersionComparison(split[3 ]);
versionConstraint.setRangeConstraint(split[2 ], comp1, split[4 ],
comp2);
Dependency dep = new Dependency(this , versionConstraint);
dependencies.add(dep);
}
}
}
}
return dependencies;
}
@Override
public List getBaseSystemDependency () throws Exception {
String dependenciesS = getPackageMetaDataElement("Depends" ).toString();
Dependency baseDep = null ;
List baseDeps = new ArrayList();
if (dependenciesS != null ) {
StringTokenizer tok = new StringTokenizer(dependenciesS, "," );
while (tok.hasMoreTokens()) {
String nextT = tok.nextToken().trim();
String[] split = splitNameVersion(nextT);
if ((split[0 ].equalsIgnoreCase(m_packageManager.getBaseSystemName()))) {
Map baseMap = new HashMap();
baseMap.put("PackageName" , "weka" );
split[2 ] = (split[2 ] == null ? "1000.1000.1000" : split[2 ]);
baseMap.put("Version" , split[2 ]);
Package basePackage = new DefaultPackage(null , m_packageManager,
baseMap);
if (split.length == 3 ) {
VersionPackageConstraint baseConstraint = new VersionPackageConstraint(
basePackage);
VersionPackageConstraint.VersionComparison baseComp = VersionPackageConstraint.VersionComparison.LESSTHANOREQUAL;
if (split[1 ] != null ) {
baseComp = VersionPackageConstraint
.getVersionComparison(split[1 ]);
}
baseConstraint.setVersionConstraint(baseComp);
baseDep = new Dependency(this , baseConstraint);
baseDeps.add(baseDep);
} else {
VersionRangePackageConstraint baseConstraint = new VersionRangePackageConstraint(
basePackage);
VersionPackageConstraint.VersionComparison comp1 = VersionPackageConstraint
.getVersionComparison(split[1 ]);
VersionPackageConstraint.VersionComparison comp2 = VersionPackageConstraint
.getVersionComparison(split[3 ]);
baseConstraint.setRangeConstraint(split[2 ], comp1, split[4 ], comp2);
baseDep = new Dependency(this , baseConstraint);
}
}
}
}
if (baseDeps.size() == 0 ) {
throw new Exception("[Package] "
+ getPackageMetaDataElement("PackageName" ).toString()
+ " can't determine what version of the base system is required!!" );
}
return baseDeps;
}
private boolean findPackage (String packageName, List packageList) {
boolean found = false ;
Iterator i = packageList.iterator();
while (i.hasNext()) {
Package p = i.next();
String pName = p.getPackageMetaDataElement("PackageName" ).toString();
if (packageName.equals(pName)) {
found = true ;
break ;
}
}
return found;
}
@Override
public List getMissingDependencies (List packages)
throws Exception {
List missing = new ArrayList();
String dependencies = getPackageMetaDataElement("Depends" ).toString();
if (dependencies != null ) {
StringTokenizer tok = new StringTokenizer(dependencies, "," );
while (tok.hasMoreTokens()) {
String nextT = tok.nextToken().trim();
String[] split = splitNameVersion(nextT);
if (!(split[0 ].equalsIgnoreCase(m_packageManager.getBaseSystemName()))) {
Package tempDep = m_packageManager.getRepositoryPackageInfo(split[0 ],
split[2 ]);
if (!findPackage(split[0 ], packages)) {
VersionPackageConstraint versionConstraint = new VersionPackageConstraint(
tempDep);
if (split[2 ] == null ) {
versionConstraint
.setVersionConstraint(VersionPackageConstraint.VersionComparison.LESSTHANOREQUAL);
missing.add(new Dependency(this , versionConstraint));
} else {
if (split.length == 3 ) {
versionConstraint.setVersionConstraint(split[1 ]);
missing.add(new Dependency(this , versionConstraint));
} else {
VersionRangePackageConstraint versionRConstraint = new VersionRangePackageConstraint(
tempDep);
VersionPackageConstraint.VersionComparison comp1 = VersionPackageConstraint
.getVersionComparison(split[1 ]);
VersionPackageConstraint.VersionComparison comp2 = VersionPackageConstraint
.getVersionComparison(split[3 ]);
versionRConstraint.setRangeConstraint(split[2 ], comp1,
split[4 ], comp2);
missing.add(new Dependency(this , versionRConstraint));
}
}
}
}
}
}
return missing;
}
@Override
public List getMissingDependencies () throws Exception {
List installedPackages = m_packageManager.getInstalledPackages();
String dependencies = getPackageMetaDataElement("Depends" ).toString();
return getMissingDependencies(installedPackages);
}
@Override
public List getIncompatibleDependencies (List packages)
throws Exception {
List incompatible = new ArrayList();
String dependencies = getPackageMetaDataElement("Depends" ).toString();
if (dependencies != null ) {
StringTokenizer tok = new StringTokenizer(dependencies, "," );
while (tok.hasMoreTokens()) {
String nextT = tok.nextToken().trim();
String[] splitD = splitNameVersion(nextT);
if (splitD[1 ] != null && splitD[2 ] != null ) {
for (Package p : packages) {
String packageNameI = p.getPackageMetaDataElement("PackageName" )
.toString();
if (packageNameI.trim().equalsIgnoreCase(splitD[0 ].trim())) {
String versionI = p.getPackageMetaDataElement("Version" )
.toString().trim();
if (splitD.length == 3 ) {
VersionPackageConstraint.VersionComparison constraint = VersionPackageConstraint
.getVersionComparison(splitD[1 ]);
if (!VersionPackageConstraint.checkConstraint(versionI,
constraint, splitD[2 ])) {
VersionPackageConstraint vpc = new VersionPackageConstraint(p);
vpc.setVersionConstraint(constraint);
incompatible.add(new Dependency(this , vpc));
}
} else {
VersionRangePackageConstraint versionRConstraint = new VersionRangePackageConstraint(
p);
VersionPackageConstraint.VersionComparison comp1 = VersionPackageConstraint
.getVersionComparison(splitD[1 ]);
VersionPackageConstraint.VersionComparison comp2 = VersionPackageConstraint
.getVersionComparison(splitD[3 ]);
versionRConstraint.setRangeConstraint(splitD[2 ], comp1,
splitD[4 ], comp2);
incompatible.add(new Dependency(this , versionRConstraint));
}
}
}
}
}
}
return incompatible;
}
@Override
public List getIncompatibleDependencies () throws Exception {
List installedP = m_packageManager.getInstalledPackages();
String dependencies = getPackageMetaDataElement("Depends" ).toString();
return getIncompatibleDependencies(installedP);
}
@Override
public boolean isCompatibleBaseSystem () throws Exception {
String baseSystemName = m_packageManager.getBaseSystemName();
String systemVersion = m_packageManager.getBaseSystemVersion().toString();
String dependencies = getPackageMetaDataElement("Depends" ).toString();
if (dependencies == null ) {
return true ;
}
boolean ok = true ;
StringTokenizer tok = new StringTokenizer(dependencies, "," );
while (tok.hasMoreTokens()) {
String nextT = tok.nextToken().trim();
String[] split = splitNameVersion(nextT);
if (split[0 ].startsWith(baseSystemName.toLowerCase())) {
if (split[1 ] != null ) {
if (split.length == 3 ) {
VersionPackageConstraint.VersionComparison constraint = VersionPackageConstraint
.getVersionComparison(split[1 ]);
if (!VersionPackageConstraint.checkConstraint(systemVersion,
constraint, split[2 ])) {
ok = false ;
break ;
}
} else {
Map baseMap = new HashMap();
baseMap.put("PackageName" , "weka" );
baseMap.put("Version" , systemVersion);
Package basePackage = new DefaultPackage(null , m_packageManager,
baseMap);
VersionRangePackageConstraint versionRConstraint = new VersionRangePackageConstraint(
basePackage);
VersionPackageConstraint.VersionComparison comp1 = VersionPackageConstraint
.getVersionComparison(split[1 ]);
VersionPackageConstraint.VersionComparison comp2 = VersionPackageConstraint
.getVersionComparison(split[3 ]);
versionRConstraint.setRangeConstraint(split[2 ], comp1, split[4 ],
comp2);
if (!versionRConstraint.checkConstraint(basePackage)) {
ok = false ;
break ;
}
}
}
}
}
return ok;
}
@Override
public void install () throws Exception {
URL packageURL = getPackageURL();
m_packageManager.installPackageFromURL(packageURL);
}
@Override
public boolean isInstalled () {
File packageDir = new File(m_packageHome.getAbsoluteFile() + File.separator
+ m_packageMetaData.get("PackageName" ) + File.separator
+ "Description.props" );
return (packageDir.exists());
}
public static void main (String[] args) {
String installed = args[0 ];
String toCheckAgainst = args[1 ];
String[] splitI = splitNameVersion(installed);
String[] splitA = splitNameVersion(toCheckAgainst);
try {
if (splitA.length == 3 ) {
System.out
.println("Checking first version number against second constraint" );
VersionPackageConstraint.VersionComparison constraint = VersionPackageConstraint
.getVersionComparison(splitA[1 ]);
if (VersionPackageConstraint.checkConstraint(splitI[2 ], constraint,
splitA[2 ])) {
System.out.println(splitI[2 ] + " is compatible with " + args[1 ]);
} else {
System.out.println(splitI[2 ] + " is not compatible with " + args[1 ]);
}
Map baseMap = new HashMap();
baseMap.put("PackageName" , splitA[0 ]);
baseMap.put("Version" , splitA[2 ]);
Package packageA = new DefaultPackage(null , null , baseMap);
packageA.setPackageMetaData(baseMap);
VersionPackageConstraint constrA = new VersionPackageConstraint(
packageA);
constrA.setVersionConstraint(constraint);
if (splitI.length == 3 ) {
VersionPackageConstraint.VersionComparison constraintI = VersionPackageConstraint
.getVersionComparison(splitI[1 ]);
Package packageI = (Package) packageA.clone();
packageI.setPackageMetaDataElement(
VersionPackageConstraint.VERSION_KEY, splitI[2 ]);
VersionPackageConstraint constrI = new VersionPackageConstraint(
packageI);
constrI.setVersionConstraint(constraintI);
PackageConstraint pc = null ;
if ((pc = constrI.checkConstraint(constrA)) != null ) {
System.out.println(constrI + " and " + constrA
+ " are compatible\n\n" + "compatible constraint " + pc);
} else {
System.out.println(constrI + " and " + constrA
+ " are not compatible" );
}
} else {
}
} else {
System.out
.println("Checking first version number against second constraint" );
Map baseMap = new HashMap();
baseMap.put("PackageName" , splitI[0 ]);
baseMap.put("Version" , splitI[2 ]);
Package p = new DefaultPackage(null , null , baseMap);
VersionRangePackageConstraint c = new VersionRangePackageConstraint(p);
VersionPackageConstraint.VersionComparison comp1 = VersionPackageConstraint
.getVersionComparison(splitA[1 ]);
VersionPackageConstraint.VersionComparison comp2 = VersionPackageConstraint
.getVersionComparison(splitA[3 ]);
c.setRangeConstraint(splitA[2 ], comp1, splitA[4 ], comp2);
if (c.checkConstraint(p)) {
System.out.println(splitI[2 ] + " is compatible with " + args[1 ]);
} else {
System.out.println(splitI[2 ] + " is not compatible with " + args[1 ]);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
@Override
public void setPackageMetaDataElement (Object key, Object value)
throws Exception {
if (m_packageMetaData == null ) {
throw new Exception("[DefaultPackage] no meta data map has been set!" );
}
Map meta = (Map) m_packageMetaData;
meta.put(key, value);
}
@Override
public String toString () {
String packageName = getPackageMetaDataElement("PackageName" ).toString();
String version = getPackageMetaDataElement("Version" ).toString();
return packageName + " (" + version + ")" ;
}
}