From 8037c43159c591069402cad4dc3434fdd90e1c83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Kit?= Date: Thu, 14 Nov 2013 12:11:09 +0100 Subject: [PATCH] BaseKnowledgeManager update --- .../deeco/knowledge/BaseKnowledgeManager.java | 79 +++++++++---------- 1 file changed, 37 insertions(+), 42 deletions(-) diff --git a/jdeeco-core/src/cz/cuni/mff/d3s/deeco/knowledge/BaseKnowledgeManager.java b/jdeeco-core/src/cz/cuni/mff/d3s/deeco/knowledge/BaseKnowledgeManager.java index b9270953e..c41fb6cec 100644 --- a/jdeeco-core/src/cz/cuni/mff/d3s/deeco/knowledge/BaseKnowledgeManager.java +++ b/jdeeco-core/src/cz/cuni/mff/d3s/deeco/knowledge/BaseKnowledgeManager.java @@ -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 @@ -25,20 +24,20 @@ * @author Michal Kit * */ +@SuppressWarnings("unchecked") public class BaseKnowledgeManager implements KnowledgeManager { private final Map knowledge; private final Map> 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; @@ -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) @@ -162,32 +161,48 @@ protected Object getKnowledge(List knowledgePath) } protected void updateKnowledge(KnowledgePath knowledgePath, Object value) { - List 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 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) 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) 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 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 knowledgePath, Object node) throws KnowledgeNotFoundException { Object currentObject = node; @@ -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