0

This start that is found in my codes are from my JSON file. Now that in my JSON file, i have a lot of "start" with the same name. For example in this JSON file, i have duplicates in the start (having both the content as "audi"):

{"rel": "AtLocation", "weight": 0.5, "dataset": "/d/dbpedia/en", "sources": ["/s/dbpedia/3.7"], "id": "0209765bf7185615ef242513f61ca0f61efe2a04", "features": ["audi AtLocation -", "audi - curitiba", "- AtLocation curitiba"], "end": "curitiba", "license": "/l/CC/By-SA", "uri": "/a/[AtLocation/,audi/,curitiba/]", "start": "audi", "context": "/ctx/all", "surfaceText": null}

{"rel": "AtLocation", "weight": 0.5, "dataset": "/d/dbpedia/en", "sources": ["/s/dbpedia/3.7"], "id": "5158d1cfce728efb3e42840d166ec06153a3d77d", "features": ["audi AtLocation -", "audi - ingolstadt", "- AtLocation ingolstadt"], "end": "unite_state", "license": "/l/CC/By-SA", "uri": "/a/[AtLocation/,audi/,ingolstadt/]", "start": "audi", "context": "/ctx/all", "surfaceText": null}

This is my Java codes:

String start = (String) jsonObject.get("start");  
StartVertex startV = new StartVertex(start);  
helloGraphDB.addVertex(startV);    

I doesnt want it to addVertex 2 times of the same "audi", how do i write the logic if else out?

Thank you.

4
  • 1
    what is type of helloGraphDB? Commented Sep 26, 2012 at 9:38
  • @AmitD, thank you for your reply! :) it is actually a database thingy.. i linked it to a software called infinitegraph, and to create a Vertex (something like a node), the code is the database name (in this case helloGraphDB) followed by the addVertex(name). Commented Sep 26, 2012 at 9:42
  • When you debug, what is the value of your String start? Commented Sep 26, 2012 at 9:43
  • @FlorianParain Thank you for your reply! In this situation, the value is "audi". Because the start value in this json file is "audi". Commented Sep 26, 2012 at 9:45

3 Answers 3

1

`You can use arraylist to check whether the value already inserted into addVertex

Like :

ArrayList addedVertex_Val = new ArrayList();

    String start = (String) jsonObject.get("start");
    boolean shouldInsert = true;

    for (int al_length = 0; al_length < addedVertex_val.size(); al_length++) {
        String val = addedVertex_val.get(al_length).toString();
        if (val.equals(start)) {
            shouldInsert = false;
            break;
        }
    }

    if (shouldInsert) {
        StartVertex startV = new StartVertex(start);
        helloGraphDB.addVertex(startV);
        addedVertex_Val.add(startV);
    }

`

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the suggestion! I have knock off and will try this codes tomorrow when i am back tmr! Thank you very much! The codes are looking good! :)
1

You can also use HashMap instead of array list.

It would be more efficient than linearly checking into ArrayList.

You'll have to just check whether you have a start(eg "audi") in a HashMap,if not you can add it to the Graph and also the HashMap,if it is already there in HashMap you can skip adding it both.

String a = "{\"rel\": \"AtLocation\", \"weight\": 0.5, \"dataset\": \"/d/dbpedia/en\", \"sources\": [\"/s/dbpedia/3.7\"], \"id\": \"0209765bf7185615ef242513f61ca0f61efe2a04\", \"features\": [\"audi AtLocation -\", \"audi - curitiba\", \"- AtLocation curitiba\"], \"end\": \"curitiba\", \"license\": \"/l/CC/By-SA\", \"uri\": \"/a/[AtLocation/,audi/,curitiba/]\", \"start\": \"audi\", \"context\": \"/ctx/all\", \"surfaceText\": null}";
        String b = "{\"rel\": \"AtLocation\", \"weight\": 0.5, \"dataset\": \"/d/dbpedia/en\", \"sources\": [\"/s/dbpedia/3.7\"], \"id\": \"5158d1cfce728efb3e42840d166ec06153a3d77d\", \"features\": [\"audi AtLocation -\", \"audi - ingolstadt\", \"- AtLocation ingolstadt\"], \"end\": \"unite_state\", \"license\": \"/l/CC/By-SA\", \"uri\": \"/a/[AtLocation/,audi/,ingolstadt/]\", \"start\": \"audi\", \"context\": \"/ctx/all\", \"surfaceText\": null}";
        JSONParser p = new JSONParser();
        JSONObject jsonA = (JSONObject) p.parse( a );
        JSONObject jsonB = (JSONObject) p.parse( b );
        HashMap<String, Integer> map = new HashMap<String, Integer>();

        String start = (String) jsonA.get( "start" );
        System.out.println( "Does map contains 'audi' for start:" + map.containsKey( start ) );
        if( !map.containsKey( start ) )
        {
            map.put( start, 1 );
            System.out.println( "Adding to graph" );
        }
        System.out.println( "Does map contains 'audi' for start:" + map.containsKey( start ) );

        start = (String) jsonB.get( "start" );
        if( !map.containsKey( start ) )
        {
            map.put( start, 2 );
            System.out.println( "Adding to graph" );
        }

        System.out.println( "Does map contains 'audi' for start:" + map.containsKey( start ) );

The Output that i got:

Does map contains 'audi' for start:false
Adding to graph
Does map contains 'audi' for start:true
Does map contains 'audi' for start:true

10 Comments

