Skip to content

Commit

Permalink
Stubs for syntactic matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
fvilla committed Dec 3, 2024
1 parent f4a5e07 commit b3d04c3
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public class ReasonerService extends BaseService implements Reasoner, Reasoner.A
private Map<String, Observable> observables = Collections.synchronizedMap(new HashMap<>());
private ObservationReasoner observationReasoner;
private Worldview worldview;
private SyntacticMatcher syntacticMatcher;

// /**
// * Caches for concepts and observables, linked to the URI in the corresponding
Expand Down Expand Up @@ -300,8 +301,9 @@ public void initializeService() {
}

this.observationReasoner = new ObservationReasoner(this);
this.syntacticMatcher = new SyntacticMatcher(this, serviceScope().getService(ResourcesService.class));

/**
/*
* Setup an embedded broker, possibly to be shared with other services, if we're local and there
* is no configured broker.
*/
Expand Down Expand Up @@ -1441,7 +1443,7 @@ public synchronized ResourceSet updateKnowledge(ResourceSet changes, UserScope s
resourceService =
services.computeIfAbsent(changes.getServices().get(resource.getServiceId()),
url -> new ResourcesClient(url, scope.getIdentity(), this,
settingsForSlaveServices));
settingsForSlaveServices));
}

var notifications = new ArrayList<Notification>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.integratedmodelling.klab.services.reasoner;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.integratedmodelling.klab.api.knowledge.SemanticType;
import org.integratedmodelling.klab.api.knowledge.Semantics;
import org.integratedmodelling.klab.api.lang.kim.KimConcept;
import org.integratedmodelling.klab.api.lang.kim.KimObservable;
import org.integratedmodelling.klab.api.services.ResourcesService;

import java.util.concurrent.TimeUnit;

/**
* Match two concept using one as a syntactic pattern for the other. Used in the rule system to filter
* resolution strategies.
* <p>
* Keeps syntactic objects in a cache to minimize traffic to the resources service.
*/
public class SyntacticMatcher {

private ReasonerService reasonerService;
private ResourcesService resourcesService;
private LoadingCache<String, KimObservable> conceptCache =
CacheBuilder.newBuilder()
.concurrencyLevel(20)
.maximumSize(400) // TODO configure
.build(new CacheLoader<>() {
@Override
public KimObservable load(String key) throws Exception {
return resourcesService.resolveObservable(key);
}
});

public SyntacticMatcher(ReasonerService reasonerService, ResourcesService resourcesService) {
this.reasonerService = reasonerService;
this.resourcesService = resourcesService;
}

public boolean match(Semantics candidate, Semantics pattern) {

KimConcept oCandidate = null;
KimConcept pCandidate = null;

try {
oCandidate = conceptCache.getUnchecked(candidate.getUrn()).getSemantics();
pCandidate = conceptCache.getUnchecked(pattern.getUrn()).getSemantics();
} catch (Throwable t) {
//
}

if (pCandidate == null || oCandidate == null) {
return false;
}

return matchConcepts (oCandidate, pCandidate);
}

private boolean matchConcepts(KimConcept candidate, KimConcept pattern) {

if (pattern.is(SemanticType.UNION) || pattern.is(SemanticType.INTERSECTION)) {
// must have same type and same number of arguments
// TODO this applies also to all ops
}

int narg = pattern.getOperands().size();
// NO - if pattern is X or Y it should match X or Y or Z with matching Y = Y or Z
if (candidate.getOperands().size() != narg) {
return false;
}


return false;

}

}

0 comments on commit b3d04c3

Please sign in to comment.