Skip to content

Commit

Permalink
Resolver fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ferdinando Villa committed Nov 12, 2024
1 parent 9acbda2 commit 5d62526
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ enum Type {
*/
long getCredits();

/**
* The activity, if any, that represent a super-activity of this one
*
* @return
*/
Activity getParent();

/**
* Logs each time that the action was executed (in lieu of having an action per each execution). Empty for
* any action that wasn't called by the scheduler. If not empty the first time could be initialization or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class ActivityImpl extends ProvenanceNodeImpl implements Activity {
private Activity.Type type;
private String description;
private long id;
private Activity parent;

@Override
public long getStart() {
Expand Down Expand Up @@ -84,4 +85,13 @@ public long getId() {
public void setId(long id) {
this.id = id;
}

@Override
public Activity getParent() {
return parent;
}

public void setParent(Activity parent) {
this.parent = parent;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ enum CoreFunctor {
URN_INSTANTIATOR("klab.core.urn.instantiator"),
EXPRESSION_RESOLVER("klab.core.urn.resolver"),
LUT_RESOLVER("klab.core.urn.resolver"),
CONSTANT_RESOLVER("klab.core.urn.resolver");
CONSTANT_RESOLVER("klab.core.urn.resolver"),
DEFER_RESOLUTION("klab.core.resolution.defer");

private String serviceCall;
private Map<String, Artifact.Type> arguments;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.integratedmodelling.klab.api.collections.Parameters;
import org.integratedmodelling.klab.api.data.RuntimeAsset;
import org.integratedmodelling.klab.api.digitaltwin.DigitalTwin;
import org.integratedmodelling.klab.api.exceptions.KlabIllegalStateException;
import org.integratedmodelling.klab.api.exceptions.KlabInternalErrorException;
import org.integratedmodelling.klab.api.exceptions.KlabResourceAccessException;
import org.integratedmodelling.klab.api.geometry.Geometry;
Expand All @@ -17,6 +18,7 @@
import org.integratedmodelling.klab.api.lang.kactors.KActorsBehavior;
import org.integratedmodelling.klab.api.provenance.Activity;
import org.integratedmodelling.klab.api.provenance.Provenance;
import org.integratedmodelling.klab.api.provenance.impl.ActivityImpl;
import org.integratedmodelling.klab.api.scope.ContextScope;
import org.integratedmodelling.klab.api.services.KlabService;
import org.integratedmodelling.klab.api.services.Resolver;
Expand Down Expand Up @@ -53,6 +55,7 @@ public class ServiceContextScope extends ServiceSessionScope implements ContextS
private Observation contextObservation;
private URL url;
private DigitalTwin digitalTwin;
private ActivityImpl activity;
// FIXME there's also parentScope (generic) and I'm not sure these should be duplicated
protected ServiceContextScope parent;
protected Map<ResolutionConstraint.Type, ResolutionConstraint> resolutionConstraints =
Expand All @@ -69,6 +72,7 @@ private ServiceContextScope(ServiceContextScope parent) {
this.digitalTwin = parent.digitalTwin;
this.observationCache = parent.observationCache;
this.resolutionConstraints.putAll(parent.resolutionConstraints);
this.activity = parent.activity;
}

@Override
Expand Down Expand Up @@ -167,24 +171,6 @@ public Collection<Observation> getRootObservations() {
return List.of();
}

// @Override
// public Scale getScale() {
// return geometry;
// }

// @Override
// public ServiceContextScope withScenarios(String... scenarios) {
// ServiceContextScope ret = new ServiceContextScope(this);
// if (scenarios == null) {
// ret.resolutionScenarios = null;
// }
// this.resolutionScenarios = new HashSet<>();
// for (String scenario : scenarios) {
// ret.resolutionScenarios.add(scenario);
// }
// return ret;
// }

@Override
public ServiceContextScope withObserver(Observation observer) {
ServiceContextScope ret = new ServiceContextScope(this);
Expand Down Expand Up @@ -243,11 +229,6 @@ public Collection<Observation> getChildrenOf(Observation observation) {
return null;
}

// @Override
// public Map<Concept, Concept> getContextualizedPredicates() {
// return contextualizedPredicates;
// }

@Override
public Collection<Observation> getOutgoingRelationshipsOf(Observation observation) {
// TODO Auto-generated method stub
Expand Down Expand Up @@ -307,25 +288,11 @@ public ContextScope within(Observation contextObservation) {
return ret;
}

// @Override
// public ContextScope withContextualizedPredicate(Concept abstractTrait, Concept concreteTrait) {
// ServiceContextScope ret = new ServiceContextScope(this);
// ret.contextualizedPredicates.put(abstractTrait, concreteTrait);
// return ret;
// }

@Override
public ContextScope between(Observation source, Observation target) {
return null;
}

// @Override
// public ContextScope withResolutionNamespace(String namespace) {
// ServiceContextScope ret = new ServiceContextScope(this);
// ret.resolutionNamespace = namespace;
// return ret;
// }

@Override
public ServiceContextScope withResolutionConstraints(ResolutionConstraint... resolutionConstraints) {
ServiceContextScope ret = new ServiceContextScope(this);
Expand Down Expand Up @@ -379,6 +346,23 @@ public <T> List<T> getConstraints(ResolutionConstraint.Type type, Class<T> resul
return constraint.payload(resultClass);
}

/**
* Non-API: scopes carry the provenance activity they're representing during contextualization. When the
* activity is added, any previously set activity becomes the parent activity.
*
* @param activity
* @return
*/
public ServiceContextScope withActivity(Activity activity) {
ServiceContextScope ret = new ServiceContextScope(this);
if (activity instanceof ActivityImpl activityImpl) {
ret.activity = activityImpl;
ret.activity.setParent(activity);
return ret;
}
throw new KlabIllegalStateException("Using unexpected Activity implementation");
}

@Override
public boolean initializeAgents(String scopeId) {
// setting the ID here is dirty as technically this is still being set and will be set again later,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.*;

/**
* A Compiler is instantiated in context. TODO should also take a parent Dataflow and fill the
Expand Down Expand Up @@ -68,15 +65,15 @@ public Dataflow<Observation> compile() {
/*
These MUST be observations. We check for now but it shouldn't happen.
*/
if (!(node instanceof Observation observation)) {
if (!(node instanceof Observation)) {
throw new KlabIllegalStateException("Resolution root is not an observation");
}
ret.getComputation().add(compileObservation(observation, Scale.create(observation.getGeometry()), null));
ret.getComputation().addAll(compileObservation(observation, Scale.create(observation.getGeometry()), null));
}

var out = new StringWriter();
var diocan = new PrintWriter(out);
new DataflowEncoder(ret, scope).encode(diocan);
var dioCan = new PrintWriter(out);
new DataflowEncoder(ret, scope).encode(dioCan);

System.out.println(out.toString());

Expand All @@ -92,32 +89,35 @@ public Dataflow<Observation> compile() {
* @return
*/

Actuator compileObservation(Observation observation, Geometry coverage, ObservationStrategy strategy) {

var ret = new ActuatorImpl();
ret.setObservable(observation.getObservable());
ret.setId(observation.getId());
ret.setActuatorType(strategy == null ? Actuator.Type.RESOLVE : Actuator.Type.OBSERVE);
ret.setCoverage(coverage.as(Geometry.class));

if (strategy != null) {
ret.setStrategyUrn(strategy.getUrn());
}
List<Actuator> compileObservation(Observation observation, Geometry coverage, ObservationStrategy strategy) {

if (catalog.contains(observation)) {
var ret = new ActuatorImpl();
ret.setObservable(observation.getObservable());
ret.setId(observation.getId());
ret.setActuatorType(strategy == null ? Actuator.Type.RESOLVE : Actuator.Type.OBSERVE);
ret.setCoverage(coverage.as(Geometry.class));
ret.setActuatorType(Actuator.Type.REFERENCE);
return ret;
return List.of(ret);
}

catalog.add(observation);

var ret = new ArrayList<Actuator>();
for (var edge : resolutionGraph.graph().outgoingEdgesOf(observation)) {

var child = resolutionGraph.graph().getEdgeTarget(edge);
var childCoverage = edge.coverage;

if (child instanceof ObservationStrategy observationStrategy) {
compileStrategy(ret, observation, childCoverage, observationStrategy);
var actuator = new ActuatorImpl();
actuator.setObservable(observation.getObservable());
actuator.setId(observation.getId());
actuator.setActuatorType(Actuator.Type.OBSERVE);
actuator.setCoverage(childCoverage == null ? null : childCoverage.as(Geometry.class));
actuator.setStrategyUrn(observationStrategy.getUrn());
compileStrategy(actuator, observation, childCoverage, observationStrategy);
ret.add(actuator);
}
}

Expand Down Expand Up @@ -173,7 +173,7 @@ void compileModel(Actuator observationActuator, Observation observation, Geometr
var coverage = edge.coverage;

if (child instanceof Observation dependentObservation) {
observationActuator.getChildren().add(compileObservation(dependentObservation, coverage, observationStrategy));
observationActuator.getChildren().addAll(compileObservation(dependentObservation, coverage, observationStrategy));
}
}

Expand Down

0 comments on commit 5d62526

Please sign in to comment.