Commit 88522ac0 authored by Marcel Lueder's avatar Marcel Lueder

Initial Commit Date Sim

parent c4aa8713
......@@ -185,12 +185,16 @@ public class DateDesc extends SimpleAttDesc {
* the description for which a new function should be created
* @return the Similarity.getFunction for description desc
*/
public DateFct addDateFct(String name, boolean active) {
DateFct f = new DateFct(owner.getProject(), this, name);
public DateFct addDateFct(String name, boolean active, DateFct.DateFunctionPrecision adjust) {
DateFct f = new DateFct(owner.getProject(), this, name, adjust);
addFunction(f, active);
return f;
}
public DateFct addDateFct(String name, boolean active) {
return addDateFct(name, active, DateFct.DateFunctionPrecision.Year);
}
/*
* (non-Javadoc)
......@@ -265,7 +269,7 @@ public class DateDesc extends SimpleAttDesc {
void addDefaultFct() {
if (owner != null && owner != owner.getProject()) {
ISimFct activeSim = addDateFct(Project.DEFAULT_FCT_NAME, false);
ISimFct activeSim = addDateFct(Project.DEFAULT_FCT_NAME, false, DateFct.DateFunctionPrecision.Day);
updateAmalgamationFcts(owner, activeSim);
}
}
......
......@@ -28,12 +28,13 @@
package de.dfki.mycbr.core.similarity;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Observable;
import de.dfki.mycbr.core.Project;
import de.dfki.mycbr.core.casebase.Attribute;
import de.dfki.mycbr.core.casebase.MultipleAttribute;
import de.dfki.mycbr.core.casebase.SpecialAttribute;
import de.dfki.mycbr.core.casebase.*;
import de.dfki.mycbr.core.model.AttributeDesc;
import de.dfki.mycbr.core.model.DateDesc;
import de.dfki.mycbr.core.similarity.config.MultipleConfig;
......@@ -46,15 +47,19 @@ import de.dfki.mycbr.core.similarity.config.MultipleConfig;
*/
public class DateFct extends Observable implements ISimFct {
public enum DateFunctionPrecision { Second, Minute, Hour, Day, Month, Year }
private Project prj;
private String name;
private DateDesc desc;
protected MultipleConfig mc = MultipleConfig.DEFAULT_CONFIG;
public DateFct(Project prj,DateDesc desc, String name) {
private DateFunctionPrecision precision;
public DateFct(Project prj,DateDesc desc, String name, DateFunctionPrecision precision) {
this.prj = prj;
this.desc = desc;
this.name = name;
this.precision = precision;
}
/* (non-Javadoc)
......@@ -67,7 +72,74 @@ public class DateFct extends Observable implements ISimFct {
return prj.calculateSpecialSimilarity(value1, value2);
} else if (value1 instanceof MultipleAttribute<?> && value2 instanceof MultipleAttribute<?>) {
return prj.calculateMultipleAttributeSimilarity(this, (MultipleAttribute<?>)value1, (MultipleAttribute<?>)value2);
} else {
} else if (value1 instanceof DateAttribute && value2 instanceof DateAttribute) {
DateAttribute dateAttr1 = (DateAttribute) value1;
DateAttribute dateAttr2 = (DateAttribute) value2;
Date date1 = dateAttr1.getDate();
Date date2 = dateAttr2.getDate();
double result = 0.0;
Calendar calendar1 = GregorianCalendar.getInstance();
calendar1.setTime(date1);
Calendar calendar2 = GregorianCalendar.getInstance();
calendar2.setTime(date2);
switch (this.precision) {
case Second:
if (calendar1.get(Calendar.MINUTE) == calendar2.get(Calendar.MINUTE)
&& calendar1.get(Calendar.HOUR_OF_DAY) == calendar2.get(Calendar.HOUR_OF_DAY)
&& calendar1.get(Calendar.DAY_OF_MONTH) == calendar2.get(Calendar.DAY_OF_MONTH)
&& calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH)
&& calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) {
int secondsDate1 = calendar1.get(Calendar.SECOND);
int secondsDate2 = calendar2.get(Calendar.SECOND);
result = 1 - (Math.abs(secondsDate2 - secondsDate1) / 60.0);
}
break;
case Minute:
if (calendar1.get(Calendar.HOUR_OF_DAY) == calendar2.get(Calendar.HOUR_OF_DAY)
&& calendar1.get(Calendar.DAY_OF_MONTH) == calendar2.get(Calendar.DAY_OF_MONTH)
&& calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH)
&& calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) {
int minutesDate1 = calendar1.get(Calendar.MINUTE);
int minutesDate2 = calendar2.get(Calendar.MINUTE);
result = 1 - (Math.abs(minutesDate2 - minutesDate1) / 60.0);
}
break;
case Hour:
if (calendar1.get(Calendar.DAY_OF_MONTH) == calendar2.get(Calendar.DAY_OF_MONTH)
&& calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH)
&& calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) {
int hoursDate1 = calendar1.get(Calendar.HOUR_OF_DAY);
int hoursDate2 = calendar2.get(Calendar.HOUR_OF_DAY);
result = 1 - (Math.abs(hoursDate2 - hoursDate1) / 24.0);
}
break;
case Day:
if (calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH)
&& calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) {
int daysDate1 = calendar1.get(Calendar.DAY_OF_MONTH);
int daysDate2 = calendar2.get(Calendar.DAY_OF_MONTH);
result = 1 - (Math.abs(daysDate2 - daysDate1) / 31.0); // Muss bzgl. Schaltjahr angepasst werden
}
break;
case Month:
if (calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) {
int monthsDate1 = calendar1.get(Calendar.MONTH);
int monthsDate2 = calendar2.get(Calendar.MONTH);
result = 1 - (Math.abs(monthsDate2 - monthsDate1) / 12.0);
}
break;
case Year:
int yearsDate1 = calendar1.get(Calendar.YEAR);
int yearsDate2 = calendar2.get(Calendar.YEAR);
result = 1 - (Math.abs(yearsDate2 - yearsDate1) / 100.0); // Konfigurierbar machen
break;
}
return Similarity.get(result);
} else {
return Similarity.INVALID_SIM;
}
......@@ -81,6 +153,12 @@ public class DateFct extends Observable implements ISimFct {
return desc;
}
/* (non-Javadoc)
*/
public DateFunctionPrecision getPrecision() {
return precision;
}
/* (non-Javadoc)
* @see de.dfki.mycbr.core.similarity.ISimFct#getMultipleConfig()
*/
......@@ -159,9 +237,12 @@ public class DateFct extends Observable implements ISimFct {
@Override
public void clone(AttributeDesc descNEW, boolean active) {
if (descNEW instanceof DateDesc && !name.equals(Project.DEFAULT_FCT_NAME)) {
DateFct f = ((DateDesc)descNEW).addDateFct(name, active);
DateFct f = ((DateDesc)descNEW).addDateFct(name, active, this.precision);
f.mc = this.mc;
}
}
public void setPrecision(DateFunctionPrecision adj) {
this.precision = adj;
}
}
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment