Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
Copyright (c) 2012 zuendorf
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package org.sdmlib.models.pattern;
import java.io.File;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import org.sdmlib.CGUtil;
import org.sdmlib.StrUtil;
import org.sdmlib.doc.GraphFactory;
import org.sdmlib.doc.interfaze.Adapter.GuiAdapter;
import org.sdmlib.models.pattern.util.PatternElementSet;
import org.sdmlib.models.pattern.util.PatternSet;
import org.sdmlib.serialization.PropertyChangeInterface;
import org.sdmlib.storyboards.Kanban;
import de.uniks.networkparser.interfaces.SendableEntityCreator;
import de.uniks.networkparser.json.JsonArray;
import de.uniks.networkparser.json.JsonObject;
import de.uniks.networkparser.IdMap;
import org.sdmlib.models.pattern.ReachabilityGraph;
/**
*
* @see PatternModelCodeGen.java
*/
public class Pattern extends PatternElement implements PropertyChangeInterface, Iterable
{
public static final String CREATE = "create";
public static final String DESTROY = "destroy";
public static final String BOUND = "bound";
private IdMap idMap;
private GuiAdapter adapter;
public GuiAdapter getAdapter()
{
if (adapter == null)
{
adapter = GraphFactory.getAdapter();
// adapter = new GraphViz().withDrawer(new org.sdmlib.doc.GraphViz());
}
return adapter;
}
public IdMap getIdMap()
{
return idMap;
}
public void setIdMap(IdMap idMap)
{
this.idMap = idMap;
}
public void clone(ReachabilityGraph rgraph)
{
CloneOp cloneOp = new CloneOp();
this.withElements(cloneOp);
this.findMatch();
}
public void unify(ReachabilityGraph rgraph)
{
UnifyGraphsOp unifyGraphsOp = new UnifyGraphsOp();
this.withElements(unifyGraphsOp);
this.findMatch();
}
// ==========================================================================
public Pattern(IdMap createIdMap)
{
idMap = createIdMap;
setHasMatch(true);
}
public Pattern()
{
hasMatch = true;
}
public MP startCreate()
{
this.setModifier(Pattern.CREATE);
return (MP) this;
}
public MP endCreate()
{
this.setModifier(null);
return (MP) this;
}
public MP startNAC()
{
NegativeApplicationCondition nac = new NegativeApplicationCondition();
this.addToElements(nac);
if (getTopPattern().getDebugMode() >= Kanban.DEBUG_ON)
{
nac.setPatternObjectName("n" + getTopPattern().getPatternObjectCount());
}
return (MP) this;
}
public MP startDestroy()
{
this.setModifier(Pattern.DESTROY);
return (MP) this;
}
public MP endDestroy()
{
this.setModifier(null);
return (MP) this;
}
public MP matchIsomorphic()
{
MatchIsomorphicConstraint isoConstraint = new MatchIsomorphicConstraint();
this.addToElements(isoConstraint);
this.findMatch();
return (MP) this;
}
// ==========================================================================
public int allMatches()
{
this.setDoAllMatches(true);
int result = 0;
while (getHasMatch())
{
result++;
findMatch();
}
return result;
}
public boolean rebind(PatternObject boundObject, Object value)
{
boundObject.setCurrentMatch(value);
this.resetSearch();
return this.findMatch();
}
public boolean findMatch()
{
if (!this.getHasMatch())
{
return false;
}
boolean done = false;
// start with the last element and go backward until a new choice is made,
// then go forward to propagate the new choice
int i = this.getElements().size() - 1;
if (restartSearchAtIndex0)
{
restartSearchAtIndex0 = false;
i = 0;
if (getTopPattern().getDebugMode() >= Kanban.DEBUG_ON)
{
getTopPattern().addLogMsg("\n Restart pattern: ");
}
}
PatternElement currentPE = null;
while (i >= 0 && i < this.getElements().size())
{
currentPE = this.getElements().get(i);
boolean hasNextMatch = currentPE.findNextMatch();
if (hasNextMatch)
{
i++;
}
else
{
i--;
}
}
setHasMatch(i >= this.getElements().size());
return getHasMatch();
}
public boolean findNextMatch()
{
return findMatch();
}
private boolean restartSearchAtIndex0 = false;
public void resetSearch()
{
restartSearchAtIndex0 = true;
setHasMatch(true);
for (PatternElement pe : this.getElements())
{
pe.resetSearch();
}
}
// ==========================================================================
public void removeYou()
{
removeAllFromElements();
setPattern(null);
setRgraph(null);
withoutElements(this.getElements().toArray(new PatternElement[this.getElements().size()]));
getPropertyChangeSupport().firePropertyChange("REMOVE_YOU", this, null);
}
/********************************************************************
*
* one many
* Pattern ----------------------------------- PatternElement
* pattern elements
*
*/
public static final String PROPERTY_ELEMENTS = "elements";
private PatternElementSet elements = null;
private int objNo;
public PatternElementSet getElements()
{
if (this.elements == null)
{
return PatternElement.EMPTY_SET;
}
return this.elements;
}
public boolean addToElements(PatternElement value)
{
boolean changed = false;
if (currentSubPattern != null)
{
// add element to nac
changed = currentSubPattern.addToElements(value);
}
else
{
if (value != null)
{
if (this.elements == null)
{
this.elements = new PatternElementSet();
}
if (!this.elements.contains(value))
{
changed = this.elements.add(value);
value.withPattern((Pattern>) this);
getPropertyChangeSupport().firePropertyChange(PROPERTY_ELEMENTS, null, value);
if (value instanceof PatternObject || value instanceof Pattern)
{
getTopPattern().incrementPatternObjectCount();
}
}
if (value instanceof Pattern)
{
this.setCurrentSubPattern((Pattern) value);
((Pattern) value).setIdMap(this.getIdMap());
}
}
}
return changed;
}
public boolean removeFromElements(PatternElement value)
{
boolean changed = false;
if ((this.elements != null) && (value != null))
{
changed = this.elements.remove(value);
if (changed)
{
value.setPattern(null);
getPropertyChangeSupport().firePropertyChange(PROPERTY_ELEMENTS, value, null);
}
}
return changed;
}
public MP withElements(PatternElement value)
{
addToElements(value);
return (MP) this;
}
public MP withoutElements(PatternElement value)
{
removeFromElements(value);
return (MP) this;
}
public void removeAllFromElements()
{
LinkedHashSet tmpSet = new LinkedHashSet(this.getElements());
for (PatternElement value : tmpSet)
{
this.removeFromElements(value);
}
}
public String getPOClassName(String modelClassName)
{
int pos = modelClassName.lastIndexOf('.');
return modelClassName.substring(0, pos + 1) + "creators."
+ modelClassName.substring(pos + 1, modelClassName.length()) + "PO";
}
public PatternObject bind(Object hostGraphObject)
{
SendableEntityCreator creatorClass = getIdMap().getCreator(
getPOClassName(hostGraphObject.getClass().getName()), true);
PatternObject po = (PatternObject) creatorClass.getSendableInstance(false);
this.addToElements(po);
po.setCurrentMatch(hostGraphObject);
return po;
}
public String dumpDiagram(String diagramName)
{
return dumpDiagram(diagramName, true);
}
public String dumpDiagram(String diagramName, boolean showMatch)
{
JsonObject result = new JsonObject();
result.put("typ", "object");
JsonArray nodes = new JsonArray();
result.put("nodes", nodes);
JsonArray edges = new JsonArray();
result.put("edges", edges);
LinkedHashMap nameMap = new LinkedHashMap();
addNodesToDiagram(this.elements, nodes, nameMap);
addEdgesToDiagram(this.elements, edges, nameMap);
String text =
"\n";
return text;
}
private void addEdgesToDiagram(PatternElementSet elements, JsonArray edges, LinkedHashMap nameMap)
{
for (PatternElement elem : elements)
{
if (elem instanceof PatternLink)
{
PatternLink link = (PatternLink) elem;
PatternObject src = link.getSrc();
PatternObject tgt = link.getTgt();
JsonObject edge = new JsonObject();
edges.add(edge);
edge.put("typ", "EDGE");
JsonObject role = new JsonObject();
edge.put("source", role);
// role.put("cardinality", "one");
role.put("property", " ");
role.put("id", nameMap.get(src));
role = new JsonObject();
edge.put("target", role);
// role.put("cardinality", "one");
role.put("property", link.getTgtRoleName());
role.put("id", nameMap.get(tgt));
}
else if (elem instanceof Pattern)
{
Pattern subPattern = (Pattern) elem;
addEdgesToDiagram(subPattern.getElements(), edges, nameMap);
}
}
}
private void addNodesToDiagram(PatternElementSet elements, JsonArray nodes, LinkedHashMap nameMap)
{
for (PatternElement elem : elements)
{
if (elem instanceof PatternObject)
{
PatternObject po = (PatternObject) elem;
JsonObject node = new JsonObject();
node.put("typ", "patternObject");
String shortClassName = CGUtil.shortClassName(po.getClass().getName());
String firstChar = shortClassName.substring(0, 1).toLowerCase();
int num = nameMap.size() + 1;
String jsonId = firstChar + num + " : " + shortClassName;
node.put("id", jsonId);
nameMap.put(po, jsonId);
JsonArray attrs = new JsonArray();
if (num == 1)
{
attrs.add("<< start >>");
}
if (po.getModifier() != null)
{
attrs.add("<< " + po.getModifier() + ">>");
}
for (AttributeConstraint attr : po.getAttrConstraints())
{
if (attr.getUpperTgtValue() != null)
{
attrs.add("" + attr.getAttrName() + " in [" + attr.getTgtValue() + ".." + attr.getUpperTgtValue() + "]");
}
else
{
attrs.add("" + attr.getAttrName() + " == " + attr.getTgtValue());
}
}
node.put("attributes", attrs);
nodes.add(node);
}
else if (elem instanceof Pattern)
{
Pattern subPattern = (Pattern) elem;
// add subgraph
JsonObject node = new JsonObject();
node.put("typ", "objectdiagram");
node.put("style", "nac");
node.put("info", CGUtil.shortClassName(subPattern.getClass().getName()));
nodes.add(node);
JsonArray subNodes = new JsonArray();
node.put("nodes", subNodes);
// JsonArray edges = new JsonArray();
// node.put("edges", edges);
addNodesToDiagram(subPattern.getElements(), subNodes, nameMap);
System.out.println("");
}
}
}
public String dumpDiagramOld(String diagramName, boolean showMatch)
{
objNo = 0;
LinkedHashSet