Skip to content

Commit

Permalink
Activity/resolution/contextualization logics revised
Browse files Browse the repository at this point in the history
  • Loading branch information
fvilla committed Nov 12, 2024
1 parent 27df0e6 commit 86bac47
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import org.integratedmodelling.klab.api.digitaltwin.DigitalTwin;
import org.integratedmodelling.klab.api.knowledge.observation.Observation;
import org.integratedmodelling.klab.api.provenance.Activity;
import org.integratedmodelling.klab.api.provenance.Agent;
import org.integratedmodelling.klab.api.scope.ContextScope;
import org.integratedmodelling.klab.api.scope.Scope;
import org.integratedmodelling.klab.api.scope.UserScope;
import org.integratedmodelling.klab.api.services.runtime.objects.ContextInfo;
import org.integratedmodelling.klab.api.services.runtime.objects.SessionInfo;

import java.io.Closeable;
import java.net.URL;
import java.util.List;

Expand All @@ -31,8 +33,12 @@ public interface KnowledgeGraph {
* Operations are defined and run to modify the knowledge graph. The operation API guarantees the proper
* updating of provenance in the graph so that any modification is recorded, attributed and saves in
* replayable history.
* <p>
* At close, the operation commits or rolls back changes (except the activity it creates in provenance)
* according to which finalization mechanism has been called. If none has been called, the activity will
* be stored as an internal failure and everything else rolled back.
*/
interface Operation {
interface Operation extends Closeable {

/**
* Any operation on the KG is done by someone or something, dutifully recorded in the provenance.
Expand All @@ -41,6 +47,36 @@ interface Operation {
*/
Agent getAgent();

/**
* This is only used to pass the activity to a child operation.
*
* @return
*/
Activity getActivity();

/**
* Store the passed asset, return its unique long ID.
*
* @param asset
* @param additionalProperties any pair of properties we want overridden. Pass pairs and do it right
* or you'll get an exception.
* @return
*/
long store(RuntimeAsset asset, Scope scope, Object... additionalProperties);

/**
* Link the two passed assets.
*
* @param source
* @param destination
* @param additionalProperties any pair of properties we want overridden. Pass pairs and do it right
* or you'll get an exception.
*/
void link(RuntimeAsset source, RuntimeAsset destination,
DigitalTwin.Relationship relationship, Scope scope,
Object... additionalProperties);


/**
* Run the operation as configured and return the ID of the last object created or modified, or
* {@link Observation#UNASSIGNED_ID} if the operation failed or was wrongly defined.
Expand All @@ -56,6 +92,7 @@ interface Operation {
*
* @param observation
* @return
* @deprecated
*/
Operation add(RuntimeAsset observation);

Expand All @@ -66,6 +103,7 @@ interface Operation {
*
* @param source
* @return
* @deprecated
*/
Operation set(RuntimeAsset source, Object... properties);

Expand All @@ -82,6 +120,7 @@ interface Operation {
* @param assetTo
* @param linkData
* @return
* @deprecated
*/
Operation link(RuntimeAsset assetFrom, RuntimeAsset assetTo, DigitalTwin.Relationship relationship,
Object... linkData);
Expand Down Expand Up @@ -113,7 +152,6 @@ Operation link(RuntimeAsset assetFrom, RuntimeAsset assetTo, DigitalTwin.Relatio
* @return
*/
Operation fail(ContextScope scope, Object... assets);

}

/**
Expand Down Expand Up @@ -222,7 +260,7 @@ <T extends RuntimeAsset> List<T> get(RuntimeAsset source, DigitalTwin.Relationsh
*
* @param observation
* @param scope
* @param arguments additional parameters to add to the observation or to override existing ones
* @param arguments additional parameters to add to the observation or to override existing ones
*/
void update(RuntimeAsset observation, ContextScope scope, Object... arguments);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,8 @@ interface Capabilities extends ServiceCapabilities {
Concept describedType(Semantics concept);

/**
* Observational compatibility, considering all elements of the expression.
*
* @param concept
* @param other
* @return
Expand Down Expand Up @@ -1009,10 +1011,10 @@ interface Admin {

/**
* The "port" to ingest an individual concept definition, called by
* {@link #loadKnowledge(Worldview, Scope)} (Worldview)}. Provided separately to make it possible
* for a resolver service to declare individual local concepts, as long as it owns the semantic
* service. Definition must be made only in terms of known concepts (no forward declaration is
* allowed), so order of ingestion is critical.
* {@link #loadKnowledge(Worldview, Scope)} (Worldview)}. Provided separately to make it possible for
* a resolver service to declare individual local concepts, as long as it owns the semantic service.
* Definition must be made only in terms of known concepts (no forward declaration is allowed), so
* order of ingestion is critical.
*
* @param statement
* @param scope admin user scope to report and validate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ private boolean matchFilter(KimObservationStrategy.Filter filter, Observation ob
if (filter.getMatch() != null) {
var semantics = filter.getMatch().isPattern() ? reasoner.declareConcept(filter.getMatch(),
patternVariableValues) : reasoner.declareConcept(filter.getMatch());
ret = semantics != null && reasoner.compatible(observation.getObservable(), semantics);
ret = semantics != null && reasoner.match(observation.getObservable(), semantics);
}
if (ret && !filter.getFunctions().isEmpty()) {
for (var function : filter.getFunctions()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1781,7 +1781,8 @@ public Collection<Concept> created(Semantics semantics) {

@Override
public boolean match(Semantics candidate, Semantics pattern) {
return false;
// FIXME definitely not the same thing!
return compatible(candidate, pattern);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public boolean run() {
}
}

/**
/*
* Run also the empty operations because execution will update the observations
*/
if (scope.getParallelism() == Parallelism.ONE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.integratedmodelling.klab.api.knowledge.observation.impl.ObservationImpl;
import org.integratedmodelling.klab.api.lang.Contextualizable;
import org.integratedmodelling.klab.api.provenance.Activity;
import org.integratedmodelling.klab.api.provenance.impl.ActivityImpl;
import org.integratedmodelling.klab.api.scope.ContextScope;
import org.integratedmodelling.klab.api.scope.Scope;
import org.integratedmodelling.klab.api.scope.SessionScope;
Expand Down Expand Up @@ -269,6 +270,7 @@ public String registerContext(ContextScope contextScope) {
@Override
public long submit(Observation observation, ContextScope scope) {
if (scope instanceof ServiceContextScope serviceContextScope) {
// TODO this gets its operation (instantiation of observation)
return serviceContextScope.insertIntoKnowledgeGraph(observation);
}
return Observation.UNASSIGNED_ID;
Expand All @@ -282,8 +284,12 @@ public Future<Observation> resolve(long id, ContextScope scope) {
var resolver = serviceContextScope.getService(Resolver.class);
var observation = serviceContextScope.getObservation(id);
var digitalTwin = getDigitalTwin(scope);
var activity = digitalTwin.knowledgeGraph().activity(digitalTwin.knowledgeGraph().klab(), scope,
observation, Activity.Type.RESOLUTION, null);
var activity =

// TODO retrieve the activity that instantiated the observation instead, pass it down
// as the root for the resolution somehow
digitalTwin.knowledgeGraph().activity(digitalTwin.knowledgeGraph().klab(), scope,
observation, Activity.Type.RESOLUTION, null);

final var ret = new CompletableFuture<Observation>();

Expand All @@ -292,9 +298,11 @@ public Future<Observation> resolve(long id, ContextScope scope) {
var result = observation;
scope.send(Message.MessageClass.ObservationLifecycle,
Message.MessageType.ResolutionStarted, result);
// TODO resolution gets its own operation (find the instantiation activity as precursor)
var dataflow = resolver.resolve(observation, scope);
if (dataflow != null) {
if (!dataflow.isEmpty()) {
// TODO contextualization gets its own operation (dependent on resolution)
result = runDataflow(dataflow, scope);
ret.complete(result);
}
Expand Down Expand Up @@ -322,6 +330,13 @@ public Future<Observation> resolve(long id, ContextScope scope) {

@Override
public Observation runDataflow(Dataflow<Observation> dataflow, ContextScope contextScope) {
var activity = new ActivityImpl();
// TODO fill in the activity for an external dataflow run
return runDataflow(dataflow, contextScope, activity);
}

public Observation runDataflow(Dataflow<Observation> dataflow, ContextScope contextScope,
Activity activity) {

var digitalTwin = getDigitalTwin(contextScope);

Expand Down

This file was deleted.

Loading

0 comments on commit 86bac47

Please sign in to comment.