package ru.agentlab.maia.agent;

import java.util.Map;

import org.hamcrest.Matcher;

import ru.agentlab.maia.IEvent;
import ru.agentlab.maia.IPlan;

public abstract class Plan implements IPlan {

	protected Matcher<?> eventMatcher;

	protected Matcher<?> stateMatchers;

	protected final Object role;

	public Plan(Object role) {
		this.role = role;
	}

	@Override
	public Matcher<?> getEventMatcher() {
		return eventMatcher;
	}

	@Override
	public void setEventMatcher(Matcher<?> eventMatcher) {
		this.eventMatcher = eventMatcher;
	}

	@Override
	public Matcher<?> getStateMatcher() {
		return stateMatchers;
	}

	@Override
	public void setStateMatcher(Matcher<?> matcher) {
		this.stateMatchers = matcher;
	}

	@Override
	public boolean relevant(IEvent<?> event, Map<String, Object> map) {
		Object eventData = event.getPayload();
		return match(eventMatcher, eventData, map);
	}

	private <M> boolean match(Matcher<M> matcher, Object eventData, Map<String, Object> map) {
//		Class<M> eventMatcherClass = matcher.getType();
		if (eventMatcherClass.isAssignableFrom(eventData.getClass())) {
			return matcher.matches(eventMatcherClass.cast(eventData));
		} else {
			return false;
		}
	}

}
@@ -24,17 +24,19 @@
 a.agent;
-%0A
+%0D%0A%0D
 %0Aimport 
@@ -49,17 +49,19 @@
 til.Map;
-%0A
+%0D%0A%0D
 %0Aimport 
@@ -81,17 +81,19 @@
 Matcher;
-%0A
+%0D%0A%0D
 %0Aimport 
@@ -116,16 +116,17 @@
 .IEvent;
+%0D
 %0Aimport 
@@ -148,17 +148,19 @@
 a.IPlan;
-%0A
+%0D%0A%0D
 %0Apublic 
@@ -197,17 +197,19 @@
  IPlan %7B
-%0A
+%0D%0A%0D
 %0A%09protec
@@ -236,17 +236,19 @@
 Matcher;
-%0A
+%0D%0A%0D
 %0A%09protec
@@ -276,17 +276,19 @@
 atchers;
-%0A
+%0D%0A%0D
 %0A%09protec
@@ -309,17 +309,19 @@
 ct role;
-%0A
+%0D%0A%0D
 %0A%09public
@@ -340,16 +340,17 @@
  role) %7B
+%0D
 %0A%09%09this.
@@ -361,20 +361,23 @@
  = role;
+%0D
 %0A%09%7D
-%0A
+%0D%0A%0D
 %0A%09@Overr
@@ -371,32 +371,33 @@
 %09%7D%0D%0A%0D%0A%09@Override
+%0D
 %0A%09public Matcher
@@ -411,32 +411,33 @@
 EventMatcher() %7B
+%0D
 %0A%09%09return eventM
@@ -435,36 +435,39 @@
 rn eventMatcher;
+%0D
 %0A%09%7D
-%0A
+%0D%0A%0D
 %0A%09@Override%0A%09pub
@@ -453,32 +453,33 @@
 %09%7D%0D%0A%0D%0A%09@Override
+%0D
 %0A%09public void se
@@ -510,32 +510,33 @@
  eventMatcher) %7B
+%0D
 %0A%09%09this.eventMat
@@ -551,28 +551,31 @@
 ventMatcher;
+%0D
 %0A%09%7D
-%0A
+%0D%0A%0D
 %0A%09@Override%0A
@@ -565,32 +565,33 @@
 %09%7D%0D%0A%0D%0A%09@Override
+%0D
 %0A%09public Matcher
@@ -613,16 +613,17 @@
 cher() %7B
+%0D
 %0A%09%09retur
@@ -638,20 +638,23 @@
 atchers;
+%0D
 %0A%09%7D
-%0A
+%0D%0A%0D
 %0A%09@Overr
@@ -648,32 +648,33 @@
 %09%7D%0D%0A%0D%0A%09@Override
+%0D
 %0A%09public void se
@@ -708,16 +708,17 @@
 tcher) %7B
+%0D
 %0A%09%09this.
@@ -741,20 +741,23 @@
 matcher;
+%0D
 %0A%09%7D
-%0A
+%0D%0A%0D
 %0A%09@Overr
@@ -759,16 +759,17 @@
 Override
+%0D
 %0A%09public
@@ -833,49 +833,9 @@
 p) %7B
-%0A%09%09Object eventData = event.getPayload();
+%0D
 %0A%09%09r
@@ -840,22 +840,16 @@
 %09return 
-match(
 eventMat
@@ -856,336 +856,42 @@
 cher
-, eventData, map);%0A%09%7D%0A%0A%09private %3CM%3E boolean match(Matcher%3CM%3E matcher, Object eventData, Map%3CString, Object%3E map) %7B%0A//%09%09Class%3CM%3E eventMatcherClass = matcher.getType();%0A%09%09if (eventMatcherClass.isAssignableFrom(eventData.getClass())) %7B%0A%09%09%09return matcher.matches(eventMatcherClass.cast(eventData));%0A%09%09%7D else %7B%0A%09%09%09return false;%0A%09%09%7D%0A%09%7D%0A
+.matches(event.getPayload());%0D%0A%09%7D%0D%0A%0D
 %0A%7D

Fix plan is relevant