I want to change a node of a Jena TriplePath (org.apache.jena.sparql.core.TriplePath), but I haven't found any manner. Imagine I have this code:
TriplePath tp = null;
....
//tp has been defined and not null
Node domain = tp.getSubject();
Node predicate = tp.getPredicate();
Node range = tp.getObject();
Node newNode = NodeFactory.createURI("http://www.example.com/example/example");
//And now? How can I set a Node (domain/predicate/range) of tp?
The question is, how can I set any Node (domain/predicate/range) of the TriplePath tp with the newNode I've created? Is there any manner?
You need to create a new path and assign it to tp. TriplePaths are immutable, as is the rest of the SPARQL algebra in Jena (any ways to defeat this should not be used!).
For more complex setups, have a template with variables and use:
TriplePath Substitute.substitute(TriplePath triplePath, Binding binding)
Related
I have below Tree structure which is sub class of JTree
When the user selects a node, Time in image above, I need to get the Row index (index value = 3) of Demoscrigo(image above) with respect to Wells not from actual Root. Same index will be used to pre-select the row in other UI.I can get the selected Object but as the nodes has duplicate entries that is making things difficult.I have tried below options
getRowForPath(new TreePath(selectedNode.getPath()))
getSelectionRows()
this.getModel().getIndexOfChild(userObj, selectedNode);
JavaClientTreeModel treeModel = (JavaClientTreeModel) this.getModel();
selectedNode.getLevel()
this.getLastSelectedPathComponent();
this.getLeadSelectionRow();
But not able to figure out any specific combination. Please suggest me if any other solution is there.
First fetch the node for which Index need to be calculated by using the below code.
Parameter Type is used to get the above node
public DefaultMutableTreeNode findNode(DefaultMutableTreeNode node, String type){
Object data = (XXXX) node.getUserObject();
if(data.getType().equals(type)){
return node;
}else if(!node.isRoot()){
return findNode((DefaultMutableTreeNode) node.getParent(), type);
}
return null;
}
And then use
int rowIndex = node.getParent().getIndex(node);
specifically I need to represent the following:
The tree at any node can have an arbitrary number of children
Each parent node (after the root) is just a String (whose children are also Strings)
I need to be able to get parent and list out all the children (some sort of list or array of Strings) given an input string representing a given node
Dynamically populating the tree structure based on reference relationship between parent and child.
Example given is I have one member1 sponsor another member2, and member2 sponsor member 3 and so and so for. Already have the table records relationship
Is there an available structure for this ???
My data is from DB or a List, I will loop through the information with the name and the relation to determine if the node is a root, parent or a child.
So during the loop, I found a child, I need a reference to the parent so that I can compare the child relation to the parent before adding the child to its parent.
The closest code I found .
public class TreeNode<T> implements Iterable<TreeNode<T>> {
T data;
TreeNode<T> parent;
List<TreeNode<T>> children;
public TreeNode(T data) {
this.data = data;
this.children = new LinkedList<TreeNode<T>>();
}
public TreeNode<T> addChild(T child) {
TreeNode<T> childNode = new TreeNode<T>(child);
childNode.parent = this;
this.children.add(childNode);
return childNode;
}
// other features ...
}
Sample usage:
TreeNode<String> root = new TreeNode<String>("root");
{
TreeNode<String> node0 = root.addChild("node0");
TreeNode<String> node1 = root.addChild("node1");
TreeNode<String> node2 = root.addChild("node2");
{
TreeNode<String> node20 = node2.addChild(null);
TreeNode<String> node21 = node2.addChild("node21");
{
TreeNode<String> node210 = node20.addChild("node210");
}
}
}
This is what I have done so far. The parent will get overwritten by the latest entry so hence I am unable to retrieve what I have added previously .
public static TreeNode<String> getSet1() throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("test.txt"));
String line;
while ((line = reader.readLine()) != null) {
String[] items = line.split(":");
String name = items[0];
String parent = items[1];
String type = items[2];
if (parent.equalsIgnoreCase("-") && type.equalsIgnoreCase("mainparent")) {
root = new TreeNode<String>(name);
} else if (type.equalsIgnoreCase("ChildParent") && parent.equalsIgnoreCase(root.toString())) {
childParent = root.addChild(name);
} else if (type.equalsIgnoreCase("Child") && parent.equalsIgnoreCase(childParent.toString())) {
child = childParent.addChild(name);
}
}
return root;
}
Your diagram indicates a tree of arbitrary depth, but your code only handles grandparent -> parent -> child relationships (with a single grandparent at the root).
I would ignore the type, as all you need is the name of a person and the name of their parent. If the parent name is a dash, you know you have the root.
Now for each person, you need to get the parent node already in the tree (assuming parents come before children in the list - if that's not the case, the problem becomes significantly more complex, as you would have to store orphaned persons temporarily and for each new person see if they are the parent of an orphaned person).
In order to get the parent by name, you should store each person you have already processed in a second data structure, parallel to the tree. The second data structure should make it easy to look someone up by name. Maps, and in particular Hashtables, are ideal for this. This is how it works:
Map processedPersonsMap=new Hashtable<String, TreeNode<String>>();
For each person, you store them in the map, indexed by their name:
TreeNode<String> person=...;
processedPersonsMap.put(person.getData(), person);
When you read in a new person and their parent's name is not a dash, you look up the parent:
String parentName=items[1];
TreeNode<String> parent=processedPersonsMap.get(parentName);
In this way, no matter how deep the tree is, you will always find the right parents. However, keep in mind that this requires a valid input file where each child comes after their parent, and which does not contain circular references or missing parents.
If those conditions are not met, you have to handle them explicitly.
I know this is so easy and I've spent all day banging my head. I have an XML document like this:
<WMS_Capabilities version="1.3.0" xmlns="http://www.opengis.net/wms">
<Service>
<Name>WMS</Name>
<Title>Metacarta WMS VMaplv0</Title>
</Service>
<Capability>
<Layer>
<Name>Vmap0</Name>
<Title>Metacarta WMS VMaplv0</Title>
<Abstract>Vmap0</Abstract>
...
There can be multiple Layer nodes, and any Layer node can have a nested Layer node. I can quickly select all of the layer nodes and iterate through them with the following xpath code:
Map<String, String> uris = new HashMap<String, String>();
uris.put("wms", "http://www.opengis.net/wms");
XPath xpath1 = doc.createXPath("//wms:Layer");
xpath1.setNamespaceURIs(uris);
List nodes1 = xpath1.selectNodes(doc);
for (Iterator<?> layerIt = nodes1.iterator(); layerIt.hasNext();) {
Node node = (Node) layerIt.next();
}
I get back all Layer nodes. Perfect. But when I try to access each Name or Title child node, I get nothing. I've tried as many various combinations I can think of:
name = node.selectSingleNode("./wms:Name");
name = node.selectSingleNode("wms:Name");
name = node.selectSingleNode("Name");
etc etc, but it always returns null. I'm guessing it has something to do with the namespace, but all I'm after is the name and title text values for each one of the Layer nodes I've obtained. Can anybody offer any help:
I believe that Node.selectSingleNode() evaluates the supplied XPath expression with an empty namespace context. So there is no way of accessing a node in no namespace by name. It's necessary to use an expression such as *[local-name='Name']. If you want/need a namespace context, execute XPath expressions via the XPath object.
Thanks everybody for the help. It was Michael Kay's last clue that got it for me... I needed to use a relative path from the current node, include the namespace URI, and select from the context of the current node I'm iterating through:
Map<String, String> uris = new HashMap<String, String>();
uris.put("wms", "http://www.opengis.net/wms");
XPath xpath1 = doc.createXPath("//wms:Layer");
xpath1.setNamespaceURIs(uris);
List nodes1 = xpath1.selectNodes(doc);
for (Iterator<?> layerIt = nodes1.iterator(); layerIt.hasNext();) {
Node node = (Node) layerIt.next();
XPath nameXpath = node.createXPath("./wms:Name");
nameXpath.setNamespaceURIs(uris);
XPath titleXpath = node.createXPath("./wms:Title");
titleXpath.setNamespaceURIs(uris);
Node name = nameXpath.selectSingleNode(node);
Node title = titleXpath.selectSingleNode(node);
}
i want to make a duplicate node in Jtree but the code is not working inside mouse action listener....
/* DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
def obj = selectedNode.getUserObject()
DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode)node.getRoot().getChildAt(0);
model.insertNodeInto(selectedNode, parentNode, 0)*/
I don't see a call to "new" anywhere in this code. Did I miss it? Wouldn't that be a requirement or creating a new Node?
Create a new DMTN and initialize it with the state of the one you want to copy.
You are not making a copy, you just try to insert the (existing) node into a different location.
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
def obj = selectedNode.getUserObject()
DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode)node.getRoot().getChildAt(0);
model.insertNodeInto(new DefaultMutableTreeNode(obj), parentNode, 0);
(Obvious syntax errors have not been corrected, I am not your compiler.)
I have a table with fields category_id, category_name and parent_category_id. And parent_category_id has values from category_id which represents the parent child relationship. I dont have any fixed level of hierarchy, it may go up to 5 levels or 10 levels and there is no limit to that.. I need a code for how to implement this JTree to make thing work for me. I should be able to implement the same for Menu bar as well.. Please help me with this..
After googling I found this,
Map<String, Node> idToNode = new HashMap<String, Node>();
//create nodes from ResultSet
while ( resultSet.next() ){
Node node = //create node -contains info, parent id, and its own id from ResultSet
//put node into idToNode, keyed with its id
}
//link together
Iterator<String> it = idToNode.keySet().iterator();
Node root = null;
while ( it.hasNext() ){
Node node = idToNode.get(it.next());
Node parent = idToNode.get(node.getParentId());
if ( parent == null ) {
root = node;
}else{
parent.addChild(node);
}
}
How do i code those commented instructions?
Use DefaultMutableTreeNode to create your nodes
Make a map of IDs to nodes - as you get your nodes from the database, store them in the map with the id as their key.
Once you have all your nodes, go through them once more and match their parent ids up, retrieving them from the map.
Assuming your tree is structurally sound in the database, it will be sound here. Pick any node and follow the parent chain the the root.
With the root object, you can create your JTree. :)