Skip to content

Commit

Permalink
BaseKnowledgeManager update
Browse files Browse the repository at this point in the history
  • Loading branch information
Michał Kit committed Nov 14, 2013
1 parent 2aa5b1a commit 8037c43
Showing 1 changed file with 37 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import cz.cuni.mff.d3s.deeco.model.runtime.api.PathNode;
import cz.cuni.mff.d3s.deeco.model.runtime.api.PathNodeField;
import cz.cuni.mff.d3s.deeco.model.runtime.api.Trigger;
import cz.cuni.mff.d3s.deeco.model.runtime.meta.RuntimeMetadataFactory;

/**
* This class implements the KnowledgeManager interface. It allows the user to
Expand All @@ -25,20 +24,20 @@
* @author Michal Kit <kit@d3s.mff.cuni.cz>
*
*/
@SuppressWarnings("unchecked")
public class BaseKnowledgeManager implements KnowledgeManager {

private final Map<KnowledgePath, Object> knowledge;
private final Map<KnowledgeChangeTrigger, List<TriggerListener>> knowledgeChangeListeners;

private final String id;



public BaseKnowledgeManager(String id) {
this.id = id;
this.knowledge = new HashMap<>();
this.knowledgeChangeListeners = new HashMap<>();
}

@Override
public String getId() {
return this.id;
Expand Down Expand Up @@ -132,7 +131,7 @@ public synchronized void update(ChangeSet changeSet) {
// Delete list or map items
deleteKnowledge(changeSet.getDeletedReferences());
}

@Override
public boolean equals(Object that) {
if (that != null && that instanceof BaseKnowledgeManager)
Expand Down Expand Up @@ -162,32 +161,48 @@ protected Object getKnowledge(List<PathNode> knowledgePath)
}

protected void updateKnowledge(KnowledgePath knowledgePath, Object value) {
List<PathNode> pathNodesToParent = new LinkedList<>(
knowledgePath.getNodes());
String fieldName = ((PathNodeField) pathNodesToParent
.remove(pathNodesToParent.size() - 1)).getName();
Object parent = null;
try {
parent = getKnowledge(pathNodesToParent);
} catch (KnowledgeNotFoundException e) {
if (uniqueKnowledgePath(knowledgePath, knowledge.keySet())) {
knowledge.put(knowledgePath, value);
return;
}
try {
Field field = parent.getClass().getField(fieldName);
field.set(parent, value);
} catch (Exception e) {
} else {
List<PathNode> pathNodesToParent = new LinkedList<>(
knowledgePath.getNodes());
String fieldName = ((PathNodeField) pathNodesToParent
.remove(pathNodesToParent.size() - 1)).getName();
Object parent = null;
try {
parent = getKnowledge(pathNodesToParent);
} catch (KnowledgeNotFoundException e) {
return;
}
if (parent instanceof List<?>) {
((List<Object>) parent).set(Integer.parseInt(fieldName), value);
} else if (parent instanceof Map<?, ?>) {
if (parent.equals(knowledge))
knowledge.put(knowledgePath, value);
else
if (parent.equals(knowledge)) {
if (knowledge.containsKey(knowledgePath))
knowledge.put(knowledgePath, value);
} else
((Map<String, Object>) parent).put(fieldName, value);
} else {
try {
Field field = parent.getClass().getField(fieldName);
field.set(parent, value);
} catch (Exception e) {
}
}
}
}

private boolean uniqueKnowledgePath(KnowledgePath path,
Collection<KnowledgePath> paths) {
for (KnowledgePath p : paths) {
if (containmentEndIndex(p.getNodes(), path.getNodes()) > -1
|| containmentEndIndex(path.getNodes(), p.getNodes()) > -1)
return false;
}
return true;
}

private Object getKnowledgeFromNode(List<PathNode> knowledgePath,
Object node) throws KnowledgeNotFoundException {
Object currentObject = node;
Expand Down Expand Up @@ -303,29 +318,9 @@ private void notifyKnowledgeChangeListeners(KnowledgePath kp) {
}
}

private ValueSet processInitialKnowledge(Object knowledge) {
RuntimeMetadataFactory factory = RuntimeMetadataFactory.eINSTANCE;
ValueSet result = new ValueSet();
KnowledgePath kp;
PathNodeField pnf;
for (Field f : knowledge.getClass().getFields()) {
kp = factory.createKnowledgePath();
pnf = factory.createPathNodeField();
pnf.setName(new String(f.getName()));
kp.getNodes().add(pnf);
try {
result.setValue(kp, f.get(knowledge));
} catch (IllegalAccessException e) {
continue;
}
}
return result;
}

/**
* Checks whether the shorter list is contained in the longer one, keeping
* the order. It returns the containment end index. FIXME: This method
* should be somewhere else.
* the order. It returns the containment end index.
*
* @param longer
* @param shorter
Expand Down

0 comments on commit 8037c43

Please sign in to comment.