Skip to content

Commit

Permalink
Strategies get back to the resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
Ferdinando Villa committed Sep 14, 2024
1 parent f751744 commit e6ae82f
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 108 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package org.integratedmodelling.klab.services.reasoner;
package org.integratedmodelling.klab.api.services.resolver.objects;

import org.integratedmodelling.klab.api.data.Metadata;
import org.integratedmodelling.klab.api.knowledge.Observable;
import org.integratedmodelling.klab.api.knowledge.ObservationStrategy;
import org.integratedmodelling.klab.api.lang.Contextualizable;
import org.integratedmodelling.klab.api.lang.kim.KimObservationStrategy;
import org.integratedmodelling.klab.api.scope.ContextScope;
import org.integratedmodelling.klab.api.services.Reasoner;

import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ public ServiceContextScope withObserver(Observation observer) {
@Override
public Task<Observation, Long> observe(Observation observation) {

if (!isOperative()) {
return null;
}

long id = submitObservation(observation);

// create task before resolution starts so we guarantee a response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
public class ServiceSessionScope extends ServiceUserScope implements SessionScope {

private String name;
private boolean operative = true;

public void setName(String name) {
this.name = name;
Expand Down Expand Up @@ -73,6 +74,14 @@ public void logout() {
// TODO
}

public boolean isOperative() {
return operative;
}

public void setOperative(boolean operative) {
this.operative = operative;
}

public boolean initializeAgents(String scopeId) {
// setting the ID here is dirty as technically this is still being set and will be set again later,
// but
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.integratedmodelling.klab.api.scope.ContextScope;
import org.integratedmodelling.klab.api.services.Language;
import org.integratedmodelling.klab.api.services.Reasoner;
import org.integratedmodelling.klab.api.services.resolver.objects.ObservationStrategyImpl;
import org.integratedmodelling.klab.configuration.ServiceConfiguration;
import org.integratedmodelling.klab.utilities.Utils;

Expand Down Expand Up @@ -177,7 +178,6 @@ public List<ObservationStrategy> computeMatchingStrategies(Observation observati
for (var operation : strategy.getOperations()) {

var op = new ObservationStrategyImpl.OperationImpl();

op.setType(operation.getType());

if (operation.getObservable() != null) {
Expand All @@ -186,16 +186,12 @@ public List<ObservationStrategy> computeMatchingStrategies(Observation observati
reasoner.declareObservable(operation.getObservable(),
patternVariableValues));
}

for (var function : operation.getFunctions()) {
op.getContextualizables().add(new ContextualizableImpl(function));
}

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

ret.add(os);

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,7 @@ public Capabilities capabilities(Scope scope) {

@Override
public Dataflow<Observation> resolve(Observation observation, ContextScope contextScope) {

System.out.println("RISOLVIAMO STA MERDA " + observation);

var resolution = computeResolution(observation, contextScope);

if (!resolution.isEmpty()) {
return compile(observation, resolution, contextScope);
}
Expand Down Expand Up @@ -177,77 +173,72 @@ public Resolution computeResolution(Observation observation, ContextScope scope)

}

// /**
// * Top-level resolution, resolve and return an independent resolution graph. This creates a new resolution
// * graph which will contain any observations that were already resolved within the context observation in
// * the scope, if any.
// *
// * @param knowledge
// * @param scope
// * @return
// */
// public Resolution computeResolution(Resolvable knowledge, ContextScope scope) {
//
// Geometry resolutionGeometry = Geometry.EMPTY;
//
// Resolvable observable = switch (knowledge) {
// case Concept concept -> Observable.promote(concept);
// case Model model -> model.getObservables().get(0);
// case Observable obs -> obs;
// case Observation observation -> {
// resolutionGeometry = observation.getGeometry();
// yield observation.getObservable();
// }
// default -> null;
// };
//
// if (observable == null) {
// // FIXME this should just set the resolution to an error state and return it
// throw new KlabIllegalStateException("knowledge " + knowledge + " is not resolvable");
// }
//
// if (scope.getContextObservation() != null) {
// resolutionGeometry = scope.getContextObservation().getGeometry();
// } else if (resolutionGeometry.isEmpty() && scope.getObserver() != null) {
// resolutionGeometry = scope.getObserver().getObserverGeometry();
// }
//
// var scale = Scale.create(resolutionGeometry);
//
// ResolutionImpl ret = new ResolutionImpl(observable, scale, scope);
// if (knowledge instanceof Model) {
// resolveModel((Model) knowledge, observable, scale,
// scope.withResolutionNamespace(((Model) knowledge).getNamespace()),
// ret);
// } else if (observable instanceof Observable obs) {
// resolveObservable(obs, scale, scope, ret, null);
// } // TODO the rest
//
// return ret;
//
// }
// /**
// * Top-level resolution, resolve and return an independent resolution graph. This creates a new
// resolution
// * graph which will contain any observations that were already resolved within the context
// observation in
// * the scope, if any.
// *
// * @param knowledge
// * @param scope
// * @return
// */
// public Resolution computeResolution(Resolvable knowledge, ContextScope scope) {
//
// Geometry resolutionGeometry = Geometry.EMPTY;
//
// Resolvable observable = switch (knowledge) {
// case Concept concept -> Observable.promote(concept);
// case Model model -> model.getObservables().get(0);
// case Observable obs -> obs;
// case Observation observation -> {
// resolutionGeometry = observation.getGeometry();
// yield observation.getObservable();
// }
// default -> null;
// };
//
// if (observable == null) {
// // FIXME this should just set the resolution to an error state and return it
// throw new KlabIllegalStateException("knowledge " + knowledge + " is not resolvable");
// }
//
// if (scope.getContextObservation() != null) {
// resolutionGeometry = scope.getContextObservation().getGeometry();
// } else if (resolutionGeometry.isEmpty() && scope.getObserver() != null) {
// resolutionGeometry = scope.getObserver().getObserverGeometry();
// }
//
// var scale = Scale.create(resolutionGeometry);
//
// ResolutionImpl ret = new ResolutionImpl(observable, scale, scope);
// if (knowledge instanceof Model) {
// resolveModel((Model) knowledge, observable, scale,
// scope.withResolutionNamespace(((Model) knowledge).getNamespace()),
// ret);
// } else if (observable instanceof Observable obs) {
// resolveObservable(obs, scale, scope, ret, null);
// } // TODO the rest
//
// return ret;
//
// }

private Coverage resolveObservation(Observation observation, Scale scale, ContextScope scope,
ResolutionImpl parent,
Model parentModel) {
ResolutionImpl parent, Model parentModel) {

var observable = observation.getObservable();
Coverage ret = Coverage.create(scale, 0.0);

// infinite recursion is nice but wastes time
if (parent.checkResolving(observable)) {
return Coverage.universal();
}

// done already, nothing to do here
if (parent.getResolved(observable) != null) {
// observation may have been resolved already. Also it could be being resolved from upstream, and
// infinite recursion is fun but helps nobody.
if (observation.isResolved() || parent.checkResolving(observable)) {
return Coverage.universal();
}

// see what the reasoner thinks of this observable
for (ObservationStrategy strategy :
scope.getService(Reasoner.class).computeObservationStrategies(observation,
scope)) {
scope.getService(Reasoner.class).computeObservationStrategies(observation, scope)) {
// this merges any useful strategy and returns the coverage
ResolutionImpl resolution = resolveStrategy(strategy, scale, scope, parent, parentModel);
ret = ret.merge(resolution.getCoverage(), LogicalConnector.UNION);
Expand All @@ -267,18 +258,17 @@ private Coverage resolveObservation(Observation observation, Scale scale, Contex
}


/**
* We always resolve an observable first. This only reports coverage as it does not directly create a
* resolution graph; this is done when resolving a model, which creates a graph and merges it with the
* parent graph if successful.
*
* @param observable
* @param parent
* @return
*/
/**
* We always resolve an observable first. This only reports coverage as it does not directly create a
* resolution graph; this is done when resolving a model, which creates a graph and merges it with the
* parent graph if successful.
*
* @param observable
* @param parent
* @return
*/
private Coverage resolveObservable(Observable observable, Scale scale, ContextScope scope,
ResolutionImpl parent,
Model parentModel) {
ResolutionImpl parent, Model parentModel) {

/**
* Make graph merging parent Set coverage to scale, 0; Strategies/models: foreach model:
Expand Down Expand Up @@ -340,7 +330,66 @@ private ResolutionImpl resolveStrategy(ObservationStrategy strategy, Scale scale
ResolutionImpl parent,
Model parentModel) {

Coverage coverage = Coverage.create(scale, 0.0);
var coverage = Coverage.create(scale, 0.0);
ResolutionImpl ret = null;

for (var operation : strategy.getOperations()) {
switch (operation.getType()) {
case RESOLVE -> {
/*
Additional resolution for a different observable, have the runtime produce the
observation, if resolved we're done, otherwise invoke resolution recursively
*/
ret = new ResolutionImpl(operation.getObservable(), scale, scope,
parent);
// TODO have the runtime create the observation
// TODO resolve it and merge the resolution
}
case OBSERVE -> {

/*
Observation is there, find models and compile them in, merge resolutions
*/
ret = new ResolutionImpl(operation.getObservable(), scale, scope,
parent);
for (Model model : queryModels(operation.getObservable(), scope,
scale)) {
ResolutionImpl resolution = resolveModel(model, operation.getObservable(),
scale,
scope.withResolutionNamespace(model.getNamespace()), parent);
coverage = coverage.merge(resolution.getCoverage(), LogicalConnector.UNION);
if (coverage.getGain() < MINIMUM_WORTHWHILE_CONTRIBUTION) {
continue;
}
// merge the model at root level within the local resolution
resolution.merge(model, coverage, operation.getObservable(),
ResolutionType.DIRECT);
if (coverage.isRelevant()) {
// merge the resolution with the parent resolution
ret.merge(parentModel, resolution, ResolutionType.DIRECT);
if (parent.getCoverage().isComplete()) {
break;
}
}
}

}
case APPLY -> {
// resolve the contextualizers merging the necessary resource set, coverage is
// unchanged unless contextualizers are not available
}
}

// add any deferrals to the compiled strategy node
if (!ret.isEmpty()) {
for (var deferral : operation.getContextualStrategies()) {

}
}
}

return ret;

// ResolutionImpl ret = new ResolutionImpl(strategy.getOriginalObservable(), scale, scope,
// parent);
//
Expand Down Expand Up @@ -383,7 +432,7 @@ private ResolutionImpl resolveStrategy(ObservationStrategy strategy, Scale scale
// }
//
// return ret;
return null;
// return null;
}

/**
Expand Down
Loading

0 comments on commit e6ae82f

Please sign in to comment.