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

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;
}
}
/*
* myCBR License 2.0
*
* Copyright (c) 2009
* Thomas Roth-Berghofer, Armin Stahl & Deutsches Forschungszentrum f&uuml;r K&uuml;nstliche Intelligenz DFKI GmbH
* Further contributors: myCBR Team (see http://mycbr-project.net/contact.html for further information
* about the myCBR Team).
* All rights reserved.
*
* myCBR is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Since myCBR uses some modules, you should be aware of their licenses for
* which you should have received a copy along with this program, too.
*
* endOfLic */
package test.junittest.similarity;
import de.dfki.mycbr.core.casebase.DateAttribute;
import de.dfki.mycbr.core.model.DateDesc;
import de.dfki.mycbr.core.similarity.DateFct;
import de.dfki.mycbr.core.similarity.Similarity;
import junit.framework.TestCase;
import org.junit.Test;
import test.junittest.TestFramework;
import java.text.SimpleDateFormat;
/**
* Created by Marcel on 15.04.2014.
*/
public class DateFunctionTest extends TestCase {
@Test
public void testCalculateSimylarityALL() {
try {
TestFramework frame = new TestFramework();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd kk:mm");
// Add new DateDescription
DateDesc newDate = new DateDesc(frame.carDesc, "newDate", df.parse("1950-01-01 00:00"), df.parse("2099-01-01 00:00"), df);
// Add new DateFunction
DateFct f = newDate.addDateFct("DateFct", true, DateFct.DateFunctionPrecision.Hour);
// Start testing
// Equaltest
DateAttribute date1 = newDate.getDateAttribute(df.parse("2014-04-15 00:00"));
DateAttribute date2 = newDate.getDateAttribute(df.parse("2014-04-15 00:00"));
Similarity sim = f.calculateSimilarity(date1, date2);
//assertTrue("Equaltest failed result: " + sim.getRoundedValue(), sim.getValue() == 1);
// Hour Tests
date1 = newDate.getDateAttribute(df.parse("2014-04-16 01:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 23:59"));
sim = f.calculateSimilarity(date1, date2);
System.out.println("Similarity Hour [00:01 - 23:59]: " + sim.getValue());
assertTrue(round(sim.getValue()) == 0.08333);
date1 = newDate.getDateAttribute(df.parse("2014-04-16 01:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 01:50"));
sim = f.calculateSimilarity(date1, date2);
System.out.println("Similarity Hour [01:00 - 01:50]: " + sim.getValue());
assertTrue(round(sim.getValue()) == 1);
date1 = newDate.getDateAttribute(df.parse("2014-04-16 01:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 02:00"));
sim = f.calculateSimilarity(date1, date2);
System.out.println("Similarity Hour [01:00 - 02:00]: " + sim.getValue());
assertTrue(round(sim.getValue()) == 0.95833);
date1 = newDate.getDateAttribute(df.parse("2014-04-16 00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 12:00"));
sim = f.calculateSimilarity(date1, date2);
System.out.println("Similarity Hour [00:00 - 12:00]: " + sim.getValue());
assertTrue(round(sim.getValue()) == 0.5);
System.out.println("\r\n=== Hour Loop Test || Start");
for (int i = 0; i < 24; i++) {
String hour = "";
if (i < 10) {
hour = "0";
}
hour += i;
date1 = newDate.getDateAttribute(df.parse("2014-04-16 " + hour + ":03"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 00:03"));
sim = f.calculateSimilarity(date1, date2);
System.out.println("Similarity Hour [" + hour + ":03 - 00:03]: " + sim.getValue());
}
System.out.println("=== Hour Loop Test || End\n");
df = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
date1 = newDate.getDateAttribute(df.parse("2014-04-17 13:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:30"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(sim.getValue() == 0);
System.out.println("Similarity Hours [2014-04-17 13:00:00 - 2014-04-16 13:00:30]: " + sim.getValue());
date1 = newDate.getDateAttribute(df.parse("2014-05-16 13:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:30"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(sim.getValue() == 0);
System.out.println("Similarity Hours [2014-05-16 13:00:00 - 2014-04-16 13:00:30]: " + sim.getValue());
date1 = newDate.getDateAttribute(df.parse("2015-04-16 13:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:30"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(sim.getValue() == 0);
System.out.println("Similarity Hours [2015-04-16 13:00:00 - 2014-04-16 13:00:30]: " + sim.getValue());
// Minute Test
df = new SimpleDateFormat("yyyy-MM-dd kk:mm");
((DateFct)newDate.getFct("DateFct")).setPrecision(DateFct.DateFunctionPrecision.Minute);
date1 = newDate.getDateAttribute(df.parse("2014-04-16 13:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:30"));
sim = f.calculateSimilarity(date1, date2);
System.out.println("Similarity Minute [13:00 - 13:30]: " + sim.getValue());
assertTrue(round(sim.getValue()) == 0.5);
date1 = newDate.getDateAttribute(df.parse("2014-04-16 13:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00"));
sim = f.calculateSimilarity(date1, date2);
System.out.println("Similarity Minute [13:00 - 13:00]: " + sim.getValue());
assertTrue(round(sim.getValue()) == 1);
date1 = newDate.getDateAttribute(df.parse("2014-04-16 13:31"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:31"));
sim = f.calculateSimilarity(date1, date2);
System.out.println("Similarity Minute [13:31 - 13:31]: " + sim.getValue());
assertTrue(round(sim.getValue()) == 1);
date1 = newDate.getDateAttribute(df.parse("2014-04-16 13:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:59"));
sim = f.calculateSimilarity(date1, date2);
System.out.println("Similarity Minute [13:00 - 13:59]: " + sim.getValue());
assertTrue(round(sim.getValue()) == 0.01667);
date1 = newDate.getDateAttribute(df.parse("2014-04-16 13:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:45"));
sim = f.calculateSimilarity(date1, date2);
System.out.println("Similarity Minute [13:00 - 13:45]: " + sim.getValue());
assertTrue(round(sim.getValue()) == 0.25);
date1 = newDate.getDateAttribute(df.parse("2014-04-16 13:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 14:45"));
sim = f.calculateSimilarity(date1, date2);
System.out.println("XXX Similarity Minute [13:00 - 14:45]: " + sim.getValue());
assertTrue(round(sim.getValue()) == 0);
System.out.println("\n=== Minute Loop Test || Start");
for (int i = 0; i < 60; i++) {
String minute = "";
if (i < 10) {
minute = "0";
}
minute += i;
date1 = newDate.getDateAttribute(df.parse("2014-04-16 13:" + i));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00"));
sim = f.calculateSimilarity(date1, date2);
System.out.println("Similarity Minute [13:00 - 13:" + minute + "]: " + sim.getValue());
}
System.out.println("=== Minute Loop Test || End\n");
df = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
date1 = newDate.getDateAttribute(df.parse("2014-04-16 14:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:30"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(sim.getValue() == 0);
System.out.println("Similarity Minutes [2014-04-16 14:00:00 - 2014-04-16 13:00:30]: " + sim.getValue());
date1 = newDate.getDateAttribute(df.parse("2014-04-17 13:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:30"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(sim.getValue() == 0);
System.out.println("Similarity Minutes [2014-04-17 13:00:00 - 2014-04-16 13:00:30]: " + sim.getValue());
date1 = newDate.getDateAttribute(df.parse("2014-05-16 13:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:30"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(sim.getValue() == 0);
System.out.println("Similarity Minutes [2014-05-16 13:00:00 - 2014-04-16 13:00:30]: " + sim.getValue());
date1 = newDate.getDateAttribute(df.parse("2015-04-16 13:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:30"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(sim.getValue() == 0);
System.out.println("Similarity Minutes [2015-04-16 13:00:00 - 2014-04-16 13:00:30]: " + sim.getValue());
// Seconds Test
((DateFct)newDate.getFct("DateFct")).setPrecision(DateFct.DateFunctionPrecision.Second);
df = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
System.out.println("\n=== Seconds Loop Test || Start");
for (int i = 0; i < 60; i++) {
String second = "";
if (i < 10) {
second = "0";
}
second += i;
date1 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:" + i));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:00"));
sim = f.calculateSimilarity(date1, date2);
System.out.println("Similarity Seconds [13:00:00 - 13:00:" + second + "]: " + sim.getValue());
}
System.out.println("=== Second Loop Test || End\n");
date1 = newDate.getDateAttribute(df.parse("2014-04-16 13:01:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:30"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(sim.getValue() == 0);
System.out.println("Similarity Seconds [13:01:00 - 13:00:30]: " + sim.getValue());
date1 = newDate.getDateAttribute(df.parse("2014-04-16 14:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:30"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(sim.getValue() == 0);
System.out.println("Similarity Seconds [14:00:00 - 13:00:30]: " + sim.getValue());
date1 = newDate.getDateAttribute(df.parse("2014-04-17 13:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:30"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(sim.getValue() == 0);
System.out.println("Similarity Seconds [2014-04-17 13:00:00 - 2014-04-16 13:00:30]: " + sim.getValue());
date1 = newDate.getDateAttribute(df.parse("2014-05-16 13:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:30"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(sim.getValue() == 0);
System.out.println("Similarity Seconds [2014-05-16 13:00:00 - 2014-04-16 13:00:30]: " + sim.getValue());
date1 = newDate.getDateAttribute(df.parse("2015-04-16 13:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:30"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(sim.getValue() == 0);
System.out.println("Similarity Seconds [2015-04-16 13:00:00 - 2014-04-16 13:00:30]: " + sim.getValue());
// Days Test
((DateFct)newDate.getFct("DateFct")).setPrecision(DateFct.DateFunctionPrecision.Day);
df = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("\n=== Days Loop Test || Start");
for (int i = 1; i <= 31; i++) {
String day = "";
if (i < 10) {
day = "0";
}
day += i;
date1 = newDate.getDateAttribute(df.parse("2014-04-" + day));
date2 = newDate.getDateAttribute(df.parse("2014-04-01"));
sim = f.calculateSimilarity(date1, date2);
System.out.println("Similarity Days [2014-04-01 | 2014-04-" + day + "]: " + sim.getValue());
}
System.out.println("=== Days Loop Test || End\n");
df = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
date1 = newDate.getDateAttribute(df.parse("2014-05-16 13:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:30"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(sim.getValue() == 0);
System.out.println("Similarity Days [2014-05-16 13:00:00 - 2014-04-16 13:00:30]: " + sim.getValue());
date1 = newDate.getDateAttribute(df.parse("2015-04-16 13:00:00"));
date2 = newDate.getDateAttribute(df.parse("2014-04-16 13:00:30"));
sim = f.calculateSimilarity(date1, date2);
assertTrue(sim.getValue() == 0);
System.out.println("Similarity Days [2015-04-16 13:00:00 - 2014-04-16 13:00:30]: " + sim.getValue());
} catch (Exception e) {
assertTrue("Excpetion in testCalculateSimylarityHOUR",false);
}
}
private double round(double d) {
return Math.round(d * 100000)/100000.0;
}
}
Supports Markdown
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