Skip to content

Commit

Permalink
KG sanity increasing
Browse files Browse the repository at this point in the history
  • Loading branch information
fvilla committed Nov 15, 2024
1 parent 3a1f5d6 commit 8c12d3e
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ interface Operation extends Closeable {
/**
* Link the two passed assets.
* <p>*
*
* @param source
* @param destination
* @param additionalProperties any pair of properties we want overridden. Pass pairs and do it right
Expand All @@ -76,6 +77,19 @@ void link(RuntimeAsset source, RuntimeAsset destination,
DigitalTwin.Relationship relationship,
Object... additionalProperties);

/**
* Link the passed asset to the root node it "naturally" belongs to in the scope.
* <p>*
*
* @param destination
* @param additionalProperties any pair of properties we want overridden. Pass pairs and do it right
* or you'll get an exception.
*/
void linkToRootNode(RuntimeAsset destination,
DigitalTwin.Relationship relationship,
Object... additionalProperties);


//
// /**
// * Run the operation as configured and return the ID of the last object created or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ enum Type {
INITIALIZATION, RESOLUTION, CONTEXTUALIZATION, INSTANTIATION
}

enum Outcome {
FAILURE, SUCCESS, INTERNAL_FAILURE
}

Type getType();

/**
Expand Down Expand Up @@ -99,4 +103,8 @@ enum Type {
*/
String getDescription();

Outcome getOutcome();

String getStackTrace();

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class ActivityImpl extends ProvenanceNodeImpl implements Activity {
private long id;
private String taskId;
private Activity parent;
private Outcome outcome;
private String stackTrace;

@Override
public long getStart() {
Expand Down Expand Up @@ -109,4 +111,22 @@ public String getTaskId() {
public void setTaskId(String taskId) {
this.taskId = taskId;
}

@Override
public Outcome getOutcome() {
return outcome;
}

public void setOutcome(Outcome outcome) {
this.outcome = outcome;
}

@Override
public String getStackTrace() {
return stackTrace;
}

public void setStackTrace(String stackTrace) {
this.stackTrace = stackTrace;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -280,29 +280,37 @@ public long submit(Observation observation, ContextScope scope) {
}

if (scope instanceof ServiceContextScope serviceContextScope) {

var digitalTwin = getDigitalTwin(scope);
var parentActivity = getInitializationActivity(observation, scope);
// var parentActivity = getInitializationActivity(observation, scope);
var agent = getAgent(scope);

/**
/*
* The initial activity should be in the scope; if not, we're observing at the
* root DT level and we get the context initialization activity as parent.
*/
var instantiation = digitalTwin.knowledgeGraph().operation(agent, parentActivity,
var instantiation = digitalTwin.knowledgeGraph().operation(agent, null,
Activity.Type.INSTANTIATION, observation);

try (instantiation) {

var ret = instantiation.store(observation);
instantiation.link(instantiation.getActivity(), observation, DigitalTwin.Relationship.CREATED);
if (scope.getContextObservation() != null) {
instantiation.link(scope.getContextObservation(), observation,
DigitalTwin.Relationship.HAS_CHILD);
} else {
instantiation.linkToRootNode(observation, DigitalTwin.Relationship.HAS_CHILD);
}

if (scope.getObserver() != null) {
instantiation.link(observation, scope.getObserver(),
DigitalTwin.Relationship.HAS_OBSERVER);
}

instantiation.success(scope, observation);
return ret;

} catch (Throwable t) {
instantiation.fail(scope, observation);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.integratedmodelling.common.runtime.ActuatorImpl;
import org.integratedmodelling.klab.api.data.KnowledgeGraph;
import org.integratedmodelling.klab.api.data.RuntimeAsset;
import org.integratedmodelling.klab.api.digitaltwin.DigitalTwin;
Expand Down Expand Up @@ -39,8 +40,6 @@ public RuntimeAsset load(Long key) throws Exception {
}
});

// protected abstract RuntimeAsset getContextNode();

/**
* Return a RuntimeAsset representing the overall dataflow related to the scope, so that it can be used
* for linking using the other CRUD methods.
Expand Down Expand Up @@ -117,6 +116,7 @@ protected Map<String, Object> asParameters(Object asset, Object... additionalPar
if (asset != null) {
switch (asset) {
case Observation observation -> {

ret.putAll(observation.getMetadata());
ret.put("name", observation.getName() == null ? observation.getObservable().codeName()
: observation.getName());
Expand All @@ -127,13 +127,15 @@ protected Map<String, Object> asParameters(Object asset, Object... additionalPar
ret.put("semantictype", SemanticType.fundamentalType(
observation.getObservable().getSemantics().getType()).name());
ret.put("semantics", observation.getObservable().getUrn());
ret.put("id", observation.getId());
}
case Agent agent -> {
// TODO
}
case Actuator actuator -> {
case ActuatorImpl actuator -> {

ret.put("observationId", actuator.getId());
ret.put("id", actuator.getInternalId());
StringBuilder code = new StringBuilder();
for (var call : actuator.getComputation()) {
// TODO skip any recursive resolution calls and prepare for linking later
Expand All @@ -153,6 +155,9 @@ protected Map<String, Object> asParameters(Object asset, Object... additionalPar
ret.put("size", activity.getSize());
ret.put("type", activity.getType().name());
ret.put("name", activity.getName());
ret.put("id", activity.getId());
ret.put("outcome", activity.getOutcome() == null ? null : activity.getOutcome().name());
ret.put("stackTrace", activity.getStackTrace());
}
default -> throw new KlabInternalErrorException(
"unexpected value for asParameters: " + asset.getClass().getCanonicalName());
Expand Down
Loading

0 comments on commit 8c12d3e

Please sign in to comment.