I have taken as HashMap as <String,Integer> so that you can also save the no of occurrences of "audi" with the key and it might be useful to you later.
thank you for your reply! Now i understand what is a HashMap for! Came across this but doesnt know how it works! I understand your codes! Cant wait to check it out with my codes in my office tmr morning! Get back to you tmr!
it has no error, no difference between the previous code and this.. 2 "audi" vertexes were being created..
it worked for me,you can check your helloGraphDB.addVertex(startV); method then.
thank you for the help! but again i have knock off! :( Wanted to test out the code desperately but i am in a hurry! let you know again tmr! i thank you sincerely for the help! cheers!
|
0
package com.infinitegraph.samples.hellograph;

// Import all InfiniteGraph packages
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import com.infinitegraph.*;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
// Import SLF4J logging packages
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.infinitegraph.BaseVertex;
import java.util.HashMap;

public class HelloGraph
{

    public static void main(String[] args)
    {
                BufferedReader br = null;
                JSONParser parser = new JSONParser();

        // Set up logging for the HelloGraph class
        Logger logger = LoggerFactory.getLogger(HelloGraph.class);

        // Create null transaction, null graph database instance
        Transaction tx = null;
        GraphDatabase helloGraphDB = null;

        // Name for graph database and property file
        String graphDbName = "HelloGraph";
        String propertiesFileName = "HelloGraph.properties";    

        try
        {
            try
            {
                // Delete graph database if it already exists
                GraphFactory.delete(graphDbName, propertiesFileName);
            }
            catch (StorageException sE)
            {
                logger.info(sE.getMessage());
            }

            // HINT: Add code to create graph database and its contents
            // Create graph database
            logger.info("> Creating graph database ...");
            GraphFactory.create(graphDbName, propertiesFileName);

            // Open graph database
            logger.info("> Opening graph database ...");
            helloGraphDB = GraphFactory.open(graphDbName, propertiesFileName);

            // Begin transaction
            logger.info("> Starting a read/write transaction ...");
            tx = helloGraphDB.beginTransaction(AccessMode.READ_WRITE);

            //create head vertex
            HeaderVertex head = new HeaderVertex("JSON File");
            helloGraphDB.addVertex(head);

            try {
                String sCurrentLine;

                br = new BufferedReader(new FileReader("C:/Users/ji/Desktop/example3.json"));

                HashMap<String, Integer> map = new HashMap <String, Integer>();

                while ((sCurrentLine = br.readLine()) != null) {

                    Object obj;

                    try {
                        obj = parser.parse(sCurrentLine);
                        JSONObject jsonObject = (JSONObject) obj;

                        //code to create vertex
                        logger.info("> Creating Start vertices ...");     
                        String start = (String) jsonObject.get("start");
                        StartVertex startV = new StartVertex(start);
                        if (!map.containsKey(start))
                        {
                            map.put(start,1);
                            helloGraphDB.addVertex(startV); 
                        }

                        //System.out.println(end);
                        String end = (String) jsonObject.get("end");
                        EndVertex endV = new EndVertex(end);
                        helloGraphDB.addVertex(endV);

                        String rel = (String) jsonObject.get("rel");
                        //System.out.println(rel);
                        logger.info("> Creating Relationship edge ...");
                        Relationship relationship1 = new Relationship("");
                        Relationship relationship2 = new Relationship(rel);

                        // Connect edges
                        logger.info("> Connecting vertices ...");
                        startV.addEdge(relationship1, head, EdgeKind.BIDIRECTIONAL, (short) 0);
                        endV.addEdge(relationship2, startV, EdgeKind.BIDIRECTIONAL, (short) 0);

                    } catch (ParseException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (br != null)br.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }

            // Specify a named root for traversal
            logger.info("> Naming a root vertex ...");
            helloGraphDB.nameVertex("JSON File", head);

            // Commit to save your changes to the graph database
            logger.info("> Committing changes ...");
            tx.commit();
        }

        catch (ConfigurationException cE)
        {
            logger.warn("> Configuration Exception was thrown ... ");
            logger.error(cE.getMessage());
        }

        finally
        {
            // If the transaction was not committed, complete
            // will roll it back
            if (tx != null)
                tx.complete();
            if (helloGraphDB != null)
            {
                helloGraphDB.close();
                logger.info("> On Exit: Closed graph database");
            }
        }
    }

}

// HINT: Add class definitions for Person and Meeting classes here.
class HeaderVertex extends BaseVertex
{
    private String header;

    public HeaderVertex (String header)
    {
        setStartName(header);
    }

    public void setStartName (String header)
    {
        markModified();
        this.header = header;
    }

    public String toString()
    {
        fetch();
        return this.header;
    }
}

class StartVertex extends BaseVertex
{
    private String start;

    public StartVertex (String start)
    {
        setStartName(start);
    }

    public void setStartName (String start)
    {
        markModified();
        this.start = start;
    }

    public String toString()
    {
        fetch();
        return this.start;
    }
}

class EndVertex extends BaseVertex
{
    private String end;

    public EndVertex (String end)
    {
        setEndVertex(end);
    }

    public void setEndVertex (String end)
    {
        markModified();
        this.end = end;
    }

    public String toString()
    {
        fetch();
        return this.end;
    }
}

class Relationship extends BaseEdge
{
    private String rel;

    public Relationship (String rel)
    {
        setRelationship(rel);
    }

    public void setRelationship (String rel)
    {
        markModified();
        this.rel = rel;
    }

    public String toString()
    {
        fetch();
        return this.rel;
    }
}

5 Comments

declare your map variables outside the while loop. Inside a while loop they will be initialized for every iteration and you will get duplicates.
I said outside the while loop,its still inside.
@faizan thank you for the fast reply! i moved it to outside while loop already, now i got an error..
HashMap<String, Integer> map = new HashMap <String, Integer>(); have you written outside the loop?
@faizan i mean thank you for helping me once again.. It is still not working still.. Can you please continue to help me? I really need to make it working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.