Skip to content

Commit

Permalink
Some additional changes.
Browse files Browse the repository at this point in the history
WARNING: double check the phaser and stuff that was removed/added to the
MetadataKB because it is unclear how to do this.

Done:
- move some members from private -> protected
- change name to metadata
  • Loading branch information
bnouwt committed Jan 7, 2025
1 parent 636132c commit a283fbe
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 70 deletions.
8 changes: 4 additions & 4 deletions admin-ui/src/main/java/eu/knowledge/engine/admin/AdminUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public BindingSet handleNewKnowledgeBaseKnowledge(ReactExchangeInfo ei) {
// when result available (and the config is enabled), we print the
// knowledge bases to the console.
if (continuousLog) {
this.printKnowledgeBases(this.getModel());
this.printKnowledgeBases(this.getMetadata());
}
return bs;
}
Expand All @@ -58,7 +58,7 @@ public BindingSet handleChangedKnowledgeBaseKnowledge(ReactExchangeInfo ei) {
// when result available (and the config is enabled), we print the
// knowledge bases to the console.
if (continuousLog) {
this.printKnowledgeBases(this.getModel());
this.printKnowledgeBases(this.getMetadata());
}
return bs;
}
Expand All @@ -70,7 +70,7 @@ public BindingSet handleRemovedKnowledgeBaseKnowledge(ReactExchangeInfo ei) {
// when result available (and the config is enabled), we print the
// knowledge bases to the console.
if (continuousLog) {
this.printKnowledgeBases(this.getModel());
this.printKnowledgeBases(this.getMetadata());
}

return bs;
Expand All @@ -83,7 +83,7 @@ public void fetchInitialData() {
// when result available (and the config is enabled), we print the
// knowledge bases to the console.
if (continuousLog)
this.printKnowledgeBases(this.getModel());
this.printKnowledgeBases(this.getMetadata());
}

private void printKnowledgeBases(Model model) {
Expand Down
130 changes: 65 additions & 65 deletions admin-ui/src/main/java/eu/knowledge/engine/admin/MetadataKB.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.Arrays;
import java.util.HashSet;
import java.util.concurrent.Phaser;
import java.util.concurrent.ExecutionException;

import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Resource;
Expand Down Expand Up @@ -43,8 +43,6 @@ public class MetadataKB extends EasyKnowledgeBase {

private final PrefixMapping prefixes;

private Phaser readyPhaser;

// used for getting initial knowledge about other KBs
private AskKnowledgeInteraction aKI;
// used triggered when new knowledge about other KBs is available
Expand All @@ -54,18 +52,18 @@ public class MetadataKB extends EasyKnowledgeBase {
// used triggered when knowledge about other KBs is deleted
private ReactKnowledgeInteraction rKIRemoved;

private Model model;
private Model metadata;

private GraphPattern metaGraphPattern;

private boolean timeToSleepAndFetch = true;

/**
* Intialize a AdminUI that regularly retrieves and prints metadata about the
* available knowledge bases.
*/
public MetadataKB(String id, String name, String description) {
super(id, name, description);
readyPhaser = new Phaser(0);
this.setPhaser(this.readyPhaser);

// store some predefined prefixes
this.prefixes = new PrefixMappingMem();
Expand All @@ -74,11 +72,6 @@ public MetadataKB(String id, String name, String description) {

this.metaGraphPattern = new GraphPattern(this.prefixes, META_GRAPH_PATTERN_STR);

// we wait for the Smart Connector to be ready, before registering our Knowledge
// Interactions and starting the Ask job.

LOG.info("Smart connector ready, now registering Knowledge Interactions.");

// create the correct Knowledge Interactions
this.aKI = new AskKnowledgeInteraction(new CommunicativeAct(), this.metaGraphPattern, true);
this.rKINew = new ReactKnowledgeInteraction(
Expand All @@ -100,19 +93,24 @@ public MetadataKB(String id, String name, String description) {
this.register(this.rKIChanged, (rki, ei) -> this.handleChangedKnowledgeBaseKnowledge(ei));
this.register(this.rKIRemoved, (rki, ei) -> this.handleRemovedKnowledgeBaseKnowledge(ei));

this.start();
this.syncKIs();
}

// to receive the initial state, we do a single Ask (after sleeping for a
// specific amount of time)
try {
Thread.sleep(
ConfigProvider.getConfig().getValue(AdminUIConfig.CONF_KEY_INITIAL_ADMIN_UI_DELAY, Integer.class));
} catch (InterruptedException e) {
LOG.info("{}", e);
}
this.fetchInitialData();
@Override
public void syncKIs() {
super.syncKIs();

if (timeToSleepAndFetch) {
// to receive the initial state, we do a single Ask (after sleeping for a
// specific amount of time)
try {
Thread.sleep(ConfigProvider.getConfig().getValue(AdminUIConfig.CONF_KEY_INITIAL_ADMIN_UI_DELAY,
Integer.class));
} catch (InterruptedException e) {
LOG.info("{}", e);
}
this.fetchInitialData();
this.timeToSleepAndFetch = false;
}
}

public BindingSet handleNewKnowledgeBaseKnowledge(ReactExchangeInfo ei) {
Expand All @@ -123,16 +121,19 @@ public BindingSet handleNewKnowledgeBaseKnowledge(ReactExchangeInfo ei) {
Model model = eu.knowledge.engine.smartconnector.impl.Util.generateModel(this.aKI.getPattern(),
ei.getArgumentBindings());

// this we can simply add to our model
this.model.add(model);
Resource kb = model.listSubjectsWithProperty(RDF.type, Vocab.KNOWLEDGE_BASE).next();

// this we can simply add to our model
this.metadata.add(model);
LOG.debug("Modified metadata with new KB '{}'.", kb);
} catch (ParseException e) {
e.printStackTrace();
LOG.error("{}", e);
}
return new BindingSet();
}

public BindingSet handleChangedKnowledgeBaseKnowledge(ReactExchangeInfo ei) {

if (!this.canReceiveUpdates())
return new BindingSet();

Expand All @@ -150,12 +151,14 @@ public BindingSet handleChangedKnowledgeBaseKnowledge(ReactExchangeInfo ei) {
this.metaGraphPattern.getPattern(), this.metaGraphPattern.getPattern(), kb.toString());

UpdateRequest updateRequest = UpdateFactory.create(query);
UpdateAction.execute(updateRequest, this.model);
UpdateAction.execute(updateRequest, this.metadata);

this.metadata.add(model);

this.model.add(model);
LOG.debug("Modified metadata with changed KB '{}'.", kb);

} catch (ParseException e) {
e.printStackTrace();
LOG.error("{}", e);
}
return new BindingSet();
}
Expand All @@ -177,59 +180,56 @@ public BindingSet handleRemovedKnowledgeBaseKnowledge(ReactExchangeInfo ei) {
this.metaGraphPattern.getPattern(), this.metaGraphPattern.getPattern(), kb.toString());

UpdateRequest updateRequest = UpdateFactory.create(query);
UpdateAction.execute(updateRequest, this.model);
UpdateAction.execute(updateRequest, this.metadata);

LOG.debug("Modified metadata with deleted KB '{}'.", kb);

} catch (ParseException e) {
e.printStackTrace();
LOG.error("{}", e);
}
return new BindingSet();
}

public void fetchInitialData() {
LOG.info("Retrieving initial other Knowledge Base info...");

// execute actual *ask* and use previously defined Knowledge Interaction.
this.getSC().ask(this.aKI, new BindingSet()).thenAccept(askResult -> {
try {
// using the BindingSet#generateModel() helper method, we can combine the graph
// pattern and the bindings for its variables into a valid RDF Model.
this.model = eu.knowledge.engine.smartconnector.impl.Util.generateModel(this.aKI.getPattern(),
askResult.getBindings());
this.model.setNsPrefixes(this.prefixes);

} catch (ParseException e) {
LOG.error("{}", e);
}
}).handle((r, e) -> {
if (r == null && e != null) {
LOG.error("An exception has occured while retrieving other Knowledge Bases info", e);
return null;
} else {
return r;
}
});
try {

// execute actual *ask* and use previously defined Knowledge Interaction.
this.getSC().ask(this.aKI, new BindingSet()).thenAccept(askResult -> {
try {
// using the BindingSet#generateModel() helper method, we can combine the graph
// pattern and the bindings for its variables into a valid RDF Model.
this.metadata = eu.knowledge.engine.smartconnector.impl.Util.generateModel(this.aKI.getPattern(),
askResult.getBindings());
this.metadata.setNsPrefixes(this.prefixes);

} catch (ParseException e) {
LOG.error("{}", e);
}
}).handle((r, e) -> {
if (r == null && e != null) {
LOG.error("An exception has occured while retrieving other Knowledge Bases info", e);
return null;
} else {
return r;
}
}).get();
} catch (ExecutionException | InterruptedException ee) {
LOG.error("{}", ee);
}

}

private boolean canReceiveUpdates() {
return this.model != null;
protected boolean canReceiveUpdates() {
return this.metadata != null;
}

public void close() {
this.stop();
}

public Model getModel() {
return model;
}

public static String getConfigProperty(String key, String defaultValue) {
// We might replace this with something a bit more fancy in the future...
String value = System.getenv(key);
if (value == null) {
value = defaultValue;
LOG.info("No value for the configuration parameter '" + key + "' was provided, using the default value '"
+ defaultValue + "'");
}
return value;
public Model getMetadata() {
return metadata;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void getSCOverview(
@Suspended final AsyncResponse asyncResponse, @Context SecurityContext securityContext)
throws NotFoundException {
admin = AdminUI.newInstance(false);
model = this.admin.getModel(); // todo: needs locking for multi-threading? Read while write is busy.
model = this.admin.getMetadata(); // todo: needs locking for multi-threading? Read while write is busy.
if (model != null && !model.isEmpty()) {
Set<Resource> kbs = Util.getKnowledgeBaseURIs(model);
SmartConnector[] responses = findAndAddConnections(convertToModel(kbs, model, includeMeta));
Expand Down

0 comments on commit a283fbe

Please sign in to comment.