Skip to content

Commit

Permalink
Pattern handling in reasoner
Browse files Browse the repository at this point in the history
  • Loading branch information
fvilla committed Sep 12, 2024
1 parent 32ff200 commit 440b866
Showing 1 changed file with 44 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import org.integratedmodelling.klab.api.knowledge.Observable;
import org.integratedmodelling.klab.api.knowledge.ObservationStrategy;
import org.integratedmodelling.klab.api.knowledge.SemanticType;
import org.integratedmodelling.klab.api.knowledge.Semantics;
import org.integratedmodelling.klab.api.knowledge.observation.Observation;
import org.integratedmodelling.klab.api.lang.ServiceCall;
import org.integratedmodelling.klab.api.lang.kim.KimObservationStrategy;
import org.integratedmodelling.klab.api.scope.ContextScope;
import org.integratedmodelling.klab.api.services.Language;
Expand Down Expand Up @@ -70,7 +72,6 @@ public List<ObservationStrategy> matching(Observation observation, ContextScope

var observable = observation.getObservable();
List<ObservationStrategy> ret = new ArrayList<>();
var languageService = ServiceConfiguration.INSTANCE.getService(Language.class);

for (var strategy : observationStrategies) {

Expand Down Expand Up @@ -100,18 +101,24 @@ public List<ObservationStrategy> matching(Observation observation, ContextScope

} else if (!functor.getFunctions().isEmpty()) {
for (var function : functor.getFunctions()) {

// complete arguments if empty or using previously instantiated variables
if (function.getParameters().isEmpty()) {
function = function.withUnnamedParameters(observable);
} else {
// substitute parameters
}
var value = languageService.execute(function, scope, Object.class);
var value = matchFunction(function, observable, scope, Object.class,
patternVariableValues);
String[] varNames = variable.split(",");
if (value instanceof Collection<?> collection) {
for (var v : collection) {

// must be string with same amount of return values
if (varNames.length != collection.size()) {
scope.error("wrong number of return values from " + function);
}
int i = 0;
for (var o : collection) {
patternVariableValues.put(varNames[i++], o);
}
} else {
// set pattern var
if (varNames.length != 1) {
scope.error("wrong number of return values from " + function);
}
patternVariableValues.put(variable, value);
}
}
}
Expand All @@ -125,9 +132,9 @@ public List<ObservationStrategy> matching(Observation observation, ContextScope
match = true;
break;
}
if (match) {
break;
}
}
if (match) {
break;
}
}

Expand All @@ -136,8 +143,8 @@ public List<ObservationStrategy> matching(Observation observation, ContextScope
}

/*
if we get here, there is a match, compile the observation strategy
customized for the observable and scope
if we get here, the strategy definition is a match: compile the observation strategy
operations for the observable and scope
*/

var os = new ObservationStrategyImpl();
Expand All @@ -158,6 +165,8 @@ public List<ObservationStrategy> matching(Observation observation, ContextScope

for (var function : operation.getFunctions()) {
}

os.getOperations().add(op);
}

ret.add(os);
Expand All @@ -169,6 +178,21 @@ public List<ObservationStrategy> matching(Observation observation, ContextScope
return ret;
}

private Object matchFunction(ServiceCall function, Semantics observable, ContextScope scope,
Class<Object> objectClass, Map<String, Object> patternVariableValues) {

var languageService = ServiceConfiguration.INSTANCE.getService(Language.class);

// complete arguments if empty or using previously instantiated variables
if (function.getParameters().isEmpty()) {
function = function.withUnnamedParameters(observable);
} else {
// substitute parameters
}
return languageService.execute(function, scope, Object.class);

}

private boolean matchFilter(KimObservationStrategy.Filter filter, Observation observation,
ContextScope scope, Map<String, Object> patternVariableValues) {

Expand All @@ -177,11 +201,13 @@ private boolean matchFilter(KimObservationStrategy.Filter filter, Observation ob

var semantics = filter.getMatch().isPattern() ? reasoner.declareConcept(filter.getMatch(),
patternVariableValues) : reasoner.declareConcept(filter.getMatch());
ret = semantics != null; // && compatible
ret = semantics != null && reasoner.compatible(observation.getObservable(), semantics);
}
if (ret && !filter.getFunctions().isEmpty()) {
for (var function : filter.getFunctions()) {

var value = matchFunction(function, observation.getObservable(), scope, Object.class,
patternVariableValues);
ret = value instanceof Boolean bool && bool;
}
}
return filter.isNegated() != ret;
Expand Down

0 comments on commit 440b866

Please sign in to comment.