Skip to content

Commit

Permalink
[incubator-kie-drools-6010] Date Between Dates with globals Not Trigg…
Browse files Browse the repository at this point in the history
…ering for Executable Model
  • Loading branch information
tkobayas committed Jul 5, 2024
1 parent f755fa0 commit 47bfb32
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ public boolean equals(final Object obj) {
return false;
}
final GlobalExtractor other = (GlobalExtractor) obj;
if (!(this.identifier.equals(other.identifier))) {
return false;
}
return this.objectType.equals( other.objectType );
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.drools.model.codegen.execmodel.domain;

import java.util.Date;

public class SimpleDateHolder {

private String id;
private final Date date;

public SimpleDateHolder(String id, Date date) {
this.id = id;
this.date = date;
}

public Date getDate() {
return date;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.drools.model.codegen.execmodel.operators;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.drools.model.codegen.execmodel.BaseModelTest;
import org.drools.model.codegen.execmodel.domain.Person;
import org.drools.model.codegen.execmodel.domain.SimpleDateHolder;
import org.junit.Ignore;
import org.junit.Test;
import org.kie.api.runtime.KieSession;

import static org.assertj.core.api.Assertions.assertThat;

public class DateOperatorTest extends BaseModelTest {

public DateOperatorTest(RUN_TYPE testRunType) {
super(testRunType);
}

@Test
public void dateBetweenGlobals() throws ParseException {
String str =
"import " + SimpleDateHolder.class.getCanonicalName() + ";" +
"global java.util.Date $startDate;\n" +
"global java.util.Date $endDate;\n" +
"rule R when\n" +
" SimpleDateHolder(date > $startDate && date < $endDate)\n" +
"then\n" +
"end";

KieSession ksession = getKieSession(str);
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
ksession.setGlobal("$startDate", sdf.parse("04/01/2019"));
ksession.setGlobal("$endDate", sdf.parse("05/01/2019"));

SimpleDateHolder holder = new SimpleDateHolder("A", sdf.parse("04/15/2019"));
ksession.insert(holder);
int fired = ksession.fireAllRules();

assertThat(fired).isEqualTo(1);
}

@Test
public void dateBetweenVariables() throws ParseException {
String str =
"import " + SimpleDateHolder.class.getCanonicalName() + ";" +
"rule R when\n" +
" SimpleDateHolder(id == \"A\", $startDate : date)\n" +
" SimpleDateHolder(id == \"B\", $endDate : date)\n" +
" SimpleDateHolder(id == \"C\", date > $startDate && date < $endDate)\n" +
"then\n" +
"end";

KieSession ksession = getKieSession(str);
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");

SimpleDateHolder holderA = new SimpleDateHolder("A", sdf.parse("04/01/2019"));
SimpleDateHolder holderB = new SimpleDateHolder("B", sdf.parse("05/01/2019"));
SimpleDateHolder holderC = new SimpleDateHolder("C", sdf.parse("04/15/2019"));

ksession.insert(holderA);
ksession.insert(holderB);
ksession.insert(holderC);
int fired = ksession.fireAllRules();

assertThat(fired).isEqualTo(1);
}
}

0 comments on commit 47bfb32

Please sign in to comment